summaryrefslogtreecommitdiffstats
path: root/src/ccapi/common/cci_stream.c
blob: 1532f1ed8e30fcd4bf86ab38ff99f020ddb1a433 (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * $Header$
 *
 * Copyright 2006 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "cci_common.h"

#if TARGET_OS_MAC 
#include <architecture/byte_order.h>

#if !defined(htonll)
#define htonll(x) OSSwapHostToBigInt64(x)
#endif

#if !defined(ntohll)
#define ntohll(x) OSSwapBigToHostInt64(x)
#endif

#endif /* TARGET_OS_MAC */

struct cci_stream_d {
    char *data;
    cc_uint64 size;
    cc_uint64 max_size;
};

const struct cci_stream_d cci_stream_initializer = { NULL, 0, 0 };

#define CC_STREAM_SIZE_INCREMENT 128

/* ------------------------------------------------------------------------ */

static cc_uint32 cci_stream_reallocate (cci_stream_t io_stream,
                                        cc_uint64    in_new_size)
{
    cc_int32 err = ccNoError;
    cc_uint64 new_max_size = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        cc_uint64 old_max_size = io_stream->max_size;
        new_max_size = io_stream->max_size;
        
        if (in_new_size > old_max_size) {
            /* Expand the stream */
            while (in_new_size > new_max_size) {
                new_max_size += CC_STREAM_SIZE_INCREMENT;
            }
            
        
        } else if ((in_new_size + CC_STREAM_SIZE_INCREMENT) < old_max_size) {
            /* Shrink the array, but never drop below CC_LIST_COUNT_INCREMENT */
            while ((in_new_size + CC_STREAM_SIZE_INCREMENT) < new_max_size &&
                   (new_max_size > CC_STREAM_SIZE_INCREMENT)) {
                new_max_size -= CC_STREAM_SIZE_INCREMENT;
            }
        }
    }
    
    if (!err && new_max_size != io_stream->max_size) {
        char *data = io_stream->data;
 
        if (!data) {
            data = malloc (new_max_size * sizeof (*data));
        } else {
            data = realloc (data, new_max_size * sizeof (*data));
        }
        
        if (data) { 
            io_stream->data = data;
            io_stream->max_size = new_max_size;
        } else {
            err = cci_check_error (ccErrNoMem); 
        }
    }
    
    return cci_check_error (err);    
}

/* ------------------------------------------------------------------------ */

cc_int32 cci_stream_new (cci_stream_t *out_stream)
{
    cc_int32 err = ccNoError;
    cci_stream_t stream = NULL;
    
    if (!out_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        stream = malloc (sizeof (*stream));
        if (stream) { 
            *stream = cci_stream_initializer;
        } else {
            err = cci_check_error (ccErrNoMem); 
        }
    }
    
    if (!err) {
        *out_stream = stream;
        stream = NULL;
    }
    
    cci_stream_release (stream);
    
    return cci_check_error (err);    
}


/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_release (cci_stream_t io_stream)
{	
    cc_int32 err = ccNoError;
    
    if (!io_stream) { err = ccErrBadParam; }
    
    if (!err) {
        free (io_stream->data);
        free (io_stream);
    }
    
    return err;    
}

/* ------------------------------------------------------------------------ */

cc_uint64 cci_stream_size (cci_stream_t in_stream)
{
    return in_stream ? in_stream->size : 0;
}


/* ------------------------------------------------------------------------ */

const char *cci_stream_data (cci_stream_t in_stream)
{
    return in_stream ? in_stream->data : NULL;
}

#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read (cci_stream_t  io_stream, 
                           void         *io_data, 
                           cc_uint64     in_size)
{
    cc_int32 err = ccNoError;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!io_data  ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        if (in_size > io_stream->size) { 
            err = cci_check_error (ccErrBadInternalMessage); 
        }
    }
    
    if (!err) {
        memcpy (io_data, io_stream->data, in_size);
        memmove (io_stream->data, &io_stream->data[in_size], 
                 io_stream->size - in_size);
        
        err = cci_stream_reallocate (io_stream, io_stream->size - in_size);
        
        if (!err) {
            io_stream->size -= in_size;
        }
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write (cci_stream_t  io_stream,
                            const void   *in_data, 
                            cc_uint64     in_size)
{
    cc_int32 err = ccNoError;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!in_data  ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        /* Security check: Do not let the caller overflow the length */
        if (in_size > (UINT64_MAX - io_stream->size)) {
            err = cci_check_error (ccErrBadParam);
        }
    }
    
    if (!err) {
        err = cci_stream_reallocate (io_stream, io_stream->size + in_size);
    }
    
    if (!err) {
        memcpy (&io_stream->data[io_stream->size], in_data, in_size);
        io_stream->size += in_size;
    }
    
    return cci_check_error (err);
}

