/src/libodraw/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libodraw file type |
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 <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_libbfio.h" |
30 | | #include "ossfuzz_libodraw.h" |
31 | | |
32 | | #if !defined( LIBODRAW_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a handle using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBODRAW_EXTERN \ |
38 | | int libodraw_handle_open_file_io_handle( |
39 | | libodraw_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libodraw_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBODRAW_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 5.29k | { |
50 | 5.29k | uint8_t buffer[ 512 ]; |
51 | | |
52 | 5.29k | libbfio_handle_t *file_io_handle = NULL; |
53 | 5.29k | libbfio_pool_t *file_io_pool = NULL; |
54 | 5.29k | libodraw_handle_t *handle = NULL; |
55 | 5.29k | off64_t media_offset = 0; |
56 | 5.29k | size64_t media_size = 0; |
57 | 5.29k | int read_iterator = 0; |
58 | | |
59 | 5.29k | if( libbfio_memory_range_initialize( |
60 | 5.29k | &file_io_handle, |
61 | 5.29k | NULL ) != 1 ) |
62 | 0 | { |
63 | 0 | return( 0 ); |
64 | 0 | } |
65 | 5.29k | if( libbfio_memory_range_set( |
66 | 5.29k | file_io_handle, |
67 | 5.29k | (uint8_t *) data, |
68 | 5.29k | size, |
69 | 5.29k | NULL ) != 1 ) |
70 | 0 | { |
71 | 0 | goto on_error_libbfio; |
72 | 0 | } |
73 | 5.29k | if( libodraw_handle_initialize( |
74 | 5.29k | &handle, |
75 | 5.29k | NULL ) != 1 ) |
76 | 0 | { |
77 | 0 | goto on_error_libbfio; |
78 | 0 | } |
79 | 5.29k | if( libodraw_handle_open_file_io_handle( |
80 | 5.29k | handle, |
81 | 5.29k | file_io_handle, |
82 | 5.29k | LIBODRAW_OPEN_READ, |
83 | 5.29k | NULL ) != 1 ) |
84 | 3.17k | { |
85 | 3.17k | goto on_error_libodraw; |
86 | 3.17k | } |
87 | 2.11k | if( libodraw_handle_get_media_size( |
88 | 2.11k | handle, |
89 | 2.11k | &media_size, |
90 | 2.11k | NULL ) != 1 ) |
91 | 0 | { |
92 | 0 | goto on_error_libodraw; |
93 | 0 | } |
94 | 2.11k | for( read_iterator = 0; |
95 | 21.6k | read_iterator < 128; |
96 | 19.5k | read_iterator++ ) |
97 | 21.5k | { |
98 | 21.5k | if( media_offset >= media_size ) |
99 | 890 | { |
100 | 890 | break; |
101 | 890 | } |
102 | 20.7k | if( libodraw_handle_read_buffer_at_offset( |
103 | 20.7k | handle, |
104 | 20.7k | buffer, |
105 | 20.7k | 497, |
106 | 20.7k | media_offset, |
107 | 20.7k | NULL ) == -1 ) |
108 | 1.13k | { |
109 | 1.13k | goto on_error_libodraw; |
110 | 1.13k | } |
111 | 19.5k | media_offset += 497; |
112 | 19.5k | } |
113 | 984 | libodraw_handle_close( |
114 | 984 | handle, |
115 | 984 | NULL ); |
116 | | |
117 | 5.29k | on_error_libodraw: |
118 | 5.29k | libodraw_handle_free( |
119 | 5.29k | &handle, |
120 | 5.29k | NULL ); |
121 | | |
122 | 5.29k | on_error_libbfio: |
123 | 5.29k | libbfio_handle_free( |
124 | 5.29k | &file_io_handle, |
125 | 5.29k | NULL ); |
126 | | |
127 | 5.29k | return( 0 ); |
128 | 5.29k | } |
129 | | |
130 | | } /* extern "C" */ |
131 | | |