pike.git / src / encode.c

version» Context lines:

pike.git/src/encode.c:174:    return data->codec = lookup_codec (encoder_str);   }      /* Convert to/from forward reference ID. */   #define CONVERT_ENTRY_ID(ID) (-((ID) - COUNTER_START) - (-COUNTER_START + 1))      static void encode_value2(struct svalue *val, struct encode_data *data, int force_encode);      #define addstr(s, l) low_my_binary_strcat((s), (l), &(data->buf))   #define addchar(t) low_my_putchar((char)(t), &(data->buf)) + #define addchar_unsafe(t) buffer_add_char_unsafe(&(data->buf), t)      /* Code a pike string */      #if PIKE_BYTEORDER == 4321   #define ENCODE_DATA(S) \    addstr( (S)->str, (S)->len << (S)->size_shift );   #else - #define ENCODE_DATA(S) \ -  switch((S)->size_shift) \ -  { \ -  case 1: \ -  for(q=0;q<(S)->len;q++) { \ -  INT16 s=htons( STR1(S)[q] ); \ -  addstr( (char *)&s, sizeof(s)); \ -  } \ -  break; \ -  case 2: \ -  for(q=0;q<(S)->len;q++) { \ -  INT32 s=htonl( STR2(S)[q] ); \ -  addstr( (char *)&s, sizeof(s)); \ -  } \ -  break; \ -  case 0: /* Silence clang warning */ \ -  break; \ -  } + #define ENCODE_DATA(S) do { \ +  buffer_ensure_space(&(data->buf), (S)->len << (S)->size_shift); \ +  if ((S)->size_shift == 1) { \ +  const p_wchar1 *src = STR1(S); \ +  ptrdiff_t len = (S)->len, q; \ +  for(q=0;q<len;q++) { \ +  buffer_add_be16_unsafe(&(data->buf), src[q]); \ +  } \ +  } else { /* shift 2 */ \ +  const p_wchar2 *src = STR2(S); \ +  ptrdiff_t len = (S)->len, q; \ +  for(q=0;q<len;q++) { \ +  buffer_add_be32_unsafe(&(data->buf), src[q]); \ +  } \ +  } \ + } while(0)   #endif    - #define adddata(S) do { \ -  if((S)->size_shift) \ -  { \ + #define adddata(S) do { \ +  const struct pike_string *__str = (S); \ +  if(__str->size_shift) \ +  { \    int q; \ -  code_entry(TAG_STRING,-1, data); \ -  code_entry((S)->size_shift, (S)->len, data); \ -  ENCODE_DATA(S); \ -  }else{ \ -  code_entry(TAG_STRING, (S)->len, data); \ -  addstr((char *)((S)->str),(S)->len); \ -  } \ +  code_entry(TAG_STRING,-1, data); \ +  code_entry(__str->size_shift, __str->len, data); \ +  ENCODE_DATA(__str); \ +  }else{ \ +  code_entry(TAG_STRING, __str->len, data); \ +  addstr((char *)(__str->str),__str->len); \ +  } \   }while(0)    - #define adddata2(s,l) addstr((char *)(s),(l) * sizeof((s)[0])); -  +    #ifdef ENCODE_DEBUG   /* NOTE: Fix when type encodings change. */   static int tag_to_type(int tag)   {    if (tag == TAG_FLOAT) return T_FLOAT;    if (tag == TAG_TYPE) return T_TYPE;    if (tag <= MAX_TYPE) return tag ^ MIN_REF_TYPE;    return tag;   }   #endif      /* Let's cram those bits... */   static void code_entry(int tag, INT64 num, struct encode_data *data)   {    int t; -  +  /* this needs 5 bytes maximal */ +  buffer_ensure_space(&data->buf, 5);    EDB(5,    fprintf(stderr,"%*sencode: code_entry(tag=%d (%s), num=%ld)\n",    data->depth, "", 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((char)tag); +  addchar_unsafe((char)tag);    return;    }else{    num -= MAX_SMALL;    }       /* NB: There's only space for two bits of length info. */    for(t = 0; (size_t)t < 3; t++)    {    if(num >= (((INT64)256) << (t<<3)))    num -= (((INT64)256) << (t<<3));    else    break;    }       tag |= t << SIZE_SHIFT; -  addchar((char)tag); +  addchar_unsafe((char)tag);       switch(t)    { -  case 3: addchar((char)((num >> 24)&0xff)); -  case 2: addchar((char)((num >> 16)&0xff)); -  case 1: addchar((char)((num >> 8)&0xff)); -  case 0: addchar((char)(num&0xff)); +  case 3: addchar_unsafe((char)((num >> 24)&0xff)); +  case 2: addchar_unsafe((char)((num >> 16)&0xff)); +  case 1: addchar_unsafe((char)((num >> 8)&0xff)); +  case 0: addchar_unsafe((char)(num&0xff));    }   }      static void code_number(ptrdiff_t num, struct encode_data *data)   {    EDB(5, fprintf(stderr, "%*scode_number(%d)\n",    data->depth, "", num));    code_entry(num & 15, num >> 4, data);   }   
pike.git/src/encode.c:369:    case T_ARRAY:    case T_MULTISET:    case T_NOT:    t = t->car;    goto one_more_type;       case T_INT:    {    ptrdiff_t val;    -  val = CAR_TO_INT(t); -  addchar((val >> 24)&0xff); -  addchar((val >> 16)&0xff); -  addchar((val >> 8)&0xff); -  addchar(val & 0xff); -  val = CDR_TO_INT(t); -  addchar((val >> 24)&0xff); -  addchar((val >> 16)&0xff); -  addchar((val >> 8)&0xff); -  addchar(val & 0xff); +  buffer_add_be32(&data->buf, CAR_TO_INT(t)); +  buffer_add_be32(&data->buf, CDR_TO_INT(t));    }    break;       case '0':    case '1':    case '2':    case '3':    case '4':    case '5':    case '6':