/src/libmodi/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libmodi file type |
3 | | * |
4 | | * Copyright (C) 2012-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_libmodi.h" |
31 | | |
32 | | #if !defined( LIBMODI_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 | | LIBMODI_EXTERN \ |
38 | | int libmodi_handle_open_file_io_handle( |
39 | | libmodi_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libmodi_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBMODI_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 4.53k | { |
50 | 4.53k | uint8_t buffer[ 512 ]; |
51 | | |
52 | 4.53k | libbfio_handle_t *file_io_handle = NULL; |
53 | 4.53k | libmodi_handle_t *handle = NULL; |
54 | 4.53k | off64_t media_offset = 0; |
55 | 4.53k | size64_t media_size = 0; |
56 | 4.53k | int read_iterator = 0; |
57 | | |
58 | 4.53k | if( libbfio_memory_range_initialize( |
59 | 4.53k | &file_io_handle, |
60 | 4.53k | NULL ) != 1 ) |
61 | 0 | { |
62 | 0 | return( 0 ); |
63 | 0 | } |
64 | 4.53k | if( libbfio_memory_range_set( |
65 | 4.53k | file_io_handle, |
66 | 4.53k | (uint8_t *) data, |
67 | 4.53k | size, |
68 | 4.53k | NULL ) != 1 ) |
69 | 0 | { |
70 | 0 | goto on_error_libbfio; |
71 | 0 | } |
72 | 4.53k | if( libmodi_handle_initialize( |
73 | 4.53k | &handle, |
74 | 4.53k | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | goto on_error_libbfio; |
77 | 0 | } |
78 | 4.53k | if( libmodi_handle_open_file_io_handle( |
79 | 4.53k | handle, |
80 | 4.53k | file_io_handle, |
81 | 4.53k | LIBMODI_OPEN_READ, |
82 | 4.53k | NULL ) != 1 ) |
83 | 3.48k | { |
84 | 3.48k | goto on_error_libmodi; |
85 | 3.48k | } |
86 | 1.05k | if( libmodi_handle_get_media_size( |
87 | 1.05k | handle, |
88 | 1.05k | &media_size, |
89 | 1.05k | NULL ) != 1 ) |
90 | 0 | { |
91 | 0 | goto on_error_libmodi; |
92 | 0 | } |
93 | 1.05k | for( read_iterator = 0; |
94 | 7.05k | read_iterator < 128; |
95 | 5.99k | read_iterator++ ) |
96 | 7.00k | { |
97 | 7.00k | if( media_offset >= media_size ) |
98 | 864 | { |
99 | 864 | break; |
100 | 864 | } |
101 | 6.14k | if( libmodi_handle_read_buffer_at_offset( |
102 | 6.14k | handle, |
103 | 6.14k | buffer, |
104 | 6.14k | 497, |
105 | 6.14k | media_offset, |
106 | 6.14k | NULL ) == -1 ) |
107 | 149 | { |
108 | 149 | goto on_error_libmodi; |
109 | 149 | } |
110 | 5.99k | media_offset += 497; |
111 | 5.99k | } |
112 | 909 | libmodi_handle_close( |
113 | 909 | handle, |
114 | 909 | NULL ); |
115 | | |
116 | 4.53k | on_error_libmodi: |
117 | 4.53k | libmodi_handle_free( |
118 | 4.53k | &handle, |
119 | 4.53k | NULL ); |
120 | | |
121 | 4.53k | on_error_libbfio: |
122 | 4.53k | libbfio_handle_free( |
123 | 4.53k | &file_io_handle, |
124 | 4.53k | NULL ); |
125 | | |
126 | 4.53k | return( 0 ); |
127 | 4.53k | } |
128 | | |
129 | | } /* extern "C" */ |
130 | | |