コード例 #1
0
ファイル: bytespec.c プロジェクト: razzlefratz/MotleyTools
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);
}
コード例 #2
0
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);
}
コード例 #3
0
ファイル: xmledit.c プロジェクト: GSLyons/open-plc-utils
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;
}
コード例 #4
0
ファイル: serial.c プロジェクト: LucaBongiorni/open-plc-utils
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;
}
コード例 #5
0
ファイル: s_str.c プロジェクト: 01org/ixpdimm_sw
/*
 * 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;
}
コード例 #6
0
ファイル: num_put_float.cpp プロジェクト: RenEvo/dead6
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;
}
コード例 #7
0
ファイル: mac2pw.c プロジェクト: RozzieD/open-plc-utils
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;
}
コード例 #8
0
ファイル: serial.c プロジェクト: LucaBongiorni/open-plc-utils
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);
}
コード例 #9
0
ファイル: pov_1.c プロジェクト: CyberGrandChallenge/samples
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;
}
コード例 #10
0
ファイル: i3_id.c プロジェクト: surki/hipl
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;
}
コード例 #11
0
ファイル: cryptlib.c プロジェクト: Ana06/openssl
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;
}
コード例 #12
0
ファイル: xmledit.c プロジェクト: GSLyons/open-plc-utils
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);
}
コード例 #13
0
ファイル: mdioblock.c プロジェクト: tcdog001/apv5sdk-v15
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);
}
コード例 #14
0
ファイル: numencode.c プロジェクト: razzlefratz/MotleyTools
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));
}
コード例 #15
0
ファイル: s_str.c プロジェクト: 01org/ixpdimm_sw
/*
 * 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;
}
コード例 #16
0
ファイル: ast.c プロジェクト: unixfreak0037/yara-dev
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;
}
コード例 #17
0
ファイル: basespec.c プロジェクト: I2SE/open-plc-utils
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);
}
コード例 #18
0
ファイル: typeload.c プロジェクト: razzlefratz/MotleyTools
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);
}
コード例 #19
0
ファイル: uintspec.c プロジェクト: razzlefratz/MotleyTools
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);
}
コード例 #20
0
ファイル: psin.c プロジェクト: I2SE/open-plc-utils
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);
}
コード例 #21
0
ファイル: dataspec.c プロジェクト: I2SE/open-plc-utils
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);
}
コード例 #22
0
ファイル: plcfwd.c プロジェクト: LucaBongiorni/open-plc-utils
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;
}
コード例 #23
0
ファイル: mac2pwd.c プロジェクト: RozzieD/open-plc-utils
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;
}
コード例 #24
0
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);
}