xref: /freebsd/contrib/libarchive/libarchive/test/test_fuzz_consumer.h (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2025 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 /*
27  * C99 equivalent of the DataConsumer class used by the C++ fuzzers.
28  * Consumes bytes sequentially from a buffer, matching the fuzzer protocol.
29  * Not all consumers use every function, so suppress unused warnings.
30  */
31 #ifndef TEST_FUZZ_CONSUMER_H
32 #define TEST_FUZZ_CONSUMER_H
33 
34 #include <stdint.h>
35 #include <stddef.h>
36 #include <string.h>
37 
38 #if defined(__GNUC__) || defined(__clang__)
39 #define FUZZ_UNUSED __attribute__((unused))
40 #else
41 #define FUZZ_UNUSED
42 #endif
43 
44 struct fuzz_consumer {
45 	const uint8_t *data;
46 	size_t size;
47 	size_t pos;
48 	char string_buf[512];
49 };
50 
51 FUZZ_UNUSED static void
fuzz_consumer_init(struct fuzz_consumer * c,const uint8_t * data,size_t size)52 fuzz_consumer_init(struct fuzz_consumer *c, const uint8_t *data, size_t size)
53 {
54 	c->data = data;
55 	c->size = size;
56 	c->pos = 0;
57 }
58 
59 FUZZ_UNUSED static size_t
fuzz_consumer_remaining(const struct fuzz_consumer * c)60 fuzz_consumer_remaining(const struct fuzz_consumer *c)
61 {
62 	return c->size - c->pos;
63 }
64 
65 FUZZ_UNUSED static uint8_t
fuzz_consume_byte(struct fuzz_consumer * c)66 fuzz_consume_byte(struct fuzz_consumer *c)
67 {
68 	if (c->pos >= c->size)
69 		return 0;
70 	return c->data[c->pos++];
71 }
72 
73 FUZZ_UNUSED static uint16_t
fuzz_consume_u16(struct fuzz_consumer * c)74 fuzz_consume_u16(struct fuzz_consumer *c)
75 {
76 	uint16_t val = 0;
77 	if (c->pos + 2 <= c->size) {
78 		val = (uint16_t)c->data[c->pos]
79 		    | ((uint16_t)c->data[c->pos + 1] << 8);
80 		c->pos += 2;
81 	}
82 	return val;
83 }
84 
85 FUZZ_UNUSED static uint32_t
fuzz_consume_u32(struct fuzz_consumer * c)86 fuzz_consume_u32(struct fuzz_consumer *c)
87 {
88 	uint32_t val = 0;
89 	if (c->pos + 4 <= c->size) {
90 		val = (uint32_t)c->data[c->pos]
91 		    | ((uint32_t)c->data[c->pos + 1] << 8)
92 		    | ((uint32_t)c->data[c->pos + 2] << 16)
93 		    | ((uint32_t)c->data[c->pos + 3] << 24);
94 		c->pos += 4;
95 	}
96 	return val;
97 }
98 
99 FUZZ_UNUSED static int64_t
fuzz_consume_i64(struct fuzz_consumer * c)100 fuzz_consume_i64(struct fuzz_consumer *c)
101 {
102 	uint64_t val = 0;
103 	if (c->pos + 8 <= c->size) {
104 		int i;
105 		for (i = 0; i < 8; i++)
106 			val |= (uint64_t)c->data[c->pos + i] << (8 * i);
107 		c->pos += 8;
108 	}
109 	return (int64_t)val;
110 }
111 
112 FUZZ_UNUSED static const char *
fuzz_consume_string(struct fuzz_consumer * c,size_t max_len)113 fuzz_consume_string(struct fuzz_consumer *c, size_t max_len)
114 {
115 	size_t avail, len, actual_len;
116 	if (max_len > sizeof(c->string_buf) - 1)
117 		max_len = sizeof(c->string_buf) - 1;
118 	avail = c->size - c->pos;
119 	len = (avail < max_len) ? avail : max_len;
120 	actual_len = 0;
121 	while (actual_len < len && c->pos < c->size) {
122 		char ch = (char)c->data[c->pos++];
123 		if (ch == '\0')
124 			break;
125 		c->string_buf[actual_len++] = ch;
126 	}
127 	c->string_buf[actual_len] = '\0';
128 	return c->string_buf;
129 }
130 
131 FUZZ_UNUSED static size_t
fuzz_consume_bytes(struct fuzz_consumer * c,void * out,size_t len)132 fuzz_consume_bytes(struct fuzz_consumer *c, void *out, size_t len)
133 {
134 	size_t avail = c->size - c->pos;
135 	size_t to_copy = (avail < len) ? avail : len;
136 	if (to_copy > 0) {
137 		memcpy(out, c->data + c->pos, to_copy);
138 		c->pos += to_copy;
139 	}
140 	return to_copy;
141 }
142 
143 #endif /* TEST_FUZZ_CONSUMER_H */
144