Branch: Tag:

2019-07-20

2019-07-20 10:22:02 by Henrik Grubbström (Grubba) <grubba@grubba.org>

encode_value: Added debug-level 6.

This level outputs a memory dump during encode_value() and
decode_value(), which can be used to correlate the two.

180:      static void encode_value2(struct svalue *val, struct encode_data *data, int force_encode);    + #ifdef PIKE_DEBUG + static void debug_dump_mem(size_t offset, const unsigned char *bytes, +  size_t len) + { +  size_t i; +  for (i = 0; i < len; i += 8) { +  size_t j; +  fprintf(stderr, " %08x: ", offset + i); +  for (j = 0; (i+j < len) && (j < 8); j++) { +  fprintf(stderr, "%02x", bytes[i+j]); +  } +  fprintf(stderr, " "); +  for (j = 0; (i+j < len) && (j < 8); j++) { +  int c = bytes[i+j]; +  if (((c & 0177) < ' ') || (c == 0177)) { +  /* Control or DEL */ +  c = '.'; +  } +  if (c < 0177) { +  /* ASCII */ +  fprintf(stderr, "%c", c); +  } else { +  /* Latin-1 ==> Encode to UTF-8. */ +  fprintf(stderr, "%c%c", 0xc0 | (c>>6), 0x80 | (c & 0x3f)); +  } +  } +  fprintf(stderr, "\n"); +  } + } + static void low_addstr(struct byte_buffer *buf, const char *bytes, +  size_t len, int edb) + { +  if (edb >= 6) { +  debug_dump_mem(buf->length - (((char *)buf->end) - ((char *)buf->dst)), +  bytes, len); +  } +  buffer_memcpy(buf, bytes, len); + } + static void low_addchar(struct byte_buffer *buf, int c, int edb) + { +  char byte = c; +  low_addstr(buf, &byte, 1, edb); + } + #define addstr(s, l) low_addstr(&(data->buf), (s), (l), data->debug) + #define addchar(t) low_addchar(&(data->buf), (t), data->debug) + #define addchar_unsafe(t) addchar(t) + #else   #define addstr(s, l) buffer_memcpy(&(data->buf), (s), (l))   #define addchar(t) buffer_add_char(&(data->buf), (char)(t))   #define addchar_unsafe(t) buffer_add_char_unsafe(&(data->buf), t) -  + #endif      /* Code a pike string */   
1935:   {    if(data->ptr >= data->len)    Pike_error("Not enough data in string.\n"); +  + #ifdef PIKE_DEBUG +  if (data->debug >= 6) { +  debug_dump_mem(data->ptr, data->data + data->ptr, 1); +  } + #endif /* PIKE_DEBUG */    return data->data [ data->ptr++ ];   }   
1984:      #define DECODE(Z) do { \    EDB(5, \ -  fprintf(stderr,"%*sdecode(%s) at %d: ", \ +  fprintf(stderr,"%*sdecode(%s) at %d:\n", \    data->depth,"",(Z),__LINE__)); \    what=GETC(); \    e=what>>SIZE_SHIFT; \
2000:    num = ~num; \    } \    EDB(5, \ -  fprintf(stderr,"type=%d (%s), num=%ld\n", \ -  (what & TAG_MASK), \ +  fprintf(stderr,"%*s==> type=%d (%s), num=%ld\n", \ +  data->depth,"",(what & TAG_MASK), \    get_name_of_type(tag_to_type(what & TAG_MASK)), \    (long)num) ); \   } while (0)
2045:    sz, data->len - data->ptr); \    STR=begin_wide_shared_string(num, what); \    memcpy(STR->str, data->data + data->ptr, sz); \ +  EDB(6, debug_dump_mem(data->ptr, data->data + data->ptr, sz)); \    data->ptr += sz; \    BITFLIP(STR); \    STR=end_shared_string(STR); \
2056:    decode_error (data, NULL, "Too large size %td (max is %td).\n", \    sz, data->len - data->ptr); \    STR=make_shared_binary_string((char *)(data->data + data->ptr), sz); \ +  EDB(6, debug_dump_mem(data->ptr, data->data + data->ptr, sz)); \    data->ptr += sz; \    } \   }while(0)
2166:    INT32 min=0, max=0;    if(data->ptr + 8 > data->len)    decode_error(data, NULL, "Not enough data.\n"); +  EDB(6, debug_dump_mem(data->ptr, data->data + data->ptr, 8));    min = get_unaligned_be32(data->data + data->ptr);    data->ptr += 4;    max = get_unaligned_be32(data->data + data->ptr);