Home | History | Annotate | Line # | Download | only in gen
      1  1.8  christos /*	$NetBSD: dehumanize_number.c,v 1.8 2020/08/18 19:18:06 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9  1.1  christos  * 2005 program.
     10  1.1  christos  *
     11  1.1  christos  * Redistribution and use in source and binary forms, with or without
     12  1.1  christos  * modification, are permitted provided that the following conditions
     13  1.1  christos  * are met:
     14  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  christos  *    documentation and/or other materials provided with the distribution.
     19  1.1  christos  *
     20  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  christos  */
     32  1.6       apb 
     33  1.6       apb #ifdef HAVE_NBTOOL_CONFIG_H
     34  1.7       apb #include "nbtool_config.h"
     35  1.6       apb #endif /* HAVE_NBTOOL_CONFIG_H */
     36  1.5  christos 
     37  1.1  christos #include <sys/cdefs.h>
     38  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     39  1.8  christos __RCSID("$NetBSD: dehumanize_number.c,v 1.8 2020/08/18 19:18:06 christos Exp $");
     40  1.1  christos #endif /* LIBC_SCCS and not lint */
     41  1.1  christos 
     42  1.1  christos #include "namespace.h"
     43  1.4  christos #include <assert.h>
     44  1.1  christos #include <inttypes.h>
     45  1.1  christos #include <ctype.h>
     46  1.1  christos #include <stdlib.h>
     47  1.1  christos #include <string.h>
     48  1.1  christos #include <errno.h>
     49  1.1  christos #include <limits.h>
     50  1.1  christos 
     51  1.1  christos /*
     52  1.1  christos  * Converts the number given in 'str', which may be given in a humanized
     53  1.1  christos  * form (as described in humanize_number(3), but with some limitations),
     54  1.2   xtraeme  * to an int64_t without units.
     55  1.1  christos  * In case of success, 0 is returned and *size holds the value.
     56  1.1  christos  * Otherwise, -1 is returned and *size is untouched.
     57  1.1  christos  *
     58  1.1  christos  * TODO: Internationalization, SI units.
     59  1.1  christos  */
     60  1.1  christos int
     61  1.1  christos dehumanize_number(const char *str, int64_t *size)
     62  1.1  christos {
     63  1.1  christos 	char *ep, unit;
     64  1.1  christos 	const char *delimit;
     65  1.1  christos 	long multiplier;
     66  1.1  christos 	long long tmp, tmp2;
     67  1.1  christos 	size_t len;
     68  1.1  christos 
     69  1.1  christos 	len = strlen(str);
     70  1.1  christos 	if (len == 0) {
     71  1.1  christos 		errno = EINVAL;
     72  1.1  christos 		return -1;
     73  1.1  christos 	}
     74  1.1  christos 
     75  1.1  christos 	multiplier = 1;
     76  1.1  christos 
     77  1.1  christos 	unit = str[len - 1];
     78  1.1  christos 	if (isalpha((unsigned char)unit)) {
     79  1.1  christos 		switch (tolower((unsigned char)unit)) {
     80  1.1  christos 		case 'b':
     81  1.1  christos 			multiplier = 1;
     82  1.1  christos 			break;
     83  1.1  christos 
     84  1.1  christos 		case 'k':
     85  1.1  christos 			multiplier = 1024;
     86  1.1  christos 			break;
     87  1.1  christos 
     88  1.1  christos 		case 'm':
     89  1.1  christos 			multiplier = 1024 * 1024;
     90  1.1  christos 			break;
     91  1.1  christos 
     92  1.1  christos 		case 'g':
     93  1.1  christos 			multiplier = 1024 * 1024 * 1024;
     94  1.1  christos 			break;
     95  1.1  christos 
     96  1.1  christos 		default:
     97  1.1  christos 			errno = EINVAL;
     98  1.1  christos 			return -1; /* Invalid suffix. */
     99  1.1  christos 		}
    100  1.1  christos 
    101  1.1  christos 		delimit = &str[len - 1];
    102  1.1  christos 	} else
    103  1.1  christos 		delimit = NULL;
    104  1.1  christos 
    105  1.1  christos 	errno = 0;
    106  1.1  christos 	tmp = strtoll(str, &ep, 10);
    107  1.8  christos 	if (str[0] == '\0' || (ep != delimit && *ep != '\0')) {
    108  1.8  christos 		errno = EINVAL;
    109  1.1  christos 		return -1; /* Not a number. */
    110  1.8  christos 	}
    111  1.1  christos 	else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
    112  1.1  christos 		return -1; /* Out of range. */
    113  1.1  christos 
    114  1.1  christos 	tmp2 = tmp * multiplier;
    115  1.1  christos 	tmp2 = tmp2 / multiplier;
    116  1.1  christos 	if (tmp != tmp2) {
    117  1.1  christos 		errno = ERANGE;
    118  1.1  christos 		return -1; /* Out of range. */
    119  1.1  christos 	}
    120  1.4  christos 	tmp *= multiplier;
    121  1.5  christos #ifdef _DIAGASSERT
    122  1.4  christos 	_DIAGASSERT(__type_fit(int64_t, tmp));
    123  1.5  christos #endif
    124  1.4  christos 	*size = (int64_t)tmp;
    125  1.1  christos 
    126  1.1  christos 	return 0;
    127  1.1  christos }
    128