1+ /* ******************************************************************************
2+ * (c) 2025 Zondax AG
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ ********************************************************************************/
16+
17+ #include < cassert>
18+ #include < cstddef>
19+ #include < cstdint>
20+ #include < cstring>
21+
22+ #ifdef __cplusplus
23+ extern " C" {
24+ #endif
25+
26+ #include " bignum.h"
27+
28+ #ifdef __cplusplus
29+ }
30+ #endif
31+
32+ extern " C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) {
33+ if (size < 2 ) {
34+ return 0 ;
35+ }
36+
37+ // Test bignumLittleEndian_to_bcd and bignumLittleEndian_bcdprint
38+ uint8_t bcd_buffer[128 ];
39+ char print_buffer[256 ];
40+
41+ // Limit input size to avoid too large allocations
42+ const size_t input_size = (size > 64 ) ? 64 : size;
43+
44+ // Test little endian BCD conversion
45+ memset (bcd_buffer, 0 , sizeof (bcd_buffer));
46+ bignumLittleEndian_to_bcd (bcd_buffer, sizeof (bcd_buffer), data, input_size);
47+
48+ // Test BCD to string printing
49+ bignumLittleEndian_bcdprint (print_buffer, sizeof (print_buffer), bcd_buffer, sizeof (bcd_buffer));
50+
51+ // Test big endian BCD conversion
52+ memset (bcd_buffer, 0 , sizeof (bcd_buffer));
53+ bignumBigEndian_to_bcd (bcd_buffer, sizeof (bcd_buffer), data, input_size);
54+
55+ // Test BCD to string printing for big endian
56+ bignumBigEndian_bcdprint (print_buffer, sizeof (print_buffer), bcd_buffer, sizeof (bcd_buffer));
57+
58+ // Test with various buffer sizes
59+ if (size > 4 ) {
60+ const uint16_t bcd_len = (data[0 ] % 64 ) + 1 ;
61+ const uint16_t out_len = (data[1 ] % 128 ) + 1 ;
62+
63+ if (bcd_len <= sizeof (bcd_buffer) && out_len <= sizeof (print_buffer)) {
64+ memset (bcd_buffer, 0 , bcd_len);
65+ bignumLittleEndian_to_bcd (bcd_buffer, bcd_len, data + 2 , input_size - 2 );
66+ bignumLittleEndian_bcdprint (print_buffer, out_len, bcd_buffer, bcd_len);
67+
68+ memset (bcd_buffer, 0 , bcd_len);
69+ bignumBigEndian_to_bcd (bcd_buffer, bcd_len, data + 2 , input_size - 2 );
70+ bignumBigEndian_bcdprint (print_buffer, out_len, bcd_buffer, bcd_len);
71+ }
72+ }
73+
74+ return 0 ;
75+ }
0 commit comments