pike.git / src / encode.c

version» Context lines:

pike.git/src/encode.c:16:   #include "multiset.h"   #include "dynamic_buffer.h"   #include "error.h"   #include "operators.h"   #include "builtin_functions.h"   #include "module_support.h"   #include "fsort.h"   #include "threads.h"   #include "stuff.h"   #include "version.h" + #include "bignum.h"    - RCSID("$Id: encode.c,v 1.42 1999/10/19 15:58:33 hubbe Exp $"); + RCSID("$Id: encode.c,v 1.43 1999/10/25 10:26:23 hubbe Exp $");    -  + /* #define ENCODE_DEBUG */ +  + #ifdef ENCODE_DEBUG + #define EDB(X) X + #else + #define EDB(X) + #endif +    #ifdef _AIX   #include <net/nh.h>   #endif      #ifdef HAVE_NETINET_IN_H   #include <netinet/in.h>   #endif      #include <math.h>   
pike.git/src/encode.c:132:   #define T_NEG 16   #define T_SMALL 32   #define SIZE_SHIFT 6   #define MAX_SMALL (1<<(8-SIZE_SHIFT))   #define COUNTER_START -MAX_SMALL      /* Let's cram those bits... */   static void code_entry(int type, INT32 num, struct encode_data *data)   {    int t; +  EDB( +  fprintf(stderr,"encode: code_entry(type=%d (%s), num=%d)\n", +  type, +  get_name_of_type(type), +  num) );    if(num<0)    {    type|=T_NEG;    num=~num;    }       if(num < MAX_SMALL)    {    type|=T_SMALL | (num << SIZE_SHIFT);    addchar(type);
pike.git/src/encode.c:298:    return;    }else{    mapping_insert(data->encoded, val, &data->counter);    data->counter.u.integer++;    }          switch(val->type)    {    case T_INT: +  /* FIXME: +  * if INT_TYPE is larger than 32 bits (not currently happening) +  * then this must be fixed to encode numbers over 32 bits as +  * Gmp.mpz objects +  */    code_entry(T_INT, val->u.integer,data);    break;       case T_STRING:    adddata(val->u.string);    break;       case T_FLOAT:    {    if(val->u.float_number==0.0)
pike.git/src/encode.c:363:    break;       case T_MULTISET:    code_entry(T_MULTISET, val->u.multiset->ind->size,data);    for(i=0; i<val->u.multiset->ind->size; i++)    encode_value2(ITEM(val->u.multiset->ind)+i, data);    break;       case T_OBJECT:    check_stack(1); +  + #ifdef AUTO_BIGNUM +  /* This could be implemented a lot more generic, +  * but that will have to wait until next time. /Hubbe +  */ +  if(is_bignum_object(val->u.object)) +  { +  code_entry(T_OBJECT, 2, data); +  /* 256 would be better, but then negative numbers +  * doesn't work... /Hubbe +  */ +  push_int(36); +  apply(val->u.object,"digits",1); +  if(sp[-1].type != T_STRING) +  error("Gmp.mpz->digits did not return a string!\n"); +  encode_value2(sp-1, data); +  pop_stack(); +  break; +  } + #endif +     push_svalue(val);    apply(data->codec, "nameof", 1);    switch(sp[-1].type)    {    case T_INT:    if(sp[-1].subtype == NUMBER_UNDEFINED)    {    pop_stack();    push_svalue(val);    f_object_program(1);
pike.git/src/encode.c:591:      static int my_extract_char(struct decode_data *data)   {    if(data->ptr >= data->len)    error("Format error, not enough data in string.\n");    return data->data [ data->ptr++ ];   }      #define GETC() my_extract_char(data)    - #define DECODE() \ -  what=GETC(); \ -  e=what>>SIZE_SHIFT; \ -  if(what & T_SMALL) { \ -  num=e; \ -  } else { \ -  num=0; \ -  while(e-->=0) num=(num<<8) + (GETC()+1); \ -  num+=MAX_SMALL - 1; \ -  } \ -  if(what & T_NEG) num=~num + #define DECODE(Z) do { \ +  EDB( \ +  fprintf(stderr,"decode(%s) at %d: ",(Z),__LINE__)); \ +  what=GETC(); \ +  e=what>>SIZE_SHIFT; \ +  if(what & T_SMALL) { \ +  num=e; \ +  } else { \ +  num=0; \ +  while(e-->=0) num=(num<<8) + (GETC()+1); \ +  num+=MAX_SMALL - 1; \ +  } \ +  if(what & T_NEG) num=~num; \ +  EDB( \ +  fprintf(stderr,"type=%d (%s), num=%d\n", \ +  (what & T_MASK), \ +  get_name_of_type(what & T_MASK), \ +  num) ); \ + } while (0)       -  +    #define decode_entry(X,Y,Z) \    do { \    INT32 what, e, num; \ -  DECODE(); \ +  DECODE("decode_entry"); \    if((what & T_MASK) != (X)) error("Failed to decode, wrong bits (%d).\n", what & T_MASK);\    (Y)=num; \    } while(0);      #define getdata2(S,L) do { \    if(data->ptr + (long)(sizeof(S[0])*(L)) > data->len) \    error("Failed to decode string. (string range error)\n"); \    MEMCPY((S),(data->data + data->ptr), sizeof(S[0])*(L)); \    data->ptr+=sizeof(S[0])*(L); \    }while(0)
pike.git/src/encode.c:634:    { \    case 1: for(e=0;e<num;e++) STR1(S)[e]=ntohs(STR1(S)[e]); break; \    case 2: for(e=0;e<num;e++) STR2(S)[e]=ntohl(STR2(S)[e]); break; \    }   #endif      #define get_string_data(STR,LEN, data) do { \    if((LEN) == -1) \    { \    INT32 what, e, num; \ -  DECODE(); \ +  DECODE("get_string_data"); \    what&=T_MASK; \    if(data->ptr + num > data->len || num <0) \    error("Failed to decode string. (string range error)\n"); \    if(what<0 || what>2) error("Failed to decode string. (Illegal size shift)\n"); \    STR=begin_wide_shared_string(num, what); \    MEMCPY(STR->str, data->data + data->ptr, num << what); \    data->ptr+=(num << what); \    BITFLIP(STR); \    STR=end_shared_string(STR); \    }else{ \
pike.git/src/encode.c:660:   }while(0)      #define getdata(X) do { \    long length; \    decode_entry(T_STRING, length,data); \    get_string_data(X, length, data); \    }while(0)      #define getdata3(X) do { \    INT32 what, e, num; \ -  DECODE(); \ +  DECODE("getdata3"); \    switch(what & T_MASK) \    { \    case T_INT: \    X=0; \    break; \    \    case T_STRING: \    get_string_data(X,num,data); \    break; \    \    default: \    error("Failed to decode string, type is wrong: %d\n",what & T_MASK); \    } \   }while(0)      #define decode_number(X,data) do { \    int what, e, num; \ -  DECODE(); \ +  DECODE("decode_number"); \    X=(what & T_MASK) | (num<<4); \    }while(0) \         static void low_decode_type(struct decode_data *data)   {    int tmp;   one_more_type:    push_type(tmp=GETC());    switch(tmp)
pike.git/src/encode.c:793:   #ifdef PIKE_DEBUG   #undef decode_value2   #define decode_value2(X) do { struct svalue *_=sp; decode_value2_(X); if(sp!=_+1) fatal("decode_value2 failed!\n"); } while(0)   #endif         {    INT32 what, e, num;    struct svalue tmp, *tmp2;    -  DECODE(); +  DECODE("decode_value2");       check_stack(1);       switch(what & T_MASK)    {    case T_AGAIN:    tmp.type=T_INT;    tmp.subtype=0;    tmp.u.integer=num;    if((tmp2=low_mapping_lookup(data->decoded, &tmp)))
pike.git/src/encode.c:834:    break;    }       case T_FLOAT:    {    INT32 num2=num;       tmp=data->counter;    data->counter.u.integer++;    -  DECODE(); +  DECODE("float");    push_float(LDEXP((double)num2, num));    break;    }       case T_ARRAY:    {    struct array *a;    if(num < 0)    error("Failed to decode string. (array size is negative)\n");   
pike.git/src/encode.c:964:    decode_value2(data);    if(!data->codec)    error("Failed to decode (no codec)\n");    apply(data->codec,"decode_object",2);    pop_stack();    }    if(data->pickyness && sp[-1].type != T_OBJECT)    error("Failed to decode object.\n");    return;    + #ifdef AUTO_BIGNUM +  /* It is possible that we should do this even without +  * AUTO_BIGNUM /Hubbe +  * However, that requires that some of the bignum functions +  * are always available... +  */ +  case 2: +  { +  check_stack(2); +  /* 256 would be better, but then negative numbers +  * doesn't work... /Hubbe +  */ +  push_int(36); +  convert_stack_top_with_base_to_bignum(); +  break; +  } +  + #endif +     default:    error("Object coding not compatible.\n");    break;    }    if(data->pickyness && sp[-1].type != T_OBJECT)    error("Failed to decode.\n");    break;       case T_FUNCTION:    tmp=data->counter;