]> git.zerfleddert.de Git - proxmark3-svn/blame - client/tinycbor/cborparser_dup_string.c
Fido2 (#727)
[proxmark3-svn] / client / tinycbor / cborparser_dup_string.c
CommitLineData
0bb51450
OM
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#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
34
35#include "cbor.h"
36#include "compilersupport_p.h"
37#include <stdlib.h>
38
39/**
40 * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
41 *
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).
45 *
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.
49 *
50 * If \c malloc returns a NULL pointer, this function will return error
51 * condition \ref CborErrorOutOfMemory.
52 *
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.
55 *
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.
59 *
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.
63 *
64 * \note This function does not perform UTF-8 validation on the incoming text
65 * string.
66 *
67 * \sa cbor_value_get_text_string_chunk(), cbor_value_copy_text_string(), cbor_value_dup_byte_string()
68 */
69
70/**
71 * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
72 *
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).
76 *
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.
80 *
81 * If \c malloc returns a NULL pointer, this function will return error
82 * condition \ref CborErrorOutOfMemory.
83 *
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.
86 *
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.
90 *
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.
94 *
95 * \sa cbor_value_get_text_string_chunk(), cbor_value_copy_byte_string(), cbor_value_dup_text_string()
96 */
97CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
98{
99 CborError err;
100 cbor_assert(buffer);
101 cbor_assert(buflen);
102 *buflen = SIZE_MAX;
103 err = _cbor_value_copy_string(value, NULL, buflen, NULL);
104 if (err)
105 return err;
106
107 ++*buflen;
108 *buffer = malloc(*buflen);
109 if (!*buffer) {
110 /* out of memory */
111 return CborErrorOutOfMemory;
112 }
113 err = _cbor_value_copy_string(value, *buffer, buflen, next);
114 if (err) {
115 free(*buffer);
116 return err;
117 }
118 return CborNoError;
119}
Impressum, Datenschutz