1 : /* Copyright (c) 2007 James Antill -- See LICENSE file for terms. */
2 : #ifndef USTR_B_H
3 : #define USTR_B_H 1
4 :
5 : #include "ustr-main.h"
6 :
7 : #if USTR_CONF_HAVE_STDINT_H
8 :
9 : /* ---------------- add ---------------- */
10 : USTR_CONF_EI_PROTO int ustr_add_b_uint16(struct Ustr **, uint_least16_t)
11 : USTR__COMPILE_ATTR_NONNULL_A();
12 : USTR_CONF_EI_PROTO int ustr_add_b_uint32(struct Ustr **, uint_least32_t)
13 : USTR__COMPILE_ATTR_NONNULL_A();
14 : USTR_CONF_EI_PROTO int ustr_add_b_uint64(struct Ustr **, uint_least64_t)
15 : USTR__COMPILE_ATTR_NONNULL_A();
16 :
17 : /* ---------------- parse ---------------- */
18 :
19 : USTR_CONF_EI_PROTO uint_least16_t ustr_parse_b_uint16(const struct Ustr*,size_t)
20 : USTR__COMPILE_ATTR_NONNULL_A();
21 : USTR_CONF_EI_PROTO uint_least32_t ustr_parse_b_uint32(const struct Ustr*,size_t)
22 : USTR__COMPILE_ATTR_NONNULL_A();
23 : USTR_CONF_EI_PROTO uint_least64_t ustr_parse_b_uint64(const struct Ustr*,size_t)
24 : USTR__COMPILE_ATTR_NONNULL_A();
25 :
26 : /* ---------------- pool APIs ---------------- */
27 :
28 : /* ---------------- add ---------------- */
29 : USTR_CONF_EI_PROTO
30 : int ustrp_add_b_uint16(struct Ustr_pool *, struct Ustrp **, uint_least16_t)
31 : USTR__COMPILE_ATTR_NONNULL_A();
32 : USTR_CONF_EI_PROTO
33 : int ustrp_add_b_uint32(struct Ustr_pool *, struct Ustrp **, uint_least32_t)
34 : USTR__COMPILE_ATTR_NONNULL_A();
35 : USTR_CONF_EI_PROTO
36 : int ustrp_add_b_uint64(struct Ustr_pool *, struct Ustrp **, uint_least64_t)
37 : USTR__COMPILE_ATTR_NONNULL_A();
38 :
39 :
40 : /* ---------------- parse ---------------- */
41 :
42 : USTR_CONF_EI_PROTO
43 : uint_least16_t ustrp_parse_b_uint16(const struct Ustrp *, size_t)
44 : USTR__COMPILE_ATTR_NONNULL_A();
45 : USTR_CONF_EI_PROTO
46 : uint_least32_t ustrp_parse_b_uint32(const struct Ustrp *, size_t)
47 : USTR__COMPILE_ATTR_NONNULL_A();
48 : USTR_CONF_EI_PROTO
49 : uint_least64_t ustrp_parse_b_uint64(const struct Ustrp *, size_t)
50 : USTR__COMPILE_ATTR_NONNULL_A();
51 :
52 :
53 : #if USTR_CONF_COMPILE_USE_INLINE
54 : USTR_CONF_II_PROTO int ustr_add_b_uint16(struct Ustr **ps1, uint_least16_t data)
55 27 : {
56 : unsigned char buf[2];
57 :
58 36 : buf[1] = data & 0xFF; data >>= 8;
59 36 : buf[0] = data & 0xFF;
60 :
61 36 : return (ustr_add_buf(ps1, buf, sizeof(buf)));
62 : }
63 :
64 : USTR_CONF_II_PROTO int ustr_add_b_uint32(struct Ustr **ps1, uint_least32_t data)
65 54 : {
66 : unsigned char buf[4];
67 :
68 72 : buf[3] = data & 0xFF; data >>= 8;
69 72 : buf[2] = data & 0xFF; data >>= 8;
70 72 : buf[1] = data & 0xFF; data >>= 8;
71 72 : buf[0] = data & 0xFF;
72 :
73 72 : return (ustr_add_buf(ps1, buf, sizeof(buf)));
74 : }
75 :
76 : USTR_CONF_II_PROTO int ustr_add_b_uint64(struct Ustr **ps1, uint_least64_t data)
77 75 : {
78 : unsigned char buf[8];
79 :
80 100 : buf[7] = data & 0xFF; data >>= 8;
81 100 : buf[6] = data & 0xFF; data >>= 8;
82 100 : buf[5] = data & 0xFF; data >>= 8;
83 100 : buf[4] = data & 0xFF; data >>= 8;
84 :
85 100 : buf[3] = data & 0xFF; data >>= 8;
86 100 : buf[2] = data & 0xFF; data >>= 8;
87 100 : buf[1] = data & 0xFF; data >>= 8;
88 100 : buf[0] = data & 0xFF;
89 :
90 100 : return (ustr_add_buf(ps1, buf, sizeof(buf)));
91 : }
92 :
93 : USTR_CONF_II_PROTO
94 : uint_least16_t ustr_parse_b_uint16(const struct Ustr *s1, size_t off)
95 27 : {
96 36 : uint_least16_t ret = 0;
97 36 : const unsigned char *ptr = 0;
98 27 : size_t len = ustr_len(s1);
99 :
100 36 : USTR_ASSERT_RET(off <= len, 0);
101 :
102 36 : if (len < 2) return (ret);
103 36 : if ((len - 2) < off) return (ret);
104 :
105 36 : ptr = (const unsigned char *) ustr_cstr(s1);
106 36 : ptr += off;
107 36 : ret += *ptr++; ret <<= 8;
108 36 : ret += *ptr++;
109 :
110 36 : return (ret);
111 : }
112 :
113 : USTR_CONF_II_PROTO
114 : uint_least32_t ustr_parse_b_uint32(const struct Ustr *s1, size_t off)
115 54 : {
116 72 : uint_least32_t ret = 0;
117 72 : const unsigned char *ptr = 0;
118 54 : size_t len = ustr_len(s1);
119 :
120 72 : USTR_ASSERT_RET(off <= len, 0);
121 :
122 72 : if (len < 4) return (ret);
123 72 : if ((len - 4) < off) return (ret);
124 :
125 72 : ptr = (const unsigned char *) ustr_cstr(s1);
126 72 : ptr += off;
127 72 : ret += *ptr++; ret <<= 8;
128 72 : ret += *ptr++; ret <<= 8;
129 72 : ret += *ptr++; ret <<= 8;
130 72 : ret += *ptr++;
131 :
132 72 : return (ret);
133 : }
134 :
135 : USTR_CONF_II_PROTO
136 : uint_least64_t ustr_parse_b_uint64(const struct Ustr *s1, size_t off)
137 75 : {
138 100 : uint_least64_t ret = 0;
139 100 : const unsigned char *ptr = 0;
140 75 : size_t len = ustr_len(s1);
141 :
142 100 : USTR_ASSERT_RET(off <= len, 0);
143 :
144 100 : if (len < 8) return (ret);
145 100 : if ((len - 8) < off) return (ret);
146 :
147 100 : ptr = (const unsigned char *) ustr_cstr(s1);
148 100 : ptr += off;
149 100 : ret += *ptr++; ret <<= 8;
150 100 : ret += *ptr++; ret <<= 8;
151 100 : ret += *ptr++; ret <<= 8;
152 100 : ret += *ptr++; ret <<= 8;
153 :
154 100 : ret += *ptr++; ret <<= 8;
155 100 : ret += *ptr++; ret <<= 8;
156 100 : ret += *ptr++; ret <<= 8;
157 100 : ret += *ptr++;
158 :
159 100 : return (ret);
160 : }
161 :
162 : /* ----------------------------------------------- */
163 : /* copy and paste the above functions for pool API */
164 : /* ----------------------------------------------- */
165 :
166 : USTR_CONF_II_PROTO
167 : int ustrp_add_b_uint16(struct Ustr_pool *p, struct Ustrp **ps1,
168 : uint_least16_t data)
169 27 : {
170 : unsigned char buf[2];
171 :
172 36 : buf[1] = data & 0xFF; data >>= 8;
173 36 : buf[0] = data & 0xFF;
174 :
175 36 : return (ustrp_add_buf(p, ps1, buf, sizeof(buf)));
176 : }
177 :
178 : USTR_CONF_II_PROTO
179 : int ustrp_add_b_uint32(struct Ustr_pool *p, struct Ustrp **ps1,
180 : uint_least32_t data)
181 54 : {
182 : unsigned char buf[4];
183 :
184 72 : buf[3] = data & 0xFF; data >>= 8;
185 72 : buf[2] = data & 0xFF; data >>= 8;
186 72 : buf[1] = data & 0xFF; data >>= 8;
187 72 : buf[0] = data & 0xFF;
188 :
189 72 : return (ustrp_add_buf(p, ps1, buf, sizeof(buf)));
190 : }
191 :
192 : USTR_CONF_II_PROTO
193 : int ustrp_add_b_uint64(struct Ustr_pool *p, struct Ustrp **ps1,
194 : uint_least64_t data)
195 75 : {
196 : unsigned char buf[8];
197 :
198 100 : buf[7] = data & 0xFF; data >>= 8;
199 100 : buf[6] = data & 0xFF; data >>= 8;
200 100 : buf[5] = data & 0xFF; data >>= 8;
201 100 : buf[4] = data & 0xFF; data >>= 8;
202 :
203 100 : buf[3] = data & 0xFF; data >>= 8;
204 100 : buf[2] = data & 0xFF; data >>= 8;
205 100 : buf[1] = data & 0xFF; data >>= 8;
206 100 : buf[0] = data & 0xFF;
207 :
208 100 : return (ustrp_add_buf(p, ps1, buf, sizeof(buf)));
209 : }
210 :
211 : USTR_CONF_II_PROTO
212 : uint_least16_t ustrp_parse_b_uint16(const struct Ustrp *s1, size_t off)
213 27 : {
214 36 : uint_least16_t ret = 0;
215 36 : const unsigned char *ptr = 0;
216 18 : size_t len = ustrp_len(s1);
217 :
218 36 : USTR_ASSERT_RET(off <= len, 0);
219 :
220 36 : if (len < 2) return (ret);
221 36 : if ((len - 2) < off) return (ret);
222 :
223 36 : ptr = (const unsigned char *) ustrp_cstr(s1);
224 36 : ptr += off;
225 36 : ret += *ptr++; ret <<= 8;
226 36 : ret += *ptr++;
227 :
228 36 : return (ret);
229 : }
230 :
231 : USTR_CONF_II_PROTO
232 : uint_least32_t ustrp_parse_b_uint32(const struct Ustrp *s1, size_t off)
233 54 : {
234 72 : uint_least32_t ret = 0;
235 72 : const unsigned char *ptr = 0;
236 36 : size_t len = ustrp_len(s1);
237 :
238 72 : USTR_ASSERT_RET(off <= len, 0);
239 :
240 72 : if (len < 4) return (ret);
241 72 : if ((len - 4) < off) return (ret);
242 :
243 72 : ptr = (const unsigned char *) ustrp_cstr(s1);
244 72 : ptr += off;
245 72 : ret += *ptr++; ret <<= 8;
246 72 : ret += *ptr++; ret <<= 8;
247 72 : ret += *ptr++; ret <<= 8;
248 72 : ret += *ptr++;
249 :
250 72 : return (ret);
251 : }
252 :
253 : USTR_CONF_II_PROTO
254 : uint_least64_t ustrp_parse_b_uint64(const struct Ustrp *s1, size_t off)
255 75 : {
256 100 : uint_least64_t ret = 0;
257 100 : const unsigned char *ptr = 0;
258 50 : size_t len = ustrp_len(s1);
259 :
260 100 : USTR_ASSERT_RET(off <= len, 0);
261 :
262 100 : if (len < 8) return (ret);
263 100 : if ((len - 8) < off) return (ret);
264 :
265 100 : ptr = (const unsigned char *) ustrp_cstr(s1);
266 100 : ptr += off;
267 100 : ret += *ptr++; ret <<= 8;
268 100 : ret += *ptr++; ret <<= 8;
269 100 : ret += *ptr++; ret <<= 8;
270 100 : ret += *ptr++; ret <<= 8;
271 :
272 100 : ret += *ptr++; ret <<= 8;
273 100 : ret += *ptr++; ret <<= 8;
274 100 : ret += *ptr++; ret <<= 8;
275 100 : ret += *ptr++;
276 :
277 100 : return (ret);
278 : }
279 :
280 : #endif
281 :
282 : #endif
283 :
284 : #endif
|