040-gzip-source-loader.patch 4.1 KB

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