size_t bytespec(char const * string, void * memory, size_t extent) { char const * number = string; byte * offset = (byte *) (memory); if (! number) { error (1, EINVAL, __func__); } while ((* number) && (extent--)) { unsigned digit; if (((void *) (offset) > memory) && (* number == HEX_EXTENDER)) { number++; } if ((digit = todigit(* number++)) >= 0x10) { error (1, EINVAL, "Have %s but need valid hex digit", string); } * offset = digit << 4; if ((digit = todigit(* number++)) >= 0x10) { error (1, EINVAL, "Have %s but need valid hex digit", string); } * offset |= digit; offset++; } if ((* number) || (extent)) { error (1, EINVAL, "%s is not %zd bytes", string, (void *) (offset) - memory + extent); } return ((void *) (offset) - memory); }
char * ietfdecode (char *string) { char * buffer = string; char * offset = string; while ((*offset = *string)) { if (*string == '%') { unsigned upper = todigit (string [1]); unsigned lower = todigit (string [2]); if ((upper < 16) && (lower < 16)) { *offset = 0; *offset += upper << 4; *offset += lower << 0; string++; string++; } } offset++; string++; } return (buffer); }
static void xmlmemory (void * memory, size_t extent) { uint8_t * buffer = (uint8_t *)(memory); if (!*string) { offset++; length--; } while ((*string) && (length)) { uint8_t msb = todigit (*string++); uint8_t lsb = todigit (*string++); if ((msb > 0x0F) || (lsb > 0x0F)) { error (XML_BAD_NUMBER, EINVAL, "%s value is not hexadecimal", member); } buffer [offset] = (msb << 4) + lsb; offset++; length--; } if ((length) && (!series)) { error (XML_BAD_NUMBER, EINVAL, "%s value is too short", member); } if (*string) { error (XML_BAD_NUMBER, EINVAL, "%s value is too long", member); } return; }
void encode (void * memory, size_t extent) { extern struct command command; register byte * binary = (byte *)(memory); unsigned digit; while ((command.offset < command.length) && (extent--)) { *binary = 0; if ((digit = todigit (command.buffer [command.offset++])) > 0x0F) { command.buffer [command.offset] = (char)(0); error (1, EINVAL, "[%s]1", command.buffer); } *binary |= digit << 4; if ((digit = todigit (command.buffer [command.offset++])) > 0x0F) { command.buffer [command.offset] = (char)(0); error (1, EINVAL, "[%s]2", command.buffer); } *binary |= digit; binary++; } return; }
/* * safe string to unsigned long long. Will ignore all non digits * until it finds the first digit. Then it will consume all base-10 * numeric characters. In the event of failure the number of consumed * characters returned will be less than expected. */ size_t s_strtoull(const char *const str, size_t str_len, const char **pp_end, unsigned long long *p_result) { // init some vars size_t str_i = 0; if (str && (str_len != 0) && p_result) { // index through all non-digits until first digit is hit while ((str_i < str_len) && !isdigit(str[str_i]) && (str[str_i] != '\0')) { str_i++; } // convert a consecutive digit sequence starting at the most significant digit *p_result = 0; while ((str_i < str_len) && isdigit(str[str_i])) { unsigned long long tmp_result; // We have to do this little dance around ULLONG_MAX because // we have no way to store a number larger than ULLONG_MAX // So we check to see if multiplying our current result by ten // will overflow and, if not, multiply by ten and see if adding // the next digit will overflow if (*p_result * 10 >= *p_result) { tmp_result = *p_result * 10; } else { break; } if (tmp_result + todigit(str[str_i]) >= tmp_result) { *p_result = tmp_result + todigit(str[str_i]); str_i++; } else { break; } } if (pp_end) { // set p_end to NULL if we have consumed the entire buffer, so that we // do not point outside of the buffer *pp_end = (str_i < str_len) ? &(str[str_i]) : NULL; } } return str_i; }
size_t __format_float_scientific(char * buf, const char * bp, int decpt, int sign, bool is_zero, ios_base::fmtflags flags, int precision, bool /* islong */) { size_t __group_pos; char *__start_buf = buf; char * suffix; char expbuf[MAXESIZ + 2]; // sign if required if (sign) *buf++ = '-'; else if (flags & ios_base::showpos) *buf++ = '+'; // first digit of mantissa *buf++ = *bp++; __group_pos = buf - __start_buf - 1; // decimal point if required if (precision != 0 || flags & ios_base::showpoint) { *buf++ = '.'; ++__group_pos; } // rest of mantissa int rz = precision; while (rz-- > 0 && *bp != 0) *buf++ = *bp++; // exponent *(suffix = &expbuf[MAXESIZ]) = 0; if (!is_zero) { int nn = decpt - 1; if (nn < 0) nn = -nn; for (; nn > 9; nn /= 10) *--suffix = (char) todigit(nn % 10); *--suffix = (char) todigit(nn); } // prepend leading zeros to exponent while (suffix > &expbuf[MAXESIZ - 2]) *--suffix = '0'; // put in the exponent sign *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-'); // put in the e *--suffix = flags & ios_base::uppercase ? 'E' : 'e'; // copy the suffix strcpy(buf, suffix); return __group_pos; }
static void function (const char * string, uint32_t range, unsigned count, unsigned group, unsigned space, flag_t flags) { const char * offset = string; uint32_t vendor = 0; uint32_t device = 0; unsigned radix = 0x10; unsigned width; unsigned digit; for (width = 0; width < ETHER_ADDR_LEN; width++) { if ((digit = todigit (*offset)) < radix) { vendor *= radix; vendor += digit; offset++; continue; } error (1, EINVAL, "Bad MAC Address: %s", string); } if (!vendor) { error (1, EPERM, "Vendor ID can't be zero"); } for (width = 0; width < ETHER_ADDR_LEN; width++) { if ((digit = todigit (*offset)) < radix) { device *= radix; device += digit; offset++; continue; } error (1, EINVAL, "Bad MAC Address: %s", string); } if (!device) { error (1, EPERM, "Device ID can't be zero"); } if (*offset) { error (1, EINVAL, "Bad MAC address: %s", string); } if (range > (0x00FFFFFF - device)) { error (1, ERANGE, "Want %d passwords but only %d left in range", range, (0x00FFFFFF - device)); } MACPasswords (vendor, device, range, count, group, space, flags); return; }
uint64_t hextoint (unsigned bytes) { extern struct command command; uint64_t limit = -1; uint64_t value = 0; unsigned radix = 16; unsigned digit = 0; if (bytes < sizeof (limit)) { limit <<= (bytes << 3); limit = ~limit; } while ((digit = todigit (command.buffer [command.offset])) < radix) { value *= radix; value += digit; command.offset++; if (value > limit) { command.buffer [command.offset] = (char)(0); error (1, EINVAL, "[%s] exceeds %d bits", command.buffer, (bytes << 3)); } } return (value); }
int utostr(unsigned int value, unsigned int base, int uppercase, char *str, size_t num) { unsigned int i, tmp; size_t len = 1; if (base < 2 || base > 16 || num < 1) return EXIT_FAILURE; tmp = value; while (tmp /= base) len++; if (len >= num) return EXIT_FAILURE; for (i = 0; i < len; i++) { str[len - i - 1] = todigit(value % base, base); value /= base; if (uppercase) str[len - i - 1] = toupper(str[len - i - 1]); } str[len] = '\0'; return EXIT_SUCCESS; }
ID atoi3id(char *str) { ID id; int i, len; len = (int) strlen(str); assert(len <= 2*ID_LEN); memset(id.x, 0, ID_LEN); if (len % 2 != 0) { str[len] = '0'; len++; } for (i = 0; i < len/2; i++) id.x[ i ] = (todigit(str[2*i]) << 4) | todigit(str[2*i+1]); str[len--] = 0; // to restore old str return id; }
static uint64_t ossl_strtouint64(const variant_char *str) { uint64_t ret = 0; unsigned int digit, base = 10; if (*str == '0') { base = 8, str++; if (ossl_tolower(*str) == 'x') base = 16, str++; } while((digit = todigit(*str++)) < base) ret = ret * base + digit; return ret; }
static unsigned xmlinteger (NODE const * node, unsigned radix) { unsigned digit; unsigned value = 0; while ((digit = todigit (*string)) < radix) { value *= radix; value += digit; string++; } if (*string) { error (XML_BAD_NUMBER, EPERM, "%s %s is not numeric", member, node->text); } return (value); }
static uint16_t integer (unsigned radix) { extern signed c; uint16_t value = 0; unsigned digit = 0; while ((digit = todigit (c)) < radix) { value *= radix; value += digit; c = mygetc (); } while (isspace (c)) { c = mygetc (); } return (value); }
size_t numencode(void * memory, register size_t extent, register char const * string, unsigned radix, unsigned comma) { register byte * origin = (byte *) (memory); register byte * offset = (byte *) (memory); while ((extent--) && (* string)) { unsigned digit = 0; unsigned octet = 0; if ((offset > origin) && (* string == comma)) { string++; } while ((digit = todigit(* string)) < radix) { octet *= radix; octet += digit; if (octet > 255) { errno = EINVAL; return (- 1); } string++; } * offset = octet; offset++; } #if defined (WIN32) while (isspace(* string)) { string++; } #endif if ((* string) || (extent)) { errno = EINVAL; return (- 1); } return ((size_t) (offset - origin)); }
/* * safe string to unsigned int. Will ignore all non digits * until it finds the first digit. Then it will consume all base-10 * numeric characters. In the event of failure the number of consumed * characters returned will be less than expected. */ size_t s_strtoui(const char *const str, size_t str_len, const char **pp_end, unsigned int *p_result) { // init some vars size_t str_i = 0; if (str && (str_len != 0) && p_result) { // index through all non-digits until first digit is hit while ((str_i < str_len) && !isdigit(str[str_i]) && (str[str_i] != '\0')) { str_i++; } // convert a consecutive digit sequence starting at the most significant digit *p_result = 0; while ((str_i < str_len) && isdigit(str[str_i])) { // up-cast result to unsigned long long for robust comparison (next) unsigned long long tmp_result = (((unsigned long long)(*p_result)) * 10) + todigit(str[str_i]); // end before number becomes greater than what a unsigned short can represent if (tmp_result <= UINT_MAX) { *p_result = (unsigned int)tmp_result; str_i++; } else { break; } } if (pp_end) { // set p_end to NULL if we have consumed the entire buffer, so that we // do not point outside of the buffer *pp_end = (str_i < str_len) ? &(str[str_i]) : NULL; } } return str_i; }
int new_hex_string( YARA_CONTEXT* context, SIZED_STRING* charstr, unsigned char** hexstr, unsigned char** maskstr, unsigned int* length) { int i; int skip_lo; int skip_hi; int skip_exact; char c,d; char* s; char* closing_bracket; int inside_or; int or_count; int len; int result = ERROR_SUCCESS; unsigned char high_nibble = 0; unsigned char low_nibble = 0; unsigned char mask_high_nibble = 0; unsigned char mask_low_nibble = 0; unsigned char* hex; unsigned char* mask; //assert(charstr && hexstr && maskstr && length); len = (int) charstr->length; //assert(charstr[0] == '{' && charstr[len - 1] == '}'); *hexstr = hex = (unsigned char*) yr_malloc(len / 2); *maskstr = mask = (unsigned char*) yr_malloc(len); if (hex == NULL || mask == NULL) { if (hex) yr_free(hex); if (mask) yr_free(mask); return ERROR_INSUFICIENT_MEMORY; } i = 1; *length = 0; inside_or = FALSE; while (i < len - 1) { c = toupper(charstr->c_string[i]); if (isalnum(c) || (c == '?')) { d = toupper(charstr->c_string[i + 1]); if (!isalnum(d) && (d != '?')) { result = ERROR_UNPAIRED_NIBBLE; break; } if (c != '?') { high_nibble = todigit(c); mask_high_nibble = 0x0F; } else { high_nibble = 0; mask_high_nibble = 0; } if (d != '?') { low_nibble = todigit(d); mask_low_nibble = 0x0F; } else { low_nibble = 0; mask_low_nibble = 0; } *hex++ = (high_nibble << 4) | (low_nibble); *mask++ = (mask_high_nibble << 4) | (mask_low_nibble); (*length)++; i+=2; } else if (c == '(') { if (inside_or) { result = ERROR_NESTED_OR_OPERATION; break; } inside_or = TRUE; *mask++ = MASK_OR; i++; } else if (c == ')') { inside_or = FALSE; *mask++ = MASK_OR_END; i++; } else if (c == '|') { if (!inside_or) { result = ERROR_MISPLACED_OR_OPERATOR; break; } *mask++ = MASK_OR; i++; } else if (c == '[') { if (inside_or) { result = ERROR_SKIP_INSIDE_OR_OPERATION; break; } closing_bracket = strchr(charstr->c_string + i + 1, ']'); if (closing_bracket == NULL) { result = ERROR_MISMATCHED_BRACKET; break; } else { s = closing_bracket + 1; while (*s == ' ') s++; /* skip spaces */ if (*s == '}') /* no skip instruction should exists at the end of the string */ { result = ERROR_SKIP_AT_END; break; } else if (*s == '[') /* consecutive skip intructions are not allowed */ { result = ERROR_CONSECUTIVE_SKIPS; break; } } /* only decimal digits and '-' are allowed between brackets */ for (s = charstr->c_string + i + 1; s < closing_bracket; s++) { if ((*s != '-') && (*s < '0' || *s > '9')) { result = ERROR_INVALID_SKIP_VALUE; break; } } skip_lo = atoi(charstr->c_string + i + 1); if (skip_lo < 0 || skip_lo > MASK_MAX_SKIP) { result = ERROR_INVALID_SKIP_VALUE; break; } skip_exact = 1; s = strchr(charstr->c_string + i + 1, '-'); if (s != NULL && s < closing_bracket) { skip_hi = atoi(s + 1); if (skip_hi <= skip_lo || skip_hi > MASK_MAX_SKIP) { result = ERROR_INVALID_SKIP_VALUE; break; } skip_exact = 0; } if (skip_exact) { *mask++ = MASK_EXACT_SKIP; *mask++ = (unsigned char) skip_lo; } else { *mask++ = MASK_RANGE_SKIP; *mask++ = (unsigned char) skip_lo; *mask++ = (unsigned char) skip_hi; } i = (int) (closing_bracket - charstr->c_string + 1); } else if (c == ']') { result = ERROR_MISMATCHED_BRACKET; break; } else if (c == ' ' || c == '\n' || c == '\t') { i++; } else { result = ERROR_INVALID_CHAR_IN_HEX_STRING; break; } } *mask++ = MASK_END; /* wildcards or skip instructions are not allowed at the first position the string */ if ((*maskstr)[0] != 0xFF) { result = ERROR_MISPLACED_WILDCARD_OR_SKIP; } /* check if byte or syntax is correct */ i = 0; or_count = 0; while ((*maskstr)[i] != MASK_END) { if ((*maskstr)[i] == MASK_OR) { or_count++; if ( (*maskstr)[i+1] == MASK_OR || (*maskstr)[i+1] == MASK_OR_END ) { result = ERROR_INVALID_OR_OPERATION_SYNTAX; break; } } else if ((*maskstr)[i] == MASK_OR_END) { if (or_count < 2) { result = ERROR_INVALID_OR_OPERATION_SYNTAX; break; } or_count = 0; } i++; } if (result != ERROR_SUCCESS) { yr_free(*hexstr); yr_free(*maskstr); *hexstr = NULL; *maskstr = NULL; } return result; }
uint64_t basespec (char const * string, unsigned base, unsigned size) { char const * number = string; unsigned radix = RADIX_DEC; signed scale = 1; uint64_t limit = 0; uint64_t value = 0; unsigned digit = 0; limit = ~limit; if (size < sizeof (limit)) { limit <<= size << 3; limit = ~limit; } if (base) { radix = base; } if (* number == '=') { number++; } else if (* number == '+') { number++; } else if (* number == '-') { number++; scale = -1; } if (*number == '0') { number++; if ((*number == 'b') || (*number == 'B')) { radix = RADIX_BIN; number++; } else if ((*number == 'd') || (*number == 'D')) { radix = RADIX_DEC; number++; } else if ((*number == 'x') || (*number == 'X')) { radix = RADIX_HEX; number++; } } if ((base) && (base != radix)) { error (1, EINVAL, "%s is not base %d notation", string, base); } while ((digit = todigit (*number)) < radix) { value *= radix; value += digit; if (value > limit) { error (1, ERANGE, "%s exceeds %d bits", string, (size << 3)); } number++; } #ifdef WIN32 while (isspace (*number)) { number++; } #endif if (*number) { error (1, EINVAL, "%s is not base %d notation", string, radix); } return (scale * value); }
size_t typeload(struct _type_ list[], size_t size) { size_t item = 0; size_t line = 0; char string[_NAMESIZE]; signed c = getc(stdin); while (c != EOF) { size_t lower = 0; size_t upper = item; unsigned radix = 10; unsigned value = 0; unsigned digit = 0; char * sp = string; if (c == '\n') { line++; } if (isspace(c)) { c = getc(stdin); continue; } if ((c == '#') || (c == ';')) { do { c = getc(stdin); } while ((c != '\n') && (c != EOF)); continue; } if (c == '0') { c = getc(stdin); if ((c == 'x') || (c == 'X')) { c = getc(stdin); radix = 16; } else if((c == 'b') || (c == 'B')) { c = getc(stdin); radix = 2; } else { radix = 8; } } while ((digit = todigit(c)) < radix) { value *= radix; value += digit; c = getc(stdin); } while (isblank(c)) { c = getc(stdin); } if (isalpha(c) || (c == '_')) { do { if ((size_t) (sp - string) < (sizeof(string) - 1)) { * sp++ = (char) (c); } c = getc(stdin); } while (isalnum(c) || (c == '_')); } * sp = (char) (0); if (sp == string) { error (1, EINVAL, "No name on line %zd", line); } while (isblank(c)) { c = getc(stdin); } if ((c != '#') && (c != ';') && (c != '\n') && (c != EOF)) { error (1, EINVAL, "Trash on line %zd after %s", line, string); } while (lower < upper) { size_t index = (lower + upper) >> 1; signed order = value - list[index].type; if (order < 0) { upper = index - 0; continue; } if (order > 0) { lower = index + 1; continue; } error (1, EINVAL, "Duplicate value %d for %s/%s on line %zd", value, string, list[index].name, line); } if (item < size) { for (upper = item++; upper > lower; upper--) { list [upper].type = list[upper - 1].type; list [upper].name = list[upper - 1].name; } list [upper].type = value; list [upper].name = (char const *) (strdup(string)); continue; } error (1, EOVERFLOW, "Bailing Out"); } return (item); }
unsigned long long uintspec(char const * string, unsigned long long minimum, unsigned long long maximum) { char const * number = string; unsigned radix = 10; unsigned long long value = 0; unsigned digit; if (! number) { error (1, EINVAL, __func__); } while (isspace(* number)) { number++; } if (* number == '0') { number++; if ((* number == 'b') || (* number == 'B')) { radix = 2; number++; } else if((* number == 'd') || (* number == 'D')) { radix = 10; number++; } else if((* number == 'x') || (* number == 'X')) { radix = 16; number++; } } while ((digit = todigit(* number)) < radix) { value *= radix; value += digit; number++; } while (isspace(* number)) { number++; } if (* number) { error (1, EINVAL, "Have '%s' but need unsigned integer", string); } if ((value < minimum) || (value > maximum)) { #ifdef WIN32 error (1, ERANGE, "Have '%s' but need '%I64d' thru '%I64d'", string, minimum, maximum); #else error (1, ERANGE, "Have '%s' but need '%llu' thru '%llu'", string, minimum, maximum); #endif } return (value); }
static signed psin (struct _file_ * pib) { unsigned index = 0; unsigned count = 0; unsigned limit = pibscalers (pib); uint32_t value = 0; signed c; if ((limit != INT_CARRIERS) && (limit != AMP_CARRIERS) && (limit != PLC_CARRIERS)) { error (1, 0, "Don't understand this PIB's prescaler format"); } if (limit == INT_CARRIERS) { if (lseek (pib->file, INT_PRESCALER_OFFSET, SEEK_SET) != INT_PRESCALER_OFFSET) { error (1, errno, FILE_CANTSEEK, pib->name); } } else if (limit == PLC_CARRIERS) { if (lseek (pib->file, QCA_PRESCALER_OFFSET, SEEK_SET) != QCA_PRESCALER_OFFSET) { error (1, errno, FILE_CANTSEEK, pib->name); } } while ((c = getc (stdin)) != EOF) { if (isspace (c)) { continue; } if ((c == '#') || (c == ';')) { do { c = getc (stdin); } while (nobreak (c)); continue; } index = 0; while (isdigit (c)) { index *= 10; index += c - '0'; c = getc (stdin); } if (index != count) { error (1, ECANCELED, "Carrier %d out of order", index); } if (index >= limit) { error (1, EOVERFLOW, "Too many prescalers"); } while (isblank (c)) { c = getc (stdin); } value = 0; while (isxdigit (c)) { value *= 16; value += todigit (c); c = getc (stdin); } if (limit == INT_CARRIERS) { value = HTOLE32 (value); if (write (pib->file, &value, sizeof (value)) != sizeof (value)) { error (1, errno, "Can't save %s", pib->name); } } else if (limit == AMP_CARRIERS) { if (value & ~0x03FF) { error (1, errno, "Position %d has invalid prescaler value", index); } if (ar7x00_psin (pib, value, index)) { error (1, errno, "Can't update %s", pib->name); } } else if (limit == PLC_CARRIERS) { uint8_t tmp = value & 0xff; if (write (pib->file, &tmp, sizeof (tmp)) != sizeof (tmp)) { error (1, errno, "Can't save %s", pib->name); } } while (nobreak (c)) { c = getc (stdin); }; count++; } return (0); }
size_t dataspec (char const * string, void * memory, size_t extent) { char const * number = string; byte * origin = (byte *)(memory); byte * offset = (byte *)(memory); if (!number) { error (1, EFAULT, "dataspec"); } #ifdef WIN32 while (isspace (*number)) { number++; } #endif while ((*number) && (extent)) { unsigned digit = 0; #ifdef WIN32 if (isspace (*number)) { break; } #endif if ((offset > origin) && (*number == HEX_EXTENDER)) { number++; } if ((digit = todigit (*number++)) >= RADIX_HEX) { error (1, EINVAL, "You said '%s' but I want a hex digit", string); } *offset = digit << 4; if (!*number) { error (1, EINVAL, "You said '%s' but I want another hex digit", string); } if ((digit = todigit (*number++)) >= 0x10) { error (1, EINVAL, "You said '%s' but I want valid hex data", string); } *offset |= digit; offset++; extent--; } #ifdef WIN32 while (isspace (*number)) { number++; } #endif if (*number && !extent) { error (1, EINVAL, "'%s' exceeds %d bytes", string, (unsigned)(offset - origin - extent)); } if (*number) { error (1, EINVAL, "String '%s' contains trash", string); } return (offset - origin); }
static void readitem (struct item * item, char const * string) { register uint8_t * origin = (uint8_t *)(item->MAC_ADDR); register uint8_t * offset = (uint8_t *)(item->MAC_ADDR); size_t extent = sizeof (item->MAC_ADDR); memset (item, 0, sizeof (* item)); while ((extent) && (*string)) { unsigned radix = RADIX_HEX; unsigned field = sizeof (uint8_t) + sizeof (uint8_t); unsigned value = 0; unsigned digit = 0; if ((offset != origin) && (*string == HEX_EXTENDER)) { string++; } while (field--) { if ((digit = todigit (*string)) < radix) { value *= radix; value += digit; string++; continue; } error (1, EINVAL, "bad MAC address: ...[%s] (1)", string); } *offset = value; offset++; extent--; } if (extent) { error (1, EINVAL, "bad MAC address: ...[%s] (2)", string); } while (isspace (*string)) { string++; } if ((*string) && (*string != ',')) { error (1, EINVAL, "bad MAC address: ...[%s] (3)", string); } while (*string == ',') { unsigned radix = RADIX_DEC; unsigned digit = 0; unsigned value = 0; do { string++; } while (isspace (*string)); while ((digit = todigit (*string)) < radix) { value *= radix; value += digit; string++; } while (isspace (*string)) { string++; } if (item->NUM_VLANIDS < (sizeof (item->VLANID) / sizeof (uint16_t))) { item->VLANID [item->NUM_VLANIDS++] = value; } } while (isspace (*string)) { string++; } if (*string) { error (1, EINVAL, "bad VLAN ID: ...[%s]", string); } return; }
static void function (const char * file, unsigned count, unsigned group, unsigned space, flag_t flags) { unsigned line = 1; unsigned radix = 0x10; unsigned width; unsigned digit; signed c = getc (stdin); while (c != EOF) { uint32_t vendor = 0; uint32_t device = 0; while (isspace (c)) { if (c == '\n') { line++; } c = getc (stdin); } if ((c == '#') || (c == ';')) { do { c = getc (stdin); } while ((c != '\n') && (c != EOF)); continue; } for (width = 0; width < ETHER_ADDR_LEN; width++) { if ((digit = todigit (c)) < radix) { vendor *= radix; vendor += digit; c = getc (stdin); continue; } error (1, EINVAL, "%s: line %d: Illegal vendor", file, line); } if (!vendor) { error (1, EPERM, "%s: line %d: Vendor can't be zero", file, line); } for (width = 0; width < ETHER_ADDR_LEN; width++) { if ((digit = todigit (c)) < radix) { device *= radix; device += digit; c = getc (stdin); continue; } error (1, EINVAL, "%s: line %d: Illegal device", file, line); } if (!device) { error (1, EPERM, "%s: line %d: Device can't be zero", file, line); } while (isspace (c)) { if (c == '\n') { line++; } c = getc (stdin); } MACPasswords (vendor, device, 1, count, group, space, flags); } return; }
size_t codeload (struct _code_ list [], size_t size) { size_t item = 0; char string [100]; signed c; while ((c = getc (stdin)) != EOF) { size_t lower = 0; size_t upper = item; unsigned radix = 10; unsigned digit = 0; unsigned value = 0; char * sp = string; while (isblank (c)) { c = getc (stdin); } if ((char)(c) == '0') { c = getc (stdin); switch ((char)(c)) { case 'x': case 'X': radix = 16; c = getc (stdin); break; case 'b': case 'B': radix = 2; c = getc (stdin); break; default: radix = 8; break; } } while ((digit = todigit (c)) < radix) { value *= radix; value += digit; c = getc (stdin); } while (isblank (c)) { c = getc (stdin); } if (isalpha (c) || ((char)(c) == '_')) { do { if ((size_t)(sp - string) < (sizeof (string) - 1)) { *sp++ = (char)(c); } c = getc (stdin); } while (isalnum (c) || ((char)(c) == '_')); } *sp = (char)(0); while (isblank (c)) { c = getc (stdin); } if ((sp == string) || ((char)(c) != '\n')) { error (1, EINVAL, "Bailing out"); } while (lower < upper) { size_t index = (lower + upper) >> 1; signed order = value - list [index].code; if (order < 0) { upper = index - 0; continue; } if (order > 0) { lower = index + 1; continue; } error (1, EINVAL, "Duplicate 0x%04x \"%s\"", value, string); } if (item < size) { for (upper = item++; upper > lower; upper--) { list [upper].code = list [upper-1].code; list [upper].name = list [upper-1].name; } list [upper].code = value; list [upper].name = (char const *)(strdup (string)); continue; } error (1, EOVERFLOW, "Bailing Out"); } return (item); }