summaryrefslogtreecommitdiff
path: root/source/luametatex/source/luarest/lmtbasexxlib.c
blob: fca7fcef868855edb8795479acb3d7c510c776ee (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
    See license.txt in the root of this project.
*/

# include "luametatex.h"

/* # define BASEXX_PDF 1 */

# include <utiliof.h>
# include <utilbasexx.h>
# include <utillzw.h>

/*tex

    First I had a mix of own code and LHF code (base64 and base85) but in the end I decided to reuse
    some of pplibs code. Performance is ok, although we can speed up the base16 coders. When needed,
    we can have a few more bur normally pure \LUA\ is quite ok for our purpose.

*/

# define encode_nl(L) \
    (lua_type(L, 2) == LUA_TNUMBER) ? (lmt_tointeger(L, 2)) : ( (lua_isboolean(L, 2)) ? 80 : 0 )

# define lua_iof_push(L,out) \
    lua_pushlstring(L,(const char *) out->buf, iof_size(out))

static int basexxlib_encode_16(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = 2 * l;
    size_t nl = encode_nl(L);
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    if (nl) {
        base16_encode_ln(inp, out, 0, nl);
    } else {
        base16_encode(inp, out);
    }
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static int basexxlib_decode_16(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = l / 2;
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    base16_decode(inp, out);
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static int basexxlib_encode_64(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L,1,&l);
    size_t n = 4 * l;
    size_t nl = encode_nl(L);
    iof *inp = iof_filter_string_reader(s,l);
    iof *out = iof_filter_buffer_writer(n);
    if (nl) {
        base64_encode_ln(inp,out,0,nl);
    } else {
        base64_encode(inp,out);
    }
    lua_iof_push(L,out);
    iof_close(out);
    return 1;
}

static int basexxlib_decode_64(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = l;
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    base64_decode(inp, out);
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static int basexxlib_encode_85(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = 5 * l;
    size_t nl = encode_nl(L);
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    if (nl) {
        base85_encode_ln(inp, out, 0, 80);
    } else {
        base85_encode(inp, out);
    }
    lua_iof_push(L,out);
    iof_close(out);
    return 1;
}

static int basexxlib_decode_85(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = l;
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    base85_decode(inp, out);
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static int basexxlib_encode_RL(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = 2 * l;
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    runlength_encode(inp, out);
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static int basexxlib_decode_RL(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = 2 * l;
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    runlength_decode(inp, out);
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static int basexxlib_encode_LZW(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = 2 * l;
    char *t = lmt_memory_malloc(n);
    int flags = lmt_optinteger(L, 2, LZW_ENCODER_DEFAULTS);
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_string_writer(t, n);
    lzw_encode(inp, out, flags);
    lua_pushlstring(L, t, iof_size(out));
    lmt_memory_free(t);
    return 1;
}

static int basexxlib_decode_LZW(lua_State *L)
{
    size_t l;
    const unsigned char *s = (const unsigned char*) luaL_checklstring(L, 1, &l);
    size_t n = 2 * l;
    iof *inp = iof_filter_string_reader(s, l);
    iof *out = iof_filter_buffer_writer(n);
    int flags = lmt_optinteger(L, 2, LZW_DECODER_DEFAULTS);
    lzw_decode(inp, out, flags);
    lua_iof_push(L, out);
    iof_close(out);
    return 1;
}

static struct luaL_Reg basexxlib_function_list[] = {
    { "encode16",  basexxlib_encode_16  },
    { "decode16",  basexxlib_decode_16  },
    { "encode64",  basexxlib_encode_64  },
    { "decode64",  basexxlib_decode_64  },
    { "encode85",  basexxlib_encode_85  },
    { "decode85",  basexxlib_decode_85  },
    { "encodeRL",  basexxlib_encode_RL  },
    { "decodeRL",  basexxlib_decode_RL  },
    { "encodeLZW", basexxlib_encode_LZW },
    { "decodeLZW", basexxlib_decode_LZW },
    { NULL,        NULL                 },
};

int luaopen_basexx(lua_State *L) {
    lua_newtable(L);
    luaL_setfuncs(L, basexxlib_function_list, 0);
    return 1;
}