1 /*
2 * This file is part of John the Ripper password cracker,
3 * Copyright (c) 1996-99,2015 by Solar Designer
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted.
7 *
8 * There's ABSOLUTELY NO WARRANTY, express or implied.
9 */
10
11 #include <string.h>
12
13 #include "arch.h"
14 #include "common.h"
15 #include "misc.h"
16 #include "base64_convert.h"
17
18 /* This is the base64 that is used in crypt(3). It differs from MIME Base64
19 and the latter can be found in base64.[ch] */
20 const char itoa64[64] = BASE64_CRYPT;
21 unsigned char atoi64[0x100];
22 const char itoa16[16] = HEXCHARS_lc;
23 const char itoa16u[16] = HEXCHARS_uc;
24
25 unsigned char atoi16[0x100], atoi16l[0x100], atoi16u[0x100];
26
27 static int initialized = 0;
28
common_init(void)29 void common_init(void)
30 {
31 const char *pos;
32
33 if (initialized) return;
34
35 memset(atoi64, 0x7F, sizeof(atoi64));
36 for (pos = itoa64; pos <= &itoa64[63]; pos++)
37 atoi64[ARCH_INDEX(*pos)] = pos - itoa64;
38
39 memset(atoi16, 0x7F, sizeof(atoi16));
40 for (pos = itoa16; pos <= &itoa16[15]; pos++)
41 atoi16[ARCH_INDEX(*pos)] = pos - itoa16;
42
43 memcpy(atoi16l, atoi16, sizeof(atoi16l));
44
45 atoi16['A'] = atoi16['a'];
46 atoi16['B'] = atoi16['b'];
47 atoi16['C'] = atoi16['c'];
48 atoi16['D'] = atoi16['d'];
49 atoi16['E'] = atoi16['e'];
50 atoi16['F'] = atoi16['f'];
51
52 memset(atoi16u, 0x7F, sizeof(atoi16u));
53 for (pos = itoa16u; pos <= &itoa16u[15]; pos++)
54 atoi16u[ARCH_INDEX(*pos)] = pos - itoa16u;
55
56 initialized = 1;
57 }
58
ishex(const char * q)59 int ishex(const char *q)
60 {
61 const char *p=q;
62
63 if (!q || !*q)
64 return 0;
65 while (atoi16[ARCH_INDEX(*q)] != 0x7F)
66 ++q;
67 return !*q && !(((q-p))&1);
68 }
ishex_oddOK(const char * q)69 int ishex_oddOK(const char *q)
70 {
71 // Sometimes it is 'ok' to have odd length hex. Usually not. If odd is
72 // allowed, then the format will have to properly handle odd length.
73 if (!q || !*q)
74 return 0;
75 while (atoi16[ARCH_INDEX(*q)] != 0x7F)
76 ++q;
77 return !*q;
78 }
79
ishexuc(const char * q)80 int ishexuc(const char *q)
81 {
82 const char *p=q;
83
84 if (!q || !*q)
85 return 0;
86 while (atoi16u[ARCH_INDEX(*q)] != 0x7F)
87 ++q;
88 return !*q && !(((p-q))&1);
89 }
90
ishexlc(const char * q)91 int ishexlc(const char *q)
92 {
93 const char *p=q;
94
95 if (!q || !*q)
96 return 0;
97 while (atoi16l[ARCH_INDEX(*q)] != 0x7F)
98 ++q;
99 return !*q && !(((p-q))&1);
100 }
101
ishexn(const char * q,int n)102 int ishexn(const char *q, int n)
103 {
104 const char *p=q;
105
106 if (!q || !*q)
107 return 0;
108 while (atoi16[ARCH_INDEX(*q)] != 0x7F)
109 ++q;
110 return (q-p) >= n;
111 }
112
ishexucn(const char * q,int n)113 int ishexucn(const char *q, int n)
114 {
115 const char *p=q;
116
117 if (!q || !*q)
118 return 0;
119 while (atoi16u[ARCH_INDEX(*q)] != 0x7F)
120 ++q;
121 return (q-p) >= n;
122 }
123
ishexlcn(const char * q,int n)124 int ishexlcn(const char *q, int n)
125 {
126 const char *p=q;
127
128 if (!q || !*q)
129 return 0;
130 while (atoi16l[ARCH_INDEX(*q)] != 0x7F)
131 ++q;
132 return (q-p) >= n;
133 }
134
ishexuc_oddOK(const char * q)135 int ishexuc_oddOK(const char *q)
136 {
137 if (!q || !*q)
138 return 0;
139 while (atoi16[ARCH_INDEX(*q)] != 0x7F) {
140 if (*q >= 'a' && *q <= 'f')
141 return 0;
142 ++q;
143 }
144 return !*q ;
145 }
146
ishexlc_oddOK(const char * q)147 int ishexlc_oddOK(const char *q)
148 {
149 if (!q || !*q)
150 return 0;
151 while (atoi16[ARCH_INDEX(*q)] != 0x7F) {
152 if (*q >= 'A' && *q <= 'F')
153 return 0;
154 ++q;
155 }
156 return !*q ;
157 }
158
_hexlen(const char * q,unsigned char dic[0x100],int * extra_chars)159 static MAYBE_INLINE size_t _hexlen(const char *q, unsigned char dic[0x100], int *extra_chars)
160 {
161 const char *s = q;
162 size_t len = strlen(q);
163
164 if (len&1) --len;
165
166 while (dic[ARCH_INDEX(*q)] != 0x7F)
167 ++q;
168 if ((size_t)(q - s)&1) --q;
169 if (extra_chars)
170 *extra_chars = (*q != 0);
171 return (q - s);
172 }
173
hexlen(const char * q,int * extra_chars)174 size_t hexlen(const char *q, int *extra_chars)
175 {
176 if (!q || !*q)
177 return 0;
178 return _hexlen(q, atoi16, extra_chars);
179 }
180
hexlenu(const char * q,int * extra_chars)181 size_t hexlenu(const char *q, int *extra_chars)
182 {
183 if (!q || !*q)
184 return 0;
185 return _hexlen(q, atoi16u, extra_chars);
186 }
187
hexlenl(const char * q,int * extra_chars)188 size_t hexlenl(const char *q, int *extra_chars)
189 {
190 if (!q || !*q)
191 return 0;
192 return _hexlen(q, atoi16l, extra_chars);
193 }
194
isdec_len(const char * q,const char * mxv)195 static int isdec_len(const char *q, const char *mxv)
196 {
197 const char *p = q;
198
199 if (!q || !*q)
200 return 0;
201 do {
202 if (*p < '0' || *p > '9' || p - q >= 10)
203 return 0;
204 } while (*++p);
205 return p - q < 10 || strcmp(q, mxv) <= 0;
206 }
207
isdec(const char * q)208 int isdec(const char *q)
209 {
210 if (!q || !*q)
211 return 0;
212 return isdec_len(q, "2147483647");
213 }
214
isdec_negok(const char * q)215 int isdec_negok(const char *q)
216 {
217 if (!q || !*q)
218 return 0;
219 return *q == '-' ? isdec_len(q + 1, "2147483648") : isdec(q);
220 }
221
isdecu(const char * q)222 int isdecu(const char *q)
223 {
224 if (!q || !*q)
225 return 0;
226 return isdec_len(q, "4294967295");
227 }
228