summaryrefslogtreecommitdiff
path: root/src/uint32p.c
blob: 0c266a985cbe896eb8d2374c2e4da919f470877f (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
#include "uint_t.h"

/**
  @file uint32p.c
  @author djb
  @ref qmail
  @brief packing/unpacking 32 bit integer to/from char string
*/

void uint32_pack(char s[4], uint32 u)
{
  s[0] = u & 255;
  u >>= 8;
  s[1] = u & 255;
  u >>= 8;
  s[2] = u & 255;
  s[3] = u >> 8;
}
void uint32_pack_big(char s[4], uint32 u)
{
  s[3] = u & 255;
  u >>= 8;
  s[2] = u & 255;
  u >>= 8;
  s[1] = u & 255;
  s[0] = u >> 8;
}

void uint32_unpack(char s[4], uint32 *u)
{
  uint32 result;

  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 uint32_unpack_big(char s[4], uint32 *u)
{
  uint32 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];

  *u = result;
}