Newer
Older
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*********************************************************************
######################################################################
##
## Created by: Denis Filatov
##
## Copyleft (c) 2003 - 2015
## This code is provided under the CeCill-C license agreement.
######################################################################
*********************************************************************/
#include "cserialize.h"
#include "cstr.h"
#include "e4c_lite.h"
#include <errno.h>
#include <string.h>
#ifdef __GNUC__
#define cint_cpy(D,N,S) __builtin_memcpy((char*)(D),N,S)
#else
#define cint_cpy(D,N,S) memcpy((char*)(D),N,S)
static const uint64_t __one64 = 1;
#if !defined (_MSC_VER)
uint64_t cint64_swap(uint64_t);
uint32_t cint32_swap(uint32_t);
uint16_t cint16_swap(uint16_t);
#endif
#endif
#define C_ERROR(E) \
if (perror) *perror = E; \
throw(RuntimeException, E, NULL)
int _cint64_write(const uint64_t value, char** const ptr, const char* const end, int * const perror)
{
register unsigned char* p = (unsigned char*)*ptr;
if (p + 8 > (unsigned char*)end){
C_ERROR(ENOSPC);
return -1;
}
if (0 == (((intptr_t)p) & 0x7)){
*((uint64_t*)p) = cint64_hton(value);
p+=8;
}else{
int i;
for(i=7; i>=0; i--){
*p++ = (value>>(8*i))&0xFF;
}
}
*ptr = (char*)p;
if (perror) *perror = 0;
return 0;
}
int _cint32_write(const uint32_t value, char** const ptr, const char* const end, int * const perror)
{
register unsigned char* p = (unsigned char*)*ptr;
if(p + 4 > (unsigned char*)end){
C_ERROR(ENOSPC);
return -1;
}
if (0 == (((intptr_t)p) & 0x3)){
*((uint32_t*)p) = cint32_hton(value);
p+=4;
}else{
int i;
for(i=3; i>=0; i--){
*p++ = (value>>(8*i))&0xFF;
}
}
*ptr = (char*)p;
if (perror) *perror = 0;
return 0;
}
int _cint16_write(const uint16_t value, char** const ptr, const char* const end, int * const perror)
{
register unsigned char* p = (unsigned char*)*ptr;
if (p + 2 > (unsigned char*)end){
C_ERROR(ENOSPC);
return -1;
}
*p++ = (value >> 8) & 0xFF;
*p++ = value&0xFF;
*ptr = (char*)p;
if (perror) *perror = 0;
return 0;
}
int _cint8_write(const uint8_t value, char** const ptr, const char* const end, int * const perror)
{
if (*ptr >= end) {
C_ERROR(ENOSPC);
return -1;
}
if (perror) *perror = 0;
*((uint8_t*)*ptr) = value;
(*ptr) ++;
return 0;
}
uint64_t cint64_read(const char** const ptr, const char* const end, int * const perror)
{
uint64_t value;
register const uint8_t * p = (const uint8_t *)*ptr;
if (p + 8 > (const uint8_t *)end) {
C_ERROR(EFAULT);
return (unsigned)-1;
}
if (0 == (((intptr_t)p) & 0x7)){
value = *(uint64_t*)p;
value = cint64_hton(value);
*ptr = (char*)(p+8);
}else{
int i;
value=0;
for(i=0; i<8; i++){
value = (value<<8) | *(p++);
}
*ptr = (char*)p;
}
if (perror) *perror = 0;
return value;
}
uint32_t cint32_read(const char** const ptr, const char* const end, int * const perror)
{
uint32_t value;
register const uint8_t * p = (const uint8_t*)*ptr;
if(p + 4 > (const uint8_t *)end) {
C_ERROR(EFAULT);
return (unsigned)-1;
}
if (perror) *perror = 0;
value = ((uint32_t)p[0]) << 24 | ((uint32_t)p[1]) << 16 | ((uint32_t)p[2]) << 8 | p[3];
*ptr = (char*)(p+4);
return value;
}
uint16_t cint16_read(const char** const ptr, const char* const end, int * const perror)
{
uint32_t value;
register const uint8_t * p = (const uint8_t*)*ptr;
if (p + 2 > (const uint8_t *)end) {
C_ERROR(EFAULT);
return (uint16_t)-1;
}
if (perror) *perror = 0;
value = ((uint16_t)p[0]) << 8 | p[1];
*ptr = (const char*)(p+2);
return value;
}
uint8_t cint8_read(const char** const ptr, const char* const end, int * const perror)
{
if (*ptr >= end) {
C_ERROR(EFAULT);
return (uint8_t)-1;
}
if (perror) *perror = 0;
return *(const uint8_t*)((*ptr)++);
}
int cintx_bytecount(uint64_t value)
{
int num_bytes = 0;
#ifdef __GNUC__
if(value){
num_bytes = (64 + 6 - __builtin_clzll(value))/7;
}else{
num_bytes = 1;
}
#else
uint64_t overflow = 0;
while(value >= overflow){
num_bytes++;
overflow = __one64 << (7*num_bytes);
}
#endif
return num_bytes;
}
int _cintx_write (const uint64_t value, char ** const ptr, const char * const end, int * const perror)
{
int num_bytes = 0;
uint8_t c;
uint8_t *out = (uint8_t*)(*ptr);
num_bytes = cintx_bytecount(value);
if(num_bytes > 8 || out+num_bytes > ((const uint8_t*)end)){
C_ERROR(ENOSPC);
return (unsigned)-1;
}
num_bytes--;
c = ~((1<<(8-num_bytes))-1);
c |= (value >> (num_bytes*8)) & 0xFF;
*out++ = c;
while(num_bytes){
num_bytes--;
c = (value >> (num_bytes*8)) & 0xFF;
*out++ = c;
}
*ptr = (char*)out;
if (perror) *perror = 0;
return 0;
}
static int countof1(int c)
{
int r = 0;
while(c & 0x80){
#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 407) && !defined(__INTEL_COMPILER)
return 1 + __builtin_clrsb(c<<24);
#else
r++;
c<<=1;
#endif
}
return r;
}
uint64_t cintx_read(const char** const ptr, const char* const end, int * const perror)
{
uint8_t c;
const uint8_t* in;
int i, lead_ones;
in = (const uint8_t*)*ptr;
if(in <= (const uint8_t*)end){
c = *in;
lead_ones = countof1(c);
if(in + 1 + lead_ones <= (const uint8_t*)end) {
uint64_t value;
value = c & ((1<<(7-lead_ones))-1);
for(i=1; i<=lead_ones; i++){
value = (value<<8) | in[i];
}
*ptr = (const char*)(in + 1 + lead_ones);
if (perror) *perror = 0;
return value;
}
}
C_ERROR(EFAULT);
return (unsigned)-1;
}
uint32_t cxsize_read(const char ** const ptr, const char * const end, int * const perror)
{
uint32_t len = (uint32_t)cintx_read(ptr, end, perror);
if (perror == 0){
if (*ptr + len > end){
C_ERROR(EFAULT);
return (unsigned)-1;
}
}
if (perror) *perror = 0;
return len;
}
int cbuf_write(const void * const p, int length, char ** const ptr, const char * const end, int * const perror)
{
if((*ptr) + length > end) {
C_ERROR(ENOSPC);
return (unsigned)-1;
}
cint_cpy(*ptr, p, length);
*ptr = (*ptr) + length;
if (perror) *perror = 0;
return 0;
}
int cbuf_read (void * const p, int length, const char ** const ptr, const char * const end, int * const perror)
{
if((*ptr) + length > end) {
C_ERROR(EFAULT);
return -1;
}
if (p){
cint_cpy(p, *ptr, length);
}
*ptr = (*ptr) + length;
if (perror) *perror = 0;
return 0;
}
int cstr_write(const char * const p, char ** const ptr, const char * const end, int * const perror)
{
int ret;
int len = cstrlen(p);
// write size
ret = cintx_write(len, ptr, end, perror);
if (ret == 0 && len > 0 ) {
ret = cbuf_write(p, len, ptr, end, perror);
}
return ret;
}
int cstrn_write(const char * const p, int length, char ** const ptr, const char * const end, int * const perror)
{
int ret;
int len = cstrnlen(p, length);
// write size
ret = cintx_write(len, ptr, end, perror);
if (ret == 0 && len > 0) {
ret = cbuf_write(p, len, ptr, end, perror);
}
return ret;
}
int cstr_read(char * const p, const char ** const ptr, const char * const end, int * const perror)
{
// read size
int len = (int)cintx_read(ptr, end, perror);
if (*perror == 0){
return cbuf_read(p, len, ptr, end, perror);
}
return -1;
}
int cstrn_read(char * const p, int length, const char ** const ptr, const char * const end, int * const perror)
{
// read size
int len = (int)cintx_read(ptr, end, perror);
if (len <= length){
// read buf
return cbuf_read(p, len, ptr, end, perror);
}
C_ERROR(EFAULT);
return -1;
}
int cbookmark_store(cbookmark * bm, char ** const ptr, const char * const end, int * const perror)
{
char * p = *ptr;
if (bm->idx >= sizeof(bm->ptrs) / sizeof(bm->ptrs[0])){
C_ERROR(E2BIG);
return -1;
}
if (p >= end){
C_ERROR(ENOSPC);
return -1;
}
bm->ptrs[bm->idx] = p;
bm->idx++;
*ptr = p + 1;
if (perror) *perror = 0;
return 0;
}
int cbookmark_apply(cbookmark * bm, char ** const ptr, const char * const end, int * const perror)
{
int size, bcount;
char *p, * psize;
p = *ptr;
if (bm->idx == 0){
C_ERROR(E2BIG);
return -1;
}
psize = bm->ptrs[--bm->idx];
size = p - psize - 1;
bcount = cintx_bytecount(size);
if (bcount == 1){
*(unsigned char*)psize = size;
size = 0; // return value;
}
else{
if (p + bcount - 1 > end){
C_ERROR(ENOSPC);
return (unsigned)-1;
}
memmove(psize + bcount, psize + 1, p - psize - 1);
*ptr = p + bcount - 1;
size = cintx_write(size, &psize, psize + bcount, perror);
}
if (perror) *perror = 0;
return size; // size is overridden to be 0 or -1
}