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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* Bindle Binaries Tools
* Copyright (C) 2022 David M. Syzdek <david@syzdek.net>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of David M. Syzdek nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define _SRC_WIDGET_BASE32_C 1
#include "bindletools.h"
///////////////
// //
// Headers //
// //
///////////////
#pragma mark - Headers
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
///////////////////
// //
// Definitions //
// //
///////////////////
#pragma mark - Definitions
#define BINDLE_BUFF_SIZE (40*3)
#define BINDLE_FLG_ENCODE 0x00001UL
#define BINDLE_FLG_DECODE 0x00002UL
#define BINDLE_FLG_NOPAD 0x00004UL
#define BINDLE_FLG_NEWLINE 0x00008UL
/////////////////
// //
// Variables //
// //
/////////////////
#pragma mark - Variables
#pragma mark bindle_widget_encodings_options[]
const char * const bindle_widget_encodings_options[] =
{
" -d, --decode decode string",
" -e, --encode encode string",
" -i file, --input=file use input file instead of stdin",
" -n, --newline add newline to output",
" -o file, --output=file use output file instead of stdout",
" -p, --nopadding disable padding",
" -s string string to encode or decode",
NULL
};
#pragma mark bindle_fsin
static int bindle_fdin = STDIN_FILENO;
#pragma mark bindle_fsout
static int bindle_fdout = STDOUT_FILENO;
//////////////////
// //
// Prototypes //
// //
//////////////////
#pragma mark - Prototypes
static int
bindle_widget_encodings(
bindle_conf_t * cnf,
int method );
static int
bindle_widget_encodings_stdin(
bindle_conf_t * cnf,
int method );
static int
bindle_widget_encodings_string(
bindle_conf_t * cnf,
int method,
const char * str );
/////////////////
// //
// Functions //
// //
/////////////////
#pragma mark - Functions
int
bindle_widget_base16(
bindle_conf_t * cnf )
{
return(bindle_widget_encodings(cnf, BNDL_HEX));
}
int
bindle_widget_base32(
bindle_conf_t * cnf )
{
return(bindle_widget_encodings(cnf, BNDL_BASE32));
}
int
bindle_widget_base32hex(
bindle_conf_t * cnf )
{
return(bindle_widget_encodings(cnf, BNDL_BASE32HEX));
}
int
bindle_widget_base64(
bindle_conf_t * cnf )
{
return(bindle_widget_encodings(cnf, BNDL_BASE64));
}
int
bindle_widget_crockfordbase32(
bindle_conf_t * cnf )
{
return(bindle_widget_encodings(cnf, BNDL_CROCKFORD));
}
int
bindle_widget_pctenc(
bindle_conf_t * cnf )
{
return(bindle_widget_encodings(cnf, BNDL_PCTENC));
}
int
bindle_widget_encodings(
bindle_conf_t * cnf,
int method )
{
int c;
int opt_index;
const char * str;
// getopt options
static const char * short_opt = BINDLE_COMMON_SHORT "dei:no:ps:";
static struct option long_opt[] =
{
BINDLE_COMMON_LONG,
{ "decode", no_argument, NULL, 'd' },
{ "encode", no_argument, NULL, 'e' },
{ "input", optional_argument, NULL, 'i' },
{ "newline", no_argument, NULL, 'n' },
{ "nopadding", no_argument, NULL, 'p' },
{ "output", optional_argument, NULL, 'o' },
{ NULL, 0, NULL, 0 }
};
optind = 1;
opt_index = 0;
str = NULL;
while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1)
{
switch(c)
{
case -2: /* --help or --version */
return(0);
case -1: /* no more arguments */
case 0: /* long options toggles */
break;
case 1: /* error processing argument */
return(1);
case 'd':
cnf->widget_flags |= BINDLE_FLG_DECODE;
cnf->widget_flags &= ~BINDLE_FLG_ENCODE;
break;
case 'e':
cnf->widget_flags |= BINDLE_FLG_ENCODE;
cnf->widget_flags &= ~BINDLE_FLG_DECODE;
break;
case 'i':
if (bindle_fdin != STDIN_FILENO)
{
fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c);
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
};
if ((bindle_fdin = open(optarg, O_RDONLY)) == -1)
{
fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno));
return(1);
};
break;
case 'n':
cnf->widget_flags |= BINDLE_FLG_NEWLINE;
break;
case 'o':
if (bindle_fdout != STDOUT_FILENO)
{
fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c);
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
};
if ((bindle_fdout = open(optarg, (O_WRONLY|O_CREAT|O_TRUNC), 0644)) == -1)
{
fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno));
return(1);
};
break;
case 'p':
cnf->widget_flags |= BINDLE_FLG_NOPAD;
break;
case 's':
str = optarg;
break;
case 'u':
break;
case '?':
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
default:
fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
return(1);
};
};
if ((cnf->argc - optind) > 0)
{
fprintf(stderr, "%s: unknown argument -- `%s'\n", cnf->prog_name, cnf->argv[optind]);
fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name);
return(1);
};
fflush(stdout);
if ((str))
return(bindle_widget_encodings_string(cnf, method, str));
return(bindle_widget_encodings_stdin(cnf, method));
}
int
bindle_widget_encodings_stdin(
bindle_conf_t * cnf,
int method )
{
ssize_t rc;
ssize_t len;
char chunk[BINDLE_BUFF_SIZE+1];
char res[(BINDLE_BUFF_SIZE*3)+1];
while ((rc = read(bindle_fdin, chunk, BINDLE_BUFF_SIZE)) > 0)
{
chunk[rc] = '\0';
if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE)
len = bindle_decode(method, res, sizeof(res), chunk, (size_t)rc);
else
len = bindle_encode(method, res, sizeof(res), chunk, (size_t)rc, (cnf->widget_flags & BINDLE_FLG_NOPAD));
if (len > 0)
write(bindle_fdout, res, (size_t)len);
};
if (rc == -1)
{
fprintf(stderr, "%s: read(): %s\n", cnf->prog_name, strerror(errno));
return(1);
};
if ((cnf->widget_flags & BINDLE_FLG_NEWLINE))
{
bindle_strlcpy(res, "\n", sizeof(res));
write(bindle_fdout, res, strlen(res));
};
return(0);
}
int
bindle_widget_encodings_string(
bindle_conf_t * cnf,
int method,
const char * str )
{
ssize_t rc;
size_t len;
size_t off;
size_t size;
char chunk[BINDLE_BUFF_SIZE+1];
char res[(BINDLE_BUFF_SIZE*3)+1];
len = strlen(str);
if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE)
{
for(off = 0; (off < len); off += BINDLE_BUFF_SIZE)
{
size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off);
memcpy(chunk, &str[off], size);
if ((rc = bindle_decode(method, res, sizeof(res), chunk, size)) > 0)
write(bindle_fdout, res, (size_t)rc);
};
}
else
{
for(off = 0; (off < len); off += BINDLE_BUFF_SIZE)
{
size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off);
memcpy(chunk, &str[off], size);
if ((rc = bindle_encode(method, res, sizeof(res), chunk, size, (cnf->widget_flags & BINDLE_FLG_NOPAD))) > 0)
write(bindle_fdout, res, (size_t)rc);
};
};
printf("\n");
return(0);
}
/* end of source */
|