summaryrefslogtreecommitdiff
path: root/pbstream.c
blob: c300153e8658659defeb0c6da3be626c51d58473 (plain)
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
/*
 * pbstream - a stream-oriented implementation of protocol buffers.
 *
 * Copyright (c) 2008 Joshua Haberman.  See LICENSE for details.
 */

#include <limits.h>
#include <string.h>
#include "pbstream.h"

/* Branch prediction hints for GCC. */
#ifdef __GNUC__
#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)
#else
#define likely(x)
#define unlikely(x)
#endif

/* An array, indexed by pbstream_type, that indicates what wire type is
 * expected for the given pbstream type. */
static enum pbstream_wire_type expected_wire_type[] = {
  PBSTREAM_WIRE_TYPE_64BIT,    // PBSTREAM_TYPE_DOUBLE,
  PBSTREAM_WIRE_TYPE_32BIT,    // PBSTREAM_TYPE_FLOAT,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_INT32,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_INT64,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_UINT32,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_UINT64,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_SINT32,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_SINT64,
  PBSTREAM_WIRE_TYPE_32BIT,    // PBSTREAM_TYPE_FIXED32,
  PBSTREAM_WIRE_TYPE_64BIT,    // PBSTREAM_TYPE_FIXED64,
  PBSTREAM_WIRE_TYPE_32BIT,    // PBSTREAM_TYPE_SFIXED32,
  PBSTREAM_WIRE_TYPE_64BIT,    // PBSTREAM_TYPE_SFIXED64,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_BOOL,
  PBSTREAM_WIRE_TYPE_STRING,   // PBSTREAM_TYPE_STRING,
  PBSTREAM_WIRE_TYPE_STRING,   // PBSTREAM_TYPE_BYTES,
  PBSTREAM_WIRE_TYPE_VARINT,   // PBSTREAM_TYPE_ENUM,
  PBSTREAM_WIRE_TYPE_STRING,   // PBSTREAM_TYPE_MESSAGE
};

/* Reads a varint starting at buf (but not past end), storing the result
 * in out_value.  Returns whether the operation was successful.  */
enum pbstream_status get_varint(char **buf, char *end, uint64_t *out_value)
{
  *out_value = 0;
  int bitpos = 0;
  char *b = *buf;

  /* Because we don't check for buffer overrun inside the loop, we require
   * that callers use a buffer that is overallocated by at least 9 bytes
   * (the maximum we can overrun before the bitpos check catches the problem). */
  for(; *b & 0x80 && bitpos < 64; bitpos += 7, b++)
      *out_value |= (*b & 0x7F) << bitpos;

  if(unlikely(bitpos >= 64)) {
    return PBSTREAM_ERROR_UNTERMINATED_VARINT;
  }
  if(unlikely(b > end)) {
    return PBSTREAM_STATUS_INCOMPLETE;
  }

  *out_value |= (*b & 0x7F) << bitpos;
  *buf = b;
  return PBSTREAM_STATUS_OK;
}

/* TODO: the little-endian versions of these functions don't respect alignment.
 * While it's hard to believe that this could be less efficient than the
 * alternative (the big endian implementation), this deserves some tests and
 * measurements to be sure. */
enum pbstream_status get_32_le(char **buf, char *end, uint32_t *out_value)
{
  char *b = *buf;
  char *int32_end = b+4;
  if(unlikely(int32_end > end))
    return PBSTREAM_STATUS_INCOMPLETE;

#if __BYTE_ORDER == __LITTLE_ENDIAN
  *out_value = *(uint32_t*)b;
#else
  *out_value = b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
#endif
  *buf = int32_end;
  return PBSTREAM_STATUS_OK;
}

bool get_64_le(char **buf, char *end, uint64_t *out_value)
{
  char *b = *buf;
  char *int64_end = b+8;
  if(unlikely(int64_end > end))
    return PBSTREAM_STATUS_INCOMPLETE;

#if __BYTE_ORDER == __LITTLE_ENDIAN
  *out_value = *(uint64_t*)buf;
#else
  *out_value = (b[0])       | (b[1] << 8 ) | (b[2] << 16) | (b[3] << 24) |
               (b[4] << 32) | (b[5] << 40) | (b[6] << 48) | (b[7] << 56);
#endif
  *buf = int64_end;
  return PBSTREAM_STATUS_OK;
}

