Skip to content
Snippets Groups Projects
Commit 619b6aae authored by Lev Walkin's avatar Lev Walkin
Browse files

better algorithms

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