Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 506 lines (401 sloc) 12.545 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
//
// tns_core.c: core code for a tnetstring parser in C
//
// This is code for parsing and rendering data in the provisional
// typed-netstring format proposed for inclusion in Mongrel2. You can
// think of it like a JSON library that uses a simpler wire format.
//

#include "dbg.h"
#include "tns_core.h"

#ifndef TNS_MAX_LENGTH
#define TNS_MAX_LENGTH 999999999
#endif

// Current outbuf implementation writes data starting at the back of
// the allocated buffer. When finished we simply memmove it to the front.
// Here *buffer points to the allocated buffer, while *head points to the
// last characer written to the buffer (and thus decreases as we write).
struct tns_outbuf_s {
  char *buffer;
  char *head;
  size_t alloc_size;
};


// Helper function for parsing a dict; basically parses items in a loop.
static int tns_parse_dict(const tns_ops *ops, void *dict, const char *data, size_t len);

// Helper function for parsing a list; basically parses items in a loop.
static int tns_parse_list(const tns_ops *ops, void *list, const char *data, size_t len);

// Helper function for writing the length prefix onto a rendered value.
static int tns_outbuf_clamp(tns_outbuf *outbuf, size_t orig_size);

// Finalize an outbuf, turning the allocated buffer into a standard
// char* array. Can't use the outbuf once it has been finalized.
static char* tns_outbuf_finalize(tns_outbuf *outbuf, size_t *len);

// Free the memory allocated in an outbuf.
// Can't use the outbuf once it has been freed.
static void tns_outbuf_free(tns_outbuf *outbuf);

// Helper function to read a base-ten integer off a string.
// Due to additional constraints, we can do it faster than strtoi.
static size_t tns_strtosz(const char *data, size_t len, size_t *sz, char **end);


void* tns_parse(const tns_ops *ops, const char *data, size_t len, char **remain)
{
  char *valstr = NULL;
  tns_type_tag type = tns_tag_null;
  size_t vallen = 0;

  // Read the length of the value, and verify that it ends in a colon.
  check(tns_strtosz(data, len, &vallen, &valstr) != -1,
        "Not a tnetstring: invalid length prefix.");
  check(*valstr == ':',
        "Not a tnetstring: invalid length prefix.");
  valstr++;
  check((valstr+vallen) < (data+len),
        "Not a tnetstring: invalid length prefix.");

  // Grab the type tag from the end of the value.
  type = valstr[vallen];

  // Output the remainder of the string if necessary.
  if(remain != NULL) {
      *remain = valstr + vallen + 1;
  }

  // Now dispatch type parsing based on the type tag.
  return tns_parse_payload(ops, type, valstr, vallen);

error:
  return NULL;
}


// This appears to be faster than using strncmp to compare
// against a small string constant. Ugly but fast.
#define STR_EQ_TRUE(s) (s[0]=='t' && s[1]=='r' && s[2]=='u' && s[3]=='e')
#define STR_EQ_FALSE(s) (s[0]=='f' && s[1]=='a' && s[2]=='l' \
&& s[3]=='s' && s[4] == 'e')

void* tns_parse_payload(const tns_ops *ops,tns_type_tag type, const char *data, size_t len)
{
  void *val = NULL;

  assert(ops != NULL && "ops struct cannot be NULL");

  switch(type) {
    // Primitive type: a string blob.
    case tns_tag_string:
        val = ops->parse_string(ops, data, len);
        check(val != NULL, "Not a tnetstring: invalid string literal.");
        break;
    // Primitive type: an integer.
    case tns_tag_integer:
        val = ops->parse_integer(ops, data, len);
        check(val != NULL, "Not a tnetstring: invalid integer literal.");
        break;
    // Primitive type: a float.
    case tns_tag_float:
        val = ops->parse_float(ops, data, len);
        check(val != NULL, "Not a tnetstring: invalid float literal.");
        break;
    // Primitive type: a boolean.
    // The only acceptable values are "true" and "false".
    case tns_tag_bool:
        if(len == 4 && STR_EQ_TRUE(data)) {
            val = ops->get_true(ops);
        } else if(len == 5 && STR_EQ_FALSE(data)) {
            val = ops->get_false(ops);
        } else {
            sentinel("Not a tnetstring: invalid boolean literal.");
            val = NULL;
        }
        break;
    // Primitive type: a null.
    // This must be a zero-length string.
    case tns_tag_null:
        check(len == 0, "Not a tnetstring: invalid null literal.");
        val = ops->get_null(ops);
        break;
    // Compound type: a dict.
    // The data is written <key><value><key><value>
    case tns_tag_dict:
        val = ops->new_dict(ops);
        check(val != NULL, "Could not create dict.");
        check(tns_parse_dict(ops, val, data, len) != -1,
              "Not a tnetstring: broken dict items.");
        break;
    // Compound type: a list.
    // The data is written <item><item><item>
    case tns_tag_list:
        val = ops->new_list(ops);
        check(val != NULL, "Could not create list.");
        check(tns_parse_list(ops, val, data, len) != -1,
              "Not a tnetstring: broken list items.");
        break;
    // Whoops, that ain't a tnetstring.
    default:
        sentinel("Not a tnetstring: invalid type tag.");
  }

  return val;

error:
  if(val != NULL) {
      ops->free_value(ops, val);
  }
  return NULL;
}

