Commit 619b6aae authored by Lev Walkin's avatar Lev Walkin

better algorithms

parent 2d3f7339
...@@ -20,7 +20,7 @@ ber_fetch_length(int _is_constructed, void *bufptr, size_t size, ...@@ -20,7 +20,7 @@ ber_fetch_length(int _is_constructed, void *bufptr, size_t size,
/* /*
* Short definite length. * Short definite length.
*/ */
*len_r = (oct & 0x7F); *len_r = oct; /* & 0x7F */
return 1; return 1;
} else { } else {
ber_tlv_len_t len; ber_tlv_len_t len;
...@@ -38,7 +38,7 @@ ber_fetch_length(int _is_constructed, void *bufptr, size_t size, ...@@ -38,7 +38,7 @@ ber_fetch_length(int _is_constructed, void *bufptr, size_t size,
oct &= 0x7F; /* Leave only the 7 LS bits */ oct &= 0x7F; /* Leave only the 7 LS bits */
for(len = 0, buf++, skipped = 1; for(len = 0, buf++, skipped = 1;
oct && (++skipped < size); buf++, oct--) { oct && (++skipped <= size); buf++, oct--) {
len = (len << 8) | *buf; len = (len << 8) | *buf;
if(len < 0 if(len < 0
...@@ -118,10 +118,10 @@ ber_skip_length(int _is_constructed, void *ptr, size_t size) { ...@@ -118,10 +118,10 @@ ber_skip_length(int _is_constructed, void *ptr, size_t size) {
ssize_t ssize_t
der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) { der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
size_t computed_size; /* Size of len encoding */ size_t required_size; /* Size of len encoding */
uint8_t *buf = (uint8_t *)bufp; uint8_t *buf = (uint8_t *)bufp;
uint8_t *end; uint8_t *end;
int i; size_t i;
if(len <= 127) { if(len <= 127) {
/* Encoded in 1 octet */ /* Encoded in 1 octet */
...@@ -132,28 +132,25 @@ der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) { ...@@ -132,28 +132,25 @@ der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
/* /*
* Compute the size of the subsequent bytes. * Compute the size of the subsequent bytes.
*/ */
computed_size = sizeof(len); /* assert(sizeof(len)<128), n.p. */ for(required_size = 1, i = 8; i < 8 * sizeof(len); i += 8) {
for(i = (8*(sizeof(len)-1)); i > 0; i -= 8) { if(len >> i)
if((len >> i) & 0xFF) break; required_size++;
computed_size--; else
break;
} }
if(size) { if(size < required_size)
*buf++ = 0x80 | computed_size; /* Length of the encoding */ return required_size + 1;
size--;
} *buf++ = 0x80 | required_size; /* Length of the encoding */
/* /*
* Produce the len encoding, space permitting. * Produce the len encoding, space permitting.
*/ */
if(size > computed_size) end = buf + required_size;
end = buf + computed_size; for(i -= 8; buf < end; i -= 8, buf++)
else *buf = (len >> i);
end = buf + size;
for((void)i /* Reuse bits count */; buf < end; i -= 8, buf++) {
*buf = (len >> i) & 0xFF;
}
return computed_size + 1; return required_size + 1;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment