/src/libscca/libscca/libscca_filename_string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Filename string functions |
3 | | * |
4 | | * Copyright (C) 2011-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 "libscca_filename_string.h" |
27 | | #include "libscca_libcerror.h" |
28 | | #include "libscca_libuna.h" |
29 | | |
30 | | /* Creates file information |
31 | | * Make sure the value filename_string is referencing, is set to NULL |
32 | | * Returns 1 if successful or -1 on error |
33 | | */ |
34 | | int libscca_filename_string_initialize( |
35 | | libscca_filename_string_t **filename_string, |
36 | | uint32_t offset, |
37 | | const uint8_t *data, |
38 | | size_t data_size, |
39 | | libcerror_error_t **error ) |
40 | 208k | { |
41 | 208k | static char *function = "libscca_filename_string_initialize"; |
42 | | |
43 | 208k | if( filename_string == 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 file information.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 208k | if( *filename_string != 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 file information value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 208k | *filename_string = memory_allocate_structure( |
66 | 208k | libscca_filename_string_t ); |
67 | | |
68 | 208k | if( *filename_string == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
73 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
74 | 0 | "%s: unable to create file information.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 208k | if( memory_set( |
80 | 208k | *filename_string, |
81 | 208k | 0, |
82 | 208k | sizeof( libscca_filename_string_t ) ) == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
88 | 0 | "%s: unable to clear file.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 208k | ( *filename_string )->offset = offset; |
94 | 208k | ( *filename_string )->data = data; |
95 | 208k | ( *filename_string )->data_size = data_size; |
96 | | |
97 | 208k | return( 1 ); |
98 | | |
99 | 0 | on_error: |
100 | 0 | if( *filename_string != NULL ) |
101 | 0 | { |
102 | 0 | memory_free( |
103 | 0 | *filename_string ); |
104 | |
|
105 | 0 | *filename_string = NULL; |
106 | 0 | } |
107 | 0 | return( -1 ); |
108 | 208k | } |
109 | | |
110 | | /* Frees file information |
111 | | * Returns 1 if successful or -1 on error |
112 | | */ |
113 | | int libscca_filename_string_free( |
114 | | libscca_filename_string_t **filename_string, |
115 | | libcerror_error_t **error ) |
116 | 208k | { |
117 | 208k | static char *function = "libscca_filename_string_free"; |
118 | | |
119 | 208k | if( filename_string == NULL ) |
120 | 0 | { |
121 | 0 | libcerror_error_set( |
122 | 0 | error, |
123 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
124 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
125 | 0 | "%s: invalid file information.", |
126 | 0 | function ); |
127 | |
|
128 | 0 | return( -1 ); |
129 | 0 | } |
130 | 208k | if( *filename_string != NULL ) |
131 | 208k | { |
132 | 208k | memory_free( |
133 | 208k | *filename_string ); |
134 | | |
135 | 208k | *filename_string = NULL; |
136 | 208k | } |
137 | 208k | return( 1 ); |
138 | 208k | } |
139 | | |
140 | | /* Retrieves the size of a specific UTF-8 encoded string |
141 | | * This function uses UTF-8 RFC 2279 (or 6-byte UTF-8) to support characters outside Unicode |
142 | | * The returned size includes the end of string character |
143 | | * Returns 1 if successful or -1 on error |
144 | | */ |
145 | | int libscca_filename_string_get_utf8_string_size( |
146 | | libscca_filename_string_t *filename_string, |
147 | | size_t *utf8_string_size, |
148 | | libcerror_error_t **error ) |
149 | 0 | { |
150 | 0 | static char *function = "libscca_filename_string_get_utf8_string_size"; |
151 | |
|
152 | 0 | if( filename_string == NULL ) |
153 | 0 | { |
154 | 0 | libcerror_error_set( |
155 | 0 | error, |
156 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
157 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
158 | 0 | "%s: invalid filename string.", |
159 | 0 | function ); |
160 | |
|
161 | 0 | return( -1 ); |
162 | 0 | } |
163 | 0 | if( libuna_utf8_string_size_from_utf16_stream( |
164 | 0 | filename_string->data, |
165 | 0 | filename_string->data_size, |
166 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
167 | 0 | utf8_string_size, |
168 | 0 | error ) != 1 ) |
169 | 0 | { |
170 | 0 | libcerror_error_set( |
171 | 0 | error, |
172 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
173 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
174 | 0 | "%s: unable to determine size of UTF-8 string.", |
175 | 0 | function ); |
176 | |
|
177 | 0 | return( -1 ); |
178 | 0 | } |
179 | 0 | return( 1 ); |
180 | 0 | } |
181 | | |
182 | | /* Retrieves a specific UTF-8 encoded string |
183 | | * This function uses UTF-8 RFC 2279 (or 6-byte UTF-8) to support characters outside Unicode |
184 | | * The size should include the end of string character |
185 | | * Returns 1 if successful or -1 on error |
186 | | */ |
187 | | int libscca_filename_string_get_utf8_string( |
188 | | libscca_filename_string_t *filename_string, |
189 | | uint8_t *utf8_string, |
190 | | size_t utf8_string_size, |
191 | | libcerror_error_t **error ) |
192 | 0 | { |
193 | 0 | static char *function = "libscca_filename_string_get_utf8_string"; |
194 | |
|
195 | 0 | if( filename_string == NULL ) |
196 | 0 | { |
197 | 0 | libcerror_error_set( |
198 | 0 | error, |
199 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
200 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
201 | 0 | "%s: invalid filename string.", |
202 | 0 | function ); |
203 | |
|
204 | 0 | return( -1 ); |
205 | 0 | } |
206 | 0 | if( libuna_utf8_string_copy_from_utf16_stream( |
207 | 0 | (libuna_utf8_character_t *) utf8_string, |
208 | 0 | utf8_string_size, |
209 | 0 | filename_string->data, |
210 | 0 | filename_string->data_size, |
211 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
212 | 0 | error ) != 1 ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
217 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
218 | 0 | "%s: unable to set UTF-8 string.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | 0 | return( 1 ); |
224 | 0 | } |
225 | | |
226 | | /* Retrieves the size of a specific UTF-16 encoded string |
227 | | * This function uses UCS-2 (with surrogates) to support characters outside Unicode |
228 | | * The returned size includes the end of string character |
229 | | * Returns 1 if successful or -1 on error |
230 | | */ |
231 | | int libscca_filename_string_get_utf16_string_size( |
232 | | libscca_filename_string_t *filename_string, |
233 | | size_t *utf16_string_size, |
234 | | libcerror_error_t **error ) |
235 | 0 | { |
236 | 0 | static char *function = "libscca_filename_string_get_utf16_string_size"; |
237 | |
|
238 | 0 | if( filename_string == NULL ) |
239 | 0 | { |
240 | 0 | libcerror_error_set( |
241 | 0 | error, |
242 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
243 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
244 | 0 | "%s: invalid filename string.", |
245 | 0 | function ); |
246 | |
|
247 | 0 | return( -1 ); |
248 | 0 | } |
249 | 0 | if( libuna_utf16_string_size_from_utf16_stream( |
250 | 0 | filename_string->data, |
251 | 0 | filename_string->data_size, |
252 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
253 | 0 | utf16_string_size, |
254 | 0 | error ) != 1 ) |
255 | 0 | { |
256 | 0 | libcerror_error_set( |
257 | 0 | error, |
258 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
259 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
260 | 0 | "%s: unable to determine size of UTF-16 string.", |
261 | 0 | function ); |
262 | |
|
263 | 0 | return( -1 ); |
264 | 0 | } |
265 | 0 | return( 1 ); |
266 | 0 | } |
267 | | |
268 | | /* Retrieves a specific UTF-16 encoded string |
269 | | * This function uses UCS-2 (with surrogates) to support characters outside Unicode |
270 | | * The size should include the end of string character |
271 | | * Returns 1 if successful or -1 on error |
272 | | */ |
273 | | int libscca_filename_string_get_utf16_string( |
274 | | libscca_filename_string_t *filename_string, |
275 | | uint16_t *utf16_string, |
276 | | size_t utf16_string_size, |
277 | | libcerror_error_t **error ) |
278 | 0 | { |
279 | 0 | static char *function = "libscca_filename_string_get_utf16_string"; |
280 | |
|
281 | 0 | if( filename_string == NULL ) |
282 | 0 | { |
283 | 0 | libcerror_error_set( |
284 | 0 | error, |
285 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
286 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
287 | 0 | "%s: invalid filename string.", |
288 | 0 | function ); |
289 | |
|
290 | 0 | return( -1 ); |
291 | 0 | } |
292 | 0 | if( libuna_utf16_string_copy_from_utf16_stream( |
293 | 0 | (libuna_utf16_character_t *) utf16_string, |
294 | 0 | utf16_string_size, |
295 | 0 | filename_string->data, |
296 | 0 | filename_string->data_size, |
297 | 0 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
298 | 0 | error ) != 1 ) |
299 | 0 | { |
300 | 0 | libcerror_error_set( |
301 | 0 | error, |
302 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
303 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
304 | 0 | "%s: unable to set UTF-16 string.", |
305 | 0 | function ); |
306 | |
|
307 | 0 | return( -1 ); |
308 | 0 | } |
309 | 0 | return( 1 ); |
310 | 0 | } |
311 | | |