/src/libfsntfs/libfsntfs/libfsntfs_compressed_block.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Compressed block functions |
3 | | * |
4 | | * Copyright (C) 2010-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 "libfsntfs_compressed_block.h" |
27 | | #include "libfsntfs_libcerror.h" |
28 | | |
29 | | /* Creates a compressed block |
30 | | * Make sure the value compressed_block is referencing, is set to NULL |
31 | | * Returns 1 if successful or -1 on error |
32 | | */ |
33 | | int libfsntfs_compressed_block_initialize( |
34 | | libfsntfs_compressed_block_t **compressed_block, |
35 | | size_t data_size, |
36 | | libcerror_error_t **error ) |
37 | 12.8k | { |
38 | 12.8k | static char *function = "libfsntfs_compressed_block_initialize"; |
39 | | |
40 | 12.8k | if( compressed_block == NULL ) |
41 | 0 | { |
42 | 0 | libcerror_error_set( |
43 | 0 | error, |
44 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
45 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
46 | 0 | "%s: invalid compressed block.", |
47 | 0 | function ); |
48 | |
|
49 | 0 | return( -1 ); |
50 | 0 | } |
51 | 12.8k | if( *compressed_block != NULL ) |
52 | 0 | { |
53 | 0 | libcerror_error_set( |
54 | 0 | error, |
55 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
56 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
57 | 0 | "%s: invalid compressed block value already set.", |
58 | 0 | function ); |
59 | |
|
60 | 0 | return( -1 ); |
61 | 0 | } |
62 | 12.8k | if( ( data_size == 0 ) |
63 | 12.8k | || ( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
64 | 23 | { |
65 | 23 | libcerror_error_set( |
66 | 23 | error, |
67 | 23 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
68 | 23 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
69 | 23 | "%s: invalid data size value out of bounds.", |
70 | 23 | function ); |
71 | | |
72 | 23 | return( -1 ); |
73 | 23 | } |
74 | 12.8k | *compressed_block = memory_allocate_structure( |
75 | 12.8k | libfsntfs_compressed_block_t ); |
76 | | |
77 | 12.8k | if( *compressed_block == NULL ) |
78 | 0 | { |
79 | 0 | libcerror_error_set( |
80 | 0 | error, |
81 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
82 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
83 | 0 | "%s: unable to create compressed block.", |
84 | 0 | function ); |
85 | |
|
86 | 0 | goto on_error; |
87 | 0 | } |
88 | 12.8k | if( memory_set( |
89 | 12.8k | *compressed_block, |
90 | 12.8k | 0, |
91 | 12.8k | sizeof( libfsntfs_compressed_block_t ) ) == NULL ) |
92 | 0 | { |
93 | 0 | libcerror_error_set( |
94 | 0 | error, |
95 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
96 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
97 | 0 | "%s: unable to clear compressed block.", |
98 | 0 | function ); |
99 | |
|
100 | 0 | memory_free( |
101 | 0 | *compressed_block ); |
102 | |
|
103 | 0 | *compressed_block = NULL; |
104 | |
|
105 | 0 | return( -1 ); |
106 | 0 | } |
107 | 12.8k | ( *compressed_block )->data = (uint8_t *) memory_allocate( |
108 | 12.8k | sizeof( uint8_t ) * data_size ); |
109 | | |
110 | 12.8k | if( ( *compressed_block )->data == NULL ) |
111 | 0 | { |
112 | 0 | libcerror_error_set( |
113 | 0 | error, |
114 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
115 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
116 | 0 | "%s: unable to create data.", |
117 | 0 | function ); |
118 | |
|
119 | 0 | goto on_error; |
120 | 0 | } |
121 | 12.8k | ( *compressed_block )->data_size = data_size; |
122 | | |
123 | 12.8k | return( 1 ); |
124 | | |
125 | 0 | on_error: |
126 | 0 | if( *compressed_block != NULL ) |
127 | 0 | { |
128 | 0 | memory_free( |
129 | 0 | *compressed_block ); |
130 | |
|
131 | 0 | *compressed_block = NULL; |
132 | 0 | } |
133 | 0 | return( -1 ); |
134 | 12.8k | } |
135 | | |
136 | | /* Frees a compressed block |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libfsntfs_compressed_block_free( |
140 | | libfsntfs_compressed_block_t **compressed_block, |
141 | | libcerror_error_t **error ) |
142 | 12.8k | { |
143 | 12.8k | static char *function = "libfsntfs_compressed_block_free"; |
144 | | |
145 | 12.8k | if( compressed_block == NULL ) |
146 | 0 | { |
147 | 0 | libcerror_error_set( |
148 | 0 | error, |
149 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
150 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
151 | 0 | "%s: invalid compressed block.", |
152 | 0 | function ); |
153 | |
|
154 | 0 | return( -1 ); |
155 | 0 | } |
156 | 12.8k | if( *compressed_block != NULL ) |
157 | 12.8k | { |
158 | 12.8k | if( ( *compressed_block )->data != NULL ) |
159 | 12.8k | { |
160 | 12.8k | memory_free( |
161 | 12.8k | ( *compressed_block )->data ); |
162 | 12.8k | } |
163 | 12.8k | memory_free( |
164 | 12.8k | *compressed_block ); |
165 | | |
166 | 12.8k | *compressed_block = NULL; |
167 | 12.8k | } |
168 | 12.8k | return( 1 ); |
169 | 12.8k | } |
170 | | |