File: fmt_varint.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (24 lines) | stat: -rw-r--r-- 668 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "fmt.h"

/* write int in least amount of bytes, return number of bytes */
/* as used in varints from Google protocol buffers */
size_t fmt_varint(char* dest,unsigned long long l) {
  /* high bit says if more bytes are coming, lower 7 bits are payload; little endian */
  size_t i;
  for (i=0; l; ++i, l>>=7) {
    if (dest) dest[i]=(l&0x7f) | ((!!(l&~0x7f))<<7);
  }
  if (!i) {	/* l was 0 */
    if (dest) dest[0]=0;
    ++i;
  }
  return i;
}

#ifdef __ELF__
size_t fmt_pb_type0_int(char* dest,unsigned long long l) __attribute__((alias("fmt_varint")));
#else
size_t fmt_pb_type0_int(char* dest,unsigned long long l) {
  return fmt_varint(dest,l);
}
#endif