summaryrefslogtreecommitdiffstats
path: root/fmt_asn1intpayload.c
blob: 15e71ca4f36068afa95346b67e99c124b77c6a87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "asn1.h"

size_t fmt_asn1intpayload(char* dest,unsigned long l) {
  size_t needed=sizeof l,i,fixup;
  for (i=1; i<needed; ++i) {
    if (!(l>>(i*8)))
      break;
  }
  fixup=(l>>((i-1)*8))&0x80 ? 1 : 0;
  if (dest) {
    size_t j=i;
    if (fixup) *dest++=0;
    while (j) {
      --j;
      *dest=(l>>(j*8))&0xff;
      ++dest;
    }
  }
  return i+fixup;
}