pike.git / src / modules / _Stdio / buffer.cmod

version» Context lines:

pike.git/src/modules/_Stdio/buffer.cmod:589:    static INT_TYPE io_read_number_uc( Buffer *io, size_t len )    {    INT_TYPE res = 0;    while( LIKELY(len--) ) {    res <<= 8;    res |= io_read_byte_uc(io);    }    return res;    }    +  static INT_TYPE io_read_le_number_uc( Buffer *io, size_t len ) +  { +  INT_TYPE res = 0; +  size_t i; +  for(i=0; i<len; i++) +  res |= (io_read_byte_uc(io) << i*8); +  return res; +  } +     static INT_TYPE io_read_signed_number_uc( Buffer *io, size_t len )    {    INT_TYPE res = 0;    if( LIKELY(len--) ) {    res = (INT8)io_read_byte_uc(io);    while( LIKELY(len--) ) {    res <<= 8;    res |= io_read_byte_uc(io);    }    }    return res;    }    -  static INT64 io_read_number( Buffer *io, size_t len ) +  static INT64 io_read_number( Buffer *io, size_t len, int endian )    {    INT64 res;    if( !io_avail(io, len) )    return -1;    /* ensure only leading 0:s */    for (; UNLIKELY(len > SIZEOF_INT_TYPE); len--)    if( UNLIKELY(io_read_byte_uc(io)) )    Pike_error("Integer (%dbit) overflow.\n", SIZEOF_INT_TYPE*8);    -  +  if( endian )    res = io_read_number_uc( io, len ); -  +  else +  res = io_read_le_number_uc( io, len );       if ( UNLIKELY(res < 0) )    Pike_error("Signed (%dbit) overflow.\n", SIZEOF_INT_TYPE*8);    return res;    }    -  static struct object *io_read_bignum( Buffer *io, size_t len ) +  static struct object *io_read_bignum( Buffer *io, size_t len, int endian )    {    struct object *o;    MP_INT *i;    unsigned char *p;       if( !io_avail(io,len) ) return NULL;    o = fast_clone_object(bignum_program);    i = (void*)o->storage; -  mpz_import( i, len, 1, 1, 0, 0, io_read_pointer(io) ); +  mpz_import( i, len, 1, endian, 0, 0, io_read_pointer(io) );    io_consume(io,len);    return o;    }       static void io_add_bignum( Buffer *io, struct object *o, int width )    ATTRIBUTE((noclone,noinline));       static void io_add_bignum( Buffer *io, struct object *o, int width )    {    MP_INT *i = (void*)o->storage;
pike.git/src/modules/_Stdio/buffer.cmod:1709:    *! Note that pike string can not be longer than 0x7fffffff bytes (~2Gb).    */    PIKEFUN string(8bit) read_hstring( int bytes, void|int offset )    {    INT64 len;    Buffer *io = THIS;    struct pike_string *s;    ONERROR e;       io_rewind_on_error( io, &e ); -  len = io_read_number( io, bytes ); +  len = io_read_number( io, bytes, 1 );       if (offset)    len -= offset->u.integer;       if (len < 0) {    /* io_avail() in io_read_number() failed. */    CALL_AND_UNSET_ONERROR(e);    Pike_sp[-1].u.integer = 0;    return;    }
pike.git/src/modules/_Stdio/buffer.cmod:1971:    INT64 len;    int do_copy = 0;    Buffer *io = THIS;    ONERROR e;       io_rewind_on_error( io, &e );       if( copy ) do_copy = copy->u.integer;    Pike_sp-=args;    -  len = io_read_number( io, bytes ); +  len = io_read_number( io, bytes, 1 );    if( len >= 0 && io_avail( io, len ) )    {    push_object( io_read_buffer( io, len, do_copy ) );    io_unset_rewind_on_error( io, &e );    return;    }    CALL_AND_UNSET_ONERROR(e);    push_int(0);    }   
pike.git/src/modules/_Stdio/buffer.cmod:2400:    {    Buffer *io = THIS;    if( LIKELY(io_avail( io, 4 )) )    {    push_int( io_read_number_uc(io,4) );   #if SIZEOF_INT_TYPE < 5    if( UNLIKELY(Pike_sp[-1].u.integer < 0) )    {    io_rewind( io, 4 );    pop_stack(); -  push_object( io_read_bignum(io, 4) ); +  push_object( io_read_bignum(io, 4, 1) );    }   #endif    }    else    push_int(-1);    }       /*! @decl int read_int( int n )    *!    *! Read a network byte order unsigned number of size n*8 bits, then    *! return it.    *!    *! Will return -1 if there is not enough buffer space available    *! unless error mode is set to throw errors. -  +  *! +  *! @seealso +  *! @[read_le_int]    */    PIKEFUN int(0..) read_int( int len )    {    Buffer *io = THIS;    struct object *o;       Pike_sp--;       if( len < SIZEOF_INT_TYPE-1 ) /* will for sure fit. */    { -  push_int( io_read_number( io, len ) ); +  push_int( io_read_number( io, len, 1 ) );    return;    }    -  if( (o = io_read_bignum(io, len )) ) +  if( (o = io_read_bignum(io, len, 1)) )    {    push_object(o);    reduce_stack_top_bignum();    return;    }    push_int(-1);    }    -  +  /*! @decl int read_int( int n ) +  *! +  *! Read a big endian byte order unsigned number of size n*8 bits, +  *! then return it. +  *! +  *! Will return -1 if there is not enough buffer space available +  *! unless error mode is set to throw errors. +  *! +  *! @seealso +  *! @[read_int] +  */ +  PIKEFUN int(0..) read_le_int( int len ) +  { +  Buffer *io = THIS; +  struct object *o; +  +  Pike_sp--; +  +  if( len < SIZEOF_INT_TYPE-1 ) /* will for sure fit. */ +  { +  push_int( io_read_number( io, len, 0 ) ); +  return; +  } +  +  if( (o = io_read_bignum(io, len, 0)) ) +  { +  push_object(o); +  reduce_stack_top_bignum(); +  return; +  } +  push_int(-1); +  } +     /*! @decl int read_hint( int n )    *!    *! Read a network byte order unsigned number of size n*8 bits, then    *! read another network byte order number of the size indicated by    *! the first size.    *!    *! Will return -1 if there is not enough buffer space available    *! unless error mode is set to throw errors.    */    PIKEFUN int(0..) read_hint( int size_len )    {    Buffer *io = THIS;    ONERROR e;    INT_TYPE len;    struct object *o;       io_rewind_on_error( io, &e );    -  len = io_read_number( io, size_len ); +  len = io_read_number( io, size_len, 1 );    if( len >= 0 )    {    if( len < SIZEOF_INT_TYPE )    { -  if( (Pike_sp[-1].u.integer = io_read_number( io, len )) == -1 ) +  if( (Pike_sp[-1].u.integer = io_read_number( io, len, 1 )) == -1 )    goto neg_one;    } -  else if( (o = io_read_bignum( io, len )) ) +  else if( (o = io_read_bignum( io, len, 1 )) )    {    Pike_sp--;    push_object(o);    reduce_stack_top_bignum();    }    else    goto neg_one;    io_unset_rewind_on_error( io, &e );    return;    }
pike.git/src/modules/_Stdio/buffer.cmod:2513:    if( len < SIZEOF_INT_TYPE-1 ) /* will for sure fit. */    {    push_array(a = allocate_array(num));    for( i=0;i<num;i++ )    a->item[i].u.integer = io_read_number_uc( io, len );    return;    }       for( i=0; i<num; i++ )    { -  push_object(io_read_bignum( io, len )); +  push_object(io_read_bignum( io, len, 1 ));    reduce_stack_top_bignum();    }    f_aggregate(num);    }      /*! @decl string _encode()    *! @decl void _decode(string x)    *!    *! Encode and decode Stdio.Buffer objects.    *! Only the buffer data is kept, no other state is saved.