aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/lua/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/3rdParty/lua/lauxlib.c')
-rw-r--r--src/3rdParty/lua/lauxlib.c52
1 files changed, 29 insertions, 23 deletions
diff --git a/src/3rdParty/lua/lauxlib.c b/src/3rdParty/lua/lauxlib.c
index 8ed1da1..cba5df9 100644
--- a/src/3rdParty/lua/lauxlib.c
+++ b/src/3rdParty/lua/lauxlib.c
@@ -526,7 +526,8 @@ static void newbox (lua_State *L) {
526 526
527/* 527/*
528** Compute new size for buffer 'B', enough to accommodate extra 'sz' 528** Compute new size for buffer 'B', enough to accommodate extra 'sz'
529** bytes. 529** bytes. (The test for "double is not big enough" also gets the
530** case when the multiplication by 2 overflows.)
530*/ 531*/
531static size_t newbuffsize (luaL_Buffer *B, size_t sz) { 532static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
532 size_t newsize = B->size * 2; /* double buffer size */ 533 size_t newsize = B->size * 2; /* double buffer size */
@@ -611,7 +612,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
611** box (if existent) is not on the top of the stack. So, instead of 612** box (if existent) is not on the top of the stack. So, instead of
612** calling 'luaL_addlstring', it replicates the code using -2 as the 613** calling 'luaL_addlstring', it replicates the code using -2 as the
613** last argument to 'prepbuffsize', signaling that the box is (or will 614** last argument to 'prepbuffsize', signaling that the box is (or will
614** be) bellow the string being added to the buffer. (Box creation can 615** be) below the string being added to the buffer. (Box creation can
615** trigger an emergency GC, so we should not remove the string from the 616** trigger an emergency GC, so we should not remove the string from the
616** stack before we have the space guaranteed.) 617** stack before we have the space guaranteed.)
617*/ 618*/
@@ -739,17 +740,18 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
739} 740}
740 741
741 742
742static int skipBOM (LoadF *lf) { 743/*
743 const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ 744** Skip an optional BOM at the start of a stream. If there is an
744 int c; 745** incomplete BOM (the first character is correct but the rest is
745 lf->n = 0; 746** not), returns the first character anyway to force an error
746 do { 747** (as no chunk can start with 0xEF).
747 c = getc(lf->f); 748*/
748 if (c == EOF || c != *(const unsigned char *)p++) return c; 749static int skipBOM (FILE *f) {
749 lf->buff[lf->n++] = c; /* to be read by the parser */ 750 int c = getc(f); /* read first character */
750 } while (*p != '\0'); 751 if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */
751 lf->n = 0; /* prefix matched; discard it */ 752 return getc(f); /* ignore BOM and return next char */
752 return getc(lf->f); /* return next character */ 753 else /* no (valid) BOM */
754 return c; /* return first character */
753} 755}
754 756
755 757
@@ -760,13 +762,13 @@ static int skipBOM (LoadF *lf) {
760** first "valid" character of the file (after the optional BOM and 762** first "valid" character of the file (after the optional BOM and
761** a first-line comment). 763** a first-line comment).
762*/ 764*/
763static int skipcomment (LoadF *lf, int *cp) { 765static int skipcomment (FILE *f, int *cp) {
764 int c = *cp = skipBOM(lf); 766 int c = *cp = skipBOM(f);
765 if (c == '#') { /* first line is a comment (Unix exec. file)? */ 767 if (c == '#') { /* first line is a comment (Unix exec. file)? */
766 do { /* skip first line */ 768 do { /* skip first line */
767 c = getc(lf->f); 769 c = getc(f);
768 } while (c != EOF && c != '\n'); 770 } while (c != EOF && c != '\n');
769 *cp = getc(lf->f); /* skip end-of-line, if present */ 771 *cp = getc(f); /* next character after comment, if present */
770 return 1; /* there was a comment */ 772 return 1; /* there was a comment */
771 } 773 }
772 else return 0; /* no comment */ 774 else return 0; /* no comment */
@@ -788,12 +790,16 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
788 lf.f = fopen(filename, "r"); 790 lf.f = fopen(filename, "r");
789 if (lf.f == NULL) return errfile(L, "open", fnameindex); 791 if (lf.f == NULL) return errfile(L, "open", fnameindex);
790 } 792 }
791 if (skipcomment(&lf, &c)) /* read initial portion */ 793 lf.n = 0;
792 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ 794 if (skipcomment(lf.f, &c)) /* read initial portion */
793 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ 795 lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */
794 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ 796 if (c == LUA_SIGNATURE[0]) { /* binary file? */
795 if (lf.f == NULL) return errfile(L, "reopen", fnameindex); 797 lf.n = 0; /* remove possible newline */
796 skipcomment(&lf, &c); /* re-read initial portion */ 798 if (filename) { /* "real" file? */
799 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
800 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
801 skipcomment(lf.f, &c); /* re-read initial portion */
802 }
797 } 803 }
798 if (c != EOF) 804 if (c != EOF)
799 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ 805 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */