-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneral.c
259 lines (206 loc) · 5.42 KB
/
general.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
11/21/2016
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/md4.h>
#include <openssl/mdc2.h>
#include <openssl/ripemd.h>
#include <openssl/whrlpool.h>
#include "gawkapi.h"
#include "general.h"
static const gawk_api_t *api;
static awk_ext_id_t *ext_id;
static const char *ext_version = "general extension: version 1.0";
static awk_bool_t (*init_func)(void) = NULL;
int plugin_is_GPL_compatible;
static awk_value_t *
do_repeat(int nargs, awk_value_t *result) {
awk_value_t arg1, arg2;
char *buf = NULL, *src = NULL, *ptr = NULL;
size_t x = 0, len = 0, len2 = 0, unum = 0;
static const size_t hard_limit = 2000;
if (!(get_argument(0, AWK_STRING, &arg1))) {
goto out;
}
if (!(get_argument(1, AWK_NUMBER, &arg2))) {
goto out;
}
src = arg1.str_value.str;
if (!*src) {
goto out;
}
len = strlen(src);
unum = (size_t)arg2.num_value;
if (0 == unum || hard_limit < unum || 50 < len) {
goto out;
}
buf = (char *)malloc((len * sizeof(char *)) * unum);
if (NULL == buf) {
goto out;
}
ptr = buf;
for (; x <= unum; x++, ptr += len, len2 += len) {
memcpy(ptr, src, len);
}
buf[len2] = '\0';
make_const_string(buf, len2, result);
free(buf);
out:
return result;
}
static awk_value_t *
do_basename(int nargs, awk_value_t *result) {
awk_value_t arg1;
char *temp = NULL, buf[500];
char *srcptr = NULL, *temp2 = NULL;
size_t len = 0;
if (!(get_argument(0, AWK_STRING, &arg1))) {
goto out;
}
temp = arg1.str_value.str;
if (!*temp) {
goto out;
}
temp2 = arg1.str_value.str;
if (NULL == (srcptr = strrchr(temp, '/'))) {
goto nothing_found;
}
if (!*(srcptr+1)) {
if (1 >= (len = arg1.str_value.len)) {
goto nothing_found;
}
temp2[len-1] = '\0';
if (NULL == (srcptr = strrchr(temp2, '/'))) {
goto nothing_found;
}
}
++srcptr;
snprintf(buf, 499, "%s", srcptr);
make_const_string(buf, strlen(buf), result);
out:
return result;
nothing_found:
make_malloced_string(temp, strlen(temp), result);
return result;
}
static awk_value_t *
do_dirname(int nargs, awk_value_t *result) {
awk_value_t arg1;
char *src = NULL, buf[1000];
char *srcptr = NULL, *temp = NULL;
size_t x = 0, cut_here = 0, len = 0;
if (!(get_argument(0, AWK_STRING, &arg1))) {
goto out;
}
src = arg1.str_value.str;
if (!*src) {
goto out;
}
temp = arg1.str_value.str;
len = arg1.str_value.len;
if (1 >= len || 998 < len) {
goto nothing_found;
}
if (NULL == (srcptr = strrchr(src, '/'))) {
goto nothing_found;
}
if (!*(srcptr+1)) {
src[len-1] = '\0';
}
for (; *temp; x++, temp++) {
if ('/' == *temp) {
cut_here = x;
}
}
if (0 == cut_here) {
goto nothing_found;
}
memcpy(buf, src, cut_here);
buf[cut_here] = '\0';
make_const_string(buf, strlen(buf), result);
out:
return result;
nothing_found:
make_malloced_string(src, strlen(src), result);
return result;
}
/*
* For our simple needs we do not need to use the
* "high level" cousins.
* https://www.openssl.org/docs/manmaster/man3/EVP_sha1.html
*/
#define SHA_TEMPLATE(func_name, sha_func) \
static awk_value_t * \
do_##func_name(int nargs, awk_value_t *result) { \
awk_value_t arg1; \
char *src = NULL, buf[300]; \
char *bufptr = buf; \
unsigned char *shasrc = NULL; \
int x = 0; \
if (!(get_argument(0, AWK_STRING, &arg1))) { \
goto out; \
} \
src = arg1.str_value.str; \
if (!*src) { \
goto out; \
} \
shasrc = sha_func((unsigned char *)src, strlen(src), NULL); \
if (NULL == shasrc) { \
goto out; \
} \
for (; x < sha_func##_DIGEST_LENGTH; x++) { \
bufptr += snprintf(bufptr, 299, "%02x", shasrc[x]); \
} \
make_const_string(buf, strlen(buf), result); \
out: \
return result; \
}
SHA_TEMPLATE(sha1, SHA)
SHA_TEMPLATE(sha224, SHA224)
SHA_TEMPLATE(sha256, SHA256)
SHA_TEMPLATE(sha384, SHA384)
SHA_TEMPLATE(sha512, SHA512)
SHA_TEMPLATE(md5, MD5)
SHA_TEMPLATE(md4, MD4)
SHA_TEMPLATE(mdc2, MDC2)
SHA_TEMPLATE(ripemd160, RIPEMD160)
SHA_TEMPLATE(whirlpool, WHIRLPOOL)
static awk_ext_func_t func_table[] = {
{ "repeat", do_repeat, 2 },
{ "basename", do_basename, 1 },
{ "dirname", do_dirname, 1 },
{ "sha1", do_sha1, 1 },
{ "sha224", do_sha224, 1 },
{ "sha256", do_sha256, 1 },
{ "sha384", do_sha384, 1 },
{ "sha512", do_sha512, 1 },
{ "md5", do_md5, 1 },
{ "md4", do_md4, 1 },
{ "mdc2", do_mdc2, 1 },
{ "whirlpool", do_whirlpool, 1 },
{ "ripemd160", do_ripemd160, 1 }
};
/* define the dl_load function using the boilerplate macro */
dl_load_func(func_table, repeat_me, "")