Files
mars-tinyldap/fmt_asn1tag.c
2007-06-28 22:17:33 +00:00

26 lines
607 B
C

#include "asn1.h"
/* write int in least amount of bytes, return number of bytes */
/* as used in ASN.1 tags */
size_t fmt_asn1tag(char* dest,enum asn1_tagclass tc,enum asn1_tagtype tt,unsigned long l) {
/* encoding is either l%128 or (0x1f,...) */
size_t needed=(sizeof l)*7/8,i;
if (l<0x1f) {
if (dest) *dest=(int)tc+(int)tt+(l&0x1f);
return 1;
}
for (i=1; i<needed; ++i)
if (!(l>>(i*7)))
break;
if (dest) {
size_t j=i;
*dest=(int)tc+(int)tt+0x1f; ++dest;
while (j) {
--j;
*dest=((l>>(j*7))&0x7f) + (j?0x80:0);
++dest;
}
}
return i+1;
}