1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include <string.h>
|
---|
6 | #include <stdbool.h>
|
---|
7 | #include "azure_c_shared_utility/gballoc.h"
|
---|
8 | #include "azure_c_shared_utility/buffer_.h"
|
---|
9 | #include "azure_c_shared_utility/optimize_size.h"
|
---|
10 | #include "azure_c_shared_utility/xlogging.h"
|
---|
11 |
|
---|
12 | typedef struct BUFFER_TAG
|
---|
13 | {
|
---|
14 | unsigned char* buffer;
|
---|
15 | size_t size;
|
---|
16 | } BUFFER;
|
---|
17 |
|
---|
18 | /* Codes_SRS_BUFFER_07_001: [BUFFER_new shall allocate a BUFFER_HANDLE that will contain a NULL unsigned char*.] */
|
---|
19 | BUFFER_HANDLE BUFFER_new(void)
|
---|
20 | {
|
---|
21 | BUFFER* temp = (BUFFER*)calloc(1, sizeof(BUFFER));
|
---|
22 | /* Codes_SRS_BUFFER_07_002: [BUFFER_new shall return NULL on any error that occurs.] */
|
---|
23 | if (temp != NULL)
|
---|
24 | {
|
---|
25 | temp->buffer = NULL;
|
---|
26 | temp->size = 0;
|
---|
27 | }
|
---|
28 | return (BUFFER_HANDLE)temp;
|
---|
29 | }
|
---|
30 |
|
---|
31 | static int BUFFER_safemalloc(BUFFER* handleptr, size_t size)
|
---|
32 | {
|
---|
33 | int result;
|
---|
34 | size_t sizetomalloc = size;
|
---|
35 | if (size == 0)
|
---|
36 | {
|
---|
37 | sizetomalloc = 1;
|
---|
38 | }
|
---|
39 | handleptr->buffer = (unsigned char*)malloc(sizetomalloc);
|
---|
40 | if (handleptr->buffer == NULL)
|
---|
41 | {
|
---|
42 | /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.]*/
|
---|
43 | LogError("Failure allocating data");
|
---|
44 | result = MU_FAILURE;
|
---|
45 | }
|
---|
46 | else
|
---|
47 | {
|
---|
48 | // we still consider the real buffer size is 0
|
---|
49 | handleptr->size = size;
|
---|
50 | result = 0;
|
---|
51 | }
|
---|
52 | return result;
|
---|
53 | }
|
---|
54 |
|
---|
55 | BUFFER_HANDLE BUFFER_create(const unsigned char* source, size_t size)
|
---|
56 | {
|
---|
57 | BUFFER* result;
|
---|
58 | /*Codes_SRS_BUFFER_02_001: [If source is NULL then BUFFER_create shall return NULL.]*/
|
---|
59 | if (source == NULL)
|
---|
60 | {
|
---|
61 | LogError("invalid parameter source: %p", source);
|
---|
62 | result = NULL;
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | /*Codes_SRS_BUFFER_02_002: [Otherwise, BUFFER_create shall allocate memory to hold size bytes and shall copy from source size bytes into the newly allocated memory.] */
|
---|
67 | result = (BUFFER*)calloc(1, sizeof(BUFFER));
|
---|
68 | if (result == NULL)
|
---|
69 | {
|
---|
70 | /*Codes_SRS_BUFFER_02_003: [If allocating memory fails, then BUFFER_create shall return NULL.] */
|
---|
71 | /*fallthrough*/
|
---|
72 | LogError("Failure allocating BUFFER structure");
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | /* Codes_SRS_BUFFER_02_005: [If size parameter is 0 then 1 byte of memory shall be allocated yet size of the buffer shall be set to 0.]*/
|
---|
77 | if (BUFFER_safemalloc(result, size) != 0)
|
---|
78 | {
|
---|
79 | LogError("unable to BUFFER_safemalloc ");
|
---|
80 | free(result);
|
---|
81 | result = NULL;
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | /*Codes_SRS_BUFFER_02_004: [Otherwise, BUFFER_create shall return a non-NULL handle.] */
|
---|
86 | (void)memcpy(result->buffer, source, size);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return (BUFFER_HANDLE)result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // Codes_SRS_BUFFER_07_029: [ BUFFER_create_with_size shall create a BUFFER_HANDLE with a pre allocated underlying buffer size.]
|
---|
94 | BUFFER_HANDLE BUFFER_create_with_size(size_t buff_size)
|
---|
95 | {
|
---|
96 | BUFFER* result;
|
---|
97 | result = (BUFFER*)calloc(1, sizeof(BUFFER));
|
---|
98 | if (result != NULL)
|
---|
99 | {
|
---|
100 | if (buff_size == 0)
|
---|
101 | {
|
---|
102 | // Codes_SRS_BUFFER_07_030: [ If buff_size is 0 BUFFER_create_with_size shall create a valid non-NULL handle of zero size. ]
|
---|
103 | result->size = 0;
|
---|
104 | result->buffer = NULL;
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | // Codes_SRS_BUFFER_07_031: [ BUFFER_create_with_size shall allocate a buffer of buff_size. ]
|
---|
109 | result->size = buff_size;
|
---|
110 | if ((result->buffer = (unsigned char*)malloc(result->size)) == NULL)
|
---|
111 | {
|
---|
112 | // Codes_SRS_BUFFER_07_032: [ If allocating memory fails, then BUFFER_create_with_size shall return NULL. ]
|
---|
113 | LogError("unable to allocate buffer");
|
---|
114 | free(result);
|
---|
115 | result = NULL;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | // Codes_SRS_BUFFER_07_032: [ If allocating memory fails, then BUFFER_create_with_size shall return NULL. ]
|
---|
122 | LogError("unable to allocate BUFFER");
|
---|
123 | }
|
---|
124 | // Codes_SRS_BUFFER_07_033: [ Otherwise, BUFFER_create_with_size shall return a non-NULL handle. ]
|
---|
125 | return (BUFFER_HANDLE)result;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* Codes_SRS_BUFFER_07_003: [BUFFER_delete shall delete the data associated with the BUFFER_HANDLE along with the Buffer.] */
|
---|
129 | void BUFFER_delete(BUFFER_HANDLE handle)
|
---|
130 | {
|
---|
131 | /* Codes_SRS_BUFFER_07_004: [BUFFER_delete shall not delete any BUFFER_HANDLE that is NULL.] */
|
---|
132 | if (handle != NULL)
|
---|
133 | {
|
---|
134 | BUFFER* b = (BUFFER*)handle;
|
---|
135 | if (b->buffer != NULL)
|
---|
136 | {
|
---|
137 | /* Codes_SRS_BUFFER_07_003: [BUFFER_delete shall delete the data associated with the BUFFER_HANDLE along with the Buffer.] */
|
---|
138 | free(b->buffer);
|
---|
139 | }
|
---|
140 | free(b);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*return 0 if the buffer was copied*/
|
---|
145 | /*else return different than zero*/
|
---|
146 | /* Codes_SRS_BUFFER_07_008: [BUFFER_build allocates size_t bytes, copies the unsigned char* into the buffer and returns zero on success.] */
|
---|
147 | int BUFFER_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size)
|
---|
148 | {
|
---|
149 | int result;
|
---|
150 | if (handle == NULL)
|
---|
151 | {
|
---|
152 | /* Codes_SRS_BUFFER_07_009: [BUFFER_build shall return nonzero if handle is NULL ] */
|
---|
153 | result = MU_FAILURE;
|
---|
154 | }
|
---|
155 | /* Codes_SRS_BUFFER_01_002: [The size argument can be zero, in which case the underlying buffer held by the buffer instance shall be freed.] */
|
---|
156 | else if (size == 0)
|
---|
157 | {
|
---|
158 | /* Codes_SRS_BUFFER_01_003: [If size is zero, source can be NULL.] */
|
---|
159 | BUFFER* b = (BUFFER*)handle;
|
---|
160 | free(b->buffer);
|
---|
161 | b->buffer = NULL;
|
---|
162 | b->size = 0;
|
---|
163 |
|
---|
164 | result = 0;
|
---|
165 | }
|
---|
166 | else
|
---|
167 | {
|
---|
168 | if (source == NULL)
|
---|
169 | {
|
---|
170 | /* Codes_SRS_BUFFER_01_001: [If size is positive and source is NULL, BUFFER_build shall return nonzero] */
|
---|
171 | result = MU_FAILURE;
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | BUFFER* b = (BUFFER*)handle;
|
---|
176 | /* Codes_SRS_BUFFER_07_011: [BUFFER_build shall overwrite previous contents if the buffer has been previously allocated.] */
|
---|
177 | unsigned char* newBuffer = (unsigned char*)realloc(b->buffer, size);
|
---|
178 | if (newBuffer == NULL)
|
---|
179 | {
|
---|
180 | /* Codes_SRS_BUFFER_07_010: [BUFFER_build shall return nonzero if any error is encountered.] */
|
---|
181 | LogError("Failure reallocating buffer");
|
---|
182 | result = MU_FAILURE;
|
---|
183 | }
|
---|
184 | else
|
---|
185 | {
|
---|
186 | b->buffer = newBuffer;
|
---|
187 | b->size = size;
|
---|
188 | /* Codes_SRS_BUFFER_01_002: [The size argument can be zero, in which case nothing shall be copied from source.] */
|
---|
189 | (void)memcpy(b->buffer, source, size);
|
---|
190 |
|
---|
191 | result = 0;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | return result;
|
---|
197 | }
|
---|
198 |
|
---|
199 | int BUFFER_append_build(BUFFER_HANDLE handle, const unsigned char* source, size_t size)
|
---|
200 | {
|
---|
201 | int result;
|
---|
202 | if (handle == NULL || source == NULL || size == 0)
|
---|
203 | {
|
---|
204 | /* Codes_SRS_BUFFER_07_029: [ BUFFER_append_build shall return nonzero if handle or source are NULL or if size is 0. ] */
|
---|
205 | LogError("BUFFER_append_build failed invalid parameter handle: %p, source: %p, size: %lu", handle, source, (unsigned long)size);
|
---|
206 | result = MU_FAILURE;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | if (handle->buffer == NULL)
|
---|
211 | {
|
---|
212 | /* Codes_SRS_BUFFER_07_030: [ if handle->buffer is NULL BUFFER_append_build shall allocate the a buffer of size bytes... ] */
|
---|
213 | if (BUFFER_safemalloc(handle, size) != 0)
|
---|
214 | {
|
---|
215 | /* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
|
---|
216 | LogError("Failure with BUFFER_safemalloc");
|
---|
217 | result = MU_FAILURE;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | /* Codes_SRS_BUFFER_07_031: [ ... and copy the contents of source to handle->buffer. ] */
|
---|
222 | (void)memcpy(handle->buffer, source, size);
|
---|
223 | /* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
|
---|
224 | result = 0;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | else
|
---|
228 | {
|
---|
229 | /* Codes_SRS_BUFFER_07_032: [ if handle->buffer is not NULL BUFFER_append_build shall realloc the buffer to be the handle->size + size ] */
|
---|
230 | unsigned char* temp = (unsigned char*)realloc(handle->buffer, handle->size + size);
|
---|
231 | if (temp == NULL)
|
---|
232 | {
|
---|
233 | /* Codes_SRS_BUFFER_07_035: [ If any error is encountered BUFFER_append_build shall return a non-null value. ] */
|
---|
234 | LogError("Failure reallocating temporary buffer");
|
---|
235 | result = MU_FAILURE;
|
---|
236 | }
|
---|
237 | else
|
---|
238 | {
|
---|
239 | /* Codes_SRS_BUFFER_07_033: [ ... and copy the contents of source to the end of the buffer. ] */
|
---|
240 | handle->buffer = temp;
|
---|
241 | // Append the BUFFER
|
---|
242 | (void)memcpy(&handle->buffer[handle->size], source, size);
|
---|
243 | handle->size += size;
|
---|
244 | /* Codes_SRS_BUFFER_07_034: [ On success BUFFER_append_build shall return 0 ] */
|
---|
245 | result = 0;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | return result;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*return 0 if the buffer was pre-build(that is, had its space allocated)*/
|
---|
253 | /*else return different than zero*/
|
---|
254 | /* Codes_SRS_BUFFER_07_005: [BUFFER_pre_build allocates size_t bytes of BUFFER_HANDLE and returns zero on success.] */
|
---|
255 | int BUFFER_pre_build(BUFFER_HANDLE handle, size_t size)
|
---|
256 | {
|
---|
257 | int result;
|
---|
258 | if (handle == NULL)
|
---|
259 | {
|
---|
260 | /* Codes_SRS_BUFFER_07_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */
|
---|
261 | result = MU_FAILURE;
|
---|
262 | }
|
---|
263 | else if (size == 0)
|
---|
264 | {
|
---|
265 | /* Codes_SRS_BUFFER_07_006: [If handle is NULL or size is 0 then BUFFER_pre_build shall return a nonzero value.] */
|
---|
266 | result = MU_FAILURE;
|
---|
267 | }
|
---|
268 | else
|
---|
269 | {
|
---|
270 | BUFFER* b = (BUFFER*)handle;
|
---|
271 | if (b->buffer != NULL)
|
---|
272 | {
|
---|
273 | /* Codes_SRS_BUFFER_07_007: [BUFFER_pre_build shall return nonzero if the buffer has been previously allocated and is not NULL.] */
|
---|
274 | LogError("Failure buffer data is NULL");
|
---|
275 | result = MU_FAILURE;
|
---|
276 | }
|
---|
277 | else
|
---|
278 | {
|
---|
279 | if ((b->buffer = (unsigned char*)malloc(size)) == NULL)
|
---|
280 | {
|
---|
281 | /* Codes_SRS_BUFFER_07_013: [BUFFER_pre_build shall return nonzero if any error is encountered.] */
|
---|
282 | LogError("Failure allocating buffer");
|
---|
283 | result = MU_FAILURE;
|
---|
284 | }
|
---|
285 | else
|
---|
286 | {
|
---|
287 | b->size = size;
|
---|
288 | result = 0;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | return result;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* Codes_SRS_BUFFER_07_019: [BUFFER_content shall return the data contained within the BUFFER_HANDLE.] */
|
---|
296 | int BUFFER_content(BUFFER_HANDLE handle, const unsigned char** content)
|
---|
297 | {
|
---|
298 | int result;
|
---|
299 | if ((handle == NULL) || (content == NULL))
|
---|
300 | {
|
---|
301 | /* Codes_SRS_BUFFER_07_020: [If the handle and/or content*is NULL BUFFER_content shall return nonzero.] */
|
---|
302 | result = MU_FAILURE;
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | BUFFER* b = (BUFFER*)handle;
|
---|
307 | *content = b->buffer;
|
---|
308 | result = 0;
|
---|
309 | }
|
---|
310 | return result;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /*return 0 if everything went ok and whatever was built in the buffer was unbuilt*/
|
---|
314 | /* Codes_SRS_BUFFER_07_012: [BUFFER_unbuild shall clear the underlying unsigned char* data associated with the BUFFER_HANDLE this will return zero on success.] */
|
---|
315 | extern int BUFFER_unbuild(BUFFER_HANDLE handle)
|
---|
316 | {
|
---|
317 | int result;
|
---|
318 | if (handle == NULL)
|
---|
319 | {
|
---|
320 | /* Codes_SRS_BUFFER_07_014: [BUFFER_unbuild shall return a nonzero value if BUFFER_HANDLE is NULL.] */
|
---|
321 | LogError("Failure: handle is invalid.");
|
---|
322 | result = MU_FAILURE;
|
---|
323 | }
|
---|
324 | else
|
---|
325 | {
|
---|
326 | BUFFER* b = (BUFFER*)handle;
|
---|
327 | if (b->buffer != NULL)
|
---|
328 | {
|
---|
329 | free(b->buffer);
|
---|
330 | b->buffer = NULL;
|
---|
331 | b->size = 0;
|
---|
332 |
|
---|
333 | }
|
---|
334 |
|
---|
335 | /* Codes_SRS_BUFFER_07_015: [BUFFER_unbuild shall always return success if the unsigned char* referenced by BUFFER_HANDLE is NULL.] */
|
---|
336 | result = 0;
|
---|
337 | }
|
---|
338 | return result;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* Codes_SRS_BUFFER_07_016: [BUFFER_enlarge shall increase the size of the unsigned char* referenced by BUFFER_HANDLE.] */
|
---|
342 | int BUFFER_enlarge(BUFFER_HANDLE handle, size_t enlargeSize)
|
---|
343 | {
|
---|
344 | int result;
|
---|
345 | if (handle == NULL)
|
---|
346 | {
|
---|
347 | /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */
|
---|
348 | LogError("Failure: handle is invalid.");
|
---|
349 | result = MU_FAILURE;
|
---|
350 | }
|
---|
351 | else if (enlargeSize == 0)
|
---|
352 | {
|
---|
353 | /* Codes_SRS_BUFFER_07_017: [BUFFER_enlarge shall return a nonzero result if any parameters are NULL or zero.] */
|
---|
354 | LogError("Failure: enlargeSize size is 0.");
|
---|
355 | result = MU_FAILURE;
|
---|
356 | }
|
---|
357 | else
|
---|
358 | {
|
---|
359 | BUFFER* b = (BUFFER*)handle;
|
---|
360 | unsigned char* temp = (unsigned char*)realloc(b->buffer, b->size + enlargeSize);
|
---|
361 | if (temp == NULL)
|
---|
362 | {
|
---|
363 | /* Codes_SRS_BUFFER_07_018: [BUFFER_enlarge shall return a nonzero result if any error is encountered.] */
|
---|
364 | LogError("Failure: allocating temp buffer.");
|
---|
365 | result = MU_FAILURE;
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | b->buffer = temp;
|
---|
370 | b->size += enlargeSize;
|
---|
371 | result = 0;
|
---|
372 | }
|
---|
373 | }
|
---|
374 | return result;
|
---|
375 | }
|
---|
376 |
|
---|
377 | int BUFFER_shrink(BUFFER_HANDLE handle, size_t decreaseSize, bool fromEnd)
|
---|
378 | {
|
---|
379 | int result;
|
---|
380 | if (handle == NULL)
|
---|
381 | {
|
---|
382 | /* Codes_SRS_BUFFER_07_036: [ if handle is NULL, BUFFER_shrink shall return a non-null value ]*/
|
---|
383 | LogError("Failure: handle is invalid.");
|
---|
384 | result = MU_FAILURE;
|
---|
385 | }
|
---|
386 | else if (decreaseSize == 0)
|
---|
387 | {
|
---|
388 | /* Codes_SRS_BUFFER_07_037: [ If decreaseSize is equal zero, BUFFER_shrink shall return a non-null value ] */
|
---|
389 | LogError("Failure: decrease size is 0.");
|
---|
390 | result = MU_FAILURE;
|
---|
391 | }
|
---|
392 | else if (decreaseSize > handle->size)
|
---|
393 | {
|
---|
394 | /* Codes_SRS_BUFFER_07_038: [ If decreaseSize is less than the size of the buffer, BUFFER_shrink shall return a non-null value ] */
|
---|
395 | LogError("Failure: decrease size is less than buffer size.");
|
---|
396 | result = MU_FAILURE;
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | /* Codes_SRS_BUFFER_07_039: [ BUFFER_shrink shall allocate a temporary buffer of existing buffer size minus decreaseSize. ] */
|
---|
401 | size_t alloc_size = handle->size - decreaseSize;
|
---|
402 | if (alloc_size == 0)
|
---|
403 | {
|
---|
404 | /* Codes_SRS_BUFFER_07_043: [ If the decreaseSize is equal the buffer size , BUFFER_shrink shall deallocate the buffer and set the size to zero. ] */
|
---|
405 | free(handle->buffer);
|
---|
406 | handle->buffer = NULL;
|
---|
407 | handle->size = 0;
|
---|
408 | result = 0;
|
---|
409 | }
|
---|
410 | else
|
---|
411 | {
|
---|
412 | unsigned char* tmp = malloc(alloc_size);
|
---|
413 | if (tmp == NULL)
|
---|
414 | {
|
---|
415 | /* Codes_SRS_BUFFER_07_042: [ If a failure is encountered, BUFFER_shrink shall return a non-null value ] */
|
---|
416 | LogError("Failure: allocating temp buffer.");
|
---|
417 | result = MU_FAILURE;
|
---|
418 | }
|
---|
419 | else
|
---|
420 | {
|
---|
421 | if (fromEnd)
|
---|
422 | {
|
---|
423 | /* Codes_SRS_BUFFER_07_040: [ if the fromEnd variable is true, BUFFER_shrink shall remove the end of the buffer of size decreaseSize. ] */
|
---|
424 | memcpy(tmp, handle->buffer, alloc_size);
|
---|
425 | free(handle->buffer);
|
---|
426 | handle->buffer = tmp;
|
---|
427 | handle->size = alloc_size;
|
---|
428 | result = 0;
|
---|
429 | }
|
---|
430 | else
|
---|
431 | {
|
---|
432 | /* Codes_SRS_BUFFER_07_041: [ if the fromEnd variable is false, BUFFER_shrink shall remove the beginning of the buffer of size decreaseSize. ] */
|
---|
433 | memcpy(tmp, handle->buffer + decreaseSize, alloc_size);
|
---|
434 | free(handle->buffer);
|
---|
435 | handle->buffer = tmp;
|
---|
436 | handle->size = alloc_size;
|
---|
437 | result = 0;
|
---|
438 | }
|
---|
439 | }
|
---|
440 | }
|
---|
441 | }
|
---|
442 | return result;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /* Codes_SRS_BUFFER_07_021: [BUFFER_size shall place the size of the associated buffer in the size variable and return zero on success.] */
|
---|
446 | int BUFFER_size(BUFFER_HANDLE handle, size_t* size)
|
---|
447 | {
|
---|
448 | int result;
|
---|
449 | if ((handle == NULL) || (size == NULL))
|
---|
450 | {
|
---|
451 | /* Codes_SRS_BUFFER_07_022: [BUFFER_size shall return a nonzero value for any error that is encountered.] */
|
---|
452 | result = MU_FAILURE;
|
---|
453 | }
|
---|
454 | else
|
---|
455 | {
|
---|
456 | BUFFER* b = (BUFFER*)handle;
|
---|
457 | *size = b->size;
|
---|
458 | result = 0;
|
---|
459 | }
|
---|
460 | return result;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.] */
|
---|
464 | int BUFFER_append(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
|
---|
465 | {
|
---|
466 | int result;
|
---|
467 | if ( (handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2) )
|
---|
468 | {
|
---|
469 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
|
---|
470 | result = MU_FAILURE;
|
---|
471 | }
|
---|
472 | else
|
---|
473 | {
|
---|
474 | BUFFER* b1 = (BUFFER*)handle1;
|
---|
475 | BUFFER* b2 = (BUFFER*)handle2;
|
---|
476 | if (b1->buffer == NULL)
|
---|
477 | {
|
---|
478 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
|
---|
479 | result = MU_FAILURE;
|
---|
480 | }
|
---|
481 | else if (b2->buffer == NULL)
|
---|
482 | {
|
---|
483 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
|
---|
484 | result = MU_FAILURE;
|
---|
485 | }
|
---|
486 | else
|
---|
487 | {
|
---|
488 | if (b2->size ==0)
|
---|
489 | {
|
---|
490 | // b2->size = 0, whatever b1->size is, do nothing
|
---|
491 | result = 0;
|
---|
492 | }
|
---|
493 | else
|
---|
494 | {
|
---|
495 | // b2->size != 0, whatever b1->size is
|
---|
496 | unsigned char* temp = (unsigned char*)realloc(b1->buffer, b1->size + b2->size);
|
---|
497 | if (temp == NULL)
|
---|
498 | {
|
---|
499 | /* Codes_SRS_BUFFER_07_023: [BUFFER_append shall return a nonzero upon any error that is encountered.] */
|
---|
500 | LogError("Failure: allocating temp buffer.");
|
---|
501 | result = MU_FAILURE;
|
---|
502 | }
|
---|
503 | else
|
---|
504 | {
|
---|
505 | /* Codes_SRS_BUFFER_07_024: [BUFFER_append concatenates b2 onto b1 without modifying b2 and shall return zero on success.]*/
|
---|
506 | b1->buffer = temp;
|
---|
507 | // Append the BUFFER
|
---|
508 | (void)memcpy(&b1->buffer[b1->size], b2->buffer, b2->size);
|
---|
509 | b1->size += b2->size;
|
---|
510 | result = 0;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 | }
|
---|
515 | return result;
|
---|
516 | }
|
---|
517 |
|
---|
518 | int BUFFER_prepend(BUFFER_HANDLE handle1, BUFFER_HANDLE handle2)
|
---|
519 | {
|
---|
520 | int result;
|
---|
521 | if ((handle1 == NULL) || (handle2 == NULL) || (handle1 == handle2))
|
---|
522 | {
|
---|
523 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
|
---|
524 | result = MU_FAILURE;
|
---|
525 | }
|
---|
526 | else
|
---|
527 | {
|
---|
528 | BUFFER* b1 = (BUFFER*)handle1;
|
---|
529 | BUFFER* b2 = (BUFFER*)handle2;
|
---|
530 | if (b1->buffer == NULL)
|
---|
531 | {
|
---|
532 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
|
---|
533 | result = MU_FAILURE;
|
---|
534 | }
|
---|
535 | else if (b2->buffer == NULL)
|
---|
536 | {
|
---|
537 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
|
---|
538 | result = MU_FAILURE;
|
---|
539 | }
|
---|
540 | else
|
---|
541 | {
|
---|
542 | //put b2 ahead of b1: [b2][b1], return b1
|
---|
543 | if (b2->size ==0)
|
---|
544 | {
|
---|
545 | // do nothing
|
---|
546 | result = 0;
|
---|
547 | }
|
---|
548 | else
|
---|
549 | {
|
---|
550 | // b2->size != 0
|
---|
551 | unsigned char* temp = (unsigned char*)malloc(b1->size + b2->size);
|
---|
552 | if (temp == NULL)
|
---|
553 | {
|
---|
554 | /* Codes_SRS_BUFFER_01_005: [ BUFFER_prepend shall return a non-zero upon value any error that is encountered. ]*/
|
---|
555 | LogError("Failure: allocating temp buffer.");
|
---|
556 | result = MU_FAILURE;
|
---|
557 | }
|
---|
558 | else
|
---|
559 | {
|
---|
560 | /* Codes_SRS_BUFFER_01_004: [ BUFFER_prepend concatenates handle1 onto handle2 without modifying handle1 and shall return zero on success. ]*/
|
---|
561 | // Append the BUFFER
|
---|
562 | (void)memcpy(temp, b2->buffer, b2->size);
|
---|
563 | // start from b1->size to append b1
|
---|
564 | (void)memcpy(&temp[b2->size], b1->buffer, b1->size);
|
---|
565 | free(b1->buffer);
|
---|
566 | b1->buffer = temp;
|
---|
567 | b1->size += b2->size;
|
---|
568 | result = 0;
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 | return result;
|
---|
574 | }
|
---|
575 |
|
---|
576 | int BUFFER_fill(BUFFER_HANDLE handle, unsigned char fill_char)
|
---|
577 | {
|
---|
578 | int result;
|
---|
579 | if (handle == NULL)
|
---|
580 | {
|
---|
581 | /* Codes_SRS_BUFFER_07_002: [ If handle is NULL BUFFER_fill shall return a non-zero value. ] */
|
---|
582 | LogError("Invalid parameter specified, handle == NULL.");
|
---|
583 | result = MU_FAILURE;
|
---|
584 | }
|
---|
585 | else
|
---|
586 | {
|
---|
587 | size_t index;
|
---|
588 | /* Codes_SRS_BUFFER_07_001: [ BUFFER_fill shall fill the supplied BUFFER_HANDLE with the supplied fill character. ] */
|
---|
589 | BUFFER* buffer_data = (BUFFER*)handle;
|
---|
590 | for (index = 0; index < buffer_data->size; index++)
|
---|
591 | {
|
---|
592 | buffer_data->buffer[index] = fill_char;
|
---|
593 | }
|
---|
594 | result = 0;
|
---|
595 | }
|
---|
596 | return result;
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | /* Codes_SRS_BUFFER_07_025: [BUFFER_u_char shall return a pointer to the underlying unsigned char*.] */
|
---|
601 | unsigned char* BUFFER_u_char(BUFFER_HANDLE handle)
|
---|
602 | {
|
---|
603 | BUFFER* handleData = (BUFFER*)handle;
|
---|
604 | unsigned char* result;
|
---|
605 | if (handle == NULL || handleData->size == 0)
|
---|
606 | {
|
---|
607 | /* Codes_SRS_BUFFER_07_026: [BUFFER_u_char shall return NULL for any error that is encountered.] */
|
---|
608 | /* Codes_SRS_BUFFER_07_029: [BUFFER_u_char shall return NULL if underlying buffer size is zero.] */
|
---|
609 | result = NULL;
|
---|
610 | }
|
---|
611 | else
|
---|
612 | {
|
---|
613 | result = handleData->buffer;
|
---|
614 | }
|
---|
615 | return result;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /* Codes_SRS_BUFFER_07_027: [BUFFER_length shall return the size of the underlying buffer.] */
|
---|
619 | size_t BUFFER_length(BUFFER_HANDLE handle)
|
---|
620 | {
|
---|
621 | size_t result;
|
---|
622 | if (handle == NULL)
|
---|
623 | {
|
---|
624 | /* Codes_SRS_BUFFER_07_028: [BUFFER_length shall return zero for any error that is encountered.] */
|
---|
625 | result = 0;
|
---|
626 | }
|
---|
627 | else
|
---|
628 | {
|
---|
629 | BUFFER* b = (BUFFER*)handle;
|
---|
630 | result = b->size;
|
---|
631 | }
|
---|
632 | return result;
|
---|
633 | }
|
---|
634 |
|
---|
635 | BUFFER_HANDLE BUFFER_clone(BUFFER_HANDLE handle)
|
---|
636 | {
|
---|
637 | BUFFER_HANDLE result;
|
---|
638 | if (handle == NULL)
|
---|
639 | {
|
---|
640 | result = NULL;
|
---|
641 | }
|
---|
642 | else
|
---|
643 | {
|
---|
644 | BUFFER* suppliedBuff = (BUFFER*)handle;
|
---|
645 | BUFFER* b = (BUFFER*)calloc(1, sizeof(BUFFER));
|
---|
646 | if (b != NULL)
|
---|
647 | {
|
---|
648 | if (BUFFER_safemalloc(b, suppliedBuff->size) != 0)
|
---|
649 | {
|
---|
650 | free(b);
|
---|
651 | LogError("Failure: allocating temp buffer.");
|
---|
652 | result = NULL;
|
---|
653 | }
|
---|
654 | else
|
---|
655 | {
|
---|
656 | (void)memcpy(b->buffer, suppliedBuff->buffer, suppliedBuff->size);
|
---|
657 | b->size = suppliedBuff->size;
|
---|
658 | result = (BUFFER_HANDLE)b;
|
---|
659 | }
|
---|
660 | }
|
---|
661 | else
|
---|
662 | {
|
---|
663 | result = NULL;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | return result;
|
---|
667 | }
|
---|