/src/libfsext/libfsext/libfsext_block_data.c
Line | Count | Source |
1 | | /* |
2 | | * Block functions |
3 | | * |
4 | | * Copyright (C) 2010-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 <types.h> |
24 | | |
25 | | #include "libfsext_libcerror.h" |
26 | | #include "libfsext_types.h" |
27 | | |
28 | | /* Checks if a buffer block data is filled with 0-byte values (empty-block) |
29 | | * Returns 1 if empty, 0 if not or -1 on error |
30 | | */ |
31 | | int libfsext_block_data_check_empty( |
32 | | const uint8_t *data, |
33 | | size_t data_size, |
34 | | libcerror_error_t **error ) |
35 | 4.63k | { |
36 | 4.63k | libfsext_aligned_t *aligned_data_index = NULL; |
37 | 4.63k | libfsext_aligned_t *aligned_data_start = NULL; |
38 | 4.63k | uint8_t *data_index = NULL; |
39 | 4.63k | uint8_t *data_start = NULL; |
40 | 4.63k | static char *function = "libfsext_block_data_check_empty"; |
41 | | |
42 | 4.63k | if( data == NULL ) |
43 | 0 | { |
44 | 0 | libcerror_error_set( |
45 | 0 | error, |
46 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
47 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
48 | 0 | "%s: invalid data.", |
49 | 0 | function ); |
50 | |
|
51 | 0 | return( -1 ); |
52 | 0 | } |
53 | 4.63k | if( data_size > (size_t) SSIZE_MAX ) |
54 | 0 | { |
55 | 0 | libcerror_error_set( |
56 | 0 | error, |
57 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
58 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
59 | 0 | "%s: invalid data size value exceeds maximum.", |
60 | 0 | function ); |
61 | |
|
62 | 0 | return( -1 ); |
63 | 0 | } |
64 | 4.63k | data_start = (uint8_t *) data; |
65 | 4.63k | data_index = (uint8_t *) data + 1; |
66 | 4.63k | data_size -= 1; |
67 | | |
68 | | /* Only optimize for data larger than the alignment |
69 | | */ |
70 | 4.63k | if( data_size > ( 2 * sizeof( libfsext_aligned_t ) ) ) |
71 | 4.63k | { |
72 | | /* Align the data start |
73 | | */ |
74 | 4.63k | while( ( (intptr_t) data_start % sizeof( libfsext_aligned_t ) ) != 0 ) |
75 | 0 | { |
76 | 0 | if( *data_start != *data_index ) |
77 | 0 | { |
78 | 0 | return( 0 ); |
79 | 0 | } |
80 | 0 | data_start += 1; |
81 | 0 | data_index += 1; |
82 | 0 | data_size -= 1; |
83 | 0 | } |
84 | | /* Align the data index |
85 | | */ |
86 | 9.17k | while( ( (intptr_t) data_index % sizeof( libfsext_aligned_t ) ) != 0 ) |
87 | 8.86k | { |
88 | 8.86k | if( *data_start != *data_index ) |
89 | 4.31k | { |
90 | 4.31k | return( 0 ); |
91 | 4.31k | } |
92 | 4.54k | data_index += 1; |
93 | 4.54k | data_size -= 1; |
94 | 4.54k | } |
95 | 314 | aligned_data_start = (libfsext_aligned_t *) data_start; |
96 | 314 | aligned_data_index = (libfsext_aligned_t *) data_index; |
97 | | |
98 | 2.15k | while( data_size > sizeof( libfsext_aligned_t ) ) |
99 | 2.12k | { |
100 | 2.12k | if( *aligned_data_start != *aligned_data_index ) |
101 | 283 | { |
102 | 283 | return( 0 ); |
103 | 283 | } |
104 | 1.84k | aligned_data_index += 1; |
105 | 1.84k | data_size -= sizeof( libfsext_aligned_t ); |
106 | 1.84k | } |
107 | 31 | data_index = (uint8_t *) aligned_data_index; |
108 | 31 | } |
109 | 181 | while( data_size != 0 ) |
110 | 173 | { |
111 | 173 | if( *data_start != *data_index ) |
112 | 23 | { |
113 | 23 | return( 0 ); |
114 | 23 | } |
115 | 150 | data_index += 1; |
116 | 150 | data_size -= 1; |
117 | 150 | } |
118 | 8 | return( 1 ); |
119 | 31 | } |
120 | | |