#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read_string (cci_stream_t   io_stream, 
                                  char         **out_string)
{
    cc_int32 err = ccNoError;
    cc_uint32 length = 0;
    char *string = NULL;
    
    if (!io_stream ) { err = cci_check_error (ccErrBadParam); }
    if (!out_string) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_read_uint32 (io_stream, &length);
    }
    
    if (!err) {
        string = malloc (length);
        if (!string) { err = cci_check_error (ccErrNoMem); }
    }
    
    if (!err) {
        err = cci_stream_read (io_stream, string, length);
    }
    
    if (!err) {
        *out_string = string;
        string = NULL;
    }
    
    free (string);

    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write_string (cci_stream_t  io_stream, 
                                   const char   *in_string)
{
    cc_int32 err = ccNoError;
    cc_uint32 length = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!in_string) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        length = strlen (in_string) + 1;
        
        err = cci_stream_write_uint32 (io_stream, length);
    }
    
    if (!err) {
        err = cci_stream_write (io_stream, in_string, length);
    }
    
    return cci_check_error (err);
}

#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read_int32 (cci_stream_t  io_stream, 
                                 cc_int32     *out_int32)
{
    cc_int32 err = ccNoError;
    cc_int32 int32 = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!out_int32) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_read (io_stream, &int32, sizeof (int32));
    }
    
    if (!err) {
        *out_int32 = ntohl (int32);
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write_int32 (cci_stream_t io_stream, 
                                  cc_int32     in_int32)
{
    cc_int32 err = ccNoError;
    cc_int32 int32 = htonl (in_int32);
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_write (io_stream, &int32, sizeof (int32));
    }
    
    return cci_check_error (err);
}

#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read_uint32 (cci_stream_t  io_stream, 
                                  cc_uint32    *out_uint32)
{
    cc_int32 err = ccNoError;
    cc_uint32 uint32 = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!out_uint32) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_read (io_stream, &uint32, sizeof (uint32));
    }
    
    if (!err) {
        *out_uint32 = ntohl (uint32);
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write_uint32 (cci_stream_t io_stream, 
                                   cc_uint32    in_uint32)
{
    cc_int32 err = ccNoError;
    cc_int32 uint32 = htonl (in_uint32);
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_write (io_stream, &uint32, sizeof (uint32));
    }
    
    return cci_check_error (err);
}

#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read_int64 (cci_stream_t  io_stream, 
                                 cc_int64     *out_int64)
{
    cc_int32 err = ccNoError;
    cc_uint64 int64 = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!out_int64) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_read (io_stream, &int64, sizeof (int64));
    }
    
    if (!err) {
        *out_int64 = ntohll (int64);
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write_int64 (cci_stream_t io_stream, 
                                  cc_int64     in_int64)
{
    cc_int32 err = ccNoError;
    cc_int64 int64 = htonll (in_int64);
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_write (io_stream, &int64, sizeof (int64));
    }
    
    return cci_check_error (err);
}


#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read_uint64 (cci_stream_t  io_stream, 
                                  cc_uint64     *out_uint64)
{
    cc_int32 err = ccNoError;
    cc_uint64 uint64 = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!out_uint64) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_read (io_stream, &uint64, sizeof (uint64));
    }
    
    if (!err) {
        *out_uint64 = ntohll (uint64);
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write_uint64 (cci_stream_t io_stream, 
                                   cc_uint64     in_uint64)
{
    cc_int32 err = ccNoError;
    cc_int64 uint64 = htonll (in_uint64);
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_write (io_stream, &uint64, sizeof (uint64));
    }
    
    return cci_check_error (err);
}

#pragma mark - 

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_read_time (cci_stream_t  io_stream, 
                                cc_time_t    *out_time)
{
    cc_int32 err = ccNoError;
    cc_int64 t = 0;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    if (!out_time ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_read_int64 (io_stream, &t);
    }
    
    if (!err) {
        *out_time = t;
    }
    
    return cci_check_error (err);
}

/* ------------------------------------------------------------------------ */

cc_uint32 cci_stream_write_time (cci_stream_t io_stream, 
                                 cc_time_t    in_time)
{
    cc_int32 err = ccNoError;
    
    if (!io_stream) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        err = cci_stream_write_int64 (io_stream, in_time);
    }
    
    return cci_check_error (err);
}