pike.git / src / encode.c

version» Context lines:

pike.git/src/encode.c:18:   #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.70 2000/08/31 00:55:46 hubbe Exp $"); + RCSID("$Id: encode.c,v 1.71 2000/09/11 18:39:01 grubba Exp $");      /* #define ENCODE_DEBUG */      #ifdef ENCODE_DEBUG   #define EDB(X) X   #else   #define EDB(X)   #endif      /* The sp macro conflicts with Solaris 2.5.1's <sys/conf.h>. */
pike.git/src/encode.c:84:   #endif         /* Tags used by encode value.    * Currently they only differ from the PIKE_T variants by    * TAG_FLOAT == PIKE_T_TYPE == 7    * and    * TAG_TYPE == PIKE_T_FLOAT == 9    * These are NOT to be renumbered unless the file-format version is changed!    */ - /* Current encoding: ¶ik0 */ + /* Current encoding: ¶ik0 +  * +  * +---+-+-+-------+ +  * |s z|s|n|t y p e| +  * +---+-+-+-------+ +  * sz size/small int +  * s small int indicator +  * n negative (or rather inverted) +  * type TAG_type +  */   #define TAG_ARRAY 0   #define TAG_MAPPING 1   #define TAG_MULTISET 2   #define TAG_OBJECT 3   #define TAG_FUNCTION 4   #define TAG_PROGRAM 5   #define TAG_STRING 6   #define TAG_FLOAT 7   #define TAG_INT 8   #define TAG_TYPE 9 /* Not supported yet */
pike.git/src/encode.c:118:    int canonic;    struct object *codec;    struct svalue counter;    struct mapping *encoded;    dynamic_buffer buf;   };      static void encode_value2(struct svalue *val, struct encode_data *data);      #define addstr(s, l) low_my_binary_strcat((s), (l), &(data->buf)) - #define addchar(t) low_my_putchar((t),&(data->buf)) + #define addchar(t) low_my_putchar((char)(t), &(data->buf))      /* Code a pike string */      #if BYTEORDER == 4321   #define ENCODE_DATA(S) \    addstr( (S)->str, (S)->len << (S)->size_shift );   #else   #define ENCODE_DATA(S) \    switch((S)->size_shift) \    { \
pike.git/src/encode.c:179:   /* NOTE: Fix when type encodings change. */   static int type_to_tag(int type)   {    if (type == T_FLOAT) return TAG_FLOAT;    if (type == T_TYPE) return TAG_TYPE;    return type;   }   static int (*tag_to_type)(int) = type_to_tag;      /* Let's cram those bits... */ - static void code_entry(int tag, ptrdiff_t num, struct encode_data *data) + static void code_entry(int tag, INT64 num, struct encode_data *data)   {    int t;    EDB(    fprintf(stderr,"encode: code_entry(tag=%d (%s), num=%ld)\n",    tag,    get_name_of_type(tag_to_type(tag)),    (long)num) );    if(num<0)    {    tag |= TAG_NEG;    num = ~num;    }       if(num < MAX_SMALL)    {    tag |= TAG_SMALL | (num << SIZE_SHIFT); -  addchar(tag); +  addchar((char)tag);    return;    }else{    num -= MAX_SMALL;    }    -  for(t=0;t<3;t++) +  for(t = 0; (size_t)t < + #if 0 +  (sizeof(INT64)-1); + #else /* !0 */ +  (size_t)3; + #endif /* 0 */ +  t++)    { -  if(num >= (256 << (t<<3))) -  num-=(256 << (t<<3)); +  if(num >= (((INT64)256) << (t<<3))) +  num -= (((INT64)256) << (t<<3));    else    break;    }       tag |= t << SIZE_SHIFT; -  addchar(tag); +  addchar((char)tag);       switch(t)    { -  case 3: addchar(DO_NOT_WARN((num >> 24)&0xff)); -  case 2: addchar(DO_NOT_WARN((num >> 16)&0xff)); -  case 1: addchar(DO_NOT_WARN((num >> 8)&0xff)); -  case 0: addchar(DO_NOT_WARN(num&0xff)); + #if 0 +  case 7: addchar(DO_NOT_WARN((char)((num >> 56)&0xff))); +  case 6: addchar(DO_NOT_WARN((char)((num >> 48)&0xff))); +  case 5: addchar(DO_NOT_WARN((char)((num >> 40)&0xff))); +  case 4: addchar(DO_NOT_WARN((char)((num >> 32)&0xff))); + #endif /* 0 */ +  case 3: addchar(DO_NOT_WARN((char)((num >> 24)&0xff))); +  case 2: addchar(DO_NOT_WARN((char)((num >> 16)&0xff))); +  case 1: addchar(DO_NOT_WARN((char)((num >> 8)&0xff))); +  case 0: addchar(DO_NOT_WARN((char)(num&0xff)));    }   }      static void code_number(ptrdiff_t num, struct encode_data *data)   {    code_entry(DO_NOT_WARN(num & 15),    num >> 4, data);   }      #ifdef _REENTRANT
pike.git/src/encode.c:338:   #ifdef PIKE_DEBUG   #undef encode_value2   #define encode_value2(X,Y) do { struct svalue *_=Pike_sp; encode_value2_(X,Y); if(Pike_sp!=_) fatal("encode_value2 failed!\n"); } while(0)   #endif      {    static struct svalue dested = { T_INT, NUMBER_DESTRUCTED };    INT32 i;    struct svalue *tmp;    -  if((val->type == T_OBJECT || (val->type==T_FUNCTION && \ -  val->subtype!=FUNCTION_BUILTIN)) && !val->u.object->prog) -  val=&dested; +  if((val->type == T_OBJECT || +  (val->type==T_FUNCTION && val->subtype!=FUNCTION_BUILTIN)) && +  !val->u.object->prog) +  val = &dested;       if((tmp=low_mapping_lookup(data->encoded, val)))    {    code_entry(TAG_AGAIN, tmp->u.integer, data);    return;    }else{    mapping_insert(data->encoded, val, &data->counter);    data->counter.u.integer++;    }   
pike.git/src/encode.c:380:    error("Encoding of the type type not supported yet!\n");    break;       case T_FLOAT:    {    if(val->u.float_number==0.0)    {    code_entry(TAG_FLOAT,0,data);    code_entry(TAG_FLOAT,0,data);    }else{ -  INT32 x; +  INT64 x;    int y;    double tmp;       tmp = FREXP((double)val->u.float_number, &y); -  x = DO_NOT_WARN((INT32)((1<<30)*tmp)); -  y-=30; +  x = DO_NOT_WARN((INT64)((((INT64)1)<<(sizeof(INT64)*8 - 2))*tmp)); +  y -= sizeof(INT64)*8 - 2; +  +  EDB(fprintf(stderr, +  "Encoding float... tmp: %10g, x: 0x%016llx, y: %d\n", +  tmp, x, y)); +    #if 0 -  +  if (x && !(x & 0xffffffffUL)) { + #endif /* 0 */ +  x >>= 32; +  y += 32; +  +  EDB(fprintf(stderr, +  "Reducing float... x: 0x%08llx, y: %d\n", +  x, y)); + #if 0 +  } + #endif /* 0 */ + #if 0    while(x && y && !(x&1))    {    x>>=1;    y++;    }   #endif    code_entry(TAG_FLOAT,x,data);    code_entry(TAG_FLOAT,y,data);    }    break;
pike.git/src/encode.c:760:    return data->data [ data->ptr++ ];   }      #define GETC() my_extract_char(data)      #define DECODE(Z) do { \    EDB( \    fprintf(stderr,"decode(%s) at %d: ",(Z),__LINE__)); \    what=GETC(); \    e=what>>SIZE_SHIFT; \ +  numh=0; \    if(what & TAG_SMALL) { \    num=e; \    } else { \ -  +  INT32 numl; \    num=0; \ -  +  while(e > 4) { \ +  numh = (numh<<8) + (GETC()+1); \ +  e--; \ +  } \    while(e-->=0) num=(num<<8) + (GETC()+1); \ -  num+=MAX_SMALL - 1; \ +  numl = num + MAX_SMALL - 1; \ +  if (numl < num) numh++; \ +  num = numl; \    } \ -  if(what & TAG_NEG) num=~num; \ +  if(what & TAG_NEG) { \ +  num = ~num; \ +  numh = ~numh; \ +  } \    EDB( \ -  fprintf(stderr,"type=%d (%s), num=%d\n", \ +  fprintf(stderr,"type=%d (%s), num=%ld\n", \    (what & TAG_MASK), \    get_name_of_type(tag_to_type(what & TAG_MASK)), \ -  num) ); \ +  (long)num) ); \   } while (0)            #define decode_entry(X,Y,Z) \    do { \ -  INT32 what, e, num; \ -  DECODE("decode_entry"); \ -  if((what & TAG_MASK) != (X)) error("Failed to decode, wrong bits (%d).\n", what & TAG_MASK);\ +  INT32 what, e, num, numh; \ +  DECODE("decode_entry"); \ +  if((what & TAG_MASK) != (X)) \ +  error("Failed to decode, wrong bits (%d).\n", what & TAG_MASK); \    (Y)=num; \    } while(0);      #define getdata2(S,L) do { \    if(data->ptr + (ptrdiff_t)(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:806:    switch(what) \    { \    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; \ +  INT32 what, e, num, numh; \    DECODE("get_string_data"); \ -  what&=TAG_MASK; \ +  what &= TAG_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"); \ +  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{ \    if(data->ptr + (LEN) > data->len || (LEN) <0) \    error("Failed to decode string. (string range error)\n"); \    STR=make_shared_binary_string((char *)(data->data + data->ptr), (LEN)); \    data->ptr+=(LEN); \    } \   }while(0)      #define getdata(X) do { \    long length; \    decode_entry(TAG_STRING, length,data); \    get_string_data(X, length, data); \    }while(0)      #define getdata3(X) do { \ -  INT32 what, e, num; \ +  INT32 what, e, num, numh; \    DECODE("getdata3"); \    switch(what & TAG_MASK) \    { \    case TAG_INT: \    X=0; \    break; \    \    case TAG_STRING: \    get_string_data(X,num,data); \    break; \    \    default: \    error("Failed to decode string, tag is wrong: %d\n", \    what & TAG_MASK); \    } \   }while(0)      #define decode_number(X,data) do { \ -  int what, e, num; \ +  INT32 what, e, num, numh; \    DECODE("decode_number"); \    X=(what & TAG_MASK) | (num<<4); \    }while(0) \         static void restore_type_stack(unsigned char *old_stackp)   {   #if 0    fprintf(stderr, "Restoring type-stack: %p => %p\n",    Pike_compiler->type_stackp, old_stackp);
pike.git/src/encode.c:1023:      static void decode_value2(struct decode_data *data)      #ifdef PIKE_DEBUG   #undef decode_value2   #define decode_value2(X) do { struct svalue *_=Pike_sp; decode_value2_(X); if(Pike_sp!=_+1) fatal("decode_value2 failed!\n"); } while(0)   #endif         { -  INT32 what, e, num; +  INT32 what, e, num, numh;    struct svalue tmp, *tmp2;       DECODE("decode_value2");       check_stack(1);       switch(what & TAG_MASK)    {    case TAG_AGAIN:    tmp.type=T_INT;
pike.git/src/encode.c:1062:    struct pike_string *str;    tmp=data->counter;    data->counter.u.integer++;    get_string_data(str, num, data);    push_string(str);    break;    }       case TAG_FLOAT:    { -  INT32 num2=num; +  double res; // = (double)num;    -  tmp=data->counter; +  EDB(fprintf(stderr, "Decoding float... numh:0x%08x, num:0x%08x\n", +  numh, num)); +  +  res = LDEXP((double)numh, 32) + (double)(unsigned INT32)num; +  +  EDB(fprintf(stderr, "Mantissa: %10g\n", res)); +  +  tmp = data->counter;    data->counter.u.integer++;       DECODE("float"); -  push_float(LDEXP((double)num2, num)); +  +  EDB(fprintf(stderr, "Exponent: %d\n", num)); +  +  push_float(LDEXP(res, num));    break;    }       case TAG_TYPE:    {    error("Failed to decode string. "    "(decode of the type type isn't supported yet).\n");    break;    }