lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I've run into the following unexpected behaviour (Lua 5.1.2, LuaSocket 2.0.2):

> require 'mime'
> binary=string.char(0x00,0x44,0x1D,0x14,0x0F,0xF4,0xDA,0x11,0xA9,0x78,0x00,0x14,0x38,0x50,0x60,0xCE)
> =#binary
16
> base64=mime.b64(binary)
> =base64
AEQdFA/02hGpeAAUOFBgzg==
> decoded=mime.unb64(base64)
> =decoded
nil
>

I've dug through the sources and found in mime.c:

static int mime_global_unb64(lua_State *L)
{
    UC atom[4];
    size_t isize = 0, asize = 0;
    const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize);
    const UC *last = input + isize;
    luaL_Buffer buffer;
    /* end-of-input blackhole */
    if (!input) {
        lua_pushnil(L);
        lua_pushnil(L);
        return 2;
    }
    /* process first part of the input */
    luaL_buffinit(L, &buffer);
    while (input < last)
        asize = b64decode(*input++, atom, asize, &buffer);
    input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
    /* if second is nil, we are done */
    if (!input) {
        luaL_pushresult(&buffer);
        if (!(*lua_tostring(L, -1))) lua_pushnil(L);    /* <----- ??? */
        lua_pushnil(L);
        return 2;
    }
    /* otherwise, process the rest of the input */
    last = input + isize;
    while (input < last)
        asize = b64decode(*input++, atom, asize, &buffer);
    luaL_pushresult(&buffer);
    lua_pushlstring(L, (char *) atom, asize);
    return 2;
}

There's a line in there (marked by my ??? comment) that attempts to
convert the just pushed string of decoded binary into a Lua string.
But this can quite easily fail; it is after all a buffer of binary
bytes, not a "normal" string. Removing that line makes the decoding
work fine and proper.

I can see a similar line in the mime_global_b64() function, where it
makes proper sense. Looks a little bit like a cut-n-paste error to me.

Robby