8214ae9f8720b4c66b9101886f76e18b8ce5274c
[openwrt.git] / package / lua / patches / 040-gzip-source-loader.patch
1 diff -ur lua-5.1.4.orig/src/Makefile lua-5.1.4/src/Makefile
2 --- lua-5.1.4.orig/src/Makefile 2009-04-04 23:06:04.000000000 +0200
3 +++ lua-5.1.4/src/Makefile      2009-04-04 23:06:15.000000000 +0200
4 @@ -12,7 +12,7 @@
5  AR= ar rcu
6  RANLIB= ranlib
7  RM= rm -f
8 -LIBS= -lm $(MYLIBS)
9 +LIBS= -lm -lz $(MYLIBS)
10  
11  MYCFLAGS=
12  MYLDFLAGS=
13 diff -ur lua-5.1.4.orig/src/lauxlib.c lua-5.1.4/src/lauxlib.c
14 --- lua-5.1.4.orig/src/lauxlib.c        2009-04-04 23:06:04.000000000 +0200
15 +++ lua-5.1.4/src/lauxlib.c     2009-04-05 03:35:24.000000000 +0200
16 @@ -11,6 +11,7 @@
17  #include <stdio.h>
18  #include <stdlib.h>
19  #include <string.h>
20 +#include <zlib.h>
21  
22  
23  /* This file uses only the official API of Lua.
24 @@ -535,6 +536,12 @@
25    char buff[LUAL_BUFFERSIZE];
26  } LoadF;
27  
28 +typedef struct LoadGZ {
29 +  int first_chunk;
30 +  gzFile f;
31 +  char buffer[LUAL_GZLDBUFFER];
32 +} LoadGZ;
33 +
34  
35  static const char *getF (lua_State *L, void *ud, size_t *size) {
36    LoadF *lf = (LoadF *)ud;
37 @@ -550,6 +557,28 @@
38  }
39  
40  
41 +static const char *getGZ (lua_State *L, void *ud, size_t *size) {
42 +  LoadGZ *lf = (LoadGZ *)ud;
43 +  char *sp = 0;
44 +  (void)L;
45 +  if (gzeof(lf->f)) return NULL;
46 +  *size = gzread(lf->f, lf->buffer, sizeof(lf->buffer));
47 +  if (*size > 0) {
48 +    if (lf->first_chunk) {
49 +      lf->first_chunk = 0;
50 +      if ((lf->buffer[0] == '#') && (lf->buffer[1] == '!') &&
51 +          (sp=strstr(lf->buffer, "\n")) != NULL)
52 +      {
53 +        *size -= ((uint)sp - (uint)lf->buffer);
54 +        return sp;
55 +      }
56 +    }
57 +    return lf->buffer;
58 +  }
59 +  return NULL;
60 +}
61 +
62 +
63  static int errfile (lua_State *L, const char *what, int fnameindex) {
64    const char *serr = strerror(errno);
65    const char *filename = lua_tostring(L, fnameindex) + 1;
66 @@ -560,6 +589,31 @@
67  
68  
69  LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
70 +  if ((filename != NULL) && strstr(filename, ".lua.gz")) {
71 +    return luaL_loadfile_gzip(L, filename);
72 +  }
73 +  else {
74 +    return luaL_loadfile_plain(L, filename);
75 +  }
76 +}
77 +
78 +
79 +LUALIB_API int luaL_loadfile_gzip (lua_State *L, const char *filename) {
80 +  LoadGZ gzf;
81 +  int status;
82 +  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
83 +  lua_pushfstring(L, "@%s", filename);
84 +  gzf.f = gzopen(filename, "r");
85 +  gzf.first_chunk = 1;
86 +  if (gzf.f == Z_NULL) return errfile(L, "open", fnameindex);
87 +  status = lua_load(L, getGZ, &gzf, lua_tostring(L, -1));
88 +  (void)gzclose(gzf.f);
89 +  lua_remove(L, fnameindex);
90 +  return status;
91 +}
92 +
93 +  
94 +LUALIB_API int luaL_loadfile_plain (lua_State *L, const char *filename) {
95    LoadF lf;
96    int status, readstatus;
97    int c;
98 diff -ur lua-5.1.4.orig/src/lauxlib.h lua-5.1.4/src/lauxlib.h
99 --- lua-5.1.4.orig/src/lauxlib.h        2009-04-04 23:06:04.000000000 +0200
100 +++ lua-5.1.4/src/lauxlib.h     2009-04-04 23:06:15.000000000 +0200
101 @@ -81,6 +81,8 @@
102  LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
103  
104  LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
105 +LUALIB_API int (luaL_loadfile_gzip) (lua_State *L, const char *filename);
106 +LUALIB_API int (luaL_loadfile_plain) (lua_State *L, const char *filename);
107  LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
108                                    const char *name);
109  LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
110 diff -ur lua-5.1.4.orig/src/luaconf.h lua-5.1.4/src/luaconf.h
111 --- lua-5.1.4.orig/src/luaconf.h        2009-04-04 23:06:04.000000000 +0200
112 +++ lua-5.1.4/src/luaconf.h     2009-04-04 23:27:20.000000000 +0200
113 @@ -101,7 +101,9 @@
114  #define LUA_CDIR       LUA_ROOT "lib/lua/5.1/"
115  #define LUA_PATH_DEFAULT  \
116                 "./?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua;" \
117 -                           LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua"
118 +                           LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua;" \
119 +               "./?.lua.gz;"  LUA_LDIR"?.lua.gz;"  LUA_LDIR"?/init.lua.gz;" \
120 +                              LUA_CDIR"?.lua.gz;"  LUA_CDIR"?/init.lua.gz"
121  #define LUA_CPATH_DEFAULT \
122         "./?.so;"  LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
123  #endif
124 @@ -506,6 +508,12 @@
125  */
126  #define LUAL_BUFFERSIZE                BUFSIZ
127  
128 +
129 +/*
130 +@@ LUAL_GZLDBUFFER is the buffer size used by the gzip source loader.
131 +*/
132 +#define LUAL_GZLDBUFFER                8192
133 +
134  /* }================================================================== */
135  
136