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