int32_t zigzag_decode_32(uint64_t src)
{
  return 0;  /* TODO */
}

int64_t zigzag_decode_64(uint64_t src)
{
  return 0;  /* TODO */
}

/* Parses the next field-number/wire-value pair from the stream of bytes
 * starting at *buf, without reading past end.  Stores the parsed and wire
 * value in *field_number and *wire_value, respectively.
 *
 * Returns a status indicating whether the operation was successful.  If the
 * return status is STATUS_INCOMPLETE, returns the number of additional bytes
 * requred in *need_more_bytes.  Updates *buf to point past the end of the
 * parsed data if the operation was successful.
 */
enum pbstream_status pbstream_parse_wire_value(char **buf, char *end,
                                               pbstream_field_number_t *field_number,
                                               struct pbstream_wire_value *wire_value,
                                               int *need_more_bytes)
{
  char *b = *buf;  /* Our local buf pointer -- only update buf if we succeed. */

#define DECODE(dest, func) \
  do { \
    enum pbstream_status status = func(&b, end, &dest); \
    if(unlikely(status != PBSTREAM_STATUS_OK)) { \
      *need_more_bytes = 0;  /* This only arises below in this function. */ \
      return status; \
    } \
  } while (0)

  uint64_t key;
  DECODE(key, get_varint);

  *field_number    = key >> 3;
  wire_value->type = key & 0x07;

  switch(wire_value->type)
  {
    case PBSTREAM_WIRE_TYPE_VARINT:
      DECODE(wire_value->v.varint, get_varint);
      break;

    case PBSTREAM_WIRE_TYPE_64BIT:
      DECODE(wire_value->v._64bit, get_64_le);
      break;

    case PBSTREAM_WIRE_TYPE_STRING:
    {
      uint64_t string_len;
      DECODE(string_len, get_varint);
      if (string_len > INT_MAX) {
        /* TODO: notice this and fail. */
      }
      wire_value->v.string.len = (int)string_len;
      if(b + wire_value->v.string.len > end) {
        *need_more_bytes = b + wire_value->v.string.len - end;
        return PBSTREAM_STATUS_INCOMPLETE;
      }
      wire_value->v.string.data = b;
      b += wire_value->v.string.len;
      break;
    }

    case PBSTREAM_WIRE_TYPE_START_GROUP:
    case PBSTREAM_WIRE_TYPE_END_GROUP:
      /* TODO (though these are deprecated, so not high priority). */
      break;

    case PBSTREAM_WIRE_TYPE_32BIT:
      DECODE(wire_value->v._32bit, get_32_le);
      break;
  }

  *buf = b;
  return true;
}

/* Translates from a wire value to a .proto value.  The caller should have
 * already checked that the wire_value is of the correct type.  The pbstream
 * type must not be PBSTREAM_TYPE_MESSAGE.  This operation always succeeds. */
void pbstream_translate_field(struct pbstream_wire_value *wire_value,
                              enum pbstream_type         type,
                              struct pbstream_value      *out_value)
{
  out_value->type = type;
  switch(type) {
    case PBSTREAM_TYPE_DOUBLE:
      memcpy(&out_value->v._double, &wire_value->v._64bit, sizeof(double));
      break;

    case PBSTREAM_TYPE_FLOAT:
      memcpy(&out_value->v._float, &wire_value->v._32bit, sizeof(float));
      break;

    case PBSTREAM_TYPE_INT32:
      out_value->v.int32 = (int32_t)wire_value->v.varint;
      break;

    case PBSTREAM_TYPE_INT64:
      out_value->v.int64 = (int64_t)zigzag_decode_64(wire_value->v.varint);
      break;

    case PBSTREAM_TYPE_UINT32:
      out_value->v.uint32 = (uint32_t)wire_value->v.varint;
      break;

    case PBSTREAM_TYPE_UINT64:
      out_value->v.uint64 = (uint64_t)wire_value->v.varint;
      break;

    case PBSTREAM_TYPE_SINT32:
      out_value->v.int32 = zigzag_decode_32(wire_value->v.varint);
      break;

    case PBSTREAM_TYPE_SINT64:
      out_value->v.int64 = zigzag_decode_64(wire_value->v.varint);
      break;

    case PBSTREAM_TYPE_FIXED32:
      out_value->v.int32 = wire_value->v._32bit;
      break;

    case PBSTREAM_TYPE_FIXED64:
      out_value->v.int64 = wire_value->v._64bit;
      break;

    case PBSTREAM_TYPE_SFIXED32:
      out_value->v.int32 = (int32_t)wire_value->v._32bit;
      break;

    case PBSTREAM_TYPE_SFIXED64:
      out_value->v.int64 = (int64_t)wire_value->v._64bit;
      break;

    case PBSTREAM_TYPE_BOOL:
      out_value->v._bool = (bool)wire_value->v.varint;
      break;

    case PBSTREAM_TYPE_STRING:
      out_value->v.string.data = wire_value->v.string.data;
      out_value->v.string.len = wire_value->v.string.len;
      /* TODO: validate UTF-8? */
      break;

    case PBSTREAM_TYPE_BYTES:
      out_value->v.bytes.data = wire_value->v.string.data;
      out_value->v.bytes.len = wire_value->v.string.len;
      break;

    case PBSTREAM_TYPE_ENUM:
      out_value->v._enum = (bool)wire_value->v.varint;
      break;

    case PBSTREAM_TYPE_MESSAGE:
      /* Should never happen. */
      break;
  }
}

