Commit b2ce577f authored by Lev Walkin's avatar Lev Walkin

proper computations

parent 40c3b442
...@@ -104,7 +104,7 @@ der_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) { ...@@ -104,7 +104,7 @@ der_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) {
ber_tlv_tag_t tval = BER_TAG_VALUE(tag); ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
uint8_t *buf = (uint8_t *)bufp; uint8_t *buf = (uint8_t *)bufp;
uint8_t *end; uint8_t *end;
size_t computed_size; size_t required_size;
int i; int i;
if(tval <= 30) { if(tval <= 30) {
...@@ -118,29 +118,26 @@ der_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) { ...@@ -118,29 +118,26 @@ der_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) {
/* /*
* Compute the size of the subsequent bytes. * Compute the size of the subsequent bytes.
* The routine is written so every floating-point
* operation is done at compile time.
* Note, there is a subtle problem lurking here,
* could you guess where it is? :)
* Hint: what happens when ((8*sizeof(tag))%7) == 0?
*/ */
computed_size = 1 + 8 * sizeof(tag) / 7; for(required_size = 1, i = 7; i < 8 * sizeof(tag); i += 7) {
for(i = (8*sizeof(tag)) - ((8*sizeof(tag))%7); i >= 7; i -= 7) { if(tag >> i)
if((tval >> i) & 0x7F) break; required_size++;
computed_size--; else
break;
} }
if(size < required_size)
return required_size + 1;
/* /*
* Fill in the buffer, space permitting. * Fill in the buffer, space permitting.
*/ */
if(size > computed_size) end = buf + required_size - 1;
end = buf + computed_size; for(i -= 7; buf <= end; i -= 7, buf++) {
else *buf = 0x80 | ((tval >> i) & 0x7F);
end = buf + size;
for((void)i; buf < end; i -= 7, buf++) {
*buf = 0x80 | ((tval>>i) & 0x7F);
} }
*end &= 0x7F; /* Clear the last high bit */
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