renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / deps / tinycbor / cborparser_dup_string.c
blobb011c4e18c268769cf59f03b46d9251adff038ad
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:
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
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.
23 ****************************************************************************/
25 #ifndef _BSD_SOURCE
26 #define _BSD_SOURCE 1
27 #endif
28 #ifndef _DEFAULT_SOURCE
29 #define _DEFAULT_SOURCE 1
30 #endif
31 #ifndef __STDC_LIMIT_MACROS
32 # define __STDC_LIMIT_MACROS 1
33 #endif
35 #include "cbor.h"
36 #include "compilersupport_p.h"
37 #include <stdlib.h>
39 /**
40 * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
42 * Allocates memory for the string pointed by \a value and copies it into this
43 * buffer. The pointer to the buffer is stored in \a buffer and the number of
44 * bytes copied is stored in \a buflen (those variables must not be NULL).
46 * If the iterator \a value does not point to a text string, the behaviour is
47 * undefined, so checking with \ref cbor_value_get_type or \ref
48 * cbor_value_is_text_string is recommended.
50 * If \c malloc returns a NULL pointer, this function will return error
51 * condition \ref CborErrorOutOfMemory.
53 * On success, \c{*buffer} will contain a valid pointer that must be freed by
54 * calling \c{free()}. This is the case even for zero-length strings.
56 * The \a next pointer, if not null, will be updated to point to the next item
57 * after this string. If \a value points to the last item, then \a next will be
58 * invalid.
60 * This function may not run in constant time (it will run in O(n) time on the
61 * number of chunks). It requires constant memory (O(1)) in addition to the
62 * malloc'ed block.
64 * \note This function does not perform UTF-8 validation on the incoming text
65 * string.
67 * \sa cbor_value_get_text_string_chunk(), cbor_value_copy_text_string(), cbor_value_dup_byte_string()
70 /**
71 * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
73 * Allocates memory for the string pointed by \a value and copies it into this
74 * buffer. The pointer to the buffer is stored in \a buffer and the number of
75 * bytes copied is stored in \a buflen (those variables must not be NULL).
77 * If the iterator \a value does not point to a byte string, the behaviour is
78 * undefined, so checking with \ref cbor_value_get_type or \ref
79 * cbor_value_is_byte_string is recommended.
81 * If \c malloc returns a NULL pointer, this function will return error
82 * condition \ref CborErrorOutOfMemory.
84 * On success, \c{*buffer} will contain a valid pointer that must be freed by
85 * calling \c{free()}. This is the case even for zero-length strings.
87 * The \a next pointer, if not null, will be updated to point to the next item
88 * after this string. If \a value points to the last item, then \a next will be
89 * invalid.
91 * This function may not run in constant time (it will run in O(n) time on the
92 * number of chunks). It requires constant memory (O(1)) in addition to the
93 * malloc'ed block.
95 * \sa cbor_value_get_text_string_chunk(), cbor_value_copy_byte_string(), cbor_value_dup_text_string()
97 CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next) {
98 CborError err;
99 cbor_assert(buffer);
100 cbor_assert(buflen);
101 *buflen = SIZE_MAX;
102 err = _cbor_value_copy_string(value, NULL, buflen, NULL);
103 if (err)
104 return err;
106 ++*buflen;
107 *buffer = calloc(*buflen, sizeof(uint8_t));
108 if (!*buffer) {
109 /* out of memory */
110 return CborErrorOutOfMemory;
112 err = _cbor_value_copy_string(value, *buffer, buflen, next);
113 if (err) {
114 free(*buffer);
115 return err;
117 return CborNoError;