#undef STR_EQ_TRUE
#undef STR_EQ_FALSE


char* tns_render(const tns_ops *ops, void *val, size_t *len)
{
  tns_outbuf outbuf;

  check(tns_outbuf_init(&outbuf) != -1, "Failed to initialize outbuf.");
  check(tns_render_value(ops, val, &outbuf) != -1, "Failed to render value.");

  return tns_outbuf_finalize(&outbuf, len);
  
error:
  tns_outbuf_free(&outbuf);
  return NULL;
}


int tns_render_value(const tns_ops *ops, void *val, tns_outbuf *outbuf)
{
  tns_type_tag type = tns_tag_null;
  int res = -1;
  size_t orig_size = 0;

  assert(ops != NULL && "ops struct cannot be NULL");

  // Find out the type tag for the given value.
  type = ops->get_type(ops, val);
  check(type != 0, "type not serializable.");

  tns_outbuf_putc(outbuf, type);
  orig_size = tns_outbuf_size(outbuf);

  // Render it into the output buffer using callbacks.
  switch(type) {
    case tns_tag_string:
      res = ops->render_string(ops, val, outbuf);
      break;
    case tns_tag_integer:
      res = ops->render_integer(ops, val, outbuf);
      break;
    case tns_tag_float:
      res = ops->render_float(ops, val, outbuf);
      break;
    case tns_tag_bool:
      res = ops->render_bool(ops, val, outbuf);
      break;
    case tns_tag_null:
      res = 0;
      break;
    case tns_tag_dict:
      res = ops->render_dict(ops, val, outbuf);
      break;
    case tns_tag_list:
      res = ops->render_list(ops, val, outbuf);
      break;
    default:
      sentinel("unknown type tag: '%c'.", type);
  }

  check(res == 0, "Failed to render value of type '%c'.", type);
  return tns_outbuf_clamp(outbuf, orig_size);

error:
  return -1;
}


static int tns_parse_list(const tns_ops *ops, void *val, const char *data, size_t len)
{
  void *item = NULL;
  char *remain = NULL;

  assert(val != NULL && "value cannot be NULL");
  assert(data != NULL && "data cannot be NULL");

  while(len > 0) {
      item = tns_parse(ops, data, len, &remain);
      check(item != NULL, "Failed to parse list.");
      len = len - (remain - data);
      data = remain;
      check(ops->add_to_list(ops, val, item) != -1,
            "Failed to add item to list.");
      item = NULL;
  }

  return 0;

error:
  if(item) {
      ops->free_value(ops, item);
  }
  return -1;
}


static int tns_parse_dict(const tns_ops *ops, void *val, const char *data, size_t len)
{
  void *key = NULL;
  void *item = NULL;
  char *remain = NULL;

  assert(val != NULL && "value cannot be NULL");
  assert(data != NULL && "data cannot be NULL");

  while(len > 0) {
      key = tns_parse(ops, data, len, &remain);
      check(key != NULL, "Failed to parse dict key from tnetstring.");
      len = len - (remain - data);
      data = remain;

      item = tns_parse(ops, data, len, &remain);
      check(item != NULL, "Failed to parse dict item from tnetstring.");
      len = len - (remain - data);
      data = remain;

      check(ops->add_to_dict(ops, val, key, item) != -1,
            "Failed to add element to dict.");

      key = NULL;
      item = NULL;
  }

  return 0;

error:
  if(key) {
      ops->free_value(ops, key);
  }
  if(item) {
      ops->free_value(ops, item);
  }
  return -1;
}



