Coverage Report

Created: 2024-02-25 07:20

/src/libewf/libewf/libewf_hash_sections.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Hash sections functions
3
 *
4
 * Copyright (C) 2006-2023, 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 <narrow_string.h>
25
26
#include "libewf_libcerror.h"
27
#include "libewf_hash_sections.h"
28
#include "libewf_hash_values.h"
29
30
/* Creates hash sections
31
 * Make sure the value hash_sections is referencing, is set to NULL
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libewf_hash_sections_initialize(
35
     libewf_hash_sections_t **hash_sections,
36
     libcerror_error_t **error )
37
3.12k
{
38
3.12k
  static char *function = "libewf_hash_sections_initialize";
39
40
3.12k
  if( hash_sections == 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 hash sections.",
47
0
     function );
48
49
0
    return( -1 );
50
0
  }
51
3.12k
  if( *hash_sections != 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 hash sections value already set.",
58
0
     function );
59
60
0
    return( -1 );
61
0
  }
62
3.12k
  *hash_sections = memory_allocate_structure(
63
3.12k
                    libewf_hash_sections_t );
64
65
3.12k
  if( *hash_sections == NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
70
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
71
0
     "%s: unable to create hash sections.",
72
0
     function );
73
74
0
    goto on_error;
75
0
  }
76
3.12k
  if( memory_set(
77
3.12k
       *hash_sections,
78
3.12k
       0,
79
3.12k
       sizeof( libewf_hash_sections_t ) ) == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
85
0
     "%s: unable to clear hash sections.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
3.12k
  return( 1 );
91
92
0
on_error:
93
0
  if( *hash_sections != NULL )
94
0
  {
95
0
    memory_free(
96
0
     *hash_sections );
97
98
0
    *hash_sections = NULL;
99
0
  }
100
0
  return( -1 );
101
3.12k
}
102
103
/* Frees hash sections
104
 * Returns 1 if successful or -1 on error
105
 */
106
int libewf_hash_sections_free(
107
     libewf_hash_sections_t **hash_sections,
108
     libcerror_error_t **error )
109
3.12k
{
110
3.12k
        static char *function = "libewf_hash_sections_free";
111
112
3.12k
  if( hash_sections == NULL )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
117
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
118
0
     "%s: invalid hash sections.",
119
0
     function );
120
121
0
    return( -1 );
122
0
  }
123
3.12k
  if( *hash_sections != NULL )
124
3.12k
  {
125
3.12k
    if( ( *hash_sections )->xhash != NULL )
126
0
    {
127
0
      memory_free(
128
0
       ( *hash_sections )->xhash );
129
0
    }
130
3.12k
    memory_free(
131
3.12k
     *hash_sections );
132
133
3.12k
    *hash_sections = NULL;
134
3.12k
  }
135
3.12k
  return( 1 );
136
3.12k
}
137
138
/* Clones the hash sections
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libewf_hash_sections_clone(
142
     libewf_hash_sections_t **destination_hash_sections,
143
     libewf_hash_sections_t *source_hash_sections,
144
     libcerror_error_t **error )
145
0
{
146
0
  static char *function = "libewf_hash_sections_clone";
147
148
0
  if( destination_hash_sections == NULL )
149
0
  {
150
0
    libcerror_error_set(
151
0
     error,
152
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
153
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
154
0
     "%s: invalid destination hash sections.",
155
0
     function );
156
157
0
    return( -1 );
158
0
  }
159
0
  if( *destination_hash_sections != NULL )
160
0
  {
161
0
    libcerror_error_set(
162
0
     error,
163
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
164
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
165
0
     "%s: invalid destination hash sections already set.",
166
0
     function );
167
168
0
    return( -1 );
169
0
  }
170
0
  if( source_hash_sections == NULL )
171
0
  {
172
0
    *destination_hash_sections = NULL;
173
174
0
    return( 1 );
175
0
  }
176
0
  *destination_hash_sections = memory_allocate_structure(
177
0
                                libewf_hash_sections_t );
178
179
0
  if( *destination_hash_sections == NULL )
180
0
  {
181
0
    libcerror_error_set(
182
0
     error,
183
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
184
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
185
0
     "%s: unable to create destination hash sections.",
186
0
     function );
187
188
0
    goto on_error;
189
0
  }
190
0
  if( memory_copy(
191
0
       *destination_hash_sections,
192
0
       source_hash_sections,
193
0
       sizeof( libewf_hash_sections_t ) ) == NULL )
194
0
  {
195
0
    libcerror_error_set(
196
0
     error,
197
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
198
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
199
0
     "%s: unable to copy source to destination hash sections.",
200
0
     function );
201
202
0
    memory_free(
203
0
     *destination_hash_sections );
204
205
0
    *destination_hash_sections = NULL;
206
207
0
    return( -1 );
208
0
  }
209
0
  ( *destination_hash_sections )->xhash      = NULL;
210
0
  ( *destination_hash_sections )->xhash_size = 0;
211
212
0
  if( source_hash_sections->xhash != NULL )
213
0
  {
214
0
    ( *destination_hash_sections )->xhash = (uint8_t *) memory_allocate(
215
0
                                                         sizeof( uint8_t ) * source_hash_sections->xhash_size );
216
217
0
    if( ( *destination_hash_sections )->xhash == NULL )
218
0
    {
219
0
      libcerror_error_set(
220
0
       error,
221
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
222
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
223
0
       "%s: unable to create destination xhash.",
224
0
       function );
225
226
0
      goto on_error;
227
0
    }
228
0
    if( memory_copy(
229
0
         ( *destination_hash_sections )->xhash,
230
0
         source_hash_sections->xhash,
231
0
         sizeof( uint8_t ) * source_hash_sections->xhash_size ) == NULL )
232
0
    {
233
0
      libcerror_error_set(
234
0
       error,
235
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
236
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
237
0
       "%s: unable to copy source to destination xhash.",
238
0
       function );
239
240
0
      goto on_error;
241
0
    }
242
0
    ( *destination_hash_sections )->xhash_size = source_hash_sections->xhash_size;
243
0
  }
244
0
  return( 1 );
245
246
0
on_error:
247
0
  if( *destination_hash_sections != NULL )
248
0
  {
249
0
    if( ( *destination_hash_sections )->xhash != NULL )
250
0
    {
251
0
      memory_free(
252
0
       ( *destination_hash_sections )->xhash );
253
0
    }
254
0
    memory_free(
255
0
     *destination_hash_sections );
256
257
0
    *destination_hash_sections = NULL;
258
0
  }
259
0
  return( -1 );
260
0
}
261
262
/* Sets the digest specified by the identifier from the hash values
263
 * Returns 1 if successful or -1 on error
264
 */
