00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef lint
00039 static char copyright[] =
00040 "@(#) Copyright (c) 2002-2004\n\
00041 Netherlands Forensic Institute. All rights reserved.\n";
00042 #endif
00043
00044 #ifdef HAVE_CONFIG_H
00045 #include <config.h>
00046 #endif
00047
00048 #include <ctype.h>
00049 #include <limits.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052
00053 #include "rdd.h"
00054 #include "rdd_internals.h"
00055 #include "numparser.h"
00056
00057 #define flags_set(flags, bit) (((flags) & (bit)) == (bit))
00058
00059 static struct _RDD_MULTIPLIER {
00060 char mchar;
00061 unsigned factor;
00062 } multtab[] = {
00063 {'c', 1<<0},
00064 {'w', 1<<1},
00065 {'b', 1<<9},
00066 {'k', 1<<10},
00067 {'m', 1<<20},
00068 {'g', 1<<30},
00069 {'\000', 0}
00070 };
00071
00072
00073
00074
00075
00076
00077 int
00078 rdd_parse_bignum(const char *str, rdd_num_flags_t flags, rdd_count_t *result)
00079 {
00080 rdd_count_t sz;
00081 rdd_count_t multiplier;
00082 unsigned d, i, j;
00083 unsigned n;
00084
00085 *result = 0;
00086 n = (unsigned) strlen(str);
00087 sz = 0;
00088 for (i = 0; i < n && isdigit(str[i]); i++) {
00089 d = str[i] - '0';
00090
00091 if (sz > (RDD_COUNT_MAX - d) / 10) {
00092 return RDD_ERANGE;
00093 }
00094 sz = 10 * sz + d;
00095 }
00096 if ((i == 0) || (i < n - 1)) {
00097 return RDD_ESYNTAX;
00098 }
00099
00100
00101
00102 if (i == n - 1) {
00103
00104
00105 multiplier = 0;
00106 for (j = 0; multtab[j].mchar != '\0'; j++) {
00107 if (tolower(str[i]) == multtab[j].mchar) {
00108 multiplier = multtab[j].factor;
00109 break;
00110 }
00111 }
00112 if (multiplier == 0) {
00113 return RDD_ESYNTAX;
00114 }
00115 if (sz > (RDD_COUNT_MAX / multiplier)) {
00116 return RDD_ERANGE;
00117 }
00118 sz *= multiplier;
00119 }
00120
00121 if (flags_set(flags, RDD_POSITIVE) && sz <= 0) return RDD_ERANGE;
00122
00123 if (flags_set(flags, RDD_POWER2)) {
00124 rdd_count_t x;
00125
00126 for (x = sz; x != 0 && (x & 1) == 0; x /= 2) {
00127 }
00128 if (x != 1) return RDD_ERANGE;
00129 }
00130
00131 *result = sz;
00132 return RDD_OK;
00133 }
00134
00135
00136
00137
00138 int
00139 rdd_parse_uint(const char *str, unsigned *result)
00140 {
00141 unsigned n;
00142 const char *p;
00143
00144 *result = 0;
00145 n = 0;
00146 for (p = str; *p != '\0'; p++) {
00147 n *= 10;
00148 if (! isdigit(*p)) return RDD_ESYNTAX;
00149 n += *p - '0';
00150 }
00151
00152 *result = n;
00153 return RDD_OK;
00154 }
00155
00156 int
00157 rdd_parse_tcp_port(const char *str, unsigned *result)
00158 {
00159 unsigned n;
00160 int rc;
00161
00162 if ((rc = rdd_parse_uint(str, &n)) != RDD_OK) {
00163 return rc;
00164 }
00165
00166 if (n > 65535) return RDD_ERANGE;
00167
00168 *result = n;
00169 return RDD_OK;
00170 }