/src/oss-fuzz-example/char_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2022 Ada Logics ltd. |
2 | | * |
3 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | * you may not use this file except in compliance with the License. |
5 | | * You may obtain a copy of the License at |
6 | | * |
7 | | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | * |
9 | | * Unless required by applicable law or agreed to in writing, software |
10 | | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | * See the License for the specific language governing permissions and |
13 | | * limitations under the License. |
14 | | */ |
15 | | #include <stdlib.h> |
16 | | #include <stdio.h> |
17 | | #include <string.h> |
18 | | |
19 | | // Count the number of lowercase letters |
20 | | // input must be a null-terminated string. |
21 | 113 | int count_lowercase_letters(char *input) { |
22 | 113 | int length = 0; |
23 | 113 | int lowercase_letters = 0; |
24 | | |
25 | 7.33M | while (1) { |
26 | 7.33M | if (input[length] == '\0') { |
27 | 113 | break; |
28 | 113 | } |
29 | 7.33M | length++; |
30 | 7.33M | } |
31 | | |
32 | 113 | if (length == 0) { |
33 | 1 | return 0; |
34 | 1 | } |
35 | | |
36 | 7.33M | for (int i = 0; i < length; i++) { |
37 | 7.33M | if (input[i] >= 0x61 && input[i] <= 0x7a) { |
38 | 6.28M | lowercase_letters++; |
39 | 6.28M | } |
40 | 7.33M | } |
41 | | |
42 | 112 | if (lowercase_letters == 42) { |
43 | 1 | printf("wooo\n"); |
44 | 1 | } |
45 | 112 | if (lowercase_letters == 0x42) { |
46 | 1 | printf("WOOO!\n"); |
47 | 1 | } |
48 | | |
49 | 112 | return lowercase_letters; |
50 | 113 | } |
51 | | |
52 | 0 | int get_the_right_character(char *input, int idx) { |
53 | | // we can't read anything under idx. |
54 | 0 | if (strlen(input) < 40) { |
55 | 0 | return -1; |
56 | 0 | } |
57 | | |
58 | 0 | char c = input[idx]; |
59 | 0 | if (c == 'X') { |
60 | 0 | return 1; |
61 | 0 | } |
62 | 0 | return -1; |
63 | 0 | } |
64 | | |
65 | 0 | int read_key_figures(char *input, size_t half_length) { |
66 | 0 | if (half_length < 10) { |
67 | 0 | return -1; |
68 | 0 | } |
69 | | |
70 | | // We know half_length is under 10, so we can double it no problem. |
71 | 0 | half_length += half_length; |
72 | 0 | return get_the_right_character(input, half_length); |
73 | 0 | } |
74 | | |
75 | 0 | int parse_complex_format(char *input) { |
76 | 0 | size_t length = strlen(input); |
77 | 0 | if (length < 8) { |
78 | 0 | return -1; |
79 | 0 | } |
80 | | |
81 | 0 | if (input[0] != 'F') { |
82 | 0 | return -1; |
83 | 0 | } |
84 | 0 | if (input[1] != 'u') { |
85 | 0 | return -1; |
86 | 0 | } |
87 | 0 | if (input[2] != 'z') { |
88 | 0 | return -1; |
89 | 0 | } |
90 | 0 | if (input[3] != 'z') { |
91 | 0 | return -1; |
92 | 0 | } |
93 | | |
94 | 0 | int sum = 0; |
95 | 0 | for (int i = 0; i < length; i++) { |
96 | 0 | if (input[i] > 0x20) { |
97 | 0 | sum += 1; |
98 | 0 | } |
99 | 0 | } |
100 | |
|
101 | 0 | if (sum < 20) { |
102 | 0 | return read_key_figures(input, length/2); |
103 | 0 | } |
104 | 0 | else { |
105 | 0 | if (sum > 40) { |
106 | 0 | int *magic_ptr = (int*)input; |
107 | 0 | int magic_val1 = magic_ptr[8]; |
108 | 0 | int magic_val2 = magic_ptr[9]; |
109 | 0 | if (magic_val1 == 0x41424344 && magic_val2 == 0x5b5d5b5d) { |
110 | 0 | return 1; |
111 | 0 | } |
112 | 0 | } |
113 | 0 | } |
114 | 0 | return -1; |
115 | 0 | } |
116 | | |
117 | 0 | int parse_complex_format_second(char *input) { |
118 | 0 | size_t length = strlen(input); |
119 | 0 | if (length < 8) { |
120 | 0 | return -1; |
121 | 0 | } |
122 | | |
123 | 0 | if (input[0] != 'F') { |
124 | 0 | return -1; |
125 | 0 | } |
126 | 0 | if (input[1] != 'u') { |
127 | 0 | return -1; |
128 | 0 | } |
129 | 0 | if (input[2] != 'z') { |
130 | 0 | return -1; |
131 | 0 | } |
132 | 0 | if (input[3] != 'z') { |
133 | 0 | return -1; |
134 | 0 | } |
135 | | |
136 | 0 | int sum = 0; |
137 | 0 | for (int i = 0; i < length; i++) { |
138 | 0 | if (input[i] > 0x20) { |
139 | 0 | sum += 1; |
140 | 0 | } |
141 | 0 | } |
142 | |
|
143 | 0 | if (sum < 20) { |
144 | 0 | return read_key_figures(input, (int)((length / 2) -1) ); |
145 | 0 | } |
146 | 0 | else { |
147 | 0 | if (sum > 40) { |
148 | 0 | int *magic_ptr = (int*)input; |
149 | 0 | int magic_val1 = magic_ptr[8]; |
150 | 0 | int magic_val2 = magic_ptr[9]; |
151 | 0 | if (magic_val1 == 0x41424344 && magic_val2 == 0x5b5d5b5d) { |
152 | 0 | return 1; |
153 | 0 | } |
154 | 0 | } |
155 | 0 | } |
156 | 0 | return -1; |
157 | 0 | } |