2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <ber_tlv_length.h>
7 #include <ber_tlv_tag.h>
10 ber_fetch_length(int _is_constructed, const void *bufptr, size_t size,
11 ber_tlv_len_t *len_r) {
12 const uint8_t *buf = (const uint8_t *)bufptr;
16 return 0; /* Want more */
18 oct = *(const uint8_t *)buf;
19 if((oct & 0x80) == 0) {
21 * Short definite length.
23 *len_r = oct; /* & 0x7F */
29 if(_is_constructed && oct == 0x80) {
30 *len_r = -1; /* Indefinite length */
35 /* Reserved in standard for future use. */
39 oct &= 0x7F; /* Leave only the 7 LS bits */
40 for(len = 0, buf++, skipped = 1;
41 oct && (++skipped <= size); buf++, oct--) {
43 /* Verify that we won't overflow. */
44 if(!(len >> ((8 * sizeof(len)) - (8+1)))) {
45 len = (len << 8) | *buf;
47 /* Too large length value. */
53 if(len < 0 || len > RSSIZE_MAX) {
54 /* Length value out of sane range. */
62 return 0; /* Want more */
68 ber_skip_length(const asn_codec_ctx_t *opt_codec_ctx,
69 int _is_constructed, const void *ptr, size_t size) {
70 ber_tlv_len_t vlen; /* Length of V in TLV */
71 ssize_t tl; /* Length of L in TLV */
72 ssize_t ll; /* Length of L in TLV */
76 * Make sure we didn't exceed the maximum stack size.
78 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
82 * Determine the size of L in TLV.
84 ll = ber_fetch_length(_is_constructed, ptr, size, &vlen);
85 if(ll <= 0) return ll;
93 return 0; /* Want more */
100 ASN_DEBUG("Skipping indefinite length");
101 for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) {
105 tl = ber_fetch_tag(ptr, size, &tag);
106 if(tl <= 0) return tl;
108 ll = ber_skip_length(opt_codec_ctx,
109 BER_TLV_CONSTRUCTED(ptr),
110 ((const char *)ptr) + tl, size - tl);
111 if(ll <= 0) return ll;
116 * This may be the end of the indefinite length structure,
117 * two consecutive 0 octets.
118 * Check if it is true.
120 if(((const uint8_t *)ptr)[0] == 0
121 && ((const uint8_t *)ptr)[1] == 0)
124 ptr = ((const char *)ptr) + tl + ll;
132 der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
133 size_t required_size; /* Size of len encoding */
134 uint8_t *buf = (uint8_t *)bufp;
139 /* Encoded in 1 octet */
140 if(size) *buf = (uint8_t)len;
145 * Compute the size of the subsequent bytes.
147 for(required_size = 1, i = 8; i < 8 * (int)sizeof(len); i += 8) {
154 if(size <= required_size)
155 return required_size + 1;
157 *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */
160 * Produce the len encoding, space permitting.
162 end = buf + required_size;
163 for(i -= 8; buf < end; i -= 8, buf++)
164 *buf = (uint8_t)(len >> i);
166 return required_size + 1;