librfn
An ad-hoc utility library
pack.c
Go to the documentation of this file.
1 /*
2  * pack.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include "librfn.h"
20 
21 void rf_pack_init(rf_pack_t *pack, void *p, unsigned int sz)
22 {
23  pack->basep = p;
24  pack->p = p;
25  pack->endp = pack->basep + sz;
26 }
27 
29 {
30  return pack->endp - pack->p;
31 }
32 
33 #define PACK(pack, decl, sz) \
34  uint8_t *decl = pack->p; \
35  pack->p += sz; \
36  if (pack->p <= pack->endp)
37 
38 void rf_pack_bytes(rf_pack_t *pack, void *p, unsigned int sz)
39 {
40  PACK(pack, q, sz) {
41  if (p)
42  memcpy(q, p, sz);
43  else
44  memset(q, 0, sz);
45  }
46 }
47 
48 void rf_pack_char(rf_pack_t *pack, char c);
49 void rf_pack_s8(rf_pack_t *pack, int8_t s8);
50 void rf_pack_u8(rf_pack_t *pack, int16_t u8);
51 void rf_pack_s16be(rf_pack_t *pack, int16_t s16);
52 void rf_pack_s16le(rf_pack_t *pack, int16_t s16)
53 {
54  PACK(pack, p, 2) {
55  p[0] = s16 & 0xff;
56  p[1] = (s16 >> 8) & 0xff;
57  }
58 }
59 
60 void rf_pack_u16be(rf_pack_t *pack, uint16_t u16)
61 {
62  PACK(pack, p, 2) {
63  p[0] = (u16 >> 8) & 0xff;
64  p[1] = u16 & 0xff;
65  }
66 }
67 
68 void rf_pack_u16le(rf_pack_t *pack, uint16_t u16)
69 {
70  PACK(pack, p, 2) {
71  p[0] = u16 & 0xff;
72  p[1] = (u16 >> 8) & 0xff;
73  }
74 }
75 
76 void rf_pack_s32be(rf_pack_t *pack, int32_t s32);
77 
78 void rf_pack_s32le(rf_pack_t *pack, int32_t s32)
79 {
80  PACK(pack, p, 4) {
81  p[0] = s32 & 0xff;
82  p[1] = (s32 >> 8) & 0xff;
83  p[2] = (s32 >> 16) & 0xff;
84  p[3] = (s32 >> 24) & 0xff;
85  }
86 }
87 
88 void rf_pack_u32be(rf_pack_t *pack, uint32_t u32);
89 void rf_pack_u32le(rf_pack_t *pack, uint32_t u32)
90 {
91  PACK(pack, p, 4) {
92  p[0] = u32 & 0xff;
93  p[1] = (u32 >> 8) & 0xff;
94  p[2] = (u32 >> 16) & 0xff;
95  p[3] = (u32 >> 24) & 0xff;
96  }
97 }
98 
99 #define UNPACK(pack, decl, sz) \
100  uint8_t *decl = pack->p; \
101  pack->p += sz; \
102  if (pack->p > pack->endp) \
103  return 0; \
104  else
105 
106 void rf_unpack_bytes(rf_pack_t *pack, void *p, unsigned int sz)
107 {
108  // this is an unpack operation but we use the pack macro for
109  // this special case of an unpack macro that does not
110  // return a value.
111  PACK(pack, q, sz) {
112  if (p)
113  memcpy(p, q, sz);
114  } else {
115  if (p)
116  memset(p, 0, sz);
117  }
118 }
119 
121 {
122  UNPACK(pack, p, 1) {
123  char c = p[0];
124  return c;
125  }
126 }
127 
128 int8_t rf_unpack_s8(rf_pack_t *pack)
129 {
130  UNPACK(pack, p, 1) {
131  int8_t s8 = p[0];
132  return s8;
133  }
134 }
135 
136 uint8_t rf_unpack_u8(rf_pack_t *pack)
137 {
138  UNPACK(pack, p, 1) {
139  uint8_t u8 = p[0];
140  return u8;
141  }
142 }
143 
144 int16_t rf_unpack_s16be(rf_pack_t *pack);
145 int16_t rf_unpack_s16le(rf_pack_t *pack);
146 uint16_t rf_unpack_u16be(rf_pack_t *pack);
147 
148 uint16_t rf_unpack_u16le(rf_pack_t *pack)
149 {
150  UNPACK(pack, p, 2) {
151  uint16_t u16 = p[0] | p[1] << 8;
152  return u16;
153  }
154 }
155 
156 int32_t rf_unpack_s32be(rf_pack_t *pack);
157 int32_t rf_unpack_s32le(rf_pack_t *pack);
158 uint32_t rf_unpack_u32be(rf_pack_t *pack);
159 uint32_t rf_unpack_u32le(rf_pack_t *pack)
160 {
161  UNPACK(pack, p, 4) {
162  uint32_t u32 = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
163  return u32;
164  }
165 }
166 
struct charlie c
int16_t rf_unpack_s16be(rf_pack_t *pack)
void rf_pack_u16le(rf_pack_t *pack, uint16_t u16)
Definition: pack.c:68
void rf_pack_s8(rf_pack_t *pack, int8_t s8)
uint8_t * endp
Definition: pack.h:32
void rf_pack_s16le(rf_pack_t *pack, int16_t s16)
Definition: pack.c:52
uint16_t rf_unpack_u16be(rf_pack_t *pack)
void rf_pack_u32le(rf_pack_t *pack, uint32_t u32)
Definition: pack.c:89
void rf_pack_u32be(rf_pack_t *pack, uint32_t u32)
int8_t rf_unpack_s8(rf_pack_t *pack)
Definition: pack.c:128
void rf_pack_u16be(rf_pack_t *pack, uint16_t u16)
Definition: pack.c:60
Definition: pack.h:30
uint8_t * p
Definition: pack.h:33
void rf_pack_bytes(rf_pack_t *pack, void *p, unsigned int sz)
Definition: pack.c:38
void rf_pack_char(rf_pack_t *pack, char c)
#define UNPACK(pack, decl, sz)
Definition: pack.c:99
int32_t rf_unpack_s32be(rf_pack_t *pack)
uint32_t rf_unpack_u32be(rf_pack_t *pack)
int rf_pack_remaining(rf_pack_t *pack)
Definition: pack.c:28
uint8_t * basep
Definition: pack.h:31
int16_t rf_unpack_s16le(rf_pack_t *pack)
char rf_unpack_char(rf_pack_t *pack)
Definition: pack.c:120
uint8_t rf_unpack_u8(rf_pack_t *pack)
Definition: pack.c:136
void rf_pack_s16be(rf_pack_t *pack, int16_t s16)
uint32_t rf_unpack_u32le(rf_pack_t *pack)
Definition: pack.c:159
#define PACK(pack, decl, sz)
Definition: pack.c:33
uint16_t rf_unpack_u16le(rf_pack_t *pack)
Definition: pack.c:148
void rf_pack_init(rf_pack_t *pack, void *p, unsigned int sz)
Definition: pack.c:21
void rf_pack_s32be(rf_pack_t *pack, int32_t s32)
int32_t rf_unpack_s32le(rf_pack_t *pack)
void rf_unpack_bytes(rf_pack_t *pack, void *p, unsigned int sz)
Definition: pack.c:106
void rf_pack_s32le(rf_pack_t *pack, int32_t s32)
Definition: pack.c:78
void rf_pack_u8(rf_pack_t *pack, int16_t u8)