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

size_t fmt_asn1sintpayload(char* dest,signed long l) {
  size_t needed=sizeof l,i;
  signed long tmp=0x7f;
  if (l>=0) return fmt_asn1intpayload(dest,l);
  for (i=1; i<needed; ++i) {
    /* assumes two's complement */
    if ((l|tmp) == -1)
      break;
    tmp=(tmp<<8)|0xff;
  }
  if (dest) {
    size_t j=i;
    while (j) {
      --j;
      *dest=(l>>(j*8))&0xff;
      ++dest;
    }
  }
  return i;
}