Blame view

RIOT/sys/ubjson/ubjson-write.c 6.11 KB
fb11e647   vrobic   reseau statique a...
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * @ingroup     sys
   * @{
   * @file
   * @brief       Universal Binary JSON deserializer
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   * @}
   */
  
  #include "ubjson-internal.h"
  #include "ubjson.h"
  #include "byteorder.h"
  
  #include <limits.h>
  
  #define WRITE_CALL(FUN, ...)                                                  \
      do {                                                                      \
          ssize_t wrote = (FUN)(__VA_ARGS__);                                   \
          if (wrote < 0) {                                                      \
              return wrote;                                                     \
          }                                                                     \
          result += wrote;                                                      \
      } while (0)
  
  #define WRITE_BUF(BUF, COUNT) \
      WRITE_CALL(cookie->rw.write, cookie, (BUF), (COUNT))
  
  #define WRITE_MARKER(MARKER)                                                  \
      do {                                                                      \
          char marker_buf[] = { (char) (MARKER) };                              \
          WRITE_BUF(marker_buf, 1);                                             \
      } while (0)
  
  #define MARKER_FUN(NAME, MARKER)                                              \
      ssize_t NAME(ubjson_cookie_t *restrict cookie)                            \
      {                                                                         \
          ssize_t result = 0;                                                   \
          WRITE_MARKER((MARKER));                                               \
          return result;                                                        \
      }
  
  MARKER_FUN(ubjson_write_null, UBJSON_MARKER_NULL)
  MARKER_FUN(ubjson_write_noop, UBJSON_MARKER_NOOP)
  
  MARKER_FUN(ubjson_open_array, UBJSON_MARKER_ARRAY_START)
  MARKER_FUN(ubjson_close_array, UBJSON_MARKER_ARRAY_END)
  
  MARKER_FUN(ubjson_open_object, UBJSON_MARKER_OBJECT_START)
  MARKER_FUN(ubjson_close_object, UBJSON_MARKER_OBJECT_END)
  
  ssize_t ubjson_write_bool(ubjson_cookie_t *restrict cookie, bool value)
  {
      static const char marker_false[] = { UBJSON_MARKER_FALSE };
      static const char marker_true[] = { UBJSON_MARKER_TRUE };
      return cookie->rw.write(cookie, value ? &marker_false : &marker_true, 1);
  }
  
  ssize_t ubjson_write_i32(ubjson_cookie_t *restrict cookie, int32_t value)
  {
      ssize_t result = 0;
      if ((INT8_MIN <= value) && (value <= INT8_MAX)) {
          WRITE_MARKER(UBJSON_MARKER_INT8);
          WRITE_MARKER((uint8_t) value);
      }
      else if ((0 <= value) && (value <= UINT8_MAX)) {
          WRITE_MARKER(UBJSON_MARKER_UINT8);
          WRITE_MARKER((uint8_t) value);
      }
      else if ((INT16_MIN <= value) && (value <= UINT16_MAX)) {
          WRITE_MARKER(UBJSON_MARKER_INT16);
          network_uint16_t buf = byteorder_htons((uint16_t) value);
          WRITE_BUF(&buf, sizeof(buf));
      }
      else {
          WRITE_MARKER(UBJSON_MARKER_INT32);
          network_uint32_t buf = byteorder_htonl((uint32_t) value);
          WRITE_BUF(&buf, sizeof(buf));
      }
      return result;
  }
  
  ssize_t ubjson_write_i64(ubjson_cookie_t *restrict cookie, int64_t value)
  {
      if ((INT32_MIN <= value) && (value <= INT32_MAX)) {
          return ubjson_write_i32(cookie, (int32_t) value);
      }
  
      ssize_t result = 0;
      network_uint64_t buf = byteorder_htonll((uint64_t) value);
      WRITE_BUF(&buf, sizeof(buf));
      return result;
  }
  
  ssize_t ubjson_write_float(ubjson_cookie_t *restrict cookie, float value)
  {
      union {
          float f;
          uint32_t i;
      } v = { .f = value };
  
      ssize_t result = 0;
      WRITE_MARKER(UBJSON_MARKER_FLOAT32);
      network_uint32_t buf = byteorder_htonl(v.i);
      WRITE_BUF(&buf, sizeof(buf));
      return result;
  }
  
  ssize_t ubjson_write_double(ubjson_cookie_t *restrict cookie, double value)
  {
      union {
          double f;
          uint64_t i;
      } v = { .f = value };
  
      ssize_t result = 0;
      WRITE_MARKER(UBJSON_MARKER_FLOAT64);
      network_uint64_t buf = byteorder_htonll(v.i);
      WRITE_BUF(&buf, sizeof(buf));
      return result;
  }
  
  ssize_t ubjson_write_string(ubjson_cookie_t *restrict cookie, const void *value, size_t len)
  {
      ssize_t result = 0;
      WRITE_MARKER(UBJSON_MARKER_STRING);
      WRITE_CALL(ubjson_write_key, cookie, value, len);
      return result;
  }
  
  static ssize_t _ubjson_write_length(ubjson_cookie_t *restrict cookie, size_t len)
  {
      ssize_t result = 0;
      WRITE_MARKER(UBJSON_MARKER_COUNT);
      WRITE_CALL(ubjson_write_i64, cookie, (int64_t) len);
      return result;
  }
  
  ssize_t ubjson_open_array_len(ubjson_cookie_t *restrict cookie, size_t len)
  {
      ssize_t result = 0;
      WRITE_CALL(ubjson_open_array, cookie);
      if (len > 0) {
          WRITE_CALL(_ubjson_write_length, cookie, len);
      }
      else {
          WRITE_CALL(ubjson_close_array, cookie);
      }
      return result;
  }
  
  ssize_t ubjson_open_object_len(ubjson_cookie_t *restrict cookie, size_t len)
  {
      ssize_t result = 0;
      WRITE_CALL(ubjson_open_object, cookie);
      if (len > 0) {
          WRITE_CALL(_ubjson_write_length, cookie, len);
      }
      else {
          WRITE_CALL(ubjson_close_object, cookie);
      }
      return result;
  }
  
  ssize_t ubjson_write_key(ubjson_cookie_t *restrict cookie, const void *value, size_t len)
  {
      ssize_t result = 0;
      WRITE_CALL(ubjson_write_i64, cookie, (int64_t) len);
      WRITE_BUF(value, len);
      return result;
  }