aboutsummaryrefslogtreecommitdiff
path: root/src/mime.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-02-11 03:31:53 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-02-11 03:31:53 +0000
commit390846b640ee7013d51a766b4b2472bdcfbbdfcc (patch)
tree3bedd023a0a84feb714615245b58eab5ffb4309b /src/mime.c
parent0b2542d1a61fc5425ff65ab3dbf7ba7de174763f (diff)
downloadluasocket-390846b640ee7013d51a766b4b2472bdcfbbdfcc.tar.gz
luasocket-390846b640ee7013d51a766b4b2472bdcfbbdfcc.tar.bz2
luasocket-390846b640ee7013d51a766b4b2472bdcfbbdfcc.zip
Added ltn12 module. Modified mime to be stand alone.
Still crashes on invalid input. Dunno why.
Diffstat (limited to 'src/mime.c')
-rw-r--r--src/mime.c84
1 files changed, 39 insertions, 45 deletions
diff --git a/src/mime.c b/src/mime.c
index 9fc4f51..ae4084d 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -9,8 +9,6 @@
9#include <lua.h> 9#include <lua.h>
10#include <lauxlib.h> 10#include <lauxlib.h>
11 11
12#include "luasocket.h"
13#include "auxiliar.h"
14#include "mime.h" 12#include "mime.h"
15 13
16/*=========================================================================*\ 14/*=========================================================================*\
@@ -83,12 +81,10 @@ static UC b64unbase[256];
83\*-------------------------------------------------------------------------*/ 81\*-------------------------------------------------------------------------*/
84int mime_open(lua_State *L) 82int mime_open(lua_State *L)
85{ 83{
86 lua_pushstring(L, LUASOCKET_LIBNAME);
87 lua_gettable(L, LUA_GLOBALSINDEX);
88 lua_pushstring(L, "mime"); 84 lua_pushstring(L, "mime");
89 lua_newtable(L); 85 lua_newtable(L);
90 luaL_openlib(L, NULL, func, 0); 86 luaL_openlib(L, NULL, func, 0);
91 lua_settable(L, -3); 87 lua_settable(L, LUA_GLOBALSINDEX);
92 lua_pop(L, 1); 88 lua_pop(L, 1);
93 /* initialize lookup tables */ 89 /* initialize lookup tables */
94 qpsetup(qpclass, qpunbase); 90 qpsetup(qpclass, qpunbase);
@@ -110,7 +106,7 @@ static int mime_global_wrp(lua_State *L)
110{ 106{
111 size_t size = 0; 107 size_t size = 0;
112 int left = (int) luaL_checknumber(L, 1); 108 int left = (int) luaL_checknumber(L, 1);
113 const UC *input = (UC *) aux_optlstring(L, 2, NULL, &size); 109 const UC *input = (UC *) luaL_optlstring(L, 2, NULL, &size);
114 const UC *last = input + size; 110 const UC *last = input + size;
115 int length = (int) luaL_optnumber(L, 3, 76); 111 int length = (int) luaL_optnumber(L, 3, 76);
116 luaL_Buffer buffer; 112 luaL_Buffer buffer;
@@ -261,7 +257,7 @@ static int mime_global_b64(lua_State *L)
261 luaL_buffinit(L, &buffer); 257 luaL_buffinit(L, &buffer);
262 while (input < last) 258 while (input < last)
263 asize = b64encode(*input++, atom, asize, &buffer); 259 asize = b64encode(*input++, atom, asize, &buffer);
264 input = (UC *) aux_optlstring(L, 2, NULL, &isize); 260 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
265 if (input) { 261 if (input) {
266 last = input + isize; 262 last = input + isize;
267 while (input < last) 263 while (input < last)
@@ -289,7 +285,7 @@ static int mime_global_unb64(lua_State *L)
289 luaL_buffinit(L, &buffer); 285 luaL_buffinit(L, &buffer);
290 while (input < last) 286 while (input < last)
291 asize = b64decode(*input++, atom, asize, &buffer); 287 asize = b64decode(*input++, atom, asize, &buffer);
292 input = (UC *) aux_optlstring(L, 2, NULL, &isize); 288 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
293 if (input) { 289 if (input) {
294 last = input + isize; 290 last = input + isize;
295 while (input < last) 291 while (input < last)
@@ -426,14 +422,14 @@ static int mime_global_qp(lua_State *L)
426 422
427 size_t asize = 0, isize = 0; 423 size_t asize = 0, isize = 0;
428 UC atom[3]; 424 UC atom[3];
429 const UC *input = (UC *) aux_optlstring(L, 1, NULL, &isize); 425 const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize);
430 const UC *last = input + isize; 426 const UC *last = input + isize;
431 const char *marker = luaL_optstring(L, 3, CRLF); 427 const char *marker = luaL_optstring(L, 3, CRLF);
432 luaL_Buffer buffer; 428 luaL_Buffer buffer;
433 luaL_buffinit(L, &buffer); 429 luaL_buffinit(L, &buffer);
434 while (input < last) 430 while (input < last)
435 asize = qpencode(*input++, atom, asize, marker, &buffer); 431 asize = qpencode(*input++, atom, asize, marker, &buffer);
436 input = (UC *) aux_optlstring(L, 2, NULL, &isize); 432 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
437 if (input) { 433 if (input) {
438 last = input + isize; 434 last = input + isize;
439 while (input < last) 435 while (input < last)
@@ -495,13 +491,13 @@ static int mime_global_unqp(lua_State *L)
495 491
496 size_t asize = 0, isize = 0; 492 size_t asize = 0, isize = 0;
497 UC atom[3]; 493 UC atom[3];
498 const UC *input = (UC *) aux_optlstring(L, 1, NULL, &isize); 494 const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize);
499 const UC *last = input + isize; 495 const UC *last = input + isize;
500 luaL_Buffer buffer; 496 luaL_Buffer buffer;
501 luaL_buffinit(L, &buffer); 497 luaL_buffinit(L, &buffer);
502 while (input < last) 498 while (input < last)
503 asize = qpdecode(*input++, atom, asize, &buffer); 499 asize = qpdecode(*input++, atom, asize, &buffer);
504 input = (UC *) aux_optlstring(L, 2, NULL, &isize); 500 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
505 if (input) { 501 if (input) {
506 last = input + isize; 502 last = input + isize;
507 while (input < last) 503 while (input < last)
@@ -525,7 +521,7 @@ static int mime_global_qpwrp(lua_State *L)
525{ 521{
526 size_t size = 0; 522 size_t size = 0;
527 int left = (int) luaL_checknumber(L, 1); 523 int left = (int) luaL_checknumber(L, 1);
528 const UC *input = (UC *) aux_optlstring(L, 2, NULL, &size); 524 const UC *input = (UC *) luaL_optlstring(L, 2, NULL, &size);
529 const UC *last = input + size; 525 const UC *last = input + size;
530 int length = (int) luaL_optnumber(L, 3, 76); 526 int length = (int) luaL_optnumber(L, 3, 76);
531 luaL_Buffer buffer; 527 luaL_Buffer buffer;
@@ -576,54 +572,52 @@ static int mime_global_qpwrp(lua_State *L)
576* probably other more obscure conventions. 572* probably other more obscure conventions.
577\*-------------------------------------------------------------------------*/ 573\*-------------------------------------------------------------------------*/
578#define eolcandidate(c) (c == CR || c == LF) 574#define eolcandidate(c) (c == CR || c == LF)
579static size_t eolconvert(UC c, UC *input, size_t size, 575static size_t eolprocess(int c, int ctx, const char *marker,
580 const char *marker, luaL_Buffer *buffer) 576 luaL_Buffer *buffer)
581{ 577{
582 input[size++] = c; 578 if (eolcandidate(ctx)) {
583 /* deal with all characters we can deal */
584 if (eolcandidate(input[0])) {
585 if (size < 2) return size;
586 luaL_addstring(buffer, marker); 579 luaL_addstring(buffer, marker);
587 if (eolcandidate(input[1])) { 580 if (eolcandidate(c)) {
588 if (input[0] == input[1]) luaL_addstring(buffer, marker); 581 if (c == ctx)
589 } else luaL_putchar(buffer, input[1]); 582 luaL_addstring(buffer, marker);
590 return 0; 583 return 0;
584 } else {
585 luaL_putchar(buffer, c);
586 return 0;
587 }
591 } else { 588 } else {
592 luaL_putchar(buffer, input[0]); 589 if (!eolcandidate(c)) {
593 return 0; 590 luaL_putchar(buffer, c);
591 return 0;
592 } else
593 return c;
594 } 594 }
595} 595}
596 596
597/*-------------------------------------------------------------------------*\ 597/*-------------------------------------------------------------------------*\
598* Converts a string to uniform EOL convention. 598* Converts a string to uniform EOL convention.
599* A, B = eol(C, D, marker) 599* A, n = eol(o, B, marker)
600* A is the converted version of the largest prefix of C .. D that 600* A is the converted version of the largest prefix of B that can be
601* can be converted without doubts. 601* converted unambiguously. 'o' is the context returned by the previous
602* B has the remaining bytes of C .. D, *without* convertion. 602* call. 'n' is the new context.
603\*-------------------------------------------------------------------------*/ 603\*-------------------------------------------------------------------------*/
604static int mime_global_eol(lua_State *L) 604static int mime_global_eol(lua_State *L)
605{ 605{
606 size_t asize = 0, isize = 0; 606 int ctx = luaL_checkint(L, 1);
607 UC atom[2]; 607 size_t isize = 0;
608 const UC *input = (UC *) aux_optlstring(L, 1, NULL, &isize); 608 const char *input = luaL_optlstring(L, 2, NULL, &isize);
609 const UC *last = input + isize; 609 const char *last = input + isize;
610 const char *marker = luaL_optstring(L, 3, CRLF); 610 const char *marker = luaL_optstring(L, 3, CRLF);
611 luaL_Buffer buffer; 611 luaL_Buffer buffer;
612 luaL_buffinit(L, &buffer); 612 luaL_buffinit(L, &buffer);
613 while (input < last) 613 while (input < last)
614 asize = eolconvert(*input++, atom, asize, marker, &buffer); 614 ctx = eolprocess(*input++, ctx, marker, &buffer);
615 input = (UC *) aux_optlstring(L, 2, NULL, &isize); 615 /* if the last character was a candidate, we output a new line */
616 if (input) { 616 if (!input) {
617 last = input + isize; 617 if (eolcandidate(ctx)) luaL_addstring(&buffer, marker);
618 while (input < last) 618 ctx = 0;
619 asize = eolconvert(*input++, atom, asize, marker, &buffer);
620 /* if there is something in atom, it's one character, and it
621 * is a candidate. so we output a new line */
622 } else if (asize > 0) {
623 luaL_addstring(&buffer, marker);
624 asize = 0;
625 } 619 }
626 luaL_pushresult(&buffer); 620 luaL_pushresult(&buffer);
627 lua_pushlstring(L, (char *) atom, asize); 621 lua_pushnumber(L, ctx);
628 return 2; 622 return 2;
629} 623}