static inline size_t
tns_strtosz(const char *data, size_t len, size_t *sz, char **end)
{
  char c;
  const char *pos, *eod;
  size_t value = 0;

  pos = data;
  eod = data + len;

  // The first character must be a digit.
  // The netstring spec explicitly forbits padding zeros.
  // So if it's a zero, it must be the only char in the string.
  c = *pos++;
  switch(c) {
    case '0':
      *sz = 0;
      *end = (char*) pos;
      return 0;
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      value = c - '0';
      break;
    default:
      return -1;
  }

  // Consume the remaining digits, up to maximum value length.
  while(pos < eod) {
      c = *pos;
      if(c < '0' || c > '9') {
          *sz = value;
          *end = (char*) pos;
          return 0;
      }
      value = (value * 10) + (c - '0');
      check(value <= TNS_MAX_LENGTH,
            "Not a tnetstring: absurdly large length prefix");
      pos++;
  }

  // If we consume the entire string, that's an error.

error:
  return -1;
}

size_t tns_outbuf_size(tns_outbuf *outbuf)
{
  return outbuf->alloc_size - (outbuf->head - outbuf->buffer);
}


static inline int tns_outbuf_itoa(tns_outbuf *outbuf, size_t n)
{
  do {
      check(tns_outbuf_putc(outbuf, n%10+'0') != -1,
            "Failed to write int to tnetstring buffer.");
      n = n / 10;
  } while(n > 0);

  return 0;

error:
  return -1;
}


int tns_outbuf_init(tns_outbuf *outbuf)
{
  outbuf->buffer = malloc(64);
  check_mem(outbuf->buffer);

  outbuf->head = outbuf->buffer + 64;
  outbuf->alloc_size = 64;
  return 0;

error:
  outbuf->head = NULL;
  outbuf->alloc_size = 0;
  return -1;
}


static inline void tns_outbuf_free(tns_outbuf *outbuf)
{
  if(outbuf) {
      free(outbuf->buffer);
      outbuf->buffer = NULL;
      outbuf->head = 0;
      outbuf->alloc_size = 0;
  }
}


static inline int tns_outbuf_extend(tns_outbuf *outbuf, size_t free_size)
{
  char *new_buf = NULL;
  char *new_head = NULL;
  size_t new_size = outbuf->alloc_size * 2;
  size_t used_size;

  used_size = tns_outbuf_size(outbuf);

  while(new_size < free_size + used_size) {
      new_size = new_size * 2;
  }

  new_buf = malloc(new_size);
  check_mem(new_buf);
 
  new_head = new_buf + new_size - used_size;
  memmove(new_head, outbuf->head, used_size);

  free(outbuf->buffer);
  outbuf->buffer = new_buf;
  outbuf->head = new_head;
  outbuf->alloc_size = new_size;

  return 0;

error:
  return -1;
}


int tns_outbuf_putc(tns_outbuf *outbuf, char c)
{
  if(outbuf->buffer == outbuf->head) {
      check(tns_outbuf_extend(outbuf, 1) != -1, "Failed to extend buffer");
  }

  *(--outbuf->head) = c;

  return 0;

error:
  return -1;
}


int tns_outbuf_puts(tns_outbuf *outbuf, const char *data, size_t len)
{
  if(outbuf->head - outbuf->buffer < len) {
      check(tns_outbuf_extend(outbuf, len) != -1, "Failed to extend buffer");
  }

  outbuf->head -= len;
  memmove(outbuf->head, data, len);

  return 0;

error:
  return -1;
}


static char* tns_outbuf_finalize(tns_outbuf *outbuf, size_t *len)
{
  char *new_buf = NULL;
  size_t used_size;

  used_size = tns_outbuf_size(outbuf);

  memmove(outbuf->buffer, outbuf->head, used_size);

  if(len != NULL) {
      *len = used_size;
  } else {
      if(outbuf->head == outbuf->buffer) {
          new_buf = realloc(outbuf->buffer, outbuf->alloc_size*2);
          check_mem(new_buf);
          outbuf->buffer = new_buf;
          outbuf->alloc_size = outbuf->alloc_size * 2;
      }
      outbuf->buffer[used_size] = '\0';
  }

  return outbuf->buffer;

error:
  free(outbuf->buffer);
  outbuf->buffer = NULL;
  outbuf->alloc_size = 0;
  return NULL;
}


static inline int tns_outbuf_clamp(tns_outbuf *outbuf, size_t orig_size)
{
    size_t datalen = tns_outbuf_size(outbuf) - orig_size;

    check(tns_outbuf_putc(outbuf, ':') != -1, "Failed to clamp outbuf");
    check(tns_outbuf_itoa(outbuf, datalen) != -1, "Failed to clamp outbuf");

    return 0;

error:
    return -1;
}


void tns_outbuf_memmove(tns_outbuf *outbuf, char *dest)
{
  memmove(dest, outbuf->head, tns_outbuf_size(outbuf));
}
Something went wrong with that request. Please try again.