Coverage Report

Created: 2024-02-25 07:20

/src/libmodi/libfplist/libfplist_xml_attribute.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * XML attribute functions
3
 *
4
 * Copyright (C) 2016-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <types.h>
25
26
#include "libfplist_libcerror.h"
27
#include "libfplist_xml_attribute.h"
28
29
/* Creates an XML attribute
30
 * Make sure the value attribute is referencing, is set to NULL
31
 * Returns 1 if successful or -1 on error
32
 */
33
int libfplist_xml_attribute_initialize(
34
     libfplist_xml_attribute_t **attribute,
35
     const uint8_t *name,
36
     size_t name_length,
37
     const uint8_t *value,
38
     size_t value_length,
39
     libcerror_error_t **error )
40
6.07k
{
41
6.07k
  static char *function = "libfplist_xml_attribute_initialize";
42
43
6.07k
  if( attribute == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid XML attribute.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
6.07k
  if( *attribute != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid XML attribute value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
6.07k
  if( name == NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
70
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
71
0
     "%s: invalid name.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
6.07k
  if( ( name_length == 0 )
77
6.07k
   || ( name_length > (size_t) ( MEMORY_MAXIMUM_ALLOCATION_SIZE - 1 ) ) )
78
0
  {
79
0
    libcerror_error_set(
80
0
     error,
81
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
82
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
83
0
     "%s: invalid name length value out of bounds.",
84
0
     function );
85
86
0
    return( -1 );
87
0
  }
88
6.07k
  if( value == NULL )
89
0
  {
90
0
    libcerror_error_set(
91
0
     error,
92
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
93
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
94
0
     "%s: invalid value.",
95
0
     function );
96
97
0
    return( -1 );
98
0
  }
99
6.07k
  if( ( value_length == 0 )
100
6.07k
   || ( value_length > (size_t) ( MEMORY_MAXIMUM_ALLOCATION_SIZE - 1 ) ) )
101
43
  {
102
43
    libcerror_error_set(
103
43
     error,
104
43
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
105
43
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
106
43
     "%s: invalid value length value out of bounds.",
107
43
     function );
108
109
43
    return( -1 );
110
43
  }
111
6.03k
  *attribute = memory_allocate_structure(
112
6.03k
                libfplist_xml_attribute_t );
113
114
6.03k
  if( *attribute == NULL )
115
0
  {
116
0
    libcerror_error_set(
117
0
     error,
118
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
119
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
120
0
     "%s: unable to create XML attribute.",
121
0
     function );
122
123
0
    goto on_error;
124
0
  }
125
6.03k
  if( memory_set(
126
6.03k
       *attribute,
127
6.03k
       0,
128
6.03k
       sizeof( libfplist_xml_attribute_t ) ) == NULL )
129
0
  {
130
0
    libcerror_error_set(
131
0
     error,
132
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
133
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
134
0
     "%s: unable to clear XML attribute.",
135
0
     function );
136
137
0
    memory_free(
138
0
     *attribute );
139
140
0
    *attribute = NULL;
141
142
0
    return( -1 );
143
0
  }
144
6.03k
  ( *attribute )->name_size = name_length + 1;
145
146
6.03k
  ( *attribute )->name = (uint8_t *) memory_allocate(
147
6.03k
                                      sizeof( uint8_t ) * ( *attribute )->name_size );
148
149
6.03k
  if( ( *attribute )->name == NULL )
150
0
  {
151
0
    libcerror_error_set(
152
0
     error,
153
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
154
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
155
0
     "%s: unable to create name.",
156
0
     function );
157
158
0
    goto on_error;
159
0
  }
160
6.03k
  if( memory_copy(
161
6.03k
       ( *attribute )->name,
162
6.03k
       name,
163
6.03k
       name_length ) == NULL )
164
0
  {
165
0
    libcerror_error_set(
166
0
     error,
167
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
168
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
169
0
     "%s: unable to copy name.",
170
0
     function );
171
172
0
    goto on_error;
173
0
  }
174
6.03k
  ( *attribute )->name[ name_length ] = 0;
175
176
6.03k
  ( *attribute )->value_size = value_length + 1;
177
178
6.03k
  ( *attribute )->value = (uint8_t *) memory_allocate(
179
6.03k
                                       sizeof( uint8_t ) * ( *attribute )->value_size );
180
181
6.03k
  if( ( *attribute )->value == NULL )
182
0
  {
183
0
    libcerror_error_set(
184
0
     error,
185
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
186
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
187
0
     "%s: unable to create value.",
188
0
     function );
189
190
0
    goto on_error;
191
0
  }
192
6.03k
  if( memory_copy(
193
6.03k
       ( *attribute )->value,
194
6.03k
       value,
195
6.03k
       value_length ) == NULL )
196
0
  {
197
0
    libcerror_error_set(
198
0
     error,
199
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
200
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
201
0
     "%s: unable to copy value.",
202
0
     function );
203
204
0
    goto on_error;
205
0
  }
206
6.03k
  ( *attribute )->value[ value_length ] = 0;
207
208
6.03k
  return( 1 );
209
210
0
on_error:
211
0
  if( *attribute != NULL )
212
0
  {
213
0
    if( ( *attribute )->value != NULL )
214
0
    {
215
0
      memory_free(
216
0
       ( *attribute )->value );
217
0
    }
218
0
    if( ( *attribute )->name != NULL )
219
0
    {
220
0
      memory_free(
221
0
       ( *attribute )->name );
222
0
    }
223
0
    memory_free(
224
0
     *attribute );
225
226
0
    *attribute = NULL;
227
0
  }
228
0
  return( -1 );
229
6.03k
}
230
231
/* Frees an XML attribute
232
 * Returns 1 if successful or -1 on error
233
 */
234
int libfplist_xml_attribute_free(
235
     libfplist_xml_attribute_t **attribute,
236
     libcerror_error_t **error )
237
6.03k
{
238
6.03k
  static char *function = "libfplist_xml_attribute_free";
239
240
6.03k
  if( attribute == NULL )
241
0
  {
242
0
    libcerror_error_set(
243
0
     error,
244
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
245
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
246
0
     "%s: invalid XML attribute.",
247
0
     function );
248
249
0
    return( -1 );
250
0
  }
251
6.03k
  if( *attribute != NULL )
252
6.03k
  {
253
6.03k
    if( ( *attribute )->value != NULL )
254
6.03k
    {
255
6.03k
      memory_free(
256
6.03k
       ( *attribute )->value );
257
6.03k
    }
258
6.03k
    if( ( *attribute )->name != NULL )
259
6.03k
    {
260
6.03k
      memory_free(
261
6.03k
       ( *attribute )->name );
262
6.03k
    }
263
6.03k
    memory_free(
264
6.03k
     *attribute );
265
266
6.03k
    *attribute = NULL;
267
6.03k
  }
268
6.03k
  return( 1 );
269
6.03k
}
270