2004-01-19 06:41:30 +01:00
|
|
|
/*=========================================================================*\
|
2004-06-16 03:02:07 +02:00
|
|
|
* MIME support functions
|
2004-01-19 06:41:30 +01:00
|
|
|
* LuaSocket toolkit
|
|
|
|
\*=========================================================================*/
|
|
|
|
#include <string.h>
|
2019-03-10 03:23:48 +01:00
|
|
|
#include <ctype.h>
|
2004-01-19 06:41:30 +01:00
|
|
|
|
2005-09-29 08:11:42 +02:00
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
2015-08-21 20:39:34 +02:00
|
|
|
#include "compat.h"
|
2005-09-29 08:11:42 +02:00
|
|
|
|
2004-01-19 06:41:30 +01:00
|
|
|
#include "mime.h"
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Don't want to trust escape character constants
|
|
|
|
\*=========================================================================*/
|
|
|
|
typedef unsigned char UC;
|
2004-05-25 07:27:44 +02:00
|
|
|
static const char CRLF[] = "\r\n";
|
|
|
|
static const char EQCRLF[] = "=\r\n";
|
2004-01-19 06:41:30 +01:00
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Internal function prototypes.
|
|
|
|
\*=========================================================================*/
|
2004-01-24 03:47:24 +01:00
|
|
|
static int mime_global_wrp(lua_State *L);
|
2004-01-19 06:41:30 +01:00
|
|
|
static int mime_global_b64(lua_State *L);
|
|
|
|
static int mime_global_unb64(lua_State *L);
|
|
|
|
static int mime_global_qp(lua_State *L);
|
|
|
|
static int mime_global_unqp(lua_State *L);
|
2004-01-24 03:47:24 +01:00
|
|
|
static int mime_global_qpwrp(lua_State *L);
|
2004-01-19 06:41:30 +01:00
|
|
|
static int mime_global_eol(lua_State *L);
|
2004-06-17 08:23:13 +02:00
|
|
|
static int mime_global_dot(lua_State *L);
|
2004-01-19 06:41:30 +01:00
|
|
|
|
2004-06-17 08:23:13 +02:00
|
|
|
static size_t dot(int c, size_t state, luaL_Buffer *buffer);
|
2019-03-10 03:23:48 +01:00
|
|
|
//static void b64setup(UC *base);
|
2004-01-19 06:41:30 +01:00
|
|
|
static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
|
|
|
static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer);
|
|
|
|
static size_t b64decode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
|
|
|
|
2019-03-10 03:23:48 +01:00
|
|
|
//static void qpsetup(UC *class, UC *unbase);
|
2004-01-19 06:41:30 +01:00
|
|
|
static void qpquote(UC c, luaL_Buffer *buffer);
|
|
|
|
static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
2015-02-18 23:51:37 +01:00
|
|
|
static size_t qpencode(UC c, UC *input, size_t size,
|
2004-01-19 16:38:33 +01:00
|
|
|
const char *marker, luaL_Buffer *buffer);
|
2004-02-04 15:29:11 +01:00
|
|
|
static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer);
|
2004-01-21 02:09:50 +01:00
|
|
|
|
2004-01-19 06:41:30 +01:00
|
|
|
/* code support functions */
|
2011-07-05 00:31:14 +02:00
|
|
|
static luaL_Reg func[] = {
|
2004-06-17 08:23:13 +02:00
|
|
|
{ "dot", mime_global_dot },
|
2004-02-04 15:29:11 +01:00
|
|
|
{ "b64", mime_global_b64 },
|
2004-01-19 06:41:30 +01:00
|
|
|
{ "eol", mime_global_eol },
|
|
|
|
{ "qp", mime_global_qp },
|
2004-01-24 03:47:24 +01:00
|
|
|
{ "qpwrp", mime_global_qpwrp },
|
2004-01-19 06:41:30 +01:00
|
|
|
{ "unb64", mime_global_unb64 },
|
2004-02-04 15:29:11 +01:00
|
|
|
{ "unqp", mime_global_unqp },
|
2004-01-24 03:47:24 +01:00
|
|
|
{ "wrp", mime_global_wrp },
|
2004-01-19 06:41:30 +01:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Quoted-printable globals
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
enum {QP_PLAIN, QP_QUOTED, QP_CR, QP_IF_LAST};
|
|
|
|
|
2019-03-10 03:23:48 +01:00
|
|
|
static UC qpclass[] = {
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_IF_LAST, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_CR, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_IF_LAST, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_QUOTED, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN, QP_PLAIN,
|
|
|
|
QP_PLAIN, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED,
|
|
|
|
QP_QUOTED, QP_QUOTED, QP_QUOTED, QP_QUOTED
|
|
|
|
};
|
|
|
|
|
|
|
|
static const UC qpbase[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
static const UC qpunbase[] = {
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255
|
|
|
|
};
|
|
|
|
|
2004-01-19 06:41:30 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Base64 globals
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static const UC b64base[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
2019-03-10 03:23:48 +01:00
|
|
|
|
|
|
|
static const UC b64unbase[] = {
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 0,
|
|
|
|
255, 255, 255, 0, 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, 255, 255,
|
|
|
|
255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255
|
|
|
|
};
|
2004-01-19 06:41:30 +01:00
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Exported functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-25 23:56:17 +01:00
|
|
|
LUASOCKET_API int luaopen_mime_core(lua_State *L)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
2013-05-27 10:45:09 +02:00
|
|
|
lua_newtable(L);
|
2013-05-29 10:56:56 +02:00
|
|
|
luaL_setfuncs(L, func, 0);
|
2005-09-29 08:11:42 +02:00
|
|
|
/* make version string available to scripts */
|
|
|
|
lua_pushstring(L, "_VERSION");
|
|
|
|
lua_pushstring(L, MIME_VERSION);
|
|
|
|
lua_rawset(L, -3);
|
2004-01-19 06:41:30 +01:00
|
|
|
/* initialize lookup tables */
|
2019-03-10 03:23:48 +01:00
|
|
|
// qpsetup(qpclass, qpunbase);
|
|
|
|
// b64setup(b64unbase);
|
2004-05-30 23:36:22 +02:00
|
|
|
return 1;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Global Lua functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2004-03-16 07:42:53 +01:00
|
|
|
* Incrementaly breaks a string into lines. The string can have CRLF breaks.
|
2004-02-04 15:29:11 +01:00
|
|
|
* A, n = wrp(l, B, length)
|
2015-02-18 23:51:37 +01:00
|
|
|
* A is a copy of B, broken into lines of at most 'length' bytes.
|
|
|
|
* 'l' is how many bytes are left for the first line of B.
|
|
|
|
* 'n' is the number of bytes left in the last line of A.
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 03:47:24 +01:00
|
|
|
static int mime_global_wrp(lua_State *L)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
2004-01-21 02:09:50 +01:00
|
|
|
int left = (int) luaL_checknumber(L, 1);
|
2014-12-21 06:57:10 +01:00
|
|
|
const UC *input = (const UC *) luaL_optlstring(L, 2, NULL, &size);
|
2004-01-19 06:41:30 +01:00
|
|
|
const UC *last = input + size;
|
2004-01-21 02:09:50 +01:00
|
|
|
int length = (int) luaL_optnumber(L, 3, 76);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer buffer;
|
2004-03-16 07:42:53 +01:00
|
|
|
/* end of input black-hole */
|
|
|
|
if (!input) {
|
|
|
|
/* if last line has not been terminated, add a line break */
|
|
|
|
if (left < length) lua_pushstring(L, CRLF);
|
|
|
|
/* otherwise, we are done */
|
|
|
|
else lua_pushnil(L);
|
|
|
|
lua_pushnumber(L, length);
|
|
|
|
return 2;
|
2015-02-18 23:51:37 +01:00
|
|
|
}
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_buffinit(L, &buffer);
|
|
|
|
while (input < last) {
|
2004-02-04 15:29:11 +01:00
|
|
|
switch (*input) {
|
2004-05-25 07:27:44 +02:00
|
|
|
case '\r':
|
2004-02-04 15:29:11 +01:00
|
|
|
break;
|
2004-05-25 07:27:44 +02:00
|
|
|
case '\n':
|
2004-02-04 15:29:11 +01:00
|
|
|
luaL_addstring(&buffer, CRLF);
|
|
|
|
left = length;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (left <= 0) {
|
|
|
|
left = length;
|
|
|
|
luaL_addstring(&buffer, CRLF);
|
|
|
|
}
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(&buffer, *input);
|
2004-02-04 15:29:11 +01:00
|
|
|
left--;
|
|
|
|
break;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
2004-02-04 15:29:11 +01:00
|
|
|
input++;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
luaL_pushresult(&buffer);
|
|
|
|
lua_pushnumber(L, left);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Fill base64 decode map.
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-03-10 03:23:48 +01:00
|
|
|
#if 0
|
2015-02-18 23:51:37 +01:00
|
|
|
static void b64setup(UC *unbase)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
|
|
|
int i;
|
2011-04-27 19:42:20 +02:00
|
|
|
for (i = 0; i <= 255; i++) unbase[i] = (UC) 255;
|
|
|
|
for (i = 0; i < 64; i++) unbase[b64base[i]] = (UC) i;
|
|
|
|
unbase['='] = 0;
|
2019-03-10 03:23:48 +01:00
|
|
|
|
|
|
|
printf("static const UC b64unbase[] = {\n");
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
printf("%d, ", unbase[i]);
|
|
|
|
}
|
|
|
|
printf("\n}\n;");
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
2019-03-10 03:23:48 +01:00
|
|
|
#endif
|
2004-01-19 06:41:30 +01:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Acumulates bytes in input buffer until 3 bytes are available.
|
2004-01-19 06:41:30 +01:00
|
|
|
* Translate the 3 bytes into Base64 form and append to buffer.
|
|
|
|
* Returns new number of bytes in buffer.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2015-02-18 23:51:37 +01:00
|
|
|
static size_t b64encode(UC c, UC *input, size_t size,
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer *buffer)
|
|
|
|
{
|
|
|
|
input[size++] = c;
|
|
|
|
if (size == 3) {
|
|
|
|
UC code[4];
|
|
|
|
unsigned long value = 0;
|
|
|
|
value += input[0]; value <<= 8;
|
|
|
|
value += input[1]; value <<= 8;
|
2015-02-18 23:51:37 +01:00
|
|
|
value += input[2];
|
2004-01-19 06:41:30 +01:00
|
|
|
code[3] = b64base[value & 0x3f]; value >>= 6;
|
|
|
|
code[2] = b64base[value & 0x3f]; value >>= 6;
|
|
|
|
code[1] = b64base[value & 0x3f]; value >>= 6;
|
|
|
|
code[0] = b64base[value];
|
2004-01-19 07:07:17 +01:00
|
|
|
luaL_addlstring(buffer, (char *) code, 4);
|
2004-01-19 06:41:30 +01:00
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Encodes the Base64 last 1 or 2 bytes and adds padding '='
|
2004-01-19 06:41:30 +01:00
|
|
|
* Result, if any, is appended to buffer.
|
|
|
|
* Returns 0.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2015-02-18 23:51:37 +01:00
|
|
|
static size_t b64pad(const UC *input, size_t size,
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer *buffer)
|
|
|
|
{
|
|
|
|
unsigned long value = 0;
|
2005-11-20 08:20:26 +01:00
|
|
|
UC code[4] = {'=', '=', '=', '='};
|
2004-01-19 06:41:30 +01:00
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
value = input[0] << 4;
|
|
|
|
code[1] = b64base[value & 0x3f]; value >>= 6;
|
|
|
|
code[0] = b64base[value];
|
2004-01-19 07:07:17 +01:00
|
|
|
luaL_addlstring(buffer, (char *) code, 4);
|
2004-01-19 06:41:30 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2015-02-18 23:51:37 +01:00
|
|
|
value = input[0]; value <<= 8;
|
2004-01-19 06:41:30 +01:00
|
|
|
value |= input[1]; value <<= 2;
|
|
|
|
code[2] = b64base[value & 0x3f]; value >>= 6;
|
|
|
|
code[1] = b64base[value & 0x3f]; value >>= 6;
|
|
|
|
code[0] = b64base[value];
|
2004-01-19 07:07:17 +01:00
|
|
|
luaL_addlstring(buffer, (char *) code, 4);
|
2004-01-19 06:41:30 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Acumulates bytes in input buffer until 4 bytes are available.
|
2004-01-19 06:41:30 +01:00
|
|
|
* Translate the 4 bytes from Base64 form and append to buffer.
|
|
|
|
* Returns new number of bytes in buffer.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2015-02-18 23:51:37 +01:00
|
|
|
static size_t b64decode(UC c, UC *input, size_t size,
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer *buffer)
|
|
|
|
{
|
|
|
|
/* ignore invalid characters */
|
|
|
|
if (b64unbase[c] > 64) return size;
|
|
|
|
input[size++] = c;
|
|
|
|
/* decode atom */
|
|
|
|
if (size == 4) {
|
|
|
|
UC decoded[3];
|
|
|
|
int valid, value = 0;
|
|
|
|
value = b64unbase[input[0]]; value <<= 6;
|
|
|
|
value |= b64unbase[input[1]]; value <<= 6;
|
|
|
|
value |= b64unbase[input[2]]; value <<= 6;
|
|
|
|
value |= b64unbase[input[3]];
|
|
|
|
decoded[2] = (UC) (value & 0xff); value >>= 8;
|
|
|
|
decoded[1] = (UC) (value & 0xff); value >>= 8;
|
|
|
|
decoded[0] = (UC) value;
|
|
|
|
/* take care of paddding */
|
2015-02-18 23:51:37 +01:00
|
|
|
valid = (input[2] == '=') ? 1 : (input[3] == '=') ? 2 : 3;
|
2004-01-19 07:07:17 +01:00
|
|
|
luaL_addlstring(buffer, (char *) decoded, valid);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 0;
|
|
|
|
/* need more data */
|
|
|
|
} else return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Incrementally applies the Base64 transfer content encoding to a string
|
|
|
|
* A, B = b64(C, D)
|
|
|
|
* A is the encoded version of the largest prefix of C .. D that is
|
|
|
|
* divisible by 3. B has the remaining bytes of C .. D, *without* encoding.
|
2015-02-18 23:51:37 +01:00
|
|
|
* The easiest thing would be to concatenate the two strings and
|
2004-01-19 06:41:30 +01:00
|
|
|
* encode the result, but we can't afford that or Lua would dupplicate
|
|
|
|
* every chunk we received.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int mime_global_b64(lua_State *L)
|
|
|
|
{
|
|
|
|
UC atom[3];
|
|
|
|
size_t isize = 0, asize = 0;
|
2014-12-21 06:57:10 +01:00
|
|
|
const UC *input = (const UC *) luaL_optlstring(L, 1, NULL, &isize);
|
2004-01-19 06:41:30 +01:00
|
|
|
const UC *last = input + isize;
|
|
|
|
luaL_Buffer buffer;
|
2004-03-16 07:42:53 +01:00
|
|
|
/* end-of-input blackhole */
|
|
|
|
if (!input) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-08-01 08:06:09 +02:00
|
|
|
/* make sure we don't confuse buffer stuff with arguments */
|
|
|
|
lua_settop(L, 2);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* process first part of the input */
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_buffinit(L, &buffer);
|
2015-02-18 23:51:37 +01:00
|
|
|
while (input < last)
|
2004-01-19 06:41:30 +01:00
|
|
|
asize = b64encode(*input++, atom, asize, &buffer);
|
2014-12-21 06:57:10 +01:00
|
|
|
input = (const UC *) luaL_optlstring(L, 2, NULL, &isize);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* if second part is nil, we are done */
|
|
|
|
if (!input) {
|
2009-05-27 11:31:38 +02:00
|
|
|
size_t osize = 0;
|
2004-01-19 06:41:30 +01:00
|
|
|
asize = b64pad(atom, asize, &buffer);
|
2004-03-16 07:42:53 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2009-05-27 11:31:38 +02:00
|
|
|
/* if the output is empty and the input is nil, return nil */
|
|
|
|
lua_tolstring(L, -1, &osize);
|
|
|
|
if (osize == 0) lua_pushnil(L);
|
2004-03-16 07:42:53 +01:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
/* otherwise process the second part */
|
|
|
|
last = input + isize;
|
2015-02-18 23:51:37 +01:00
|
|
|
while (input < last)
|
2004-03-16 07:42:53 +01:00
|
|
|
asize = b64encode(*input++, atom, asize, &buffer);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2004-01-19 07:07:17 +01:00
|
|
|
lua_pushlstring(L, (char *) atom, asize);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Incrementally removes the Base64 transfer content encoding from a string
|
|
|
|
* A, B = b64(C, D)
|
|
|
|
* A is the encoded version of the largest prefix of C .. D that is
|
|
|
|
* divisible by 4. B has the remaining bytes of C .. D, *without* encoding.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int mime_global_unb64(lua_State *L)
|
|
|
|
{
|
|
|
|
UC atom[4];
|
|
|
|
size_t isize = 0, asize = 0;
|
2014-12-21 06:57:10 +01:00
|
|
|
const UC *input = (const UC *) luaL_optlstring(L, 1, NULL, &isize);
|
2004-01-19 06:41:30 +01:00
|
|
|
const UC *last = input + isize;
|
|
|
|
luaL_Buffer buffer;
|
2004-03-16 07:42:53 +01:00
|
|
|
/* end-of-input blackhole */
|
|
|
|
if (!input) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-08-01 08:06:09 +02:00
|
|
|
/* make sure we don't confuse buffer stuff with arguments */
|
|
|
|
lua_settop(L, 2);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* process first part of the input */
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_buffinit(L, &buffer);
|
2015-02-18 23:51:37 +01:00
|
|
|
while (input < last)
|
2004-01-19 06:41:30 +01:00
|
|
|
asize = b64decode(*input++, atom, asize, &buffer);
|
2014-12-21 06:57:10 +01:00
|
|
|
input = (const UC *) luaL_optlstring(L, 2, NULL, &isize);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* if second is nil, we are done */
|
|
|
|
if (!input) {
|
2009-05-27 11:31:38 +02:00
|
|
|
size_t osize = 0;
|
2004-03-16 07:42:53 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2009-05-27 11:31:38 +02:00
|
|
|
/* if the output is empty and the input is nil, return nil */
|
|
|
|
lua_tolstring(L, -1, &osize);
|
|
|
|
if (osize == 0) lua_pushnil(L);
|
2004-03-16 07:42:53 +01:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
/* otherwise, process the rest of the input */
|
|
|
|
last = input + isize;
|
2015-02-18 23:51:37 +01:00
|
|
|
while (input < last)
|
2004-03-16 07:42:53 +01:00
|
|
|
asize = b64decode(*input++, atom, asize, &buffer);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2004-01-19 07:07:17 +01:00
|
|
|
lua_pushlstring(L, (char *) atom, asize);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Quoted-printable encoding scheme
|
|
|
|
* all (except CRLF in text) can be =XX
|
|
|
|
* CLRL in not text must be =XX=XX
|
|
|
|
* 33 through 60 inclusive can be plain
|
2004-05-25 07:27:44 +02:00
|
|
|
* 62 through 126 inclusive can be plain
|
2004-01-19 06:41:30 +01:00
|
|
|
* 9 and 32 can be plain, unless in the end of a line, where must be =XX
|
|
|
|
* encoded lines must be no longer than 76 not counting CRLF
|
|
|
|
* soft line-break are =CRLF
|
2015-02-18 23:51:37 +01:00
|
|
|
* To encode one byte, we need to see the next two.
|
2004-01-19 06:41:30 +01:00
|
|
|
* Worst case is when we see a space, and wonder if a CRLF is comming
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Split quoted-printable characters into classes
|
|
|
|
* Precompute reverse map for encoding
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-03-10 03:23:48 +01:00
|
|
|
#if 0
|
2011-04-27 19:42:20 +02:00
|
|
|
static void qpsetup(UC *cl, UC *unbase)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
2019-03-10 03:23:48 +01:00
|
|
|
|
2004-01-19 06:41:30 +01:00
|
|
|
int i;
|
2011-04-27 19:42:20 +02:00
|
|
|
for (i = 0; i < 256; i++) cl[i] = QP_QUOTED;
|
|
|
|
for (i = 33; i <= 60; i++) cl[i] = QP_PLAIN;
|
|
|
|
for (i = 62; i <= 126; i++) cl[i] = QP_PLAIN;
|
2015-02-18 23:51:37 +01:00
|
|
|
cl['\t'] = QP_IF_LAST;
|
2011-04-27 19:42:20 +02:00
|
|
|
cl[' '] = QP_IF_LAST;
|
|
|
|
cl['\r'] = QP_CR;
|
|
|
|
for (i = 0; i < 256; i++) unbase[i] = 255;
|
|
|
|
unbase['0'] = 0; unbase['1'] = 1; unbase['2'] = 2;
|
|
|
|
unbase['3'] = 3; unbase['4'] = 4; unbase['5'] = 5;
|
|
|
|
unbase['6'] = 6; unbase['7'] = 7; unbase['8'] = 8;
|
|
|
|
unbase['9'] = 9; unbase['A'] = 10; unbase['a'] = 10;
|
|
|
|
unbase['B'] = 11; unbase['b'] = 11; unbase['C'] = 12;
|
|
|
|
unbase['c'] = 12; unbase['D'] = 13; unbase['d'] = 13;
|
|
|
|
unbase['E'] = 14; unbase['e'] = 14; unbase['F'] = 15;
|
|
|
|
unbase['f'] = 15;
|
2019-03-10 03:23:48 +01:00
|
|
|
|
|
|
|
printf("static UC qpclass[] = {");
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
if (i % 6 == 0) {
|
|
|
|
printf("\n ");
|
|
|
|
}
|
|
|
|
switch(cl[i]) {
|
|
|
|
case QP_QUOTED:
|
|
|
|
printf("QP_QUOTED, ");
|
|
|
|
break;
|
|
|
|
case QP_PLAIN:
|
|
|
|
printf("QP_PLAIN, ");
|
|
|
|
break;
|
|
|
|
case QP_CR:
|
|
|
|
printf("QP_CR, ");
|
|
|
|
break;
|
|
|
|
case QP_IF_LAST:
|
|
|
|
printf("QP_IF_LAST, ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n};\n");
|
|
|
|
|
|
|
|
printf("static const UC qpunbase[] = {");
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
int c = qpunbase[i];
|
|
|
|
printf("%d, ", c);
|
|
|
|
}
|
|
|
|
printf("\";\n");
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
2019-03-10 03:23:48 +01:00
|
|
|
#endif
|
2004-01-19 06:41:30 +01:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Output one character in form =XX
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static void qpquote(UC c, luaL_Buffer *buffer)
|
|
|
|
{
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(buffer, '=');
|
|
|
|
luaL_addchar(buffer, qpbase[c >> 4]);
|
|
|
|
luaL_addchar(buffer, qpbase[c & 0x0F]);
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Accumulate characters until we are sure about how to deal with them.
|
2015-02-18 23:51:37 +01:00
|
|
|
* Once we are sure, output to the buffer, in the correct form.
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2015-02-18 23:51:37 +01:00
|
|
|
static size_t qpencode(UC c, UC *input, size_t size,
|
2004-01-19 16:38:33 +01:00
|
|
|
const char *marker, luaL_Buffer *buffer)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
|
|
|
input[size++] = c;
|
|
|
|
/* deal with all characters we can have */
|
|
|
|
while (size > 0) {
|
|
|
|
switch (qpclass[input[0]]) {
|
|
|
|
/* might be the CR of a CRLF sequence */
|
|
|
|
case QP_CR:
|
|
|
|
if (size < 2) return size;
|
2004-05-25 07:27:44 +02:00
|
|
|
if (input[1] == '\n') {
|
2004-01-19 16:38:33 +01:00
|
|
|
luaL_addstring(buffer, marker);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 0;
|
|
|
|
} else qpquote(input[0], buffer);
|
|
|
|
break;
|
|
|
|
/* might be a space and that has to be quoted if last in line */
|
|
|
|
case QP_IF_LAST:
|
|
|
|
if (size < 3) return size;
|
|
|
|
/* if it is the last, quote it and we are done */
|
2004-05-25 07:27:44 +02:00
|
|
|
if (input[1] == '\r' && input[2] == '\n') {
|
2004-01-19 06:41:30 +01:00
|
|
|
qpquote(input[0], buffer);
|
2004-01-19 16:38:33 +01:00
|
|
|
luaL_addstring(buffer, marker);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 0;
|
2011-07-05 00:31:14 +02:00
|
|
|
} else luaL_addchar(buffer, input[0]);
|
2004-01-19 06:41:30 +01:00
|
|
|
break;
|
|
|
|
/* might have to be quoted always */
|
|
|
|
case QP_QUOTED:
|
|
|
|
qpquote(input[0], buffer);
|
|
|
|
break;
|
|
|
|
/* might never have to be quoted */
|
|
|
|
default:
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(buffer, input[0]);
|
2004-01-19 06:41:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
input[0] = input[1]; input[1] = input[2];
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Deal with the final characters
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-02-04 15:29:11 +01:00
|
|
|
static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < size; i++) {
|
2011-07-05 00:31:14 +02:00
|
|
|
if (qpclass[input[i]] == QP_PLAIN) luaL_addchar(buffer, input[i]);
|
2004-01-19 06:41:30 +01:00
|
|
|
else qpquote(input[i], buffer);
|
|
|
|
}
|
2004-11-28 03:36:07 +01:00
|
|
|
if (size > 0) luaL_addstring(buffer, EQCRLF);
|
2004-02-04 15:29:11 +01:00
|
|
|
return 0;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Incrementally converts a string to quoted-printable
|
|
|
|
* A, B = qp(C, D, marker)
|
2004-02-04 15:29:11 +01:00
|
|
|
* Marker is the text to be used to replace CRLF sequences found in A.
|
2015-02-18 23:51:37 +01:00
|
|
|
* A is the encoded version of the largest prefix of C .. D that
|
|
|
|
* can be encoded without doubts.
|
2004-01-19 06:41:30 +01:00
|
|
|
* B has the remaining bytes of C .. D, *without* encoding.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int mime_global_qp(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t asize = 0, isize = 0;
|
|
|
|
UC atom[3];
|
2014-12-21 06:57:10 +01:00
|
|
|
const UC *input = (const UC *) luaL_optlstring(L, 1, NULL, &isize);
|
2004-01-19 06:41:30 +01:00
|
|
|
const UC *last = input + isize;
|
2004-01-19 16:38:33 +01:00
|
|
|
const char *marker = luaL_optstring(L, 3, CRLF);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer buffer;
|
2004-03-16 07:42:53 +01:00
|
|
|
/* end-of-input blackhole */
|
|
|
|
if (!input) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-08-01 08:06:09 +02:00
|
|
|
/* make sure we don't confuse buffer stuff with arguments */
|
|
|
|
lua_settop(L, 3);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* process first part of input */
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_buffinit(L, &buffer);
|
|
|
|
while (input < last)
|
|
|
|
asize = qpencode(*input++, atom, asize, marker, &buffer);
|
2014-12-21 06:57:10 +01:00
|
|
|
input = (const UC *) luaL_optlstring(L, 2, NULL, &isize);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* if second part is nil, we are done */
|
|
|
|
if (!input) {
|
2004-02-04 15:29:11 +01:00
|
|
|
asize = qppad(atom, asize, &buffer);
|
2004-03-16 07:42:53 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2004-11-28 03:36:07 +01:00
|
|
|
if (!(*lua_tostring(L, -1))) lua_pushnil(L);
|
2004-03-16 07:42:53 +01:00
|
|
|
lua_pushnil(L);
|
2004-11-28 03:36:07 +01:00
|
|
|
return 2;
|
2004-03-16 07:42:53 +01:00
|
|
|
}
|
|
|
|
/* otherwise process rest of input */
|
|
|
|
last = input + isize;
|
|
|
|
while (input < last)
|
|
|
|
asize = qpencode(*input++, atom, asize, marker, &buffer);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2004-01-19 07:07:17 +01:00
|
|
|
lua_pushlstring(L, (char *) atom, asize);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Accumulate characters until we are sure about how to deal with them.
|
2015-02-18 23:51:37 +01:00
|
|
|
* Once we are sure, output the to the buffer, in the correct form.
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2005-11-20 08:20:26 +01:00
|
|
|
static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer) {
|
|
|
|
int d;
|
2004-01-19 06:41:30 +01:00
|
|
|
input[size++] = c;
|
|
|
|
/* deal with all characters we can deal */
|
2005-11-20 08:20:26 +01:00
|
|
|
switch (input[0]) {
|
|
|
|
/* if we have an escape character */
|
2015-02-18 23:51:37 +01:00
|
|
|
case '=':
|
|
|
|
if (size < 3) return size;
|
2005-11-20 08:20:26 +01:00
|
|
|
/* eliminate soft line break */
|
|
|
|
if (input[1] == '\r' && input[2] == '\n') return 0;
|
|
|
|
/* decode quoted representation */
|
|
|
|
c = qpunbase[input[1]]; d = qpunbase[input[2]];
|
|
|
|
/* if it is an invalid, do not decode */
|
|
|
|
if (c > 15 || d > 15) luaL_addlstring(buffer, (char *)input, 3);
|
2012-12-11 20:43:49 +01:00
|
|
|
else luaL_addchar(buffer, (char) ((c << 4) + d));
|
2005-11-20 08:20:26 +01:00
|
|
|
return 0;
|
|
|
|
case '\r':
|
2015-02-18 23:51:37 +01:00
|
|
|
if (size < 2) return size;
|
2005-11-20 08:20:26 +01:00
|
|
|
if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
if (input[0] == '\t' || (input[0] > 31 && input[0] < 127))
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(buffer, input[0]);
|
2005-11-20 08:20:26 +01:00
|
|
|
return 0;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Incrementally decodes a string in quoted-printable
|
|
|
|
* A, B = qp(C, D)
|
2015-02-18 23:51:37 +01:00
|
|
|
* A is the decoded version of the largest prefix of C .. D that
|
|
|
|
* can be decoded without doubts.
|
2004-01-19 06:41:30 +01:00
|
|
|
* B has the remaining bytes of C .. D, *without* decoding.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int mime_global_unqp(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t asize = 0, isize = 0;
|
|
|
|
UC atom[3];
|
2014-12-21 06:57:10 +01:00
|
|
|
const UC *input = (const UC *) luaL_optlstring(L, 1, NULL, &isize);
|
2004-01-19 06:41:30 +01:00
|
|
|
const UC *last = input + isize;
|
|
|
|
luaL_Buffer buffer;
|
2004-03-16 07:42:53 +01:00
|
|
|
/* end-of-input blackhole */
|
|
|
|
if (!input) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-08-01 08:06:09 +02:00
|
|
|
/* make sure we don't confuse buffer stuff with arguments */
|
|
|
|
lua_settop(L, 2);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* process first part of input */
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_buffinit(L, &buffer);
|
|
|
|
while (input < last)
|
|
|
|
asize = qpdecode(*input++, atom, asize, &buffer);
|
2014-12-21 06:57:10 +01:00
|
|
|
input = (const UC *) luaL_optlstring(L, 2, NULL, &isize);
|
2004-03-16 07:42:53 +01:00
|
|
|
/* if second part is nil, we are done */
|
|
|
|
if (!input) {
|
|
|
|
luaL_pushresult(&buffer);
|
2004-11-28 03:36:07 +01:00
|
|
|
if (!(*lua_tostring(L, -1))) lua_pushnil(L);
|
2004-03-16 07:42:53 +01:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
2015-02-18 23:51:37 +01:00
|
|
|
}
|
2004-03-16 07:42:53 +01:00
|
|
|
/* otherwise process rest of input */
|
|
|
|
last = input + isize;
|
|
|
|
while (input < last)
|
|
|
|
asize = qpdecode(*input++, atom, asize, &buffer);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2004-01-19 07:07:17 +01:00
|
|
|
lua_pushlstring(L, (char *) atom, asize);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Incrementally breaks a quoted-printed string into lines
|
2004-01-24 03:47:24 +01:00
|
|
|
* A, n = qpwrp(l, B, length)
|
2015-02-18 23:51:37 +01:00
|
|
|
* A is a copy of B, broken into lines of at most 'length' bytes.
|
|
|
|
* 'l' is how many bytes are left for the first line of B.
|
|
|
|
* 'n' is the number of bytes left in the last line of A.
|
2004-01-19 06:41:30 +01:00
|
|
|
* There are two complications: lines can't be broken in the middle
|
|
|
|
* of an encoded =XX, and there might be line breaks already
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 03:47:24 +01:00
|
|
|
static int mime_global_qpwrp(lua_State *L)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
2004-01-21 02:09:50 +01:00
|
|
|
int left = (int) luaL_checknumber(L, 1);
|
2014-12-21 06:57:10 +01:00
|
|
|
const UC *input = (const UC *) luaL_optlstring(L, 2, NULL, &size);
|
2004-01-19 06:41:30 +01:00
|
|
|
const UC *last = input + size;
|
2004-01-21 02:09:50 +01:00
|
|
|
int length = (int) luaL_optnumber(L, 3, 76);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer buffer;
|
2004-03-16 07:42:53 +01:00
|
|
|
/* end-of-input blackhole */
|
|
|
|
if (!input) {
|
|
|
|
if (left < length) lua_pushstring(L, EQCRLF);
|
|
|
|
else lua_pushnil(L);
|
|
|
|
lua_pushnumber(L, length);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
/* process all input */
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_buffinit(L, &buffer);
|
|
|
|
while (input < last) {
|
|
|
|
switch (*input) {
|
2004-05-25 07:27:44 +02:00
|
|
|
case '\r':
|
2004-01-19 06:41:30 +01:00
|
|
|
break;
|
2004-05-25 07:27:44 +02:00
|
|
|
case '\n':
|
2004-01-19 06:41:30 +01:00
|
|
|
left = length;
|
2004-02-04 15:29:11 +01:00
|
|
|
luaL_addstring(&buffer, CRLF);
|
2004-01-19 06:41:30 +01:00
|
|
|
break;
|
2004-02-04 15:29:11 +01:00
|
|
|
case '=':
|
|
|
|
if (left <= 3) {
|
|
|
|
left = length;
|
2004-01-19 16:38:33 +01:00
|
|
|
luaL_addstring(&buffer, EQCRLF);
|
2015-02-18 23:51:37 +01:00
|
|
|
}
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(&buffer, *input);
|
2004-02-04 15:29:11 +01:00
|
|
|
left--;
|
|
|
|
break;
|
2015-02-18 23:51:37 +01:00
|
|
|
default:
|
2004-02-04 15:29:11 +01:00
|
|
|
if (left <= 1) {
|
2004-01-19 06:41:30 +01:00
|
|
|
left = length;
|
2004-02-04 15:29:11 +01:00
|
|
|
luaL_addstring(&buffer, EQCRLF);
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(&buffer, *input);
|
2004-02-04 15:29:11 +01:00
|
|
|
left--;
|
|
|
|
break;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
input++;
|
|
|
|
}
|
|
|
|
luaL_pushresult(&buffer);
|
|
|
|
lua_pushnumber(L, left);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2004-02-04 15:29:11 +01:00
|
|
|
* Here is what we do: \n, and \r are considered candidates for line
|
2004-01-19 06:41:30 +01:00
|
|
|
* break. We issue *one* new line marker if any of them is seen alone, or
|
2004-02-04 15:29:11 +01:00
|
|
|
* followed by a different one. That is, \n\n and \r\r will issue two
|
|
|
|
* end of line markers each, but \r\n, \n\r etc will only issue *one*
|
2004-01-19 06:41:30 +01:00
|
|
|
* marker. This covers Mac OS, Mac OS X, VMS, Unix and DOS, as well as
|
|
|
|
* probably other more obscure conventions.
|
2004-03-21 08:50:15 +01:00
|
|
|
*
|
|
|
|
* c is the current character being processed
|
|
|
|
* last is the previous character
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-05-25 07:27:44 +02:00
|
|
|
#define eolcandidate(c) (c == '\r' || c == '\n')
|
2015-02-18 23:51:37 +01:00
|
|
|
static int eolprocess(int c, int last, const char *marker,
|
2004-02-11 04:31:53 +01:00
|
|
|
luaL_Buffer *buffer)
|
2004-01-19 06:41:30 +01:00
|
|
|
{
|
2004-03-21 08:50:15 +01:00
|
|
|
if (eolcandidate(c)) {
|
|
|
|
if (eolcandidate(last)) {
|
|
|
|
if (c == last) luaL_addstring(buffer, marker);
|
2004-02-11 04:31:53 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
2004-03-21 08:50:15 +01:00
|
|
|
luaL_addstring(buffer, marker);
|
|
|
|
return c;
|
2004-02-11 04:31:53 +01:00
|
|
|
}
|
2004-01-19 06:41:30 +01:00
|
|
|
} else {
|
2012-12-11 20:43:49 +01:00
|
|
|
luaL_addchar(buffer, (char) c);
|
2004-03-21 08:50:15 +01:00
|
|
|
return 0;
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Converts a string to uniform EOL convention.
|
2004-02-11 04:31:53 +01:00
|
|
|
* A, n = eol(o, B, marker)
|
|
|
|
* A is the converted version of the largest prefix of B that can be
|
2015-02-18 23:51:37 +01:00
|
|
|
* converted unambiguously. 'o' is the context returned by the previous
|
2004-02-11 04:31:53 +01:00
|
|
|
* call. 'n' is the new context.
|
2004-01-19 06:41:30 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int mime_global_eol(lua_State *L)
|
|
|
|
{
|
2018-08-22 22:37:32 +02:00
|
|
|
int ctx = (int) luaL_checkinteger(L, 1);
|
2004-02-11 04:31:53 +01:00
|
|
|
size_t isize = 0;
|
|
|
|
const char *input = luaL_optlstring(L, 2, NULL, &isize);
|
|
|
|
const char *last = input + isize;
|
2004-01-19 16:38:33 +01:00
|
|
|
const char *marker = luaL_optstring(L, 3, CRLF);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_Buffer buffer;
|
|
|
|
luaL_buffinit(L, &buffer);
|
2004-06-16 03:02:07 +02:00
|
|
|
/* end of input blackhole */
|
2004-02-11 04:31:53 +01:00
|
|
|
if (!input) {
|
2004-03-21 08:50:15 +01:00
|
|
|
lua_pushnil(L);
|
2004-03-16 07:42:53 +01:00
|
|
|
lua_pushnumber(L, 0);
|
|
|
|
return 2;
|
2004-02-04 15:29:11 +01:00
|
|
|
}
|
2004-03-16 07:42:53 +01:00
|
|
|
/* process all input */
|
|
|
|
while (input < last)
|
|
|
|
ctx = eolprocess(*input++, ctx, marker, &buffer);
|
2004-01-19 06:41:30 +01:00
|
|
|
luaL_pushresult(&buffer);
|
2004-02-11 04:31:53 +01:00
|
|
|
lua_pushnumber(L, ctx);
|
2004-01-19 06:41:30 +01:00
|
|
|
return 2;
|
|
|
|
}
|
2004-06-17 08:23:13 +02:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-02-18 23:51:37 +01:00
|
|
|
* Takes one byte and stuff it if needed.
|
2004-06-17 08:23:13 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static size_t dot(int c, size_t state, luaL_Buffer *buffer)
|
|
|
|
{
|
2012-12-11 20:43:49 +01:00
|
|
|
luaL_addchar(buffer, (char) c);
|
2004-06-17 08:23:13 +02:00
|
|
|
switch (c) {
|
2015-02-18 23:51:37 +01:00
|
|
|
case '\r':
|
2004-06-17 08:23:13 +02:00
|
|
|
return 1;
|
2015-02-18 23:51:37 +01:00
|
|
|
case '\n':
|
|
|
|
return (state == 1)? 2: 0;
|
|
|
|
case '.':
|
|
|
|
if (state == 2)
|
2011-07-05 00:31:14 +02:00
|
|
|
luaL_addchar(buffer, '.');
|
2018-08-06 15:30:13 +02:00
|
|
|
/* Falls through. */
|
2004-06-17 08:23:13 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Incrementally applies smtp stuffing to a string
|
|
|
|
* A, n = dot(l, D)
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int mime_global_dot(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t isize = 0, state = (size_t) luaL_checknumber(L, 1);
|
|
|
|
const char *input = luaL_optlstring(L, 2, NULL, &isize);
|
|
|
|
const char *last = input + isize;
|
|
|
|
luaL_Buffer buffer;
|
|
|
|
/* end-of-input blackhole */
|
|
|
|
if (!input) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnumber(L, 2);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
/* process all input */
|
|
|
|
luaL_buffinit(L, &buffer);
|
2015-02-18 23:51:37 +01:00
|
|
|
while (input < last)
|
2004-06-17 08:23:13 +02:00
|
|
|
state = dot(*input++, state, &buffer);
|
|
|
|
luaL_pushresult(&buffer);
|
2012-12-11 20:43:49 +01:00
|
|
|
lua_pushnumber(L, (lua_Number) state);
|
2004-06-17 08:23:13 +02:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|