/* Given a wire value that was just parsed and a matching field descriptor,
 * processes the given value and performs the appropriate actions.  These
 * actions include:
 * - checking that the wire type is as expected
 * - converting the wire type to a .proto type
 * - entering a sub-message, if that is in fact what this field implies.
 *
 * This function also calls user callbacks pertaining to any of the above at
 * the appropriate times. */
void process_value(struct pbstream_parse_state *s,
                   struct pbstream_wire_value *wire_value,
                   struct pbstream_field_descriptor *field_descriptor)
{
  /* Check that the wire type is appropriate for this .proto type. */
  if(unlikely(wire_value->type != expected_wire_type[field_descriptor->type])) {
    /* Report the type mismatch error. */
    if(s->callbacks.error_callback) {
      /* TODO: a nice formatted message. */
      s->callbacks.error_callback(PBSTREAM_ERROR_MISMATCHED_TYPE, NULL,
                                  s->offset, false);
    }

    /* Report the wire value we parsed as an unknown value. */
    if(s->callbacks.unknown_value_callback) {
      s->callbacks.unknown_value_callback(field_descriptor->field_number, wire_value,
                                          s->user_data);
    }
    return;
  }

  if(field_descriptor->type == PBSTREAM_TYPE_MESSAGE) {
    /* We're entering a sub-message. */
    if(s->callbacks.begin_message_callback) {
      s->callbacks.begin_message_callback(field_descriptor->d.message, s->user_data);
    }

    /* Push and initialize a new stack frame. */
    RESIZE_DYNARRAY(s->stack, s->stack_len+1);
    struct pbstream_parse_stack_frame *frame = DYNARRAY_GET_TOP(s->stack);
    frame->message_descriptor = field_descriptor->d.message;
    frame->end_offset = 0;  /* TODO: set this correctly. */
    int num_seen_fields = frame->message_descriptor->num_seen_fields;
    INIT_DYNARRAY(frame->seen_fields, num_seen_fields, num_seen_fields);
  }
  else {
    /* This is a scalar value. */
    struct pbstream_value value;
    pbstream_translate_field(wire_value, field_descriptor->type, &value);
    if(s->callbacks.value_callback) {
      s->callbacks.value_callback(field_descriptor, value, s->user_data);
    }
  }
}

struct pbstream_field_descriptor *find_field_descriptor_by_number(
    struct pbstream_message_descriptor* message_descriptor,
    pbstream_field_number_t field_number)
{
  /* Currently a linear search -- could be optimized to do a binary search,
   * hash table lookup, or any other number of clever things you might imagine. */
  for (int i = 0; i < message_descriptor->fields_len; i++)
    if (message_descriptor->fields[i].field_number == field_number)
      return &message_descriptor->fields[i];
  return NULL;
}

/* Parses and processes the next value from *buf (but not past end), returning
 * a status indicating whether the operation succeeded, and calling appropriate
 * callbacks.  If more data is needed to parse the last partial field, returns
 * how many more bytes are needed in need_more_bytes.  Updates *buf to point
 * past the parsed value if the operation succeeds. */
