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