KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
databuffer.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 National Institute for Subatomic Physics Nikhef
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : databuffer.c
11  * Created : 17 apr. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #include "util/databuffer.h"
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 
22 
23 #define CHECK_BUF(SIZE) \
24  if (buf->start == 0 || buf->end == 0 || buf->cur == 0) { \
25  buf->error = DB_ERR_INVALID_BUFFER; \
26  return false; \
27  } \
28  if (buf->start > buf->cur || buf->cur > buf->end ) { \
29  buf->error = DB_ERR_INVALID_BUFFER; \
30  return false; \
31  } \
32  if ((buf->cur + ( SIZE )) > buf->end) { \
33  buf->error = DB_ERR_END_OF_BUFFER; \
34  return false; \
35  }
36 
37 
38 
39 
40 bool dbWrite(DataBuffer * buf, uint8_t * buffer, int len)
41 {
42  CHECK_BUF(len);
43  memcpy(buf->cur, buffer, len);
44  buf->cur += len;
45  return true;
46 }
47 
48 bool dbRead(DataBuffer * buf, uint8_t * buffer, int len)
49 {
50  CHECK_BUF(len);
51  memcpy(buffer, buf->cur, len);
52  buf->cur += len;
53  return true;
54 }
55 /*
56 static int dbCalcJUTFLen(const char * s)
57 {
58  int c, l = 0;
59 
60  for (; *s != '\0'; ++s)
61  {
62  c = *s;
63  if (c > 0 && c <= 0x7f) l++;
64  else if (c == 0 || ( c < 0x07ff)) l += 2;
65  else l += 3;
66  }
67  return l;
68 }
69 */
70 #define W_BYTE_UNSAFE(BUF, BYTE) \
71  *(BUF)->cur++ = ( 0xFF & ( BYTE ) )
72 
73 
74 bool dbWriteString(DataBuffer * buf, const char * s, int max)
75 {
76  int len = strlen(s);
77  int i;
78 
79  if (max > 0 && len > max) len = max;
80 
81  CHECK_BUF(len + 2);
82 
83  int c;
84 
85  dbWriteI16(buf, len);
86 
87 
88  for (i = 0; i < len; ++i, ++s)
89  {
90  c = *s;
91  if (c > 0 && c <= 0x7f)
92  {
93  W_BYTE_UNSAFE(buf, c);
94  }
95  else {
96  W_BYTE_UNSAFE(buf, '@');
97  }
98  }
99 
100  return true;
101 }
102 
103 bool dbReadString(DataBuffer * buf, char * s, int max)
104 {
105  int16_t len;
106 
107  if (!dbReadI16(buf, &len)) return false;
108 
109  if (len > max - 1) len = max - 1;
110 
111  int8_t c;
112 
113  while (len > 0) {
114 
115  dbReadI8(buf, &c);
116  if (c == 0 || c > 0x7f) {
117  // currently only characters >0 and <=0x7f are supported.
118  buf->error |= DB_ERR_NOT_SUPPORTED;
119  return false;
120  }
121  *s = (char)c;
122  s++;
123  len--;
124  }
125  *s = '\0';
126  return true;
127 }
128 
129 bool dbWriteI8(DataBuffer * buf, int8_t b)
130 {
131  CHECK_BUF(1);
132  *(buf->cur++) = b;
133  return true;
134 }
135 
136 bool dbReadI8(DataBuffer * buf, int8_t * b)
137 {
138  CHECK_BUF(1);
139  *b = *(buf->cur++);
140  return true;
141 }
142 
143 
144 
145 
146 bool dbWriteU8(DataBuffer * buf, uint8_t b)
147 {
148  CHECK_BUF(1);
149  *(buf->cur++) = b;
150  return true;
151 }
152 
153 bool dbReadU8(DataBuffer * buf, uint8_t * b)
154 {
155  CHECK_BUF(1);
156  *b = *(buf->cur++);
157  return true;
158 }
159 
160 
161 
162 bool dbWriteU16(DataBuffer * buf, uint16_t u)
163 {
164  CHECK_BUF(2);
165 
166  buf->cur[0] = u >> 8;
167  buf->cur[1] = u;
168  buf->cur += 2;
169 
170  return true;
171 }
172 
173 bool dbWriteI16(DataBuffer * buf, int16_t i)
174 {
175  return dbWriteU16(buf, (uint16_t)i);
176 }
177 
178 
179 bool dbSkip(DataBuffer * buf, size_t skipSize) {
180  CHECK_BUF(skipSize);
181  buf->cur += skipSize;
182  return true;
183 }
184 
185 
186 bool dbReadU16(DataBuffer * buf, uint16_t * u)
187 {
188  CHECK_BUF(2);
189  *u = ( buf->cur[0] << 8 ) | ( buf->cur[1] );
190  buf->cur += 2;
191  return true;
192 }
193 
194 bool dbReadI16(DataBuffer * buf, int16_t * i)
195 {
196  return dbReadU16(buf, (uint16_t *)i);
197 }
198 
199 
200 bool dbWriteU32(DataBuffer * buf, uint32_t u)
201 {
202  CHECK_BUF(4);
203 
204  buf->cur[0] = u >> 24;
205  buf->cur[1] = u >> 16;
206  buf->cur[2] = u >> 8;
207  buf->cur[3] = u;
208  buf->cur += 4;
209  return true;
210 }
211 
212 
213 bool dbWriteI32(DataBuffer * buf, int32_t i)
214 {
215  return dbWriteU32(buf, (uint32_t)i);
216 }
217 
218 
219 bool dbReadU32(DataBuffer * buf, uint32_t * u)
220 {
221  CHECK_BUF(4);
222  *u = ( buf->cur[0] << 24 ) |
223  ( buf->cur[1] << 16 ) |
224  ( buf->cur[2] << 8 ) |
225  ( buf->cur[3]);
226  buf->cur += 4;
227  return true;
228 }
229 
230 bool dbReadU64(DataBuffer * buf, uint64_t * i)
231 {
232  CHECK_BUF(8);
233  *i = ( ((uint64_t)buf->cur[0]) << 56 ) |
234  ( ((uint64_t) buf->cur[1]) << 48 ) |
235  ( ((uint64_t) buf->cur[2]) << 40 ) |
236  ( ((uint64_t) buf->cur[3]) << 32 ) |
237  ( ((uint64_t) buf->cur[4]) << 24 ) |
238  ( ((uint64_t) buf->cur[5]) << 16 ) |
239  ( ((uint64_t) buf->cur[6]) << 8 ) |
240  ( ((uint64_t) buf->cur[7]) );
241  buf->cur += 8;
242  return true;
243 }
244 
245 bool dbWriteU64(DataBuffer * buf, uint64_t i)
246 {
247  CHECK_BUF(8);
248  buf->cur[0] = i >> 56;
249  buf->cur[1] = i >> 48;
250  buf->cur[2] = i >> 40;
251  buf->cur[3] = i >> 32;
252  buf->cur[4] = i >> 24;
253  buf->cur[5] = i >> 16;
254  buf->cur[6] = i >> 8;
255  buf->cur[7] = i;
256  buf->cur += 8;
257  return true;
258 }
259 
260 
261 
262 bool dbReadI32(DataBuffer * buf, int32_t * i)
263 {
264  return dbReadU32(buf, (uint32_t*) i);
265 }
266 
267 
268 
269 bool dbWriteI64(DataBuffer * buf, int64_t i)
270 {
271  CHECK_BUF(8);
272  buf->cur[0] = i >> 56;
273  buf->cur[1] = i >> 48;
274  buf->cur[2] = i >> 40;
275  buf->cur[3] = i >> 32;
276  buf->cur[4] = i >> 24;
277  buf->cur[5] = i >> 16;
278  buf->cur[6] = i >> 8;
279  buf->cur[7] = i;
280  buf->cur += 8;
281  return true;
282 }
283 
284 bool dbReadI64(DataBuffer * buf, int64_t * i)
285 {
286  CHECK_BUF(8);
287  *i = ( ((int64_t)buf->cur[0]) << 56 ) |
288  ( ((int64_t) buf->cur[1]) << 48 ) |
289  ( ((int64_t) buf->cur[2]) << 40 ) |
290  ( ((int64_t) buf->cur[3]) << 32 ) |
291  ( ((int64_t) buf->cur[4]) << 24 ) |
292  ( ((int64_t) buf->cur[5]) << 16 ) |
293  ( ((int64_t) buf->cur[6]) << 8 ) |
294  ( ((int64_t) buf->cur[7]) );
295  buf->cur += 8;
296  return true;
297 }
298 
299 
300 
301 // ---------------------------------------------------------------------------------
302 // floating point support. We simply cast it to an int, and process it from there
303 //
304 // yes, we can do this, check:
305 //
306 // http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#readFloat%28%29
307 // #pragma GCC diagnostic ignored "-Wstrict-aliasing"
308 
309 // the lines below is pure evil, and if we did not disable the warning (in the line above)
310 // GCC will complain about strict anti-aliasing rules.
311 //
312 // We do this do type-cast the type without actual float to int conversion.
313 //
314 // i.e. given uint32_t i = 123;
315 // then float f1 = (float)i;
316 // is not the same as float f2 = *((float *)i);
317 //
318 bool dbWriteF32(DataBuffer * buf, f32_t flt)
319 {
320  CHECK_BUF(4);
321 
322  uint8_t * b = (uint8_t *)&flt;
323 
324  *(buf->cur++) = b[0];
325  *(buf->cur++) = b[1];
326  *(buf->cur++) = b[2];
327  *(buf->cur++) = b[3];
328 
329  return true;
330 }
331 
332 bool dbWriteF64(DataBuffer * buf, f64_t flt)
333 {
334  return dbWriteI64(buf, (int64_t)flt);
335 }
336 
337 #pragma GCC diagnostic warning "-Wstrict-aliasing"
338 
339 
340 bool dbReadF32(DataBuffer * buf, f32_t * flt)
341 {
342  return dbReadI32(buf, (int32_t *)flt);
343 }
344 
345 bool dbReadF64(DataBuffer * buf, f64_t * flt)
346 {
347  return dbReadI64(buf, (int64_t *)flt);
348 }
bool dbReadF64(DataBuffer *buf, f64_t *flt)
Reads an 64-bit floating point.
Definition: databuffer.c:345
bool dbReadU8(DataBuffer *buf, uint8_t *byte)
Reads an unsigned byte.
Definition: databuffer.c:153
bool dbReadString(DataBuffer *buf, char *s, int size)
Reads a string from the stream.
Definition: databuffer.c:103
bool dbSkip(DataBuffer *buf, size_t skipSize)
Skips a number of bytes in the buffer.
Definition: databuffer.c:179
#define DB_ERR_NOT_SUPPORTED
De type is not supported.
Definition: databuffer.h:37
bool dbWriteI16(DataBuffer *buf, int16_t i)
Writes a short (16 bits signed) integer.
Definition: databuffer.c:173
bool dbReadU16(DataBuffer *buf, uint16_t *u)
Reads a unsigned short (16 bits unsigned) integer.
Definition: databuffer.c:186
bool dbWriteI32(DataBuffer *buf, int32_t i)
Writes an 32 bits signed integer.
Definition: databuffer.c:213
bool dbWriteI8(DataBuffer *buf, int8_t byte)
Writes a byte.
Definition: databuffer.c:129
bool dbReadF32(DataBuffer *buf, f32_t *flt)
Reads an 32-bit floating point.
Definition: databuffer.c:340
Defines a DataBuffer structure.
Definition: databuffer.h:45
bool dbReadI64(DataBuffer *buf, int64_t *i)
Reads an 64 bits signed integer.
Definition: databuffer.c:284
int error
Last error.
Definition: databuffer.h:50
bool dbWriteF64(DataBuffer *buf, f64_t flt)
Writes a 64-bit floating point.
Definition: databuffer.c:332
DataBuffer reads and writes data into a buffer in the same format as defined in the data Java DataOut...
uint32_t f32_t
32 bit representation for float.
Definition: float.h:30
bool dbWriteF32(DataBuffer *buf, f32_t flt)
Writes a 32-bit floating point.
Definition: databuffer.c:318
uint64_t f64_t
642 bit representation for float
Definition: float.h:33
bool dbWriteU8(DataBuffer *buf, uint8_t byte)
Writes a unsigned byte.
Definition: databuffer.c:146
bool dbReadI8(DataBuffer *buf, int8_t *byte)
Reads an byte.
Definition: databuffer.c:136
bool dbWriteU64(DataBuffer *buf, uint64_t i)
Writes an 64 bits unsigned integer.
Definition: databuffer.c:245
bool dbReadU32(DataBuffer *buf, uint32_t *u)
Reads an 32 bits unsigned integer.
Definition: databuffer.c:219
bool dbWrite(DataBuffer *buf, uint8_t *buffer, int len)
Writes the given buffer with the specified length.
Definition: databuffer.c:40
bool dbWriteI64(DataBuffer *buf, int64_t i)
Writes an 64 bits signed integer.
Definition: databuffer.c:269
bool dbWriteString(DataBuffer *buf, const char *s, int max)
Writes a String as &#39;sort of&#39; UTF-8 encoding, as defined in the Java DataOuput and DataInput writeUTF ...
Definition: databuffer.c:74
bool dbReadI32(DataBuffer *buf, int32_t *i)
Reads an 32 bits signed integer.
Definition: databuffer.c:262
bool dbRead(DataBuffer *buf, uint8_t *buffer, int len)
Reads into given buffer with the specified length.
Definition: databuffer.c:48
bool dbReadI16(DataBuffer *buf, int16_t *i)
Reads a short (16 bits signed) integer.
Definition: databuffer.c:194
uint8_t * cur
Current Pointer.
Definition: databuffer.h:49
bool dbWriteU32(DataBuffer *buf, uint32_t u)
Writes an 32 bits unsigned integer.
Definition: databuffer.c:200
bool dbWriteU16(DataBuffer *buf, uint16_t u)
Writes a unsigned short (16 bits unsigned) integer.
Definition: databuffer.c:162
bool dbReadU64(DataBuffer *buf, uint64_t *i)
Reads an 64 bits unsigned integer.
Definition: databuffer.c:230