Lines Matching refs:cbb

27 void CBB_zero(CBB *cbb) {  in CBB_zero()  argument
28 OPENSSL_memset(cbb, 0, sizeof(CBB)); in CBB_zero()
31 static int cbb_init(CBB *cbb, uint8_t *buf, size_t cap) { in cbb_init() argument
46 cbb->base = base; in cbb_init()
47 cbb->is_top_level = 1; in cbb_init()
51 int CBB_init(CBB *cbb, size_t initial_capacity) { in CBB_init() argument
52 CBB_zero(cbb); in CBB_init()
59 if (!cbb_init(cbb, buf, initial_capacity)) { in CBB_init()
67 int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) { in CBB_init_fixed() argument
68 CBB_zero(cbb); in CBB_init_fixed()
70 if (!cbb_init(cbb, buf, len)) { in CBB_init_fixed()
74 cbb->base->can_resize = 0; in CBB_init_fixed()
78 void CBB_cleanup(CBB *cbb) { in CBB_cleanup() argument
79 if (cbb->base) { in CBB_cleanup()
82 assert(cbb->is_top_level); in CBB_cleanup()
84 if (cbb->base->can_resize) { in CBB_cleanup()
85 OPENSSL_free(cbb->base->buf); in CBB_cleanup()
87 OPENSSL_free(cbb->base); in CBB_cleanup()
89 cbb->base = NULL; in CBB_cleanup()
171 int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) { in CBB_finish() argument
172 if (!cbb->is_top_level) { in CBB_finish()
176 if (!CBB_flush(cbb)) { in CBB_finish()
180 if (cbb->base->can_resize && (out_data == NULL || out_len == NULL)) { in CBB_finish()
186 *out_data = cbb->base->buf; in CBB_finish()
189 *out_len = cbb->base->len; in CBB_finish()
191 cbb->base->buf = NULL; in CBB_finish()
192 CBB_cleanup(cbb); in CBB_finish()
199 int CBB_flush(CBB *cbb) { in CBB_flush() argument
205 if (cbb->base == NULL || cbb->base->error) { in CBB_flush()
209 if (cbb->child == NULL || cbb->child->pending_len_len == 0) { in CBB_flush()
213 child_start = cbb->child->offset + cbb->child->pending_len_len; in CBB_flush()
215 if (!CBB_flush(cbb->child) || in CBB_flush()
216 child_start < cbb->child->offset || in CBB_flush()
217 cbb->base->len < child_start) { in CBB_flush()
221 len = cbb->base->len - child_start; in CBB_flush()
223 if (cbb->child->pending_is_asn1) { in CBB_flush()
230 assert (cbb->child->pending_len_len == 1); in CBB_flush()
256 if (!cbb_buffer_add(cbb->base, NULL, extra_bytes)) { in CBB_flush()
259 OPENSSL_memmove(cbb->base->buf + child_start + extra_bytes, in CBB_flush()
260 cbb->base->buf + child_start, len); in CBB_flush()
262 cbb->base->buf[cbb->child->offset++] = initial_length_byte; in CBB_flush()
263 cbb->child->pending_len_len = len_len - 1; in CBB_flush()
266 for (i = cbb->child->pending_len_len - 1; i < cbb->child->pending_len_len; in CBB_flush()
268 cbb->base->buf[cbb->child->offset + i] = (uint8_t)len; in CBB_flush()
275 cbb->child->base = NULL; in CBB_flush()
276 cbb->child = NULL; in CBB_flush()
281 cbb->base->error = 1; in CBB_flush()
285 const uint8_t *CBB_data(const CBB *cbb) { in CBB_data() argument
286 assert(cbb->child == NULL); in CBB_data()
287 return cbb->base->buf + cbb->offset + cbb->pending_len_len; in CBB_data()
290 size_t CBB_len(const CBB *cbb) { in CBB_len() argument
291 assert(cbb->child == NULL); in CBB_len()
292 assert(cbb->offset + cbb->pending_len_len <= cbb->base->len); in CBB_len()
294 return cbb->base->len - cbb->offset - cbb->pending_len_len; in CBB_len()
297 static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents, in cbb_add_length_prefixed() argument
301 if (!CBB_flush(cbb)) { in cbb_add_length_prefixed()
305 size_t offset = cbb->base->len; in cbb_add_length_prefixed()
306 if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len)) { in cbb_add_length_prefixed()
312 out_contents->base = cbb->base; in cbb_add_length_prefixed()
313 cbb->child = out_contents; in cbb_add_length_prefixed()
314 cbb->child->offset = offset; in cbb_add_length_prefixed()
315 cbb->child->pending_len_len = len_len; in cbb_add_length_prefixed()
316 cbb->child->pending_is_asn1 = 0; in cbb_add_length_prefixed()
321 int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) { in CBB_add_u8_length_prefixed() argument
322 return cbb_add_length_prefixed(cbb, out_contents, 1); in CBB_add_u8_length_prefixed()
325 int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) { in CBB_add_u16_length_prefixed() argument
326 return cbb_add_length_prefixed(cbb, out_contents, 2); in CBB_add_u16_length_prefixed()
329 int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) { in CBB_add_u24_length_prefixed() argument
330 return cbb_add_length_prefixed(cbb, out_contents, 3); in CBB_add_u24_length_prefixed()
336 static int add_base128_integer(CBB *cbb, uint64_t v) { in add_base128_integer() argument
352 if (!CBB_add_u8(cbb, byte)) { in add_base128_integer()
359 int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag) { in CBB_add_asn1() argument
360 if (!CBB_flush(cbb)) { in CBB_add_asn1()
369 if (!CBB_add_u8(cbb, tag_bits | 0x1f) || in CBB_add_asn1()
370 !add_base128_integer(cbb, tag_number)) { in CBB_add_asn1()
373 } else if (!CBB_add_u8(cbb, tag_bits | tag_number)) { in CBB_add_asn1()
377 size_t offset = cbb->base->len; in CBB_add_asn1()
378 if (!CBB_add_u8(cbb, 0)) { in CBB_add_asn1()
383 out_contents->base = cbb->base; in CBB_add_asn1()
384 cbb->child = out_contents; in CBB_add_asn1()
385 cbb->child->offset = offset; in CBB_add_asn1()
386 cbb->child->pending_len_len = 1; in CBB_add_asn1()
387 cbb->child->pending_is_asn1 = 1; in CBB_add_asn1()
392 int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) { in CBB_add_bytes() argument
395 if (!CBB_flush(cbb) || in CBB_add_bytes()
396 !cbb_buffer_add(cbb->base, &dest, len)) { in CBB_add_bytes()
403 int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) { in CBB_add_space() argument
404 if (!CBB_flush(cbb) || in CBB_add_space()
405 !cbb_buffer_add(cbb->base, out_data, len)) { in CBB_add_space()
411 int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) { in CBB_reserve() argument
412 if (!CBB_flush(cbb) || in CBB_reserve()
413 !cbb_buffer_reserve(cbb->base, out_data, len)) { in CBB_reserve()
419 int CBB_did_write(CBB *cbb, size_t len) { in CBB_did_write() argument
420 size_t newlen = cbb->base->len + len; in CBB_did_write()
421 if (cbb->child != NULL || in CBB_did_write()
422 newlen < cbb->base->len || in CBB_did_write()
423 newlen > cbb->base->cap) { in CBB_did_write()
426 cbb->base->len = newlen; in CBB_did_write()
430 int CBB_add_u8(CBB *cbb, uint8_t value) { in CBB_add_u8() argument
431 if (!CBB_flush(cbb)) { in CBB_add_u8()
435 return cbb_buffer_add_u(cbb->base, value, 1); in CBB_add_u8()
438 int CBB_add_u16(CBB *cbb, uint16_t value) { in CBB_add_u16() argument
439 if (!CBB_flush(cbb)) { in CBB_add_u16()
443 return cbb_buffer_add_u(cbb->base, value, 2); in CBB_add_u16()
446 int CBB_add_u24(CBB *cbb, uint32_t value) { in CBB_add_u24() argument
447 if (!CBB_flush(cbb)) { in CBB_add_u24()
451 return cbb_buffer_add_u(cbb->base, value, 3); in CBB_add_u24()
454 int CBB_add_u32(CBB *cbb, uint32_t value) { in CBB_add_u32() argument
455 if (!CBB_flush(cbb)) { in CBB_add_u32()
459 return cbb_buffer_add_u(cbb->base, value, 4); in CBB_add_u32()
462 int CBB_add_u64(CBB *cbb, uint64_t value) { in CBB_add_u64() argument
463 if (!CBB_flush(cbb)) { in CBB_add_u64()
466 return cbb_buffer_add_u(cbb->base, value, 8); in CBB_add_u64()
469 void CBB_discard_child(CBB *cbb) { in CBB_discard_child() argument
470 if (cbb->child == NULL) { in CBB_discard_child()
474 cbb->base->len = cbb->child->offset; in CBB_discard_child()
476 cbb->child->base = NULL; in CBB_discard_child()
477 cbb->child = NULL; in CBB_discard_child()
480 int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) { in CBB_add_asn1_uint64() argument
484 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER)) { in CBB_add_asn1_uint64()
512 return CBB_flush(cbb); in CBB_add_asn1_uint64()
515 int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len) { in CBB_add_asn1_octet_string() argument
517 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_OCTETSTRING) || in CBB_add_asn1_octet_string()
519 !CBB_flush(cbb)) { in CBB_add_asn1_octet_string()
526 int CBB_add_asn1_bool(CBB *cbb, int value) { in CBB_add_asn1_bool() argument
528 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_BOOLEAN) || in CBB_add_asn1_bool()
530 !CBB_flush(cbb)) { in CBB_add_asn1_bool()
568 int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) { in CBB_add_asn1_oid_from_text() argument
569 if (!CBB_flush(cbb)) { in CBB_add_asn1_oid_from_text()
588 !add_base128_integer(cbb, 40u * a + b)) { in CBB_add_asn1_oid_from_text()
595 !add_base128_integer(cbb, a)) { in CBB_add_asn1_oid_from_text()
621 int CBB_flush_asn1_set_of(CBB *cbb) { in CBB_flush_asn1_set_of() argument
622 if (!CBB_flush(cbb)) { in CBB_flush_asn1_set_of()
628 CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb)); in CBB_flush_asn1_set_of()
646 size_t buf_len = CBB_len(cbb); in CBB_flush_asn1_set_of()
647 uint8_t *buf = BUF_memdup(CBB_data(cbb), buf_len); in CBB_flush_asn1_set_of()
661 cbb->base->len = cbb->offset + cbb->pending_len_len; in CBB_flush_asn1_set_of()
663 if (!CBB_add_bytes(cbb, CBS_data(&children[i]), CBS_len(&children[i]))) { in CBB_flush_asn1_set_of()
667 assert(CBB_len(cbb) == buf_len); in CBB_flush_asn1_set_of()