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