aboutsummaryrefslogtreecommitdiff
path: root/src/mime.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-03-16 06:42:53 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-03-16 06:42:53 +0000
commitbcc0c2a9f0be2ca796ef5206a78e283fe15e6186 (patch)
tree65c269d4854aa5ff4a0b2c8eede5cdb18d716033 /src/mime.c
parentb6edaac2841137cf0ef5105f75358bbab4570d87 (diff)
downloadluasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.tar.gz
luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.tar.bz2
luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.zip
New filter scheme.
ltn12 and mime updated. smtp/ftp broken.
Diffstat (limited to 'src/mime.c')
-rw-r--r--src/mime.c137
1 files changed, 95 insertions, 42 deletions
diff --git a/src/mime.c b/src/mime.c
index 1a8bff4..77f3ae1 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -20,8 +20,8 @@
20#define SP 0x20 20#define SP 0x20
21 21
22typedef unsigned char UC; 22typedef unsigned char UC;
23static const char CRLF[2] = {CR, LF}; 23static const char CRLF[] = {CR, LF, 0};
24static const char EQCRLF[3] = {'=', CR, LF}; 24static const char EQCRLF[] = {'=', CR, LF, 0};
25 25
26/*=========================================================================*\ 26/*=========================================================================*\
27* Internal function prototypes. 27* Internal function prototypes.
@@ -95,7 +95,7 @@ int mime_open(lua_State *L)
95* Global Lua functions 95* Global Lua functions
96\*=========================================================================*/ 96\*=========================================================================*/
97/*-------------------------------------------------------------------------*\ 97/*-------------------------------------------------------------------------*\
98* Incrementaly breaks a string into lines 98* Incrementaly breaks a string into lines. The string can have CRLF breaks.
99* A, n = wrp(l, B, length) 99* A, n = wrp(l, B, length)
100* A is a copy of B, broken into lines of at most 'length' bytes. 100* A is a copy of B, broken into lines of at most 'length' bytes.
101* 'l' is how many bytes are left for the first line of B. 101* 'l' is how many bytes are left for the first line of B.
@@ -109,6 +109,15 @@ static int mime_global_wrp(lua_State *L)
109 const UC *last = input + size; 109 const UC *last = input + size;
110 int length = (int) luaL_optnumber(L, 3, 76); 110 int length = (int) luaL_optnumber(L, 3, 76);
111 luaL_Buffer buffer; 111 luaL_Buffer buffer;
112 /* end of input black-hole */
113 if (!input) {
114 /* if last line has not been terminated, add a line break */
115 if (left < length) lua_pushstring(L, CRLF);
116 /* otherwise, we are done */
117 else lua_pushnil(L);
118 lua_pushnumber(L, length);
119 return 2;
120 }
112 luaL_buffinit(L, &buffer); 121 luaL_buffinit(L, &buffer);
113 while (input < last) { 122 while (input < last) {
114 switch (*input) { 123 switch (*input) {
@@ -129,11 +138,6 @@ static int mime_global_wrp(lua_State *L)
129 } 138 }
130 input++; 139 input++;
131 } 140 }
132 /* if in last chunk and last line wasn't terminated, add a line-break */
133 if (!input && left < length) {
134 luaL_addstring(&buffer, CRLF);
135 left = length;
136 }
137 luaL_pushresult(&buffer); 141 luaL_pushresult(&buffer);
138 lua_pushnumber(L, left); 142 lua_pushnumber(L, left);
139 return 2; 143 return 2;
@@ -200,7 +204,6 @@ static size_t b64pad(const UC *input, size_t size,
200 code[0] = b64base[value]; 204 code[0] = b64base[value];
201 luaL_addlstring(buffer, (char *) code, 4); 205 luaL_addlstring(buffer, (char *) code, 4);
202 break; 206 break;
203 case 0: /* fall through */
204 default: 207 default:
205 break; 208 break;
206 } 209 }
@@ -250,19 +253,31 @@ static int mime_global_b64(lua_State *L)
250{ 253{
251 UC atom[3]; 254 UC atom[3];
252 size_t isize = 0, asize = 0; 255 size_t isize = 0, asize = 0;
253 const UC *input = (UC *) luaL_checklstring(L, 1, &isize); 256 const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize);
254 const UC *last = input + isize; 257 const UC *last = input + isize;
255 luaL_Buffer buffer; 258 luaL_Buffer buffer;
259 /* end-of-input blackhole */
260 if (!input) {
261 lua_pushnil(L);
262 lua_pushnil(L);
263 return 2;
264 }
265 /* process first part of the input */
256 luaL_buffinit(L, &buffer); 266 luaL_buffinit(L, &buffer);
257 while (input < last) 267 while (input < last)
258 asize = b64encode(*input++, atom, asize, &buffer); 268 asize = b64encode(*input++, atom, asize, &buffer);
259 input = (UC *) luaL_optlstring(L, 2, NULL, &isize); 269 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
260 if (input) { 270 /* if second part is nil, we are done */
261 last = input + isize; 271 if (!input) {
262 while (input < last)
263 asize = b64encode(*input++, atom, asize, &buffer);
264 } else
265 asize = b64pad(atom, asize, &buffer); 272 asize = b64pad(atom, asize, &buffer);
273 luaL_pushresult(&buffer);
274 lua_pushnil(L);
275 return 2;
276 }
277 /* otherwise process the second part */
278 last = input + isize;
279 while (input < last)
280 asize = b64encode(*input++, atom, asize, &buffer);
266 luaL_pushresult(&buffer); 281 luaL_pushresult(&buffer);
267 lua_pushlstring(L, (char *) atom, asize); 282 lua_pushlstring(L, (char *) atom, asize);
268 return 2; 283 return 2;
@@ -278,20 +293,30 @@ static int mime_global_unb64(lua_State *L)
278{ 293{
279 UC atom[4]; 294 UC atom[4];
280 size_t isize = 0, asize = 0; 295 size_t isize = 0, asize = 0;
281 const UC *input = (UC *) luaL_checklstring(L, 1, &isize); 296 const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize);
282 const UC *last = input + isize; 297 const UC *last = input + isize;
283 luaL_Buffer buffer; 298 luaL_Buffer buffer;
299 /* end-of-input blackhole */
300 if (!input) {
301 lua_pushnil(L);
302 lua_pushnil(L);
303 return 2;
304 }
305 /* process first part of the input */
284 luaL_buffinit(L, &buffer); 306 luaL_buffinit(L, &buffer);
285 while (input < last) 307 while (input < last)
286 asize = b64decode(*input++, atom, asize, &buffer); 308 asize = b64decode(*input++, atom, asize, &buffer);
287 input = (UC *) luaL_optlstring(L, 2, NULL, &isize); 309 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
288 if (input) { 310 /* if second is nil, we are done */
289 last = input + isize; 311 if (!input) {
290 while (input < last) 312 luaL_pushresult(&buffer);
291 asize = b64decode(*input++, atom, asize, &buffer); 313 lua_pushnil(L);
292 } 314 return 2;
293 /* if !input we are done. if atom > 0, the remaning is invalid. we just 315 }
294 * return it undecoded. */ 316 /* otherwise, process the rest of the input */
317 last = input + isize;
318 while (input < last)
319 asize = b64decode(*input++, atom, asize, &buffer);
295 luaL_pushresult(&buffer); 320 luaL_pushresult(&buffer);
296 lua_pushlstring(L, (char *) atom, asize); 321 lua_pushlstring(L, (char *) atom, asize);
297 return 2; 322 return 2;
@@ -425,16 +450,27 @@ static int mime_global_qp(lua_State *L)
425 const UC *last = input + isize; 450 const UC *last = input + isize;
426 const char *marker = luaL_optstring(L, 3, CRLF); 451 const char *marker = luaL_optstring(L, 3, CRLF);
427 luaL_Buffer buffer; 452 luaL_Buffer buffer;
453 /* end-of-input blackhole */
454 if (!input) {
455 lua_pushnil(L);
456 lua_pushnil(L);
457 return 2;
458 }
459 /* process first part of input */
428 luaL_buffinit(L, &buffer); 460 luaL_buffinit(L, &buffer);
429 while (input < last) 461 while (input < last)
430 asize = qpencode(*input++, atom, asize, marker, &buffer); 462 asize = qpencode(*input++, atom, asize, marker, &buffer);
431 input = (UC *) luaL_optlstring(L, 2, NULL, &isize); 463 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
432 if (input) { 464 /* if second part is nil, we are done */
433 last = input + isize; 465 if (!input) {
434 while (input < last)
435 asize = qpencode(*input++, atom, asize, marker, &buffer);
436 } else
437 asize = qppad(atom, asize, &buffer); 466 asize = qppad(atom, asize, &buffer);
467 luaL_pushresult(&buffer);
468 lua_pushnil(L);
469 }
470 /* otherwise process rest of input */
471 last = input + isize;
472 while (input < last)
473 asize = qpencode(*input++, atom, asize, marker, &buffer);
438 luaL_pushresult(&buffer); 474 luaL_pushresult(&buffer);
439 lua_pushlstring(L, (char *) atom, asize); 475 lua_pushlstring(L, (char *) atom, asize);
440 return 2; 476 return 2;
@@ -487,21 +523,32 @@ static size_t qpdecode(UC c, UC *input, size_t size,
487\*-------------------------------------------------------------------------*/ 523\*-------------------------------------------------------------------------*/
488static int mime_global_unqp(lua_State *L) 524static int mime_global_unqp(lua_State *L)
489{ 525{
490
491 size_t asize = 0, isize = 0; 526 size_t asize = 0, isize = 0;
492 UC atom[3]; 527 UC atom[3];
493 const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize); 528 const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize);
494 const UC *last = input + isize; 529 const UC *last = input + isize;
495 luaL_Buffer buffer; 530 luaL_Buffer buffer;
531 /* end-of-input blackhole */
532 if (!input) {
533 lua_pushnil(L);
534 lua_pushnil(L);
535 return 2;
536 }
537 /* process first part of input */
496 luaL_buffinit(L, &buffer); 538 luaL_buffinit(L, &buffer);
497 while (input < last) 539 while (input < last)
498 asize = qpdecode(*input++, atom, asize, &buffer); 540 asize = qpdecode(*input++, atom, asize, &buffer);
499 input = (UC *) luaL_optlstring(L, 2, NULL, &isize); 541 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
500 if (input) { 542 /* if second part is nil, we are done */
501 last = input + isize; 543 if (!input) {
502 while (input < last) 544 luaL_pushresult(&buffer);
503 asize = qpdecode(*input++, atom, asize, &buffer); 545 lua_pushnil(L);
546 return 2;
504 } 547 }
548 /* otherwise process rest of input */
549 last = input + isize;
550 while (input < last)
551 asize = qpdecode(*input++, atom, asize, &buffer);
505 luaL_pushresult(&buffer); 552 luaL_pushresult(&buffer);
506 lua_pushlstring(L, (char *) atom, asize); 553 lua_pushlstring(L, (char *) atom, asize);
507 return 2; 554 return 2;
@@ -524,6 +571,14 @@ static int mime_global_qpwrp(lua_State *L)
524 const UC *last = input + size; 571 const UC *last = input + size;
525 int length = (int) luaL_optnumber(L, 3, 76); 572 int length = (int) luaL_optnumber(L, 3, 76);
526 luaL_Buffer buffer; 573 luaL_Buffer buffer;
574 /* end-of-input blackhole */
575 if (!input) {
576 if (left < length) lua_pushstring(L, EQCRLF);
577 else lua_pushnil(L);
578 lua_pushnumber(L, length);
579 return 2;
580 }
581 /* process all input */
527 luaL_buffinit(L, &buffer); 582 luaL_buffinit(L, &buffer);
528 while (input < last) { 583 while (input < last) {
529 switch (*input) { 584 switch (*input) {
@@ -552,11 +607,6 @@ static int mime_global_qpwrp(lua_State *L)
552 } 607 }
553 input++; 608 input++;
554 } 609 }
555 /* if in last chunk and last line wasn't terminated, add a soft-break */
556 if (!input && left < length) {
557 luaL_addstring(&buffer, EQCRLF);
558 left = length;
559 }
560 luaL_pushresult(&buffer); 610 luaL_pushresult(&buffer);
561 lua_pushnumber(L, left); 611 lua_pushnumber(L, left);
562 return 2; 612 return 2;
@@ -609,13 +659,16 @@ static int mime_global_eol(lua_State *L)
609 const char *marker = luaL_optstring(L, 3, CRLF); 659 const char *marker = luaL_optstring(L, 3, CRLF);
610 luaL_Buffer buffer; 660 luaL_Buffer buffer;
611 luaL_buffinit(L, &buffer); 661 luaL_buffinit(L, &buffer);
612 while (input < last)
613 ctx = eolprocess(*input++, ctx, marker, &buffer);
614 /* if the last character was a candidate, we output a new line */ 662 /* if the last character was a candidate, we output a new line */
615 if (!input) { 663 if (!input) {
616 if (eolcandidate(ctx)) luaL_addstring(&buffer, marker); 664 if (eolcandidate(ctx)) lua_pushstring(L, marker);
617 ctx = 0; 665 else lua_pushnil(L);
666 lua_pushnumber(L, 0);
667 return 2;
618 } 668 }
669 /* process all input */
670 while (input < last)
671 ctx = eolprocess(*input++, ctx, marker, &buffer);
619 luaL_pushresult(&buffer); 672 luaL_pushresult(&buffer);
620 lua_pushnumber(L, ctx); 673 lua_pushnumber(L, ctx);
621 return 2; 674 return 2;