libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
encoders.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "encoders.h"
9 
10 #if HAVE_ENDIAN_H
11 #include <endian.h>
12 #endif
13 
14 size_t _cbor_encode_uint8(uint8_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
15 {
16  if (value <= 23) {
17  if (buffer_size >= 1) {
18  buffer[0] = value + offset;
19  return 1;
20  }
21  } else {
22  if (buffer_size >= 2) {
23  buffer[0] = 0x18 + offset;
24  buffer[1] = value;
25  return 2;
26  }
27  }
28  return 0;
29 }
30 
31 size_t _cbor_encode_uint16(uint16_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
32 {
33  if (buffer_size >= 3) {
34  buffer[0] = 0x19 + offset;
35 
36 #ifdef HAVE_ENDIAN_H
37  *(uint16_t *) &buffer[1] = htobe16(value);
38 #else
39  #ifdef IS_BIG_ENDIAN
40  *(uint16_t *) &buffer[1] = value;
41  #else
42  buffer[1] = value >> 8;
43  buffer[2] = value;
44  #endif
45 #endif
46 
47  return 3;
48  } else
49  return 0;
50 }
51 
52 size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
53 {
54  if (buffer_size >= 5) {
55  buffer[0] = 0x1A + offset;
56 
57 #ifdef HAVE_ENDIAN_H
58  *(uint32_t *) &buffer[1] = htobe32(value);
59 #else
60  #ifdef IS_BIG_ENDIAN
61  *(uint32_t *) &buffer[1] = value;
62  #else
63  buffer[1] = value >> 24;
64  buffer[2] = value >> 16;
65  buffer[3] = value >> 8;
66  buffer[4] = value;
67  #endif
68 #endif
69 
70  return 5;
71  } else
72  return 0;
73 }
74 
75 size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
76 {
77  if (buffer_size >= 9) {
78  buffer[0] = 0x1B + offset;
79 
80 #ifdef HAVE_ENDIAN_H
81  *(uint64_t *) &buffer[1] = htobe64(value);
82 #else
83  #ifdef IS_BIG_ENDIAN
84  *(uint64_t *) &buffer[1] = value;
85  #else
86  buffer[1] = value >> 56;
87  buffer[2] = value >> 48;
88  buffer[3] = value >> 40;
89  buffer[4] = value >> 32;
90  buffer[5] = value >> 24;
91  buffer[6] = value >> 16;
92  buffer[7] = value >> 8;
93  buffer[8] = value;
94  #endif
95 #endif
96 
97  return 9;
98  } else
99  return 0;
100 }
101 
102 size_t _cbor_encode_uint(uint64_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
103 {
104  if (value <= UINT16_MAX) if (value <= UINT8_MAX)
105  return _cbor_encode_uint8((uint8_t) value, buffer, buffer_size, offset);
106  else
107  return _cbor_encode_uint16((uint16_t) value, buffer, buffer_size, offset);
108  else if (value <= UINT32_MAX)
109  return _cbor_encode_uint32((uint32_t) value, buffer, buffer_size, offset);
110  else
111  return _cbor_encode_uint64((uint64_t) value, buffer, buffer_size, offset);
112 }
size_t _cbor_encode_uint16(uint16_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
Definition: encoders.c:31
size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
Definition: encoders.c:75
size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
Definition: encoders.c:52
size_t _cbor_encode_uint(uint64_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
Definition: encoders.c:102
size_t _cbor_encode_uint8(uint8_t value, unsigned char *buffer, size_t buffer_size, uint8_t offset)
Definition: encoders.c:14