Changeset 3bfc79e1f3545570168565ca6300ef50bb20841b
- Timestamp:
- 11/18/10 21:28:34
(3 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1290115714 +0000
- git-parent:
[3821135574760be709319f24bb718263569486b4]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1290115714 +0000
- Message:
fixes #327
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| refa9375 |
r3bfc79e |
|
| 1112 | 1112 | } |
|---|
| 1113 | 1113 | |
|---|
| | 1114 | struct gunzip_crutch { |
|---|
| | 1115 | z_stream *stream; |
|---|
| | 1116 | void *scratch_buffer; |
|---|
| | 1117 | }; |
|---|
| 1114 | 1118 | static int |
|---|
| 1115 | 1119 | nl_gunzip_deflate(lua_State *L) { |
|---|
| | 1120 | struct gunzip_crutch *crutch; |
|---|
| 1116 | 1121 | const char *input; |
|---|
| 1117 | 1122 | size_t inlen; |
|---|
| … | … | |
| 1127 | 1132 | } |
|---|
| 1128 | 1133 | |
|---|
| 1129 | | stream = lua_touserdata(L, lua_upvalueindex(1)); |
|---|
| | 1134 | crutch = lua_touserdata(L, lua_upvalueindex(1)); |
|---|
| | 1135 | stream = crutch->stream; |
|---|
| 1130 | 1136 | |
|---|
| 1131 | 1137 | input = lua_tolstring(L, 1, &inlen); |
|---|
| … | … | |
| 1177 | 1183 | return 1; |
|---|
| 1178 | 1184 | } |
|---|
| | 1185 | if(data) free(data); |
|---|
| | 1186 | switch(err) { |
|---|
| | 1187 | case Z_NEED_DICT: luaL_error(L, "zlib: dictionary error"); break; |
|---|
| | 1188 | case Z_STREAM_ERROR: luaL_error(L, "zlib: stream error"); break; |
|---|
| | 1189 | case Z_DATA_ERROR: luaL_error(L, "zlib: data error"); break; |
|---|
| | 1190 | case Z_MEM_ERROR: luaL_error(L, "zlib: out-of-memory"); break; |
|---|
| | 1191 | case Z_BUF_ERROR: luaL_error(L, "zlib: buffer error"); break; |
|---|
| | 1192 | case Z_VERSION_ERROR: luaL_error(L, "zlib: version mismatch"); break; |
|---|
| | 1193 | case Z_ERRNO: luaL_error(L, strerror(errno)); break; |
|---|
| | 1194 | } |
|---|
| 1179 | 1195 | lua_pushnil(L); |
|---|
| 1180 | 1196 | return 1; |
|---|
| … | … | |
| 1182 | 1198 | static int |
|---|
| 1183 | 1199 | nl_gunzip(lua_State *L) { |
|---|
| | 1200 | struct gunzip_crutch *crutch; |
|---|
| 1184 | 1201 | z_stream *stream; |
|---|
| 1185 | 1202 | |
|---|
| 1186 | | stream = (z_stream *)lua_newuserdata(L, sizeof(*stream)); |
|---|
| 1187 | | memset(stream, 0, sizeof(*stream)); |
|---|
| | 1203 | crutch = (struct gunzip_crutch *)lua_newuserdata(L, sizeof(*crutch)); |
|---|
| | 1204 | crutch->stream = malloc(sizeof(*stream)); |
|---|
| | 1205 | memset(crutch->stream, 0, sizeof(*crutch->stream)); |
|---|
| 1188 | 1206 | luaL_getmetatable(L, "noit.gunzip"); |
|---|
| 1189 | 1207 | lua_setmetatable(L, -2); |
|---|
| 1190 | 1208 | |
|---|
| 1191 | | stream->next_in = NULL; |
|---|
| 1192 | | stream->avail_in = 0; |
|---|
| 1193 | | stream->next_out = malloc(DEFLATE_CHUNK_SIZE); |
|---|
| 1194 | | stream->avail_out = stream->next_out ? DEFLATE_CHUNK_SIZE : 0; |
|---|
| 1195 | | inflateInit2(stream, MAX_WBITS+32); |
|---|
| | 1209 | crutch->stream->next_in = NULL; |
|---|
| | 1210 | crutch->stream->avail_in = 0; |
|---|
| | 1211 | crutch->scratch_buffer = |
|---|
| | 1212 | crutch->stream->next_out = malloc(DEFLATE_CHUNK_SIZE); |
|---|
| | 1213 | crutch->stream->avail_out = crutch->stream->next_out ? DEFLATE_CHUNK_SIZE : 0; |
|---|
| | 1214 | inflateInit2(crutch->stream, MAX_WBITS+32); |
|---|
| 1196 | 1215 | |
|---|
| 1197 | 1216 | lua_pushcclosure(L, nl_gunzip_deflate, 1); |
|---|
| … | … | |
| 1200 | 1219 | static int |
|---|
| 1201 | 1220 | noit_lua_gunzip_gc(lua_State *L) { |
|---|
| 1202 | | z_stream *stream; |
|---|
| 1203 | | stream = (z_stream *)lua_touserdata(L,1); |
|---|
| 1204 | | if(stream->next_out) free(stream->next_out); |
|---|
| 1205 | | inflateEnd(stream); |
|---|
| | 1221 | struct gunzip_crutch *crutch; |
|---|
| | 1222 | crutch = (struct gunzip_crutch *)lua_touserdata(L,1); |
|---|
| | 1223 | if(crutch->scratch_buffer) free(crutch->scratch_buffer); |
|---|
| | 1224 | inflateEnd(crutch->stream); |
|---|
| 1206 | 1225 | return 0; |
|---|
| 1207 | 1226 | } |
|---|