summaryrefslogtreecommitdiff
path: root/upb/msg.c
blob: 77521e5f0aec428d796e5043362bbbbbdad702f8 (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
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2010 Google Inc.  See LICENSE for details.
 * Author: Josh Haberman <jhaberman@gmail.com>
 *
 * Data structure for storing a message of protobuf data.
 */

#include "upb/upb.h"
#include "upb/msg.h"

void upb_msg_clear(void *msg, const upb_msgdef *md) {
  assert(msg != NULL);
  memset(msg, 0, md->hasbit_bytes);
  // TODO: set primitive fields to defaults?
}

void *upb_stdarray_append(upb_stdarray *a, size_t type_size) {
  assert(a != NULL);
  assert(a->len <= a->size);
  if (a->len == a->size) {
    size_t old_size = a->size;
    a->size = old_size == 0 ? 8 : (old_size * 2);
    a->ptr = realloc(a->ptr, a->size * type_size);
    memset(&a->ptr[old_size * type_size], 0, (a->size - old_size) * type_size);
  }
  return &a->ptr[a->len++ * type_size];
}

#if 0
static upb_flow_t upb_msg_dispatch(upb_msg *msg, upb_msgdef *md,
                                   upb_dispatcher *d);

static upb_flow_t upb_msg_pushval(upb_value val, upb_fielddef *f,
                                  upb_dispatcher *d, upb_fhandlers *hf) {
  if (upb_issubmsg(f)) {
    upb_msg *msg = upb_value_getmsg(val);
    upb_dispatch_startsubmsg(d, hf);
    upb_msg_dispatch(msg, upb_downcast_msgdef(f->def), d);
    upb_dispatch_endsubmsg(d);
  } else {
    upb_dispatch_value(d, hf, val);
  }
  return UPB_CONTINUE;
}

static upb_flow_t upb_msg_dispatch(upb_msg *msg, upb_msgdef *md,
                                   upb_dispatcher *d) {
  upb_msg_iter i;
  for(i = upb_msg_begin(md); !upb_msg_done(i); i = upb_msg_next(md, i)) {
    upb_fielddef *f = upb_msg_iter_field(i);
    if (!upb_msg_has(msg, f)) continue;
    upb_fhandlers *hf = upb_dispatcher_lookup(d, f->number);
    if (!hf) continue;
    upb_value val = upb_msg_get(msg, f);
    if (upb_isarray(f)) {
      upb_array *arr = upb_value_getarr(val);
      for (uint32_t j = 0; j < upb_array_len(arr); ++j) {
        upb_msg_pushval(upb_array_get(arr, f, j), f, d, hf);
      }
    } else {
      upb_msg_pushval(val, f, d, hf);
    }
  }
  return UPB_CONTINUE;
}

void upb_msg_runhandlers(upb_msg *msg, upb_msgdef *md, upb_handlers *h,
                         void *closure, upb_status *status) {
  upb_dispatcher d;
  upb_dispatcher_init(&d, h, NULL, NULL, NULL);
  upb_dispatcher_reset(&d, closure);

  upb_dispatch_startmsg(&d);
  upb_msg_dispatch(msg, md, &d);
  upb_dispatch_endmsg(&d, status);

  upb_dispatcher_uninit(&d);
}
#endif

/* Standard writers. **********************************************************/

void upb_stdmsg_sethas(void *_m, upb_value fval) {
  assert(_m != NULL);
  char *m = _m;
  const upb_fielddef *f = upb_value_getfielddef(fval);
  if (f->hasbit >= 0)
    m[(uint32_t)f->hasbit / 8] |= (1 << ((uint32_t)f->hasbit % 8));
}

bool upb_stdmsg_has(const void *_m, upb_value fval) {
  assert(_m != NULL);
  const char *m = _m;
  const upb_fielddef *f = upb_value_getfielddef(fval);
  return f->hasbit < 0 ||
      (m[(uint32_t)f->hasbit / 8] & (1 << ((uint32_t)f->hasbit % 8)));
}

#define UPB_ACCESSORS(type, ctype)                                            \
  upb_flow_t upb_stdmsg_set ## type (void *_m, upb_value fval,                \
                                     upb_value val) {                         \
    assert(_m != NULL);                                                       \
    const upb_fielddef *f = upb_value_getfielddef(fval);                      \
    uint8_t *m = _m;                                                          \
    /* Hasbit is set automatically by the handlers. */                        \
    *(ctype*)&m[f->offset] = upb_value_get ## type(val);                      \
    return UPB_CONTINUE;                                                      \
  }                                                                           \
                                                                              \
  upb_flow_t upb_stdmsg_set ## type ## _r(void *a, upb_value _fval,           \
                                          upb_value val) {                    \
    (void)_fval;                                                              \
    assert(a != NULL);                                                        \
    ctype *p = upb_stdarray_append((upb_stdarray*)a, sizeof(ctype));          \
    *p = upb_value_get ## type(val);                                          \
    return UPB_CONTINUE;                                                      \
  }                                                                           \
                                                                              \
  upb_value upb_stdmsg_get ## type(const void *_m, upb_value fval) {          \
    assert(_m != NULL);                                                       \
    const uint8_t *m = _m;                                                    \
    const upb_fielddef *f = upb_value_getfielddef(fval);                      \
    upb_value ret;                                                            \
    upb_value_set ## type(&ret, *(ctype*)&m[f->offset]);                      \
    return ret;                                                               \
  }                                                                           \
  upb_value upb_stdmsg_seqget ## type(const void *i) {                        \
    assert(i != NULL);                                                        \
    upb_value val;                                                            \
    upb_value_set ## type(&val, *(ctype*)i);                                  \
    return val;                                                               \
  }

