/src/libagdb/ossfuzz/compressed_block_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libagdb compressed block (de)compression |
3 | | * |
4 | | * Copyright (C) 2014-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 <stddef.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | /* Note that some of the OSS-Fuzz engines use C++ |
26 | | */ |
27 | | extern "C" { |
28 | | |
29 | | #include "ossfuzz_libagdb.h" |
30 | | #include "ossfuzz_libbfio.h" |
31 | | |
32 | | #include <libagdb_compressed_block.h> |
33 | | #include <libagdb_io_handle.h> |
34 | | |
35 | | int LLVMFuzzerTestOneInput( |
36 | | const uint8_t *data, |
37 | | size_t size ) |
38 | 803 | { |
39 | 803 | libbfio_handle_t *file_io_handle = NULL; |
40 | 803 | libagdb_compressed_block_t *compressed_block = NULL; |
41 | 803 | libagdb_io_handle_t *io_handle = NULL; |
42 | 803 | uint32_t expected_data_size = 0; |
43 | | |
44 | 803 | if( size < 6 ) |
45 | 4 | { |
46 | 4 | return( 0 ); |
47 | 4 | } |
48 | 799 | if( libagdb_io_handle_initialize( |
49 | 799 | &io_handle, |
50 | 799 | NULL ) != 1 ) |
51 | 0 | { |
52 | 0 | return( 0 ); |
53 | 0 | } |
54 | | /* The first byte of the input selects the compression algorithm: |
55 | | * 0: LZNT1 ( compressed blocks of a Windows Vista AGDB file ) |
56 | | * 1: LZXpress Huffman ( compressed blocks of a Windows 7 AGDB file ) |
57 | | * 2: LZXpress Huffman ( compressed blocks of a Windows 8.0 AGDB file ) |
58 | | * 3: LZXpress Huffman ( compressed blocks of a Windows 8.1 AGDB file ) |
59 | | * The next four bytes are the size of the expected uncompressed data |
60 | | * (little-endian) and the remainder is the compressed block. |
61 | | */ |
62 | 799 | if( data[ 0 ] == 0 ) |
63 | 410 | { |
64 | 410 | io_handle->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_VISTA; |
65 | 410 | } |
66 | 389 | else if( data[ 0 ] == 1 ) |
67 | 158 | { |
68 | 158 | io_handle->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS7; |
69 | 158 | } |
70 | 231 | else if( data[ 0 ] == 2 ) |
71 | 4 | { |
72 | 4 | io_handle->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_0; |
73 | 4 | } |
74 | 227 | else |
75 | 227 | { |
76 | 227 | io_handle->file_type = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8_1; |
77 | 227 | } |
78 | 799 | expected_data_size = (uint32_t) data[ 1 ]; |
79 | 799 | expected_data_size |= ( (uint32_t) data[ 2 ] ) << 8; |
80 | 799 | expected_data_size |= ( (uint32_t) data[ 3 ] ) << 16; |
81 | 799 | expected_data_size |= ( (uint32_t) data[ 4 ] ) << 24; |
82 | | |
83 | | /* Limit the size to keep the allocation size within bounds |
84 | | */ |
85 | 799 | if( expected_data_size > ( 8 * 1024 * 1024 ) ) |
86 | 396 | { |
87 | 396 | expected_data_size = ( 8 * 1024 * 1024 ); |
88 | 396 | } |
89 | 799 | if( libbfio_memory_range_initialize( |
90 | 799 | &file_io_handle, |
91 | 799 | NULL ) != 1 ) |
92 | 0 | { |
93 | 0 | goto on_error_io_handle; |
94 | 0 | } |
95 | 799 | if( libbfio_memory_range_set( |
96 | 799 | file_io_handle, |
97 | 799 | (uint8_t *) data, |
98 | 799 | size, |
99 | 799 | NULL ) != 1 ) |
100 | 0 | { |
101 | 0 | goto on_error_libbfio; |
102 | 0 | } |
103 | 799 | if( libbfio_handle_open( |
104 | 799 | file_io_handle, |
105 | 799 | LIBBFIO_OPEN_READ, |
106 | 799 | NULL ) != 1 ) |
107 | 0 | { |
108 | 0 | goto on_error_libbfio; |
109 | 0 | } |
110 | 799 | if( libagdb_compressed_block_initialize( |
111 | 799 | &compressed_block, |
112 | 799 | expected_data_size, |
113 | 799 | NULL ) != 1 ) |
114 | 3 | { |
115 | 3 | goto on_error_libbfio; |
116 | 3 | } |
117 | 796 | libagdb_compressed_block_read( |
118 | 796 | compressed_block, |
119 | 796 | io_handle, |
120 | 796 | file_io_handle, |
121 | 796 | 5, |
122 | 796 | size - 5, |
123 | 796 | NULL ); |
124 | 796 | libagdb_compressed_block_free( |
125 | 796 | &compressed_block, |
126 | 796 | NULL ); |
127 | | |
128 | 799 | on_error_libbfio: |
129 | 799 | libbfio_handle_free( |
130 | 799 | &file_io_handle, |
131 | 799 | NULL ); |
132 | | |
133 | 799 | on_error_io_handle: |
134 | 799 | libagdb_io_handle_free( |
135 | 799 | &io_handle, |
136 | 799 | NULL ); |
137 | | |
138 | 799 | return( 0 ); |
139 | 799 | } |
140 | | |
141 | | } /* extern "C" */ |