Files
mars-tinyldap/fmt_asn1tagint.c
2019-09-24 16:27:16 +00:00

42 lines
1.2 KiB
C

#include "asn1.h"
static size_t tagintlen(unsigned long l) {
size_t i;
size_t needed=((sizeof l)*8)/7+1;
for (i=1; i<needed; ++i)
if (!(l>>(i*7)))
break;
return i;
}
/* Common integer storing method, used in tags >= 0x1f and OIDs */
/* Store big endian, 7 bits at a time, set high bit in all but last byte */
/* Return number of bytes needed. Only write if dest!=NULL */
size_t fmt_asn1tagint(char* dest,unsigned long l) {
size_t bytes = tagintlen(l);
if (dest) {
size_t i, j=bytes, k=bytes;
for (i=0; i<k; ++i) {
--j;
dest[i]=((l>>(j*7))&0x7f) + (j?0x80:0);
}
}
return bytes;
}
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[10];
assert(tagintlen(0xfffffffful)==5);
assert(fmt_asn1tagint(buf,0xfffffffful)==5 && !memcmp(buf,"\x8f\xff\xff\xff\x7f",5));
if (sizeof(long)==8) assert(fmt_asn1tagint(buf,0xfffffffffffffffful)==10 && !memcmp(buf,"\x81\xff\xff\xff\xff\xff\xff\xff\xff\x7f",10));
assert(fmt_asn1tagint(buf,1)==1 && !memcmp(buf,"\x01",1));
assert(fmt_asn1tagint(buf,0x7f)==1 && !memcmp(buf,"\x7f",1));
assert(fmt_asn1tagint(buf,0x80)==2 && !memcmp(buf,"\x81\x00",2));
assert(fmt_asn1tagint(buf,0xffffffff)==5 && !memcmp(buf,"\x8f\xff\xff\xff\x7f",5));
}
#endif