aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-09-12 15:05:06 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-09-12 15:05:06 -0800
commita8b3e107e30f63fcee24d687946af34fd9d84c19 (patch)
tree6dd2e2a229926e8a2edd81983e56dd98a427a58c
parente93ec5df71c6aaa45a45c5298c4003724a5ae4b0 (diff)
downloadbindletools-a8b3e107e30f63fcee24d687946af34fd9d84c19.tar.gz
bindletools-a8b3e107e30f63fcee24d687946af34fd9d84c19.tar.bz2
bindletools-a8b3e107e30f63fcee24d687946af34fd9d84c19.tar.xz
bindletools-a8b3e107e30f63fcee24d687946af34fd9d84c19.zip
bindltools: refactoring bindle_encodings_src_fileno()
-rw-r--r--src/widget-encodings.c163
1 files changed, 146 insertions, 17 deletions
diff --git a/src/widget-encodings.c b/src/widget-encodings.c
index 0b8889f..eeaa760 100644
--- a/src/widget-encodings.c
+++ b/src/widget-encodings.c
@@ -63,6 +63,19 @@
63#define BINDLE_FLG_NOPAD 0x00004UL 63#define BINDLE_FLG_NOPAD 0x00004UL
64#define BINDLE_FLG_NEWLINE 0x00008UL 64#define BINDLE_FLG_NEWLINE 0x00008UL
65 65
66#define BNDLE_B16_CHUNK 2
67#define BNDLE_B16_BUFF_LEN (BNDLE_B16_CHUNK*32)
68#define BNDLE_B16_RES_LEN (BNDLE_B16_CHUNK*64)
69#define BNDLE_B32_CHUNK 40
70#define BNDLE_B32_BUFF_LEN (BNDLE_B32_CHUNK*5)
71#define BNDLE_B32_RES_LEN (BNDLE_B32_CHUNK*8)
72#define BNDLE_B64_CHUNK 12
73#define BNDLE_B64_BUFF_LEN (BNDLE_B64_CHUNK*18)
74#define BNDLE_B64_RES_LEN (BNDLE_B64_CHUNK*24)
75#define BNDLE_PCTENC_CHUNK 2048
76#define BNDLE_PCTENC_BUFF_LEN BNDLE_PCTENC_CHUNK
77#define BNDLE_PCTENC_RES_LEN BNDLE_PCTENC_CHUNK
78
66 79
67////////////////// 80//////////////////
68// // 81// //
@@ -73,6 +86,12 @@
73 86
74struct bindle_state 87struct bindle_state
75{ 88{
89 size_t chunk_size;
90 size_t res_size;
91 size_t buff_size;
92 size_t buff_len;
93 char * buff;
94 char * res;
76 int fdin; 95 int fdin;
77 int fdout; 96 int fdout;
78}; 97};
@@ -113,13 +132,25 @@ bindle_widget_encodings(
113 132
114 133
115static int 134static int
116bindle_encodings_fileno( 135bindle_encodings_action_decode(
136 bindle_conf_t * cnf,
137 int method );
138
139
140static int
141bindle_encodings_action_encode(
117 bindle_conf_t * cnf, 142 bindle_conf_t * cnf,
118 int method ); 143 int method );
119 144
120 145
121static int 146static int
122bindle_encodings_string( 147bindle_encodings_src_fileno(
148 bindle_conf_t * cnf,
149 int method );
150
151
152static int
153bindle_encodings_src_string(
123 bindle_conf_t * cnf, 154 bindle_conf_t * cnf,
124 int method, 155 int method,
125 const char * str ); 156 const char * str );
@@ -137,8 +168,15 @@ bindle_widget_base16(
137 bindle_conf_t * cnf ) 168 bindle_conf_t * cnf )
138{ 169{
139 bindle_state_t state; 170 bindle_state_t state;
171 char res[BNDLE_B16_RES_LEN+1];
172 char buff[BNDLE_B16_BUFF_LEN+1];
140 memset(&state, 0, sizeof(state)); 173 memset(&state, 0, sizeof(state));
141 cnf->state = &state; 174 cnf->state = &state;
175 state.chunk_size = BNDLE_B16_CHUNK;
176 state.buff_size = BNDLE_B16_BUFF_LEN;
177 state.res_size = BNDLE_B16_RES_LEN;
178 state.buff = buff;
179 state.res = res;
142 return(bindle_widget_encodings(cnf, BNDL_HEX)); 180 return(bindle_widget_encodings(cnf, BNDL_HEX));
143} 181}
144 182
@@ -148,8 +186,15 @@ bindle_widget_base32(
148 bindle_conf_t * cnf ) 186 bindle_conf_t * cnf )
149{ 187{
150 bindle_state_t state; 188 bindle_state_t state;
189 char res[BNDLE_B32_RES_LEN+1];
190 char buff[BNDLE_B32_BUFF_LEN+1];
151 memset(&state, 0, sizeof(state)); 191 memset(&state, 0, sizeof(state));
152 cnf->state = &state; 192 cnf->state = &state;
193 state.chunk_size = BNDLE_B32_CHUNK;
194 state.buff_size = BNDLE_B32_BUFF_LEN;
195 state.res_size = BNDLE_B32_RES_LEN;
196 state.buff = buff;
197 state.res = res;
153 return(bindle_widget_encodings(cnf, BNDL_BASE32)); 198 return(bindle_widget_encodings(cnf, BNDL_BASE32));
154} 199}
155 200
@@ -159,8 +204,15 @@ bindle_widget_base32hex(
159 bindle_conf_t * cnf ) 204 bindle_conf_t * cnf )
160{ 205{
161 bindle_state_t state; 206 bindle_state_t state;
207 char res[BNDLE_B32_RES_LEN+1];
208 char buff[BNDLE_B32_BUFF_LEN+1];
162 memset(&state, 0, sizeof(state)); 209 memset(&state, 0, sizeof(state));
163 cnf->state = &state; 210 cnf->state = &state;
211 state.chunk_size = BNDLE_B32_CHUNK;
212 state.buff_size = BNDLE_B32_BUFF_LEN;
213 state.res_size = BNDLE_B32_RES_LEN;
214 state.buff = buff;
215 state.res = res;
164 return(bindle_widget_encodings(cnf, BNDL_BASE32HEX)); 216 return(bindle_widget_encodings(cnf, BNDL_BASE32HEX));
165} 217}
166 218
@@ -170,8 +222,15 @@ bindle_widget_base64(
170 bindle_conf_t * cnf ) 222 bindle_conf_t * cnf )
171{ 223{
172 bindle_state_t state; 224 bindle_state_t state;
225 char res[BNDLE_B64_RES_LEN+1];
226 char buff[BNDLE_B64_BUFF_LEN+1];
173 memset(&state, 0, sizeof(state)); 227 memset(&state, 0, sizeof(state));
174 cnf->state = &state; 228 cnf->state = &state;
229 state.chunk_size = BNDLE_B64_CHUNK;
230 state.buff_size = BNDLE_B64_BUFF_LEN;
231 state.res_size = BNDLE_B64_RES_LEN;
232 state.buff = buff;
233 state.res = res;
175 return(bindle_widget_encodings(cnf, BNDL_BASE64)); 234 return(bindle_widget_encodings(cnf, BNDL_BASE64));
176} 235}
177 236
@@ -181,8 +240,15 @@ bindle_widget_crockfordbase32(
181 bindle_conf_t * cnf ) 240 bindle_conf_t * cnf )
182{ 241{
183 bindle_state_t state; 242 bindle_state_t state;
243 char res[BNDLE_B32_RES_LEN+1];
244 char buff[BNDLE_B32_BUFF_LEN+1];
184 memset(&state, 0, sizeof(state)); 245 memset(&state, 0, sizeof(state));
185 cnf->state = &state; 246 cnf->state = &state;
247 state.chunk_size = BNDLE_B32_CHUNK;
248 state.buff_size = BNDLE_B32_BUFF_LEN;
249 state.res_size = BNDLE_B32_RES_LEN;
250 state.buff = buff;
251 state.res = res;
186 return(bindle_widget_encodings(cnf, BNDL_CROCKFORD)); 252 return(bindle_widget_encodings(cnf, BNDL_CROCKFORD));
187} 253}
188 254
@@ -192,8 +258,15 @@ bindle_widget_pctenc(
192 bindle_conf_t * cnf ) 258 bindle_conf_t * cnf )
193{ 259{
194 bindle_state_t state; 260 bindle_state_t state;
261 char res[BNDLE_PCTENC_RES_LEN+1];
262 char buff[BNDLE_PCTENC_BUFF_LEN+1];
195 memset(&state, 0, sizeof(state)); 263 memset(&state, 0, sizeof(state));
196 cnf->state = &state; 264 cnf->state = &state;
265 state.chunk_size = BNDLE_PCTENC_CHUNK;
266 state.buff_size = BNDLE_PCTENC_BUFF_LEN;
267 state.res_size = BNDLE_PCTENC_RES_LEN;
268 state.buff = buff;
269 state.res = res;
197 return(bindle_widget_encodings(cnf, BNDL_PCTENC)); 270 return(bindle_widget_encodings(cnf, BNDL_PCTENC));
198} 271}
199 272
@@ -315,9 +388,9 @@ bindle_widget_encodings(
315 fflush(stdout); 388 fflush(stdout);
316 389
317 if ((str)) 390 if ((str))
318 rc = bindle_encodings_string(cnf, method, str); 391 rc = bindle_encodings_src_string(cnf, method, str);
319 else 392 else
320 rc = bindle_encodings_fileno(cnf, method); 393 rc = bindle_encodings_src_fileno(cnf, method);
321 394
322 if ((cnf->widget_flags & BINDLE_FLG_NEWLINE)) 395 if ((cnf->widget_flags & BINDLE_FLG_NEWLINE))
323 write(cnf->state->fdout, "\n", strlen("\n")); 396 write(cnf->state->fdout, "\n", strlen("\n"));
@@ -327,26 +400,82 @@ bindle_widget_encodings(
327 400
328 401
329int 402int
330bindle_encodings_fileno( 403bindle_encodings_action_decode(
331 bindle_conf_t * cnf, 404 bindle_conf_t * cnf,
332 int method ) 405 int method )
333{ 406{
334 ssize_t rc; 407 ssize_t len;
335 ssize_t len; 408 bindle_state_t * state;
336 char chunk[BINDLE_BUFF_SIZE+1]; 409
337 char res[(BINDLE_BUFF_SIZE*3)+1]; 410 state = cnf->state;
411
412 if ((len = bindle_decode(method, state->res, state->res_size, state->buff, state->buff_len)) == -1)
413 {
414 fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno));
415 return(-1);
416 };
417 if (len > 0)
418 write(cnf->state->fdout, cnf->state->res, (size_t)len);
419
420 state->buff_len = 0;
421
422 return(0);
423}
424
338 425
339 while ((rc = read(cnf->state->fdin, chunk, BINDLE_BUFF_SIZE)) > 0) 426int
427bindle_encodings_action_encode(
428 bindle_conf_t * cnf,
429 int method )
430{
431 ssize_t len;
432 bindle_state_t * state;
433
434 state = cnf->state;
435
436 if ((len = bindle_encode(method, state->res, state->res_size, state->buff, state->buff_len, (cnf->widget_flags & BINDLE_FLG_NOPAD))) == -1)
340 { 437 {
341 chunk[rc] = '\0'; 438 fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno));
439 return(-1);
440 };
441 if (len > 0)
442 write(cnf->state->fdout, cnf->state->res, (size_t)len);
443
444 return(0);
445}
446
447
448int
449bindle_encodings_src_fileno(
450 bindle_conf_t * cnf,
451 int method )
452{
453 ssize_t len;
454 bindle_state_t * state;
455 char * buff;
456 char * res;
457
458 state = cnf->state;
459 state->buff_len = 0;
460 buff = state->buff;
461 res = state->res;
462
463 while ((len = read(cnf->state->fdin, &buff[state->buff_len], (state->buff_size-state->buff_len))) > 0)
464 {
465 state->buff_len += (size_t)len;
466 buff[state->buff_len] = '\0';
342 if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE) 467 if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE)
343 len = bindle_decode(method, res, sizeof(res), chunk, (size_t)rc); 468 {
469 if (bindle_encodings_action_decode(cnf, method) == -1)
470 return(1);
471 }
344 else 472 else
345 len = bindle_encode(method, res, sizeof(res), chunk, (size_t)rc, (cnf->widget_flags & BINDLE_FLG_NOPAD)); 473 {
346 if (len > 0) 474 if (bindle_encodings_action_encode(cnf, method) == -1)
347 write(cnf->state->fdout, res, (size_t)len); 475 return(1);
476 }
348 }; 477 };
349 if (rc == -1) 478 if (len == -1)
350 { 479 {
351 fprintf(stderr, "%s: read(): %s\n", cnf->prog_name, strerror(errno)); 480 fprintf(stderr, "%s: read(): %s\n", cnf->prog_name, strerror(errno));
352 return(1); 481 return(1);
@@ -357,7 +486,7 @@ bindle_encodings_fileno(
357 486
358 487
359int 488int
360bindle_encodings_string( 489bindle_encodings_src_string(
361 bindle_conf_t * cnf, 490 bindle_conf_t * cnf,
362 int method, 491 int method,
363 const char * str ) 492 const char * str )