1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Intel Corporation
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
14 **
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 ** THE SOFTWARE.
22 **
23 ****************************************************************************/
24 
25 #define _BSD_SOURCE 1
26 #define _DEFAULT_SOURCE 1
27 #ifndef __STDC_LIMIT_MACROS
28 #  define __STDC_LIMIT_MACROS 1
29 #endif
30 
31 #include "tinycbor/cbor.h"
32 #include <stdlib.h>
33 
34 /**
35  * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
36  *
37  * Allocates memory for the string pointed by \a value and copies it into this
38  * buffer. The pointer to the buffer is stored in \a buffer and the number of
39  * bytes copied is stored in \a len (those variables must not be NULL).
40  *
41  * If the iterator \a value does not point to a text string, the behaviour is
42  * undefined, so checking with \ref cbor_value_get_type or \ref
43  * cbor_value_is_text_string is recommended.
44  *
45  * If \c malloc returns a NULL pointer, this function will return error
46  * condition \ref CborErrorOutOfMemory.
47  *
48  * On success, \c{*buffer} will contain a valid pointer that must be freed by
49  * calling \c{free()}. This is the case even for zero-length strings.
50  *
51  * The \a next pointer, if not null, will be updated to point to the next item
52  * after this string. If \a value points to the last item, then \a next will be
53  * invalid.
54  *
55  * This function may not run in constant time (it will run in O(n) time on the
56  * number of chunks). It requires constant memory (O(1)) in addition to the
57  * malloc'ed block.
58  *
59  * \note This function does not perform UTF-8 validation on the incoming text
60  * string.
61  *
62  * \sa cbor_value_copy_text_string(), cbor_value_dup_byte_string()
63  */
64 
65 /**
66  * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
67  *
68  * Allocates memory for the string pointed by \a value and copies it into this
69  * buffer. The pointer to the buffer is stored in \a buffer and the number of
70  * bytes copied is stored in \a len (those variables must not be NULL).
71  *
72  * If the iterator \a value does not point to a byte string, the behaviour is
73  * undefined, so checking with \ref cbor_value_get_type or \ref
74  * cbor_value_is_byte_string is recommended.
75  *
76  * If \c malloc returns a NULL pointer, this function will return error
77  * condition \ref CborErrorOutOfMemory.
78  *
79  * On success, \c{*buffer} will contain a valid pointer that must be freed by
80  * calling \c{free()}. This is the case even for zero-length strings.
81  *
82  * The \a next pointer, if not null, will be updated to point to the next item
83  * after this string. If \a value points to the last item, then \a next will be
84  * invalid.
85  *
86  * This function may not run in constant time (it will run in O(n) time on the
87  * number of chunks). It requires constant memory (O(1)) in addition to the
88  * malloc'ed block.
89  *
90  * \sa cbor_value_copy_byte_string(), cbor_value_dup_text_string()
91  */
_cbor_value_dup_string(const CborValue * value,void ** buffer,size_t * buflen,CborValue * next)92 CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
93 {
94     assert(buffer);
95     assert(buflen);
96     *buflen = SIZE_MAX;
97     CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL);
98     if (err)
99         return err;
100 
101     ++*buflen;
102     *buffer = malloc(*buflen);
103     if (!*buffer) {
104         /* out of memory */
105         return CborErrorOutOfMemory;
106     }
107     err = _cbor_value_copy_string(value, *buffer, buflen, next);
108     if (err) {
109         free(*buffer);
110         return err;
111     }
112     return CborNoError;
113 }
114