summaryrefslogtreecommitdiff
path: root/src/uint64p.c
blob: 4631bce55538b6f25a45d1b47d840ed3cb0b5dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "uint_t.h"

/**
  @file uint64p.c
  @author feh, jannis
  @source djbdns6
  @brief packing/unpacking 64 bit integer to/from char string
*/

void uint64_pack(char s[8], uint64 u)
{
  s[0] = u & 255;
  u >>= 8;
  s[1] = u & 255;
  u >>= 8;
  s[2] = u & 255;
  u >>= 8;
  s[3] = u & 255;
  u >>= 8;
  s[4] = u & 255;
  u >>= 8;
  s[5] = u & 255;
  u >>= 8;
  s[6] = u & 255;
  u >>= 8;
  s[7] = u & 255;
}
void uint64_pack_big(char s[8], uint64 u)
{
  s[7] = u & 255;
  u >>= 8;
  s[6] = u & 255;
  u >>= 8;
  s[5] = u & 255;
  u >>= 8;
  s[4] = u & 255;
  u >>= 8;
  s[3] = u & 255;
  u >>= 8;
  s[2] = u & 255;
  u >>= 8;
  s[1] = u & 255;
  u >>= 8;
  s[0] = u & 255;
}

void uint64_unpack(char s[8], uint64 *u)
{
  uint64 result;

  result = (unsigned char)s[7];
  result <<= 8;
  result += (unsigned char)s[6];
  result <<= 8;
  result += (unsigned char)s[5];
  result <<= 8;
  result += (unsigned char)s[4];
  result <<= 8;
  result += (unsigned char)s[3];
  result <<= 8;
  result += (unsigned char)s[2];
  result <<= 8;
  result += (unsigned char)s[1];
  result <<= 8;
  result += (unsigned char)s[0];

  *u = result;
}
void uint64_unpack_big(char s[8], uint64 *u)
{
  uint64 result;

  result = (unsigned char)s[0];
  result <<= 8;
  result += (unsigned char)s[1];
  result <<= 8;
  result += (unsigned char)s[2];
  result <<= 8;
  result += (unsigned char)s[3];
  result <<= 8;
  result += (unsigned char)s[4];
  result <<= 8;
  result += (unsigned char)s[5];
  result <<= 8;
  result += (unsigned char)s[6];
  result <<= 8;
  result += (unsigned char)s[7];

  *u = result;
}