6aa8ef2001-05-23Per Hedbor #include "global.h"
4ad44b2001-08-14Henrik Grubbström (Grubba) #include "config.h" #ifdef HAVE_NETINET_IN_H #include <netinet/in.h> #endif #ifdef HAVE_INTTYPES_H #include <inttypes.h> #endif
6aa8ef2001-05-23Per Hedbor #include "stralloc.h" #include "interpret.h" #include "whitefish.h" #include "resultset.h" #include "blob.h" #include "buffer.h"
01b9212016-01-12Per Hedbor static inline int range( int n, int m )
1885102001-05-31Per Hedbor {
b3b2c52004-08-17Henrik Grubbström (Grubba)  int o; int f;
40a44d2004-08-07Johan Schön  if( !m ) m = 8; if( m >= 32*1024 ) { if( n < (32*1024-1) ) return 32*1024; return (n+1); }
b3b2c52004-08-17Henrik Grubbström (Grubba)  o = m; f = m+n;
40a44d2004-08-07Johan Schön  while( m<f ) m*=2; return m-o;
1885102001-05-31Per Hedbor }
40a44d2004-08-07Johan Schön static void wf_buffer_make_space( struct buffer *b, unsigned int n )
6aa8ef2001-05-23Per Hedbor {
6488792001-05-24Per Hedbor #ifdef PIKE_DEBUG
1885102001-05-31Per Hedbor  if( b->read_only )
a6f2292016-01-29Martin Nilsson  Pike_fatal("Oops\n");
6488792001-05-24Per Hedbor #endif
1885102001-05-31Per Hedbor  if( b->allocated_size-b->size < n )
6aa8ef2001-05-23Per Hedbor  {
1885102001-05-31Per Hedbor  b->allocated_size += range(n,b->allocated_size);
ca02712015-05-19Martin Nilsson  b->data = xrealloc(b->data,b->allocated_size);
6aa8ef2001-05-23Per Hedbor  } } void wf_buffer_wbyte( struct buffer *b, unsigned char s ) {
1885102001-05-31Per Hedbor  if( b->allocated_size == b->size ) wf_buffer_make_space( b, 1 );
6aa8ef2001-05-23Per Hedbor  b->data[b->size++] = s; } void wf_buffer_wshort( struct buffer *b, unsigned short s ) {
6488792001-05-24Per Hedbor  wf_buffer_make_space( b, 2 ); b->data[b->size++] = (s>>8); b->data[b->size++] = (s)&255;
6aa8ef2001-05-23Per Hedbor } void wf_buffer_wint( struct buffer *b, unsigned int s ) { s = htonl(s); wf_buffer_make_space( b, 4 );
59fc9e2014-09-03Martin Nilsson  memcpy( b->data+b->size, &s, 4 );
6aa8ef2001-05-23Per Hedbor  b->size += 4; }
6488792001-05-24Per Hedbor void wf_buffer_rewind_r( struct buffer *b, int n ) { if( n == -1 ) b->rpos = 0;
1b7a452001-05-31Per Hedbor  else if( b->rpos > (unsigned int)n )
6488792001-05-24Per Hedbor  b->rpos -= n; else b->rpos = 0; } void wf_buffer_rewind_w( struct buffer *b, int n ) { if( n == -1 ) b->size = 0;
1b7a452001-05-31Per Hedbor  else if( b->size > (unsigned int)n )
6488792001-05-24Per Hedbor  b->size -= n; else b->size = 0;
a6bba82017-10-04Henrik Grubbström (Grubba)  if (b->size > b->rpos) b->rpos = b->size;
6488792001-05-24Per Hedbor }
6aa8ef2001-05-23Per Hedbor 
1b7a452001-05-31Per Hedbor int wf_buffer_rbyte( struct buffer *b )
6aa8ef2001-05-23Per Hedbor { if( b->rpos < b->size )
6488792001-05-24Per Hedbor  return ((unsigned char *)b->data)[ b->rpos++ ];
6aa8ef2001-05-23Per Hedbor  return 0; }
1b7a452001-05-31Per Hedbor unsigned int wf_buffer_rint( struct buffer *b )
6aa8ef2001-05-23Per Hedbor {
d26f172018-02-13Henrik Grubbström (Grubba)  unsigned int res = wf_buffer_rbyte(b); res = res<<8 | wf_buffer_rbyte(b); res = res<<8 | wf_buffer_rbyte(b); return res<<8 | wf_buffer_rbyte(b);
6488792001-05-24Per Hedbor } int wf_buffer_memcpy( struct buffer *d, struct buffer *s, int nelems ) {
e81c402017-10-04Henrik Grubbström (Grubba)  if( (s->rpos+nelems) > s->size ) { #ifdef PIKE_DEBUG Pike_fatal("wf_buffer_memcpy(%p, %p, %d): Attempt to copy more than source (%d).\n", d, s, nelems, s->size-s->rpos); #endif
6488792001-05-24Per Hedbor  nelems = s->size-s->rpos;
e81c402017-10-04Henrik Grubbström (Grubba)  }
6488792001-05-24Per Hedbor  if( nelems <= 0 ) return 0; wf_buffer_make_space( d, nelems );
59fc9e2014-09-03Martin Nilsson  memcpy( d->data+d->size, s->data+s->rpos, nelems );
6488792001-05-24Per Hedbor  s->rpos += nelems; d->size += nelems; return nelems;
6aa8ef2001-05-23Per Hedbor } int wf_buffer_rshort( struct buffer *b ) {
d26f172018-02-13Henrik Grubbström (Grubba)  int res = wf_buffer_rbyte(b); return res<<8 | wf_buffer_rbyte(b);
6aa8ef2001-05-23Per Hedbor } int wf_buffer_eof( struct buffer *b ) { return b->rpos >= b->size; }
1b7a452001-05-31Per Hedbor void wf_buffer_seek( struct buffer *b, unsigned int pos )
6aa8ef2001-05-23Per Hedbor { b->rpos = pos; }
1b7a452001-05-31Per Hedbor  void wf_buffer_seek_w( struct buffer *b, unsigned int pos ) { #ifdef PIKE_DEBUG if( b->read_only )
a6f2292016-01-29Martin Nilsson  Pike_fatal( "Oops, read_only\n");
1b7a452001-05-31Per Hedbor #endif if( pos > b->size ) { wf_buffer_make_space( b, (unsigned int)(pos-b->size) );
21b12a2014-09-03Martin Nilsson  memset( b->data+b->size, 0, (unsigned int)(pos-b->size) );
1b7a452001-05-31Per Hedbor  } b->size = pos; }
13670c2015-05-25Martin Nilsson 
6aa8ef2001-05-23Per Hedbor void wf_buffer_clear( struct buffer *b ) {
1885102001-05-31Per Hedbor  if( !b->read_only && b->data ) free( b->data );
6aa8ef2001-05-23Per Hedbor  if( b->read_only && b->str ) free_string( b->str );
21b12a2014-09-03Martin Nilsson  memset(b, 0, sizeof(struct buffer));
6aa8ef2001-05-23Per Hedbor } void wf_buffer_free( struct buffer *b ) { wf_buffer_clear( b ); free( b ); } void wf_buffer_set_empty( struct buffer *b ) { wf_buffer_clear( b );
350d4b2004-07-20Henrik Grubbström (Grubba)  b->data = xalloc( 16 );
22b6762001-05-25Per Hedbor  b->allocated_size = 16;
6aa8ef2001-05-23Per Hedbor } void wf_buffer_set_pike_string( struct buffer *b, struct pike_string *data, int read_only ) { wf_buffer_clear( b ); if( read_only ) { b->read_only = 1; b->str = data;
107c242002-01-02Johan Schön  add_ref(data);
6aa8ef2001-05-23Per Hedbor  b->size = data->len;
396fbd2016-08-10Anders Johansson  b->data = STR0(data);
6aa8ef2001-05-23Per Hedbor  } else { b->size = data->len; b->data = malloc( b->size ); b->allocated_size = b->size;
59fc9e2014-09-03Martin Nilsson  memcpy( b->data, data->str, b->size );
6aa8ef2001-05-23Per Hedbor  } } struct buffer *wf_buffer_new( ) {
9c14f32014-04-27Martin Nilsson  return xcalloc( 1, sizeof( struct buffer ) );
6aa8ef2001-05-23Per Hedbor } void wf_buffer_append( struct buffer *b,
396fbd2016-08-10Anders Johansson  unsigned char *data,
6aa8ef2001-05-23Per Hedbor  int size ) { wf_buffer_make_space( b, size );
59fc9e2014-09-03Martin Nilsson  memcpy( b->data+b->size, data, size );
6111a72001-05-24Per Hedbor  b->size+=size;
6aa8ef2001-05-23Per Hedbor }