UPB_ACCESSORS(double, double)
UPB_ACCESSORS(float, float)
UPB_ACCESSORS(int32, int32_t)
UPB_ACCESSORS(int64, int64_t)
UPB_ACCESSORS(uint32, uint32_t)
UPB_ACCESSORS(uint64, uint64_t)
UPB_ACCESSORS(bool, bool)
UPB_ACCESSORS(ptr, void*)
#undef UPB_ACCESSORS

static void _upb_stdmsg_setstr(void *_dst, upb_value src) {
  upb_stdarray **dstp = _dst;
  upb_stdarray *dst = *dstp;
  if (!dst) {
    dst = malloc(sizeof(*dst));
    dst->size = 0;
    dst->ptr = NULL;
    *dstp = dst;
  }
  dst->len = 0;
  const upb_byteregion *bytes = upb_value_getbyteregion(src);
  uint32_t len = upb_byteregion_len(bytes);
  if (len > dst->size) {
    dst->size = len;
    dst->ptr = realloc(dst->ptr, dst->size);
  }
  dst->len = len;
  upb_byteregion_copyall(bytes, dst->ptr);
}

upb_flow_t upb_stdmsg_setstr(void *_m, upb_value fval, upb_value val) {
  assert(_m != NULL);
  char *m = _m;
  const upb_fielddef *f = upb_value_getfielddef(fval);
  // Hasbit automatically set by the handlers.
  _upb_stdmsg_setstr(&m[f->offset], val);
  return UPB_CONTINUE;
}

upb_flow_t upb_stdmsg_setstr_r(void *a, upb_value fval, upb_value val) {
  assert(a != NULL);
  (void)fval;
  _upb_stdmsg_setstr(upb_stdarray_append((upb_stdarray*)a, sizeof(void*)), val);
  return UPB_CONTINUE;
}

upb_value upb_stdmsg_getstr(const void *m, upb_value fval) {
  assert(m != NULL);
  return upb_stdmsg_getptr(m, fval);
}

upb_value upb_stdmsg_seqgetstr(const void *i) {
  assert(i != NULL);
  return upb_stdmsg_seqgetptr(i);
}

void *upb_stdmsg_new(const upb_msgdef *md) {
  void *m = malloc(md->size);
  memset(m, 0, md->size);
  upb_msg_clear(m, md);
  return m;
}

void upb_stdseq_free(void *s, upb_fielddef *f) {
  upb_stdarray *a = s;
  if (upb_issubmsg(f) || upb_isstring(f)) {
    void **p = (void**)a->ptr;
    for (uint32_t i = 0; i < a->size; i++) {
      if (upb_issubmsg(f)) {
        upb_stdmsg_free(p[i], upb_downcast_msgdef(f->def));
      } else {
        upb_stdarray *str = p[i];
        free(str->ptr);
        free(str);
      }
    }
  }
  free(a->ptr);
  free(a);
}

void upb_stdmsg_free(void *m, const upb_msgdef *md) {
  if (m == NULL) return;
  upb_msg_iter i;
  for(i = upb_msg_begin(md); !upb_msg_done(i); i = upb_msg_next(md, i)) {
    upb_fielddef *f = upb_msg_iter_field(i);
    if (!upb_isseq(f) && !upb_issubmsg(f) && !upb_isstring(f)) continue;
    void *subp = upb_value_getptr(upb_stdmsg_getptr(m, f->fval));
    if (subp == NULL) continue;
    if (upb_isseq(f)) {
      upb_stdseq_free(subp, f);
    } else if (upb_issubmsg(f)) {
      upb_stdmsg_free(subp, upb_downcast_msgdef(f->def));
    } else {
      upb_stdarray *str = subp;
      free(str->ptr);
      free(str);
    }
  }
  free(m);
}

upb_sflow_t upb_stdmsg_startseq(void *_m, upb_value fval) {
  char *m = _m;
  const upb_fielddef *f = upb_value_getfielddef(fval);
  upb_stdarray **arr = (void*)&m[f->offset];
  if (!upb_stdmsg_has(_m, fval)) {
    if (!*arr) {
      *arr = malloc(sizeof(**arr));
      (*arr)->size = 0;
      (*arr)->ptr = NULL;
    }
    (*arr)->len = 0;
    upb_stdmsg_sethas(m, fval);
  }
  return UPB_CONTINUE_WITH(*arr);
}

void upb_stdmsg_recycle(void **m, const upb_msgdef *md) {
  if (*m)
    upb_msg_clear(*m, md);
  else
    *m = upb_stdmsg_new(md);
}

upb_sflow_t upb_stdmsg_startsubmsg(void *_m, upb_value fval) {
  assert(_m != NULL);
  char *m = _m;
  const upb_fielddef *f = upb_value_getfielddef(fval);
  void **subm = (void*)&m[f->offset];
  if (!upb_stdmsg_has(m, fval)) {
    upb_stdmsg_recycle(subm, upb_downcast_msgdef(f->def));
    upb_stdmsg_sethas(m, fval);
  }
  return UPB_CONTINUE_WITH(*subm);
}

upb_sflow_t upb_stdmsg_startsubmsg_r(void *a, upb_value fval) {
  assert(a != NULL);
  const upb_fielddef *f = upb_value_getfielddef(fval);
  void **subm = upb_stdarray_append((upb_stdarray*)a, sizeof(void*));
  upb_stdmsg_recycle(subm, upb_downcast_msgdef(f->def));
  return UPB_CONTINUE_WITH(*subm);
}

const void *upb_stdmsg_seqbegin(const void *_a) {
  const upb_stdarray *a = _a;
  return a->len > 0 ? a->ptr : NULL;
}

#define NEXTFUNC(size) \
  const void *upb_stdmsg_ ## size ## byte_seqnext(const void *_a, const void *iter) {\
    const upb_stdarray *a = _a;                                          \
    const void *next = (char*)iter + size;                               \
    return (char*)next < (char*)a->ptr + (a->len * size) ? next : NULL;  \
  }

NEXTFUNC(8)
NEXTFUNC(4)
NEXTFUNC(1)

#define STDMSG(type, size) { static upb_accessor_vtbl vtbl = { \
    &upb_stdmsg_startsubmsg, \
    &upb_stdmsg_set ## type, \
    &upb_stdmsg_startseq, \
    &upb_stdmsg_startsubmsg_r, \
    &upb_stdmsg_set ## type ## _r, \
    &upb_stdmsg_has, \
    &upb_stdmsg_getptr, \
    &upb_stdmsg_get ## type, \
    &upb_stdmsg_seqbegin, \
    &upb_stdmsg_ ## size ## byte_seqnext, \
    &upb_stdmsg_seqget ## type}; \
  return &vtbl; }

upb_accessor_vtbl *upb_stdmsg_accessor(upb_fielddef *f) {
  switch (f->type) {
    case UPB_TYPE(DOUBLE): STDMSG(double, 8)
    case UPB_TYPE(FLOAT): STDMSG(float, 4)
    case UPB_TYPE(UINT64):
    case UPB_TYPE(FIXED64): STDMSG(uint64, 8)
    case UPB_TYPE(INT64):
    case UPB_TYPE(SFIXED64):
    case UPB_TYPE(SINT64): STDMSG(int64, 8)
    case UPB_TYPE(INT32):
    case UPB_TYPE(SINT32):
    case UPB_TYPE(ENUM):
    case UPB_TYPE(SFIXED32): STDMSG(int32, 4)
    case UPB_TYPE(UINT32):
    case UPB_TYPE(FIXED32): STDMSG(uint32, 4)
    case UPB_TYPE(BOOL): STDMSG(bool, 1)
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES):
    case UPB_TYPE(GROUP):
    case UPB_TYPE(MESSAGE): STDMSG(str, 8)  // TODO: 32-bit
  }
  return NULL;
}

static void upb_accessors_onfreg(void *c, upb_fhandlers *fh,
                                 const upb_fielddef *f) {
  (void)c;
  if (f->accessor) {
    upb_fhandlers_setfval(fh, f->fval);
    if (upb_isseq(f)) {
      upb_fhandlers_setstartseq(fh, f->accessor->startseq);
      upb_fhandlers_setvalue(fh, f->accessor->append);
      upb_fhandlers_setstartsubmsg(fh, f->accessor->appendsubmsg);
    } else {
      upb_fhandlers_setvalue(fh, f->accessor->set);
      upb_fhandlers_setstartsubmsg(fh, f->accessor->startsubmsg);
      upb_fhandlers_setvaluehasbit(fh, f->hasbit);
    }
  }
}

upb_mhandlers *upb_accessors_reghandlers(upb_handlers *h, const upb_msgdef *m) {
  return upb_handlers_regmsgdef(h, m, NULL, &upb_accessors_onfreg, NULL);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback