/src/libewf/libewf/libewf_line_reader.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * The line reader functions |
3 | | * |
4 | | * Copyright (C) 2006-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 | | #if defined( HAVE_STRING_H ) || defined( WINAPI ) |
27 | | #include <string.h> |
28 | | #endif |
29 | | |
30 | | #include "libewf_libbfio.h" |
31 | | #include "libewf_libcerror.h" |
32 | | #include "libewf_libcnotify.h" |
33 | | #include "libewf_libfdata.h" |
34 | | #include "libewf_libhmac.h" |
35 | | #include "libewf_libuna.h" |
36 | | #include "libewf_line_reader.h" |
37 | | |
38 | | /* Creates a line reader |
39 | | * Make sure the value line_reader is referencing, is set to NULL |
40 | | * Returns 1 if successful or -1 on error |
41 | | */ |
42 | | int libewf_line_reader_initialize( |
43 | | libewf_line_reader_t **line_reader, |
44 | | libfdata_stream_t *data_stream, |
45 | | libbfio_pool_t *file_io_pool, |
46 | | libcerror_error_t **error ) |
47 | 92 | { |
48 | 92 | static char *function = "libewf_line_reader_initialize"; |
49 | | |
50 | 92 | if( line_reader == NULL ) |
51 | 0 | { |
52 | 0 | libcerror_error_set( |
53 | 0 | error, |
54 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
55 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
56 | 0 | "%s: invalid line reader.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 92 | if( *line_reader != NULL ) |
62 | 0 | { |
63 | 0 | libcerror_error_set( |
64 | 0 | error, |
65 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
66 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
67 | 0 | "%s: invalid line reader value already set.", |
68 | 0 | function ); |
69 | |
|
70 | 0 | return( -1 ); |
71 | 0 | } |
72 | 92 | if( data_stream == NULL ) |
73 | 0 | { |
74 | 0 | libcerror_error_set( |
75 | 0 | error, |
76 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
77 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
78 | 0 | "%s: invalid data stream.", |
79 | 0 | function ); |
80 | |
|
81 | 0 | return( -1 ); |
82 | 0 | } |
83 | 92 | *line_reader = memory_allocate_structure( |
84 | 92 | libewf_line_reader_t ); |
85 | | |
86 | 92 | if( *line_reader == NULL ) |
87 | 0 | { |
88 | 0 | libcerror_error_set( |
89 | 0 | error, |
90 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
91 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
92 | 0 | "%s: unable to create line reader.", |
93 | 0 | function ); |
94 | |
|
95 | 0 | goto on_error; |
96 | 0 | } |
97 | 92 | if( memory_set( |
98 | 92 | *line_reader, |
99 | 92 | 0, |
100 | 92 | sizeof( libewf_line_reader_t ) ) == NULL ) |
101 | 0 | { |
102 | 0 | libcerror_error_set( |
103 | 0 | error, |
104 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
105 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
106 | 0 | "%s: unable to clear line reader.", |
107 | 0 | function ); |
108 | |
|
109 | 0 | memory_free( |
110 | 0 | *line_reader ); |
111 | |
|
112 | 0 | *line_reader = NULL; |
113 | |
|
114 | 0 | return( -1 ); |
115 | 0 | } |
116 | 92 | if( libfdata_stream_get_size( |
117 | 92 | data_stream, |
118 | 92 | &( ( *line_reader )->stream_size ), |
119 | 92 | error ) != 1 ) |
120 | 0 | { |
121 | 0 | libcerror_error_set( |
122 | 0 | error, |
123 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
124 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
125 | 0 | "%s: unable to retrieve data stream size.", |
126 | 0 | function ); |
127 | |
|
128 | 0 | goto on_error; |
129 | 0 | } |
130 | 92 | ( *line_reader )->buffer_size = 16 * 1024 * 1024; |
131 | | |
132 | 92 | ( *line_reader )->buffer = (uint8_t *) memory_allocate( |
133 | 92 | ( *line_reader )->buffer_size ); |
134 | | |
135 | 92 | if( ( *line_reader )->buffer == NULL ) |
136 | 0 | { |
137 | 0 | libcerror_error_set( |
138 | 0 | error, |
139 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
140 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
141 | 0 | "%s: unable to create buffer.", |
142 | 0 | function ); |
143 | |
|
144 | 0 | goto on_error; |
145 | 0 | } |
146 | 92 | ( *line_reader )->utf8_string_size = 1 * 1024 * 1024; |
147 | | |
148 | 92 | ( *line_reader )->utf8_string = (uint8_t *) memory_allocate( |
149 | 92 | ( *line_reader )->utf8_string_size ); |
150 | | |
151 | 92 | if( ( *line_reader )->utf8_string == NULL ) |
152 | 0 | { |
153 | 0 | libcerror_error_set( |
154 | 0 | error, |
155 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
156 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
157 | 0 | "%s: unable to create UTF-8 string.", |
158 | 0 | function ); |
159 | |
|
160 | 0 | goto on_error; |
161 | 0 | } |
162 | 92 | if( libhmac_md5_initialize( |
163 | 92 | &( ( *line_reader )->md5_context ), |
164 | 92 | error ) != 1 ) |
165 | 0 | { |
166 | 0 | libcerror_error_set( |
167 | 0 | error, |
168 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
169 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
170 | 0 | "%s: unable to initialize MD5 context.", |
171 | 0 | function ); |
172 | |
|
173 | 0 | goto on_error; |
174 | 0 | } |
175 | 92 | ( *line_reader )->data_stream = data_stream; |
176 | 92 | ( *line_reader )->file_io_pool = file_io_pool; |
177 | | |
178 | 92 | return( 1 ); |
179 | | |
180 | 0 | on_error: |
181 | 0 | if( *line_reader != NULL ) |
182 | 0 | { |
183 | 0 | if( ( *line_reader )->utf8_string != NULL ) |
184 | 0 | { |
185 | 0 | memory_free( |
186 | 0 | ( *line_reader )->utf8_string ); |
187 | 0 | } |
188 | 0 | if( ( *line_reader )->buffer != NULL ) |
189 | 0 | { |
190 | 0 | memory_free( |
191 | 0 | ( *line_reader )->buffer ); |
192 | 0 | } |
193 | 0 | memory_free( |
194 | 0 | *line_reader ); |
195 | |
|
196 | 0 | *line_reader = NULL; |
197 | 0 | } |
198 | 0 | return( -1 ); |
199 | 92 | } |
200 | | |
201 | | /* Frees a line reader |
202 | | * Returns 1 if successful or -1 on error |
203 | | */ |
204 | | int libewf_line_reader_free( |
205 | | libewf_line_reader_t **line_reader, |
206 | | libcerror_error_t **error ) |
207 | 92 | { |
208 | 92 | static char *function = "libewf_line_reader_free"; |
209 | 92 | int result = 1; |
210 | | |
211 | 92 | if( line_reader == NULL ) |
212 | 0 | { |
213 | 0 | libcerror_error_set( |
214 | 0 | error, |
215 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
216 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
217 | 0 | "%s: invalid line reader.", |
218 | 0 | function ); |
219 | |
|
220 | 0 | return( -1 ); |
221 | 0 | } |
222 | 92 | if( *line_reader != NULL ) |
223 | 92 | { |
224 | | /* The data_stream reference is freed elsewhere |
225 | | */ |
226 | 92 | if( libhmac_md5_free( |
227 | 92 | &( ( *line_reader )->md5_context ), |
228 | 92 | error ) != 1 ) |
229 | 0 | { |
230 | 0 | libcerror_error_set( |
231 | 0 | error, |
232 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
233 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
234 | 0 | "%s: unable to free MD5 context.", |
235 | 0 | function ); |
236 | |
|
237 | 0 | result = -1; |
238 | 0 | } |
239 | 92 | memory_free( |
240 | 92 | ( *line_reader )->utf8_string ); |
241 | | |
242 | 92 | memory_free( |
243 | 92 | ( *line_reader )->buffer ); |
244 | | |
245 | 92 | memory_free( |
246 | 92 | *line_reader ); |
247 | | |
248 | 92 | *line_reader = NULL; |
249 | 92 | } |
250 | 92 | return( result ); |
251 | 92 | } |
252 | | |
253 | | /* Reads a line as data |
254 | | * Returns 1 if successful or -1 on error |
255 | | */ |
256 | | int libewf_line_reader_read_data( |
257 | | libewf_line_reader_t *line_reader, |
258 | | const uint8_t **line_data, |
259 | | size_t *line_data_size, |
260 | | libcerror_error_t **error ) |
261 | 116 | { |
262 | 116 | const uint8_t *safe_line_data = NULL; |
263 | 116 | static char *function = "libewf_line_reader_read_data"; |
264 | 116 | size_t end_of_line_offset = 0; |
265 | 116 | size_t read_size = 0; |
266 | 116 | size_t safe_line_data_size = 0; |
267 | 116 | ssize_t read_count = 0; |
268 | 116 | int safe_line_index = 0; |
269 | | |
270 | 116 | if( line_reader == NULL ) |
271 | 0 | { |
272 | 0 | libcerror_error_set( |
273 | 0 | error, |
274 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
275 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
276 | 0 | "%s: invalid line reader.", |
277 | 0 | function ); |
278 | |
|
279 | 0 | return( -1 ); |
280 | 0 | } |
281 | 116 | if( line_reader->buffer_offset >= line_reader->buffer_size ) |
282 | 0 | { |
283 | 0 | libcerror_error_set( |
284 | 0 | error, |
285 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
286 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
287 | 0 | "%s: invalid line reader - buffer offset value out of bounds.", |
288 | 0 | function ); |
289 | |
|
290 | 0 | return( -1 ); |
291 | 0 | } |
292 | 116 | if( line_data == NULL ) |
293 | 0 | { |
294 | 0 | libcerror_error_set( |
295 | 0 | error, |
296 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
297 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
298 | 0 | "%s: invalid line data.", |
299 | 0 | function ); |
300 | |
|
301 | 0 | return( -1 ); |
302 | 0 | } |
303 | 116 | if( line_data_size == NULL ) |
304 | 0 | { |
305 | 0 | libcerror_error_set( |
306 | 0 | error, |
307 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
308 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
309 | 0 | "%s: invalid line data size.", |
310 | 0 | function ); |
311 | |
|
312 | 0 | return( -1 ); |
313 | 0 | } |
314 | 116 | read_size = line_reader->buffer_size - line_reader->buffer_offset; |
315 | | |
316 | 116 | if( (size64_t) line_reader->stream_offset < line_reader->stream_size ) |
317 | 92 | { |
318 | 92 | if( ( line_reader->stream_offset == 0 ) |
319 | 92 | || ( read_size < line_reader->utf8_string_size ) ) |
320 | 92 | { |
321 | | #if defined( HAVE_DEBUG_OUTPUT ) |
322 | | if( libcnotify_verbose != 0 ) |
323 | | { |
324 | | libcnotify_printf( |
325 | | "%s: additional line data needed at offset: %" PRIi64 " (0x%08" PRIx64 ").\n", |
326 | | function, |
327 | | line_reader->line_offset, |
328 | | line_reader->line_offset ); |
329 | | } |
330 | | #endif |
331 | 92 | if( line_reader->buffer_offset > 0 ) |
332 | 0 | { |
333 | 0 | if( memmove( |
334 | 0 | line_reader->buffer, |
335 | 0 | &( line_reader->buffer[ line_reader->buffer_offset ] ), |
336 | 0 | read_size ) == NULL ) |
337 | 0 | { |
338 | 0 | libcerror_error_set( |
339 | 0 | error, |
340 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
341 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
342 | 0 | "%s: unable to move buffer content to start.", |
343 | 0 | function ); |
344 | |
|
345 | 0 | return( -1 ); |
346 | 0 | } |
347 | 0 | line_reader->buffer_offset = read_size; |
348 | |
|
349 | 0 | read_size = line_reader->buffer_size - read_size; |
350 | 0 | } |
351 | 92 | if( read_size > ( line_reader->stream_size - line_reader->stream_offset ) ) |
352 | 92 | { |
353 | 92 | read_size = (size_t) ( line_reader->stream_size - line_reader->stream_offset ); |
354 | 92 | } |
355 | | #if defined( HAVE_DEBUG_OUTPUT ) |
356 | | if( libcnotify_verbose != 0 ) |
357 | | { |
358 | | libcnotify_printf( |
359 | | "%s: reading %" PRIzd " bytes of section data at offset: %" PRIi64 " (0x%08" PRIx64 ").\n", |
360 | | function, |
361 | | read_size, |
362 | | line_reader->stream_offset, |
363 | | line_reader->stream_offset ); |
364 | | } |
365 | | #endif |
366 | 92 | read_count = libfdata_stream_read_buffer_at_offset( |
367 | 92 | line_reader->data_stream, |
368 | 92 | (intptr_t *) line_reader->file_io_pool, |
369 | 92 | &( line_reader->buffer[ line_reader->buffer_offset ] ), |
370 | 92 | read_size, |
371 | 92 | line_reader->stream_offset, |
372 | 92 | 0, |
373 | 92 | error ); |
374 | | |
375 | 92 | if( read_count != (ssize_t) read_size ) |
376 | 9 | { |
377 | 9 | libcerror_error_set( |
378 | 9 | error, |
379 | 9 | LIBCERROR_ERROR_DOMAIN_IO, |
380 | 9 | LIBCERROR_IO_ERROR_READ_FAILED, |
381 | 9 | "%s: unable to read section data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
382 | 9 | function, |
383 | 9 | line_reader->stream_offset, |
384 | 9 | line_reader->stream_offset ); |
385 | | |
386 | 9 | return( -1 ); |
387 | 9 | } |
388 | 83 | line_reader->stream_offset += read_count; |
389 | | |
390 | 83 | if( libhmac_md5_update( |
391 | 83 | line_reader->md5_context, |
392 | 83 | &( line_reader->buffer[ line_reader->buffer_offset ] ), |
393 | 83 | read_size, |
394 | 83 | error ) != 1 ) |
395 | 0 | { |
396 | 0 | libcerror_error_set( |
397 | 0 | error, |
398 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
399 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
400 | 0 | "%s: unable to update MD5 digest hash.", |
401 | 0 | function ); |
402 | |
|
403 | 0 | return( -1 ); |
404 | 0 | } |
405 | 83 | line_reader->buffer_offset = 0; |
406 | 83 | } |
407 | 92 | } |
408 | 107 | read_size = 0; |
409 | 107 | safe_line_index = line_reader->line_index; |
410 | | |
411 | 107 | for( end_of_line_offset = line_reader->buffer_offset; |
412 | 92.3M | end_of_line_offset < line_reader->buffer_size; |
413 | 92.3M | end_of_line_offset += 2 ) |
414 | 92.3M | { |
415 | 92.3M | if( ( line_reader->buffer[ end_of_line_offset ] == (uint8_t) '\n' ) |
416 | 92.3M | && ( line_reader->buffer[ end_of_line_offset + 1 ] == 0 ) ) |
417 | 96 | { |
418 | 96 | read_size = ( end_of_line_offset + 2 ) - line_reader->buffer_offset; |
419 | | |
420 | 96 | safe_line_index++; |
421 | | |
422 | 96 | break; |
423 | 96 | } |
424 | 92.3M | } |
425 | | /* Remove trailing carriage return |
426 | | */ |
427 | 107 | if( end_of_line_offset >= 2 ) |
428 | 105 | { |
429 | 105 | if( ( line_reader->buffer[ end_of_line_offset - 2 ] == (uint8_t) '\r' ) |
430 | 105 | && ( line_reader->buffer[ end_of_line_offset - 1 ] == 0 ) ) |
431 | 3 | { |
432 | 3 | end_of_line_offset -= 2; |
433 | 3 | } |
434 | 105 | } |
435 | 107 | safe_line_data_size = end_of_line_offset - line_reader->buffer_offset; |
436 | | |
437 | 107 | if( read_size == 0 ) |
438 | 11 | { |
439 | 11 | read_size = safe_line_data_size; |
440 | 11 | } |
441 | 107 | if( safe_line_data_size > 0 ) |
442 | 105 | { |
443 | 105 | safe_line_data = &( line_reader->buffer[ line_reader->buffer_offset ] ); |
444 | | |
445 | | #if defined( HAVE_DEBUG_OUTPUT ) |
446 | | if( libcnotify_verbose != 0 ) |
447 | | { |
448 | | libcnotify_printf( |
449 | | "%s: line: %d data at offset: %" PRIi64 " (0x%08" PRIx64 "):\n", |
450 | | function, |
451 | | line_reader->line_index, |
452 | | line_reader->line_offset, |
453 | | line_reader->line_offset ); |
454 | | libcnotify_print_data( |
455 | | safe_line_data, |
456 | | safe_line_data_size, |
457 | | 0 ); |
458 | | } |
459 | | #endif |
460 | 105 | } |
461 | 107 | line_reader->buffer_offset += read_size; |
462 | 107 | line_reader->line_offset += read_size; |
463 | 107 | line_reader->line_index = safe_line_index; |
464 | | |
465 | 107 | *line_data = safe_line_data; |
466 | 107 | *line_data_size = safe_line_data_size; |
467 | | |
468 | 107 | return( 1 ); |
469 | 116 | } |
470 | | |
471 | | /* Reads a line as UTF-8 string |
472 | | * Returns 1 if successful or -1 on error |
473 | | */ |
474 | | int libewf_line_reader_read_utf8_string( |
475 | | libewf_line_reader_t *line_reader, |
476 | | uint8_t **utf8_string, |
477 | | size_t *utf8_string_size, |
478 | | libcerror_error_t **error ) |
479 | 116 | { |
480 | 116 | const uint8_t *utf16_stream = NULL; |
481 | 116 | static char *function = "libewf_line_reader_read_utf8_string"; |
482 | 116 | size_t safe_utf8_string_size = 0; |
483 | 116 | size_t utf16_stream_size = 0; |
484 | | |
485 | 116 | if( line_reader == NULL ) |
486 | 0 | { |
487 | 0 | libcerror_error_set( |
488 | 0 | error, |
489 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
490 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
491 | 0 | "%s: invalid line reader.", |
492 | 0 | function ); |
493 | |
|
494 | 0 | return( -1 ); |
495 | 0 | } |
496 | 116 | if( utf8_string == NULL ) |
497 | 0 | { |
498 | 0 | libcerror_error_set( |
499 | 0 | error, |
500 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
501 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
502 | 0 | "%s: invalid UTF-8 string.", |
503 | 0 | function ); |
504 | |
|
505 | 0 | return( -1 ); |
506 | 0 | } |
507 | 116 | if( utf8_string_size == NULL ) |
508 | 0 | { |
509 | 0 | libcerror_error_set( |
510 | 0 | error, |
511 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
512 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
513 | 0 | "%s: invalid UTF-8 string size.", |
514 | 0 | function ); |
515 | |
|
516 | 0 | return( -1 ); |
517 | 0 | } |
518 | 116 | if( libewf_line_reader_read_data( |
519 | 116 | line_reader, |
520 | 116 | &utf16_stream, |
521 | 116 | &utf16_stream_size, |
522 | 116 | error ) != 1 ) |
523 | 9 | { |
524 | 9 | libcerror_error_set( |
525 | 9 | error, |
526 | 9 | LIBCERROR_ERROR_DOMAIN_IO, |
527 | 9 | LIBCERROR_IO_ERROR_READ_FAILED, |
528 | 9 | "%s: unable to read line data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
529 | 9 | function, |
530 | 9 | line_reader->stream_offset, |
531 | 9 | line_reader->stream_offset ); |
532 | | |
533 | 9 | return( -1 ); |
534 | 9 | } |
535 | 107 | if( utf16_stream_size == 0 ) |
536 | 2 | { |
537 | 2 | line_reader->utf8_string[ 0 ] = 0; |
538 | 2 | safe_utf8_string_size = 1; |
539 | 2 | } |
540 | 105 | else |
541 | 105 | { |
542 | 105 | if( libuna_utf8_string_size_from_utf16_stream( |
543 | 105 | utf16_stream, |
544 | 105 | utf16_stream_size, |
545 | 105 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
546 | 105 | &safe_utf8_string_size, |
547 | 105 | error ) != 1 ) |
548 | 0 | { |
549 | 0 | libcerror_error_set( |
550 | 0 | error, |
551 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
552 | 0 | LIBCERROR_CONVERSION_ERROR_GENERIC, |
553 | 0 | "%s: unable to set UTF-8 string.", |
554 | 0 | function ); |
555 | |
|
556 | 0 | return( -1 ); |
557 | 0 | } |
558 | 105 | if( safe_utf8_string_size > line_reader->utf8_string_size ) |
559 | 0 | { |
560 | 0 | libcerror_error_set( |
561 | 0 | error, |
562 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
563 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
564 | 0 | "%s: invalid UTF-8 string size value out of bounds.", |
565 | 0 | function ); |
566 | |
|
567 | 0 | return( -1 ); |
568 | 0 | } |
569 | 105 | if( libuna_utf8_string_copy_from_utf16_stream( |
570 | 105 | line_reader->utf8_string, |
571 | 105 | safe_utf8_string_size, |
572 | 105 | utf16_stream, |
573 | 105 | utf16_stream_size, |
574 | 105 | LIBUNA_ENDIAN_LITTLE | LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE, |
575 | 105 | error ) != 1 ) |
576 | 0 | { |
577 | 0 | libcerror_error_set( |
578 | 0 | error, |
579 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
580 | 0 | LIBCERROR_CONVERSION_ERROR_GENERIC, |
581 | 0 | "%s: unable to set UTF-8 string.", |
582 | 0 | function ); |
583 | |
|
584 | 0 | return( -1 ); |
585 | 0 | } |
586 | 105 | } |
587 | 107 | *utf8_string = line_reader->utf8_string; |
588 | 107 | *utf8_string_size = safe_utf8_string_size; |
589 | | |
590 | 107 | return( 1 ); |
591 | 107 | } |
592 | | |
593 | | /* Finalizes the line reader |
594 | | * Returns 1 if successful or -1 on error |
595 | | */ |
596 | | int libewf_line_reader_finalize( |
597 | | libewf_line_reader_t *line_reader, |
598 | | libcerror_error_t **error ) |
599 | 0 | { |
600 | 0 | uint8_t calculated_md5_hash[ LIBHMAC_MD5_HASH_SIZE ]; |
601 | |
|
602 | 0 | static char *function = "libewf_line_reader_finalize"; |
603 | 0 | size_t read_size = 0; |
604 | 0 | ssize_t read_count = 0; |
605 | |
|
606 | 0 | if( line_reader == NULL ) |
607 | 0 | { |
608 | 0 | libcerror_error_set( |
609 | 0 | error, |
610 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
611 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
612 | 0 | "%s: invalid line reader.", |
613 | 0 | function ); |
614 | |
|
615 | 0 | return( -1 ); |
616 | 0 | } |
617 | 0 | while( (size64_t) line_reader->stream_offset < line_reader->stream_size ) |
618 | 0 | { |
619 | 0 | read_size = line_reader->buffer_size; |
620 | |
|
621 | 0 | if( read_size > ( line_reader->stream_size - line_reader->stream_offset ) ) |
622 | 0 | { |
623 | 0 | read_size = (size_t) ( line_reader->stream_size - line_reader->stream_offset ); |
624 | 0 | } |
625 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
626 | 0 | line_reader->data_stream, |
627 | 0 | (intptr_t *) line_reader->file_io_pool, |
628 | 0 | line_reader->buffer, |
629 | 0 | read_size, |
630 | 0 | line_reader->stream_offset, |
631 | 0 | 0, |
632 | 0 | error ); |
633 | |
|
634 | 0 | if( read_count != (ssize_t) read_size ) |
635 | 0 | { |
636 | 0 | libcerror_error_set( |
637 | 0 | error, |
638 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
639 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
640 | 0 | "%s: unable to read section data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
641 | 0 | function, |
642 | 0 | line_reader->stream_offset, |
643 | 0 | line_reader->stream_offset ); |
644 | |
|
645 | 0 | return( -1 ); |
646 | 0 | } |
647 | 0 | line_reader->stream_offset += read_count; |
648 | |
|
649 | 0 | if( libhmac_md5_update( |
650 | 0 | line_reader->md5_context, |
651 | 0 | line_reader->buffer, |
652 | 0 | read_size, |
653 | 0 | error ) != 1 ) |
654 | 0 | { |
655 | 0 | libcerror_error_set( |
656 | 0 | error, |
657 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
658 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
659 | 0 | "%s: unable to update MD5 digest hash.", |
660 | 0 | function ); |
661 | |
|
662 | 0 | return( -1 ); |
663 | 0 | } |
664 | 0 | } |
665 | 0 | if( libhmac_md5_finalize( |
666 | 0 | line_reader->md5_context, |
667 | 0 | calculated_md5_hash, |
668 | 0 | LIBHMAC_MD5_HASH_SIZE, |
669 | 0 | error ) != 1 ) |
670 | 0 | { |
671 | 0 | libcerror_error_set( |
672 | 0 | error, |
673 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
674 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
675 | 0 | "%s: unable to finalize MD5 hash.", |
676 | 0 | function ); |
677 | |
|
678 | 0 | return( -1 ); |
679 | 0 | } |
680 | | #if defined( HAVE_DEBUG_OUTPUT ) |
681 | | if( libcnotify_verbose != 0 ) |
682 | | { |
683 | | libcnotify_printf( |
684 | | "%s: calculated MD5 hash:\n", |
685 | | function ); |
686 | | libcnotify_print_data( |
687 | | calculated_md5_hash, |
688 | | 16, |
689 | | 0 ); |
690 | | } |
691 | | #endif |
692 | | /* TODO |
693 | | if( memory_compare( |
694 | | stored_md5_hash, |
695 | | calculated_md5_hash, |
696 | | 16 ) != 0 ) |
697 | | { |
698 | | libcerror_error_set( |
699 | | error, |
700 | | LIBCERROR_ERROR_DOMAIN_INPUT, |
701 | | LIBCERROR_INPUT_ERROR_VALUE_MISMATCH, |
702 | | "%s: mismatch in integrity hash.", |
703 | | function ); |
704 | | |
705 | | return( -1 ); |
706 | | } |
707 | | */ |
708 | 0 | return( 1 ); |
709 | 0 | } |
710 | | |