265
int libewf_hash_sections_set_digest_from_hash_values(
266
     libewf_hash_sections_t *hash_sections,
267
     const uint8_t *identifier,
268
     size_t identifier_length,
269
     libfvalue_table_t *hash_values,
270
     libcerror_error_t **error )
271
0
{
272
0
  static char *function = "libewf_hash_sections_set_digest_from_hash_values";
273
274
0
  if( hash_sections == NULL )
275
0
  {
276
0
    libcerror_error_set(
277
0
     error,
278
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
279
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
280
0
     "%s: invalid hash sections.",
281
0
     function );
282
283
0
    return( -1 );
284
0
  }
285
0
  if( ( identifier_length == 3 )
286
0
   && ( narrow_string_compare(
287
0
         (char *) identifier,
288
0
         "MD5",
289
0
         identifier_length ) == 0 ) )
290
0
  {
291
0
    if( libewf_hash_values_generate_md5_hash(
292
0
         hash_values,
293
0
         hash_sections->md5_hash,
294
0
         16,
295
0
         &( hash_sections->md5_hash_set ),
296
0
         error ) != 1 )
297
0
    {
298
0
      libcerror_error_set(
299
0
       error,
300
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
301
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
302
0
       "%s: unable to parse MD5 hash value for its value.",
303
0
       function );
304
305
0
      return( -1 );
306
0
    }
307
0
    if( libewf_hash_values_generate_md5_hash(
308
0
         hash_values,
309
0
         hash_sections->md5_digest,
310
0
         16,
311
0
         &( hash_sections->md5_digest_set ),
312
0
         error ) != 1 )
313
0
    {
314
0
      libcerror_error_set(
315
0
       error,
316
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
317
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
318
0
       "%s: unable to parse MD5 hash value for its value.",
319
0
       function );
320
321
0
      return( -1 );
322
0
    }
323
0
  }
324
0
  else if( ( identifier_length == 4 )
325
0
        && ( narrow_string_compare(
326
0
        (char *) identifier,
327
0
        "SHA1",
328
0
        identifier_length ) == 0 ) )
329
0
  {
330
0
    if( libewf_hash_values_generate_sha1_hash(
331
0
         hash_values,
332
0
         hash_sections->sha1_hash,
333
0
         20,
334
0
         &( hash_sections->sha1_hash_set ),
335
0
         error ) != 1 )
336
0
    {
337
0
      libcerror_error_set(
338
0
       error,
339
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
340
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
341
0
       "%s: unable to parse SHA1 hash value for its value.",
342
0
       function );
343
344
0
      return( -1 );
345
0
    }
346
0
    if( libewf_hash_values_generate_sha1_hash(
347
0
         hash_values,
348
0
         hash_sections->sha1_digest,
349
0
         20,
350
0
         &( hash_sections->sha1_digest_set ),
351
0
         error ) != 1 )
352
0
    {
353
0
      libcerror_error_set(
354
0
       error,
355
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
356
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
357
0
       "%s: unable to parse SHA1 hash value for its value.",
358
0
       function );
359
360
0
      return( -1 );
361
0
    }
362
0
  }
363
0
  return( 1 );
364
0
}
365