Example #1
0
/* A simplified loop for vi. Don't dispatch key at end.
   Don't recognize minus sign? */
static int
rl_digit_loop1 (void)
{
  int key, c;

  while (1)
    {
      rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
      key = c = rl_read_key ();

      if (_rl_keymap[c].type == ISFUNC &&
	  _rl_keymap[c].function == (Function *)rl_universal_argument)
	{
	  rl_numeric_arg *= 4;
	  continue;
	}

      c = UNMETA (c);
      if (digit_p (c))
	{
	  if (rl_explicit_arg)
	    rl_numeric_arg = (rl_numeric_arg * 10) + digit_value (c);
	  else
	    rl_numeric_arg = digit_value (c);
	  rl_explicit_arg = 1;
	}
      else
	{
	  rl_clear_message ();
	  rl_stuff_char (key);
	  break;
	}
    }
  return (0);
}
Example #2
0
long unsigned int strtoul(const char *nptr, char **endptr, int base) {
  /* First, figure out the base. */
  if (base == 0 && nptr[0] && nptr[0] == '0' &&
      nptr[1] && (nptr[1] == 'x' || nptr[1] == 'X')) {
    base = 16;
  } else if (base == 0 && nptr[0] && nptr[0] == '0') {
    base = 8;
  } else if (base == 0 && nptr[0] && isdigit_for_base(nptr[0], 10)) {
    base = 10;
  }

  if (base == 0) {
    /* Bail out at this point. Failed to auto-detect
       the base. */
    if (endptr) *endptr = (char*)nptr;
    return 0;
  }

  /* Skip over '0x' if needed. */
  if (base == 16 && nptr[0] && nptr[0] == '0' &&
      nptr[1] && (nptr[1] == 'x' || nptr[1] == 'X')) {
    nptr += 2;
  }

  long unsigned int accum = 0;
  while (*nptr && isdigit_for_base(*nptr, base)) {
    accum *= base;
    accum += digit_value(*nptr++);
  }
  
  /* Success! */
  if (endptr) *endptr = (char*)nptr;
  return accum;
}
bool string_to_int(string s, int & n)
{ int base=10, start=0, len=s.length(), sign=1;
  if (s[0]=='+')
    s=s.substr(1);
  else if (s[0]=='-')
  { sign=-1;
    s=s.substr(1); }
  if (s[0]=='0')
  { if (s[1]=='X' || s[1]=='x')
    { base=16;
      start=2; }
    else if (s[1]=='B' || s[1]=='b')
    { base=2;
      start=2; }
    else if (s[1]=='O' || s[1]=='o')
    { base=2;
      start=2; }
    else if (s[1]=='D' || s[1]=='d')
    { base=10;
      start=2; } }
  if (start>=len)
    return false;
  int val=0;
  for (int i=start; i<len; i+=1)
  { int d=digit_value(s[i]);
    if (d>=base)
      return false;
    val=val*base+d; }
  if (sign==-1)
    n=-val;
  else
    n=val;
  return true; }
Example #4
0
/**
 * Convert string to numeric value
 *
 * @v string		String
 * @v endp		End pointer (or NULL)
 * @v base		Numeric base (or zero to autodetect)
 * @ret value		Numeric value
 */