enum pbstream_status pbstream_parse_field(struct pbstream_parse_state *s,
                                          char **buf, char *end,
                                          int *need_more_bytes)
{
  struct pbstream_parse_stack_frame *frame = DYNARRAY_GET_TOP(s->stack);
  struct pbstream_message_descriptor *message_descriptor = frame->message_descriptor;

  pbstream_field_number_t field_number;
  struct pbstream_wire_value wire_value;
  enum pbstream_status status;

  /* Decode the raw wire data. */
  status = pbstream_parse_wire_value(buf, end, &field_number, &wire_value,
                                     need_more_bytes);

  if(unlikely(status != PBSTREAM_STATUS_OK)) {
    if(status == PBSTREAM_ERROR_UNTERMINATED_VARINT && s->callbacks.error_callback) {
      /* TODO: a nice formatted message. */
      s->callbacks.error_callback(PBSTREAM_ERROR_UNTERMINATED_VARINT, NULL,
                                  s->offset, true);
    }
    s->fatal_error = true;
    return status;
  }

  /* Find the corresponding field definition from the .proto file. */
  struct pbstream_field_descriptor *field_descriptor;
  field_descriptor = find_field_descriptor_by_number(message_descriptor, field_number);


  if(likely(field_descriptor != NULL)) {
    if(field_descriptor->seen_field_num > 0) {
      /* Check that this field has not been seen before (unless it's a repeated field) */
      if(frame->seen_fields[field_descriptor->seen_field_num] &&
         field_descriptor->cardinality != PBSTREAM_CARDINALITY_REPEATED) {
        if(s->callbacks.error_callback) {
          s->callbacks.error_callback(PBSTREAM_ERROR_DUPLICATE_FIELD, NULL,
                                      s->offset, false);
        }
        return PBSTREAM_STATUS_ERROR;
      }

      /* Mark the field as seen. */
      frame->seen_fields[field_descriptor->seen_field_num] = true;
    }
    process_value(s, &wire_value, field_descriptor);
  } else {
    /* This field was not defined in the .proto file. */
    if(s->callbacks.unknown_value_callback) {
      s->callbacks.unknown_value_callback(field_number, &wire_value, s->user_data);
    }
  }
  return PBSTREAM_STATUS_OK;
}

/* Process actions associated with the end of a submessage.  This includes:
 * - emittin default values for all optional elements (either explicit
 *   defaults or implicit defaults).
 * - emitting errors for any required fields that were not seen.
 * - calling the user's callback.
 * - popping the stack frame. */
void process_submessage_end(struct pbstream_parse_state *s)
{
  /* TODO: emit default values for optional elements.  either explicit defaults
   * (specified in the .proto file) or implicit defaults (which are specified
   * in the pbstream definition, by type. */

  /* TODO: emit errors for required fields that were not seen. */

  /* Process the end of message by calling the user's callback and popping
   * our stack frame. */
  if(s->callbacks.end_message_callback) {
    s->callbacks.end_message_callback(s->user_data);
  }

  /* Pop the stack frame associated with this submessage. */
  RESIZE_DYNARRAY(s->stack, s->stack_len-1);
}

/* The user-exposed parsing function -- see the header file for documentation. */
enum pbstream_status pbstream_parse(struct pbstream_parse_state *s,
                                    char *buf_start, int buf_len,
                                    int *consumed_bytes, int *need_more_bytes)
{
  char *buf = buf_start;
  char *end = buf_start + buf_len;
  int buf_start_offset = s->offset;
  enum pbstream_status status = PBSTREAM_STATUS_OK;

  while(buf < end) {
    /* Check for a submessage ending. */
    while(s->offset >= DYNARRAY_GET_TOP(s->stack)->end_offset) {
      /* A submessage should end exactly at a field boundary.  If we find that
       * the submessage length indicated an end in the middle of a field, that
       * is an error that indicates data corruption, and we refuse to proceed. */
      if(unlikely(s->offset != DYNARRAY_GET_TOP(s->stack)->end_offset)) {
        if(s->callbacks.error_callback) {
          s->callbacks.error_callback(PBSTREAM_ERROR_BAD_SUBMESSAGE_END, NULL,
                                      s->offset, true);
        }
        s->fatal_error = true;
        break;
      }
      process_submessage_end(s);
    }

    status = pbstream_parse_field(s, &buf, end, need_more_bytes);
    if(status != PBSTREAM_STATUS_OK)
      break;

    s->offset = buf_start_offset + (buf - buf_start);
  }
  return status;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback