static asn1_error_code just_encode_sequence(asn1buf *buf, const void *val, const struct seq_info *seq, unsigned int *retlen) { const struct field_info *fields = seq->fields; size_t nfields = seq->n_fields; unsigned int optional; asn1_error_code retval; unsigned int sum = 0; if (seq->optional) optional = seq->optional(val); else /* * In this case, none of the field descriptors should indicate * that we examine any bits of this value. */ optional = 0; { unsigned int length; retval = encode_fields(buf, val, fields, nfields, optional, &length); if (retval) return retval; sum += length; } { unsigned int length; retval = asn1_make_sequence(buf, sum, &length); if (retval) return retval; sum += length; } *retlen = sum; return 0; }
int serialize_table_as_pb(lua_sandbox* lsb, int index) { output_data* d = &lsb->output; d->pos = 0; size_t needed = 18; if (needed > d->size) { if (realloc_output(d, needed)) return 1; } // create a type 4 uuid d->data[d->pos++] = 2 | (1 << 3); d->data[d->pos++] = 16; for (int x = 0; x < 16; ++x) { d->data[d->pos++] = rand() % 255; } d->data[8] = (d->data[8] & 0x0F) | 0x40; d->data[10] = (d->data[10] & 0x0F) | 0xA0; // use existing or create a timestamp lua_getfield(lsb->lua, index, "Timestamp"); long long ts; if (lua_isnumber(lsb->lua, -1)) { ts = (long long)lua_tonumber(lsb->lua, -1); } else { ts = (long long)(time(NULL) * 1e9); } lua_pop(lsb->lua, 1); if (pb_write_tag(d, 2, 0)) return 1; if (pb_write_varint(d, ts)) return 1; if (encode_string(lsb, d, 3, "Type", index)) return 1; if (encode_string(lsb, d, 4, "Logger", index)) return 1; if (encode_int(lsb, d, 5, "Severity", index)) return 1; if (encode_string(lsb, d, 6, "Payload", index)) return 1; if (encode_string(lsb, d, 7, "EnvVersion", index)) return 1; if (encode_int(lsb, d, 8, "Pid", index)) return 1; if (encode_string(lsb, d, 9, "Hostname", index)) return 1; if (encode_fields(lsb, d, 10, "Fields", index)) return 1; // if we go above 15 pb_write_tag will need to start varint encoding needed = 1; if (needed > d->size - d->pos) { if (realloc_output(d, needed)) return 1; } d->data[d->pos] = 0; // NULL terminate incase someone tries to treat this // as a string return 0; }
lsb_err_value heka_encode_message_table(lsb_lua_sandbox *lsb, int idx) { lsb_err_value ret = NULL; lsb_output_buffer *ob = &lsb->output; ob->pos = 0; // use existing or create a type 4 uuid lua_getfield(lsb->lua, idx, LSB_UUID); size_t len; const char *uuid = lua_tolstring(lsb->lua, -1, &len); ret = lsb_write_heka_uuid(ob, uuid, len); lua_pop(lsb->lua, 1); // remove uuid if (ret) return ret; // use existing or create a timestamp lua_getfield(lsb->lua, idx, LSB_TIMESTAMP); long long ts; if (lua_isnumber(lsb->lua, -1)) { ts = (long long)lua_tonumber(lsb->lua, -1); } else { ts = time(NULL) * 1000000000LL; } lua_pop(lsb->lua, 1); // remove timestamp ret = lsb_pb_write_key(ob, LSB_PB_TIMESTAMP, LSB_PB_WT_VARINT); if (!ret) ret = lsb_pb_write_varint(ob, ts); if (!ret) ret = encode_string(lsb, ob, LSB_PB_TYPE, LSB_TYPE, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_LOGGER, LSB_LOGGER, idx); if (!ret) ret = encode_int(lsb, ob, LSB_PB_SEVERITY, LSB_SEVERITY, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_PAYLOAD, LSB_PAYLOAD, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_ENV_VERSION, LSB_ENV_VERSION, idx); if (!ret) ret = encode_int(lsb, ob, LSB_PB_PID, LSB_PID, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_HOSTNAME, LSB_HOSTNAME, idx); if (!ret) ret = encode_fields(lsb, ob, LSB_PB_FIELDS, LSB_FIELDS, idx); if (!ret) ret = lsb_expand_output_buffer(ob, 1); ob->buf[ob->pos] = 0; // prevent possible overrun if treated as a string return ret; }