Fix: counter: cast UINT*_MAX to 64-bit signed type before negative
[lttng-ust.git] / libmsgpack / msgpack.c
CommitLineData
49705576
FD
1/*
2 * msgpack.c
3 *
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#define _GNU_SOURCE
22#define _LGPL_SOURCE
23#include <stddef.h>
24
25#define MSGPACK_FIXSTR_ID_MASK 0xA0
26#define MSGPACK_FIXMAP_ID_MASK 0x80
27#define MSGPACK_FIXARRAY_ID_MASK 0x90
28
29#define MSGPACK_NIL_ID 0xC0
30#define MSGPACK_FALSE_ID 0xC2
31#define MSGPACK_TRUE_ID 0xC3
32#define MSGPACK_MAP16_ID 0xDE
33#define MSGPACK_ARRAY16_ID 0xDC
34
35#define MSGPACK_UINT8_ID 0xCC
36#define MSGPACK_UINT16_ID 0xCD
37#define MSGPACK_UINT32_ID 0xCE
38#define MSGPACK_UINT64_ID 0xCF
39
40#define MSGPACK_INT8_ID 0xD0
41#define MSGPACK_INT16_ID 0xD1
42#define MSGPACK_INT32_ID 0xD2
43#define MSGPACK_INT64_ID 0xD3
44
45#define MSGPACK_FLOAT64_ID 0xCB
46#define MSGPACK_STR16_ID 0xDA
47
48#define MSGPACK_FIXINT_MAX ((1 << 7) - 1)
49#define MSGPACK_FIXINT_MIN -(1 << 5)
50#define MSGPACK_FIXMAP_MAX_COUNT 15
51#define MSGPACK_FIXARRAY_MAX_COUNT 15
52#define MSGPACK_FIXSTR_MAX_LENGTH 31
53
54#ifdef __KERNEL__
55#include <linux/bug.h>
56#include <linux/string.h>
57#include <linux/types.h>
58
59#include <lttng/msgpack.h>
60
61#define INT8_MIN (-128)
62#define INT16_MIN (-32767-1)
63#define INT32_MIN (-2147483647-1)
64#define INT8_MAX (127)
65#define INT16_MAX (32767)
66#define INT32_MAX (2147483647)
67#define UINT8_MAX (255)
68#define UINT16_MAX (65535)
69#define UINT32_MAX (4294967295U)
70
71#define byteswap_host_to_be16(_tmp) cpu_to_be16(_tmp)
72#define byteswap_host_to_be32(_tmp) cpu_to_be32(_tmp)
73#define byteswap_host_to_be64(_tmp) cpu_to_be64(_tmp)
74
75#define lttng_msgpack_assert(cond) WARN_ON(!(cond))
76
77#else /* __KERNEL__ */
78
79#include <endian.h>
80#include <stdio.h>
81#include <string.h>
82
83#include "msgpack.h"
84
85#define byteswap_host_to_be16(_tmp) htobe16(_tmp)
86#define byteswap_host_to_be32(_tmp) htobe32(_tmp)
87#define byteswap_host_to_be64(_tmp) htobe64(_tmp)
88
89#define lttng_msgpack_assert(cond) ({ \
90 if (!(cond)) \
91 fprintf(stderr, "Assertion failed. %s:%d\n", __FILE__, __LINE__); \
92 })
93#endif /* __KERNEL__ */
94
95static inline int lttng_msgpack_append_buffer(
96 struct lttng_msgpack_writer *writer,
97 const uint8_t *buf,
98 size_t length)
99{
100 int ret = 0;
101
102 lttng_msgpack_assert(buf);
103
104 /* Ensure we are not trying to write after the end of the buffer. */
105 if (writer->write_pos + length > writer->end_write_pos) {
106 ret = -1;
107 goto end;
108 }
109
110 memcpy(writer->write_pos, buf, length);
111 writer->write_pos += length;
112end:
113 return ret;
114}
115
116static inline int lttng_msgpack_append_u8(
117 struct lttng_msgpack_writer *writer, uint8_t value)
118{
119 return lttng_msgpack_append_buffer(writer, &value, sizeof(value));
120}
121
122static inline int lttng_msgpack_append_u16(
123 struct lttng_msgpack_writer *writer, uint16_t value)
124{
125 value = byteswap_host_to_be16(value);
126
127 return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
128}
129
130static inline int lttng_msgpack_append_u32(
131 struct lttng_msgpack_writer *writer, uint32_t value)
132{
133 value = byteswap_host_to_be32(value);
134
135 return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
136}
137
138static inline int lttng_msgpack_append_u64(
139 struct lttng_msgpack_writer *writer, uint64_t value)
140{
141 value = byteswap_host_to_be64(value);
142
143 return lttng_msgpack_append_buffer(writer, (uint8_t *) &value, sizeof(value));
144}
145
146static inline int lttng_msgpack_append_f64(
147 struct lttng_msgpack_writer *writer, double value)
148{
149
150 union {
151 double d;
152 uint64_t u;
153 } u;
154
155 u.d = value;
156
157 return lttng_msgpack_append_u64(writer, u.u);
158}
159
160static inline int lttng_msgpack_append_i8(
161 struct lttng_msgpack_writer *writer, int8_t value)
162{
163 return lttng_msgpack_append_u8(writer, (uint8_t) value);
164}
165
166static inline int lttng_msgpack_append_i16(
167 struct lttng_msgpack_writer *writer, int16_t value)
168{
169 return lttng_msgpack_append_u16(writer, (uint16_t) value);
170}
171
172static inline int lttng_msgpack_append_i32(
173 struct lttng_msgpack_writer *writer, int32_t value)
174{
175 return lttng_msgpack_append_u32(writer, (uint32_t) value);
176}
177
178static inline int lttng_msgpack_append_i64(
179 struct lttng_msgpack_writer *writer, int64_t value)
180{
181 return lttng_msgpack_append_u64(writer, (uint64_t) value);
182}
183
184static inline int lttng_msgpack_encode_f64(
185 struct lttng_msgpack_writer *writer, double value)
186{
187 int ret;
188
189 ret = lttng_msgpack_append_u8(writer, MSGPACK_FLOAT64_ID);
190 if (ret)
191 goto end;
192
193 ret = lttng_msgpack_append_f64(writer, value);
194 if (ret)
195 goto end;
196
197end:
198 return ret;
199}
200
201static inline int lttng_msgpack_encode_fixmap(
202 struct lttng_msgpack_writer *writer, uint8_t count)
203{
204 int ret = 0;
205
206 lttng_msgpack_assert(count <= MSGPACK_FIXMAP_MAX_COUNT);
207
208 ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXMAP_ID_MASK | count);
209 if (ret)
210 goto end;
211
212end:
213 return ret;
214}
215
216static inline int lttng_msgpack_encode_map16(
217 struct lttng_msgpack_writer *writer, uint16_t count)
218{
219 int ret;
220
221 lttng_msgpack_assert(count > MSGPACK_FIXMAP_MAX_COUNT);
222
223 ret = lttng_msgpack_append_u8(writer, MSGPACK_MAP16_ID);
224 if (ret)
225 goto end;
226
227 ret = lttng_msgpack_append_u16(writer, count);
228 if (ret)
229 goto end;
230
231end:
232 return ret;
233}
234
235static inline int lttng_msgpack_encode_fixarray(
236 struct lttng_msgpack_writer *writer, uint8_t count)
237{
238 int ret = 0;
239
240 lttng_msgpack_assert(count <= MSGPACK_FIXARRAY_MAX_COUNT);
241
242 ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXARRAY_ID_MASK | count);
243 if (ret)
244 goto end;
245
246end:
247 return ret;
248}
249
250static inline int lttng_msgpack_encode_array16(
251 struct lttng_msgpack_writer *writer, uint16_t count)
252{
253 int ret;
254
255 lttng_msgpack_assert(count > MSGPACK_FIXARRAY_MAX_COUNT);
256
257 ret = lttng_msgpack_append_u8(writer, MSGPACK_ARRAY16_ID);
258 if (ret)
259 goto end;
260
261 ret = lttng_msgpack_append_u16(writer, count);
262 if (ret)
263 goto end;
264
265end:
266 return ret;
267}
268
269static inline int lttng_msgpack_encode_fixstr(
270 struct lttng_msgpack_writer *writer,
271 const char *str,
272 uint8_t len)
273{
274 int ret;
275
276 lttng_msgpack_assert(len <= MSGPACK_FIXSTR_MAX_LENGTH);
277
278 ret = lttng_msgpack_append_u8(writer, MSGPACK_FIXSTR_ID_MASK | len);
279 if (ret)
280 goto end;
281
282 ret = lttng_msgpack_append_buffer(writer, (uint8_t *) str, len);
283 if (ret)
284 goto end;
285
286end:
287 return ret;
288}
289
290static inline int lttng_msgpack_encode_str16(
291 struct lttng_msgpack_writer *writer,
292 const char *str,
293 uint16_t len)
294{
295 int ret;
296
297 lttng_msgpack_assert(len > MSGPACK_FIXSTR_MAX_LENGTH);
298
299 ret = lttng_msgpack_append_u8(writer, MSGPACK_STR16_ID);
300 if (ret)
301 goto end;
302
303 ret = lttng_msgpack_append_u16(writer, len);
304 if (ret)
305 goto end;
306
307 ret = lttng_msgpack_append_buffer(writer, (uint8_t *) str, len);
308 if (ret)
309 goto end;
310
311end:
312 return ret;
313}
314
315int lttng_msgpack_begin_map(struct lttng_msgpack_writer *writer, size_t count)
316{
317 int ret;
318
319 if (count < 0 || count >= (1 << 16)) {
320 ret = -1;
321 goto end;
322 }
323
324 if (count <= MSGPACK_FIXMAP_MAX_COUNT)
325 ret = lttng_msgpack_encode_fixmap(writer, count);
326 else
327 ret = lttng_msgpack_encode_map16(writer, count);
328
329 writer->map_nesting++;
330end:
331 return ret;
332}
333
334int lttng_msgpack_end_map(struct lttng_msgpack_writer *writer)
335{
336 lttng_msgpack_assert(writer->map_nesting > 0);
337 writer->map_nesting--;
338 return 0;
339}
340
341int lttng_msgpack_begin_array(
342 struct lttng_msgpack_writer *writer, size_t count)
343{
344 int ret;
345
346 if (count < 0 || count >= (1 << 16)) {
347 ret = -1;
348 goto end;
349 }
350
351 if (count <= MSGPACK_FIXARRAY_MAX_COUNT)
352 ret = lttng_msgpack_encode_fixarray(writer, count);
353 else
354 ret = lttng_msgpack_encode_array16(writer, count);
355
356 writer->array_nesting++;
357end:
358 return ret;
359}
360
361int lttng_msgpack_end_array(struct lttng_msgpack_writer *writer)
362{
363 lttng_msgpack_assert(writer->array_nesting > 0);
364 writer->array_nesting--;
365 return 0;
366}
367
368int lttng_msgpack_write_str(struct lttng_msgpack_writer *writer,
369 const char *str)
370{
371 int ret;
372 size_t length = strlen(str);
373 if (length < 0 || length >= (1 << 16)) {
374 ret = -1;
375 goto end;
376 }
377
378 if (length <= MSGPACK_FIXSTR_MAX_LENGTH)
379 ret = lttng_msgpack_encode_fixstr(writer, str, length);
380 else
381 ret = lttng_msgpack_encode_str16(writer, str, length);
382
383end:
384 return ret;
385}
386
387int lttng_msgpack_write_nil(struct lttng_msgpack_writer *writer)
388{
389 return lttng_msgpack_append_u8(writer, MSGPACK_NIL_ID);
390}
391
392int lttng_msgpack_write_true(struct lttng_msgpack_writer *writer)
393{
394 return lttng_msgpack_append_u8(writer, MSGPACK_TRUE_ID);
395}
396
397int lttng_msgpack_write_false(struct lttng_msgpack_writer *writer)
398{
399 return lttng_msgpack_append_u8(writer, MSGPACK_FALSE_ID);
400}
401
402int lttng_msgpack_write_unsigned_integer(
403 struct lttng_msgpack_writer *writer, uint64_t value)
404{
405 int ret = 0;
406
407 if (value <= MSGPACK_FIXINT_MAX) {
408 ret = lttng_msgpack_append_u8(writer, (uint8_t) value);
409 if (ret)
410 goto end;
411 } else if (value <= UINT8_MAX) {
412 ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT8_ID);
413 if (ret)
414 goto end;
415
416 ret = lttng_msgpack_append_u8(writer, (uint8_t) value);
417 if (ret)
418 goto end;
419 } else if (value <= UINT16_MAX) {
420 ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT16_ID);
421 if (ret)
422 goto end;
423
424 ret = lttng_msgpack_append_u16(writer, (uint16_t) value);
425 if (ret)
426 goto end;
427 } else if (value <= UINT32_MAX) {
428 ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT32_ID);
429 if (ret)
430 goto end;
431
432 ret = lttng_msgpack_append_u32(writer, (uint32_t) value);
433 if (ret)
434 goto end;
435 } else {
436 ret = lttng_msgpack_append_u8(writer, MSGPACK_UINT64_ID);
437 if (ret)
438 goto end;
439
440 ret = lttng_msgpack_append_u64(writer, value);
441 if (ret)
442 goto end;
443 }
444
445end:
446 return ret;
447}
448
449int lttng_msgpack_write_signed_integer(struct lttng_msgpack_writer *writer, int64_t value)
450{
451 int ret;
452
453 if (value >= MSGPACK_FIXINT_MIN && value <= MSGPACK_FIXINT_MAX){
454 ret = lttng_msgpack_append_i8(writer, (int8_t) value);
455 if (ret)
456 goto end;
457 } else if (value >= INT8_MIN && value <= INT8_MAX) {
458 ret = lttng_msgpack_append_u8(writer, MSGPACK_INT8_ID);
459 if (ret)
460 goto end;
461
462 ret = lttng_msgpack_append_i8(writer, (int8_t) value);
463 if (ret)
464 goto end;
465 } else if (value >= INT16_MIN && value <= INT16_MAX) {
466 ret = lttng_msgpack_append_u8(writer, MSGPACK_INT16_ID);
467 if (ret)
468 goto end;
469
470 ret = lttng_msgpack_append_i16(writer, (int16_t) value);
471 if (ret)
472 goto end;
473 } else if (value >= INT32_MIN && value <= INT32_MAX) {
474 ret = lttng_msgpack_append_u8(writer, MSGPACK_INT32_ID);
475 if (ret)
476 goto end;
477
478 ret = lttng_msgpack_append_i32(writer, (int32_t) value);
479 if (ret)
480 goto end;
481 } else {
482 ret = lttng_msgpack_append_u8(writer, MSGPACK_INT64_ID);
483 if (ret)
484 goto end;
485
486 ret = lttng_msgpack_append_i64(writer, value);
487 if (ret)
488 goto end;
489 }
490
491end:
492 return ret;
493}
494
495int lttng_msgpack_write_double(struct lttng_msgpack_writer *writer, double value)
496{
497 return lttng_msgpack_encode_f64(writer, value);
498}
499
500void lttng_msgpack_writer_init(struct lttng_msgpack_writer *writer,
501 uint8_t *buffer, size_t size)
502{
503 lttng_msgpack_assert(buffer);
504 lttng_msgpack_assert(size >= 0);
505
506 writer->buffer = buffer;
507 writer->write_pos = buffer;
508 writer->end_write_pos = buffer + size;
509
510 writer->array_nesting = 0;
511 writer->map_nesting = 0;
512}
513
514void lttng_msgpack_writer_fini(struct lttng_msgpack_writer *writer)
515{
516 memset(writer, 0, sizeof(*writer));
517}
This page took 0.046023 seconds and 4 git commands to generate.