Example #1
0
static __LA_SSIZE_T ar_write_cb(struct archive * self,
                                void *opaque,
                                const void *buff, size_t len)
{
    size_t result;
    lua_State* L = (lua_State*)opaque;

    // We are missing!?
    if ( ! ar_registry_get(L, self) ) {
        archive_set_error(self, 0,
                          "InternalError: write callback called on archive that should already have been garbage collected!");
        return -1;
    }

    ar_write_get_writer(L, -1); // {ud}, writer
    lua_pushvalue(L, -2); // {ud}, writer, {ud}
    lua_pushlstring(L, (const char *)buff, len); // {ud}, writer, {ud}, str

    if ( 0 != lua_pcall(L, 2, 1, 0) ) { // {ud}, "err"
        archive_set_error(self, 0, "%s", lua_tostring(L, -1));
        lua_pop(L, 2); // <nothing>
        return -1;
    }
    result = lua_tointeger(L, -1); // {ud}, result
    lua_pop(L, 2); // <nothing>

    return result;
}
Example #2
0
static __LA_SSIZE_T ar_read_cb(struct archive * self,
                               void *opaque,
                               const void **result)
{
    lua_State* L = (lua_State*)opaque;
    size_t result_len;
    *result = NULL;

    // We are missing!?
    if ( ! ar_registry_get(L, self) ) {
        archive_set_error(self, 0,
                          "InternalError: read callback called on archive that should already have been garbage collected!");
        return -1;
    }

    ar_read_get_reader(L, -1); // {ud}, reader
    lua_pushvalue(L, -2); // {ud}, reader, {ud}

    if ( 0 != lua_pcall(L, 1, 1, 0) ) { // {ud}, "err"
        archive_set_error(self, 0, "%s", lua_tostring(L, -1));
        lua_pop(L, 2); // <nothing>
        return -1;
    }

    *result = lua_tolstring(L, -1, &result_len); // {ud}, result

    // We directly return the raw internal buffer, so we need to keep
    // a reference around:
    lua_getfenv(L, -2); // {ud}, result, {fenv}
    lua_insert(L, -2); // {ud}, {fenv}, result
    lua_pushliteral(L, "read_buffer"); // {ud}, {fenv}, result, "read_buffer"
    lua_insert(L, -2); // {ud}, {fenv}, "read_buffer", result
    lua_rawset(L, -3); // {ud}, {fenv}
    lua_pop(L, 2); // <nothing>

    return result_len;
}