Blame src/mime.c

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