unsigned long long strtoull ( const char *string, char **endp, int base ) {
	unsigned long long value = 0;
	unsigned int digit;
	int negate;

	/* Preprocess string */
	string = strtoul_pre ( string, &negate, &base );

	/* Process digits */
	for ( ; ; string++ ) {
		digit = digit_value ( *string );
		if ( digit >= ( unsigned int ) base )
			break;
		value = ( ( value * base ) + digit );
	}

	/* Negate value if, applicable */
	if ( negate )
		value = -value;

	/* Fill in end pointer, if applicable */
	if ( endp )
		*endp = ( ( char * ) string );

	return value;
}
Example #5
0
static int decrypt_keysig(const char kas[NWZ_KAS_SIZE], char key[NWZ_KEY_SIZE],
    char sig[NWZ_SIG_SIZE])
{
    uint8_t src[NWZ_KAS_SIZE / 2];
    for(int index = 0; index < NWZ_KAS_SIZE / 2; index++)
    {
        int a = digit_value(kas[index * 2]);
        int b = digit_value(kas[index * 2 + 1]);
        if(a < 0 || b < 0)
        {
            cprintf(GREY, "Invalid KAS !\n");
            return -1;
        }
        src[index] = a << 4 | b;
    }
    fwp_setkey("ed295076");
    fwp_crypt(src, sizeof(src), 1);
    memcpy(key, src, NWZ_KEY_SIZE);
    memcpy(sig, src + NWZ_KEY_SIZE, NWZ_SIG_SIZE);
    return 0;
}
Example #6
0
obj string_to_fixnum( char *str_in, UINT_32 len, unsigned radix )
{
int i;
rs_bool neg = NO;
UINT_32 v, preq, prem;
UINT_8 *lim = ((UINT_8*)str_in) + len;
UINT_8 *str = (UINT_8*)str_in;

    if (*str == '-')
    {
	str++;
	neg = YES;
    }
    else if (*str == '+')
    {
	str++;
    }

    /* compute the maximum value & digit that is
       allowed BEFORE a new digit is added.
       For example, if the limit is 1024 (max 1023)
       and we're in base 10, the preq is 102 and prem is 4
       is if the value so far is 102 and we see a 4, we know
       that's too much, but if we see a 3, that's OK */

    preq = (1UL<<(WORD_SIZE_BITS-PRIMARY_TAG_SIZE-1)) / radix;
    prem = (1UL<<(WORD_SIZE_BITS-PRIMARY_TAG_SIZE-1)) % radix;

    /* printf( "preq = %d, prem = %d\n", preq, prem ); */
    
    if (str >= lim)
      return FALSE_OBJ;  /* no digits! */
    
    v = 0;
    while (str < lim)
      {
	i = digit_value( *str++ );
	if (i >= radix)
	  {
	    return FALSE_OBJ;
	  }
	if ((v > preq)
	    || ((v == preq) 
		&& ((i > prem) || ((i == prem) && !neg))))
	  {
	    /* too big -- doesn't fit as a fixnum */
	    return FALSE_OBJ;
	  }
	v = v * radix + i;
      }
    return int2fx( neg ? -v : v );
}
Example #7
0
static INT read_int(ptr *v, ptr n, INT r, IBOOL sign) {
  INT i, c;

  for (;;) {
    if ((i = digit_value((c = getchar()), r)) == -1) {
      ungetc(c, stdin);
      break;
    }
    n = S_add(S_mul(n, FIX(r)), FIX(i));
  }
  *v = sign ? S_sub(FIX(0), n) : n;
  return r_CONST;
}
Example #8
0
sexp sexp_read_bignum (sexp ctx, sexp in, sexp_uint_t init,
                       signed char sign, sexp_uint_t base) {
  int c, digit;
  sexp_gc_var1(res);
  sexp_gc_preserve1(ctx, res);
  res = sexp_make_bignum(ctx, SEXP_INIT_BIGNUM_SIZE);
  sexp_bignum_sign(res) = sign;
  sexp_bignum_data(res)[0] = init;
  for (c=sexp_read_char(ctx, in); sexp_isxdigit(c); c=sexp_read_char(ctx, in)) {
    digit = digit_value(c);
    if ((digit < 0) || (digit >= base))
      break;
    res = sexp_bignum_fxmul(ctx, res, res, base, 0);
    res = sexp_bignum_fxadd(ctx, res, digit);
  }
  if (c=='.' || c=='e' || c=='E') {
    if (base != 10) {
      res = sexp_read_error(ctx, "found non-base 10 float", SEXP_NULL, in);
    } else {
      if (c!='.') sexp_push_char(ctx, c, in); /* push the e back */
      res = sexp_read_float_tail(ctx, in, sexp_bignum_to_double(res), (sign==-1));
    }
#if SEXP_USE_RATIOS
  } else if (c=='/') {
    res = sexp_bignum_normalize(res);
    res = sexp_make_ratio(ctx, res, SEXP_ONE);
    sexp_ratio_denominator(res) = sexp_read_number(ctx, in, 10);
    res = sexp_ratio_normalize(ctx, res, in);
#endif
#if SEXP_USE_COMPLEX
  } else if (c=='i' || c=='i' || c=='+' || c=='-') {
    sexp_push_char(ctx, c, in);
    res = sexp_bignum_normalize(res);
    res = sexp_read_complex_tail(ctx, in, res);
#endif
  } else if ((c!=EOF) && ! sexp_is_separator(c)) {
    res = sexp_read_error(ctx, "invalid numeric syntax",
                          sexp_make_character(c), in);
  } else {
    sexp_push_char(ctx, c, in);
  }
  sexp_gc_release1(ctx);
  return sexp_bignum_normalize(res);
}
Example #9
0
/* read a constant from the input buffer */
Boolean parse_constant(Token *t, int *value)
{
    /* remember where it starts */
    int b0 = token_index;
    int base = 10;
    int n = 0;
    int sign = 1;

    /* negative numbers */
    if (input_buffer[token_index] == '-')
        {
            sign = -1;
            token_index += 1;
        }

    /* base is decimal unless ... */
    if (input_buffer[token_index] == '0')
        {
            /* 0...  -- octal */
            token_index += 1;
            base = 8;
            if (input_buffer[token_index] == 'x')
                {
                    /* 0x...  -- hex */
                    token_index += 1;
                    base = 16;
                }
        }
    while (is_valid_digit(input_buffer[token_index], base))
        {
            n = n * base + digit_value(input_buffer[token_index], base);
            token_index += 1;
        }
    *value = sign * n;

    t->token_string = copy_token_string(b0, token_index);

    if (isalpha(input_buffer[token_index]))
        return(FALSE);
    else
        return(TRUE);
}
Example #10
0
long long
os_strtoll(
    const char *str,
    char **endptr,
    os_int32 base)
{
    long long value = 0LL;
    long long sign = 1LL;
    long long radix;
    long long dvalue;

    errno = 0;

    if (endptr) {
        *endptr = (char *)str;
    }
    while (isspace ((int)*str)) {
        str++;
    }
    if (*str == '-') {
        sign = -1LL;
        str++;
    } else {
        if (*str == '+') {
            sign = 1LL;
            str++;
    	}
    }
    if (base == 0) {
        /* determine radix from string str */
        if (*str == '\0') {
            errno = EINVAL;
            return value;
        }
        if (*str == '0') {
            str++;
            /* base = 8, 10 or 16 */
            if (*str == '\0') {
                base = 10;
            } else {
                if (*str == 'x' || *str == 'X') {
                    base = 16;
                    str++;
                } else {
                    if (*str >= '0' || *str <= '7') {
                        base = 8;
                    } else {
                    	errno = EINVAL;
                    	return value;
                    }
            	}
            }
        } else {
            if (*str >= '1' || *str <= '9') {
                base = 10;
            }
        }
    } else {
        if (base < 2) {
            /* invalid radix */
            errno = EINVAL;
            return value;
        } else {
            if (base > 36) {
                /* invalid radix */
                errno = EINVAL;
                return value;
            } else {
                if (base == 16) {
                    /* Check if prefixed by 0x or 0X */
                    if ((*str == '0') &&
                        (*(str+1) == 'x' || *(str+1) == 'X')) {
                        str++;
                        str++;
                    }
                }
            }
        }
    }
    radix = (long long)base;
    dvalue = digit_value (*str, base);
    while (dvalue >= 0) {
        value = value * radix + dvalue;
        str++;
        dvalue = digit_value (*str, base);
    }
    if (endptr) {
        *endptr = (char *)str;
    }

    return value*sign;
}
Example #11
0
obj string_to_float( char *str_in, UINT_32 len, unsigned radix )
{
  UINT_8 *str = (UINT_8*)str_in;
  double x = 0.0, r = radix;
  double v = 0.0;
  unsigned i, num_digits = 0;
  int exp, exp_neg;
  rs_bool neg = NO;
  
  if (*str == '-')
    {
      str++;
      neg = YES;
    }
  else if (*str == '+')
    {
      str++;
    }

  while (*str && *str != '.')
    {
      i = digit_value( *str++ );
      if (i >= radix)
	{
	  /* note that this notation cannot be used in radix >= 15 */
	  if (i == 14) /* e|E */
	    {
	      goto float_exp;
	    }
	  return FALSE_OBJ;
	}
      v = v * radix + i;
      num_digits++;
    }
  if (*str == '.')
  {
    str++;
    r = 1.0 / r;
    x = r;
    while (*str)
      {
	i = digit_value( *str++ );
	if (i >= radix)
	  {
	    if (i == 14) /* e|E */
	      {
		goto float_exp;
	      }
	    return FALSE_OBJ;
	  }
	v += i * x;
	x *= r;
	num_digits++;
      }
  }
  if (num_digits == 0)
    return FALSE_OBJ;  /* no digits -- "." is invalid */
  
  return make_float( neg ? -v : v );

 float_exp:

  exp = 0;
  exp_neg = 0;

  if (*str == '-')
    {
      exp_neg = 1;
      str++;
    }
  else if (*str == '+')
    {
      str++;
    }

  while (*str)
    {
      i = digit_value( *str++ );
      if (i >= 10)
	{
	  /* exponents are always in decimal */
	  return FALSE_OBJ;
	}
      exp = (exp * 10) + i;
      if (exp > 1000000)
	return FALSE_OBJ;  /* exponent too big! */
    }
  return make_float( (neg ? -v : v) * pow( radix, exp_neg ? -exp : exp ) );
}
Example #12
0
static INT read_token(ptr *v) {
  ICHAR c = getchar();
  switch (c) {
    case SEOF: return r_EOF;
    case '\n':
    case ' ': return read_token(v);
    case ';':
      for (;;) {
        switch (getchar()) {
          case SEOF:
           return r_EOF;
          case '\n':
           return read_token(v);
          default:
           break;
        }
      }
    case '(': return r_LPAREN;
    case ')': return r_RPAREN;
    case '#': {
      ICHAR c = getchar();
      INT r = 10;
      switch (c) {
        case 'x':
          r = 16;
        case 'o':
          if (r == 0) r = 8;
        case 'b':
          if (r == 10) r = 2;
        case 'd': {
          INT i;
          IBOOL sign = 0;
          c = getchar();
          if (c == '+')
            c = getchar();
          else if (c == '-') {
            sign = 1;
            c = getchar();
          }

          if ((i = digit_value(c, r)) != -1)
            return read_int(v, FIX(i), r, sign);
        }
        default:
          printf("malformed hash prefix ignored\n");
          return read_token(v);
      }
    }
    case '+':
    case '-': {
      INT i, c2;
      if ((i = digit_value((c2 = getchar()), 10)) == -1) {
        ungetc(c2, stdin);
      } else {
        return read_int(v, FIX(i), 10, c == '-');
      }
    }
    case '*':
    case '/':
    case 'q':
    case 'r':
    case 'g':
    case '=':
    case '<':
    case 'f':
    case 'c':
    case 'd':
      *v = Schar(c);
      return r_CONST;
    default: {
      INT i;
      if ((i = digit_value(c, 10)) != -1)
        return read_int(v, FIX(i), 10, 0);
      }
      break;
  }
  printf("invalid character %d ignored\n", c);
  return read_token(v);
}
Example #13
0
int
rl_vi_domove (int key, int *nextkey)
{
  int c, save;
  int old_end;

  rl_mark = rl_point;
  c = rl_read_key ();
  *nextkey = c;

  if (!member (c, vi_motion))
    {
      if (digit_p (c))
	{
	  save = rl_numeric_arg;
	  rl_numeric_arg = digit_value (c);
	  rl_digit_loop1 ();
	  rl_numeric_arg *= save;
	  c = rl_read_key ();	/* real command */
	  *nextkey = c;
	}
      else if (key == c && (key == 'd' || key == 'y' || key == 'c'))
	{
	  rl_mark = rl_end;
	  rl_beg_of_line (1, c);
	  _rl_vi_last_motion = c;
	  return (0);
	}
      else
	return (-1);
    }

  _rl_vi_last_motion = c;

  /* Append a blank character temporarily so that the motion routines
     work right at the end of the line. */
  old_end = rl_end;
  rl_line_buffer[rl_end++] = ' ';
  rl_line_buffer[rl_end] = '\0';

  _rl_dispatch (c, _rl_keymap);

  /* Remove the blank that we added. */
  rl_end = old_end;
  rl_line_buffer[rl_end] = '\0';
  if (rl_point > rl_end)
    rl_point = rl_end;

  /* No change in position means the command failed. */
  if (rl_mark == rl_point)
    return (-1);

  /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
     word.  If we are not at the end of the line, and we are on a
     non-whitespace character, move back one (presumably to whitespace). */
  if ((to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
      !whitespace (rl_line_buffer[rl_point]))
    rl_point--;

  /* If cw or cW, back up to the end of a word, so the behaviour of ce
     or cE is the actual result.  Brute-force, no subtlety. */
  if (key == 'c' && rl_point >= rl_mark && (to_upper (c) == 'W'))
    {
      /* Don't move farther back than where we started. */
      while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
	rl_point--;

      /* Posix.2 says that if cw or cW moves the cursor towards the end of
	 the line, the character under the cursor should be deleted. */
      if (rl_point == rl_mark)
        rl_point++;
      else
	{
	  /* Move past the end of the word so that the kill doesn't
	     remove the last letter of the previous word.  Only do this
	     if we are not at the end of the line. */
	  if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point]))
	    rl_point++;
	}
    }

  if (rl_mark < rl_point)
    exchange (rl_point, rl_mark);

  return (0);
}