├── .github └── workflows │ └── test.yml ├── LICENSE ├── Makefile ├── README.md ├── bin └── slua ├── src ├── linenoise.c ├── linenoise.h ├── linenoise.md ├── lsccore.c ├── lua-5.4.3 │ ├── lua.c │ ├── luac.c │ └── src │ │ ├── Makefile │ │ ├── lapi.c │ │ ├── lapi.h │ │ ├── lauxlib.c │ │ ├── lauxlib.h │ │ ├── lbaselib.c │ │ ├── lcode.c │ │ ├── lcode.h │ │ ├── lcorolib.c │ │ ├── lctype.c │ │ ├── lctype.h │ │ ├── ldblib.c │ │ ├── ldebug.c │ │ ├── ldebug.h │ │ ├── ldo.c │ │ ├── ldo.h │ │ ├── ldump.c │ │ ├── lfunc.c │ │ ├── lfunc.h │ │ ├── lgc.c │ │ ├── lgc.h │ │ ├── linit.c │ │ ├── liolib.c │ │ ├── ljumptab.h │ │ ├── llex.c │ │ ├── llex.h │ │ ├── llimits.h │ │ ├── lmathlib.c │ │ ├── lmem.c │ │ ├── lmem.h │ │ ├── loadlib.c │ │ ├── lobject.c │ │ ├── lobject.h │ │ ├── lopcodes.c │ │ ├── lopcodes.h │ │ ├── lopnames.h │ │ ├── loslib.c │ │ ├── lparser.c │ │ ├── lparser.h │ │ ├── lprefix.h │ │ ├── lstate.c │ │ ├── lstate.h │ │ ├── lstring.c │ │ ├── lstring.h │ │ ├── lstrlib.c │ │ ├── ltable.c │ │ ├── ltable.h │ │ ├── ltablib.c │ │ ├── ltm.c │ │ ├── ltm.h │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── luaconf.h │ │ ├── lualib.h │ │ ├── lundump.c │ │ ├── lundump.h │ │ ├── lutf8lib.c │ │ ├── lvm.c │ │ ├── lvm.h │ │ ├── lzio.c │ │ └── lzio.h ├── lualinux-0.3 │ └── lualinux.c ├── luazen-2.1 │ ├── base64.c │ ├── luazen.c │ ├── lzma │ │ ├── 7zTypes.h │ │ ├── Alloc.c │ │ ├── Alloc.h │ │ ├── Compiler.h │ │ ├── LzFind.c │ │ ├── LzFind.h │ │ ├── LzHash.h │ │ ├── LzmaDec.c │ │ ├── LzmaDec.h │ │ ├── LzmaEnc.c │ │ ├── LzmaEnc.h │ │ ├── LzmaLib.c │ │ ├── LzmaLib.h │ │ ├── Precomp.h │ │ └── lualzma.c │ ├── md5.c │ ├── mono │ │ ├── luamonocypher.c │ │ ├── monocypher-LICENCE.md │ │ ├── monocypher-ed25519.c │ │ ├── monocypher-ed25519.h │ │ ├── monocypher.c │ │ └── monocypher.h │ └── random.c ├── slua.c ├── slualibs.h ├── sluaversion.h ├── srglue.h └── srlua-102 │ ├── Makefile │ ├── README │ ├── srglue.c │ ├── srglue.h │ ├── srlua.c │ └── test.lua └── test ├── smoketest.lua └── test_luazen.lua /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: test slua 13 | run: | 14 | # !! no leading tabs here - spaces only !! 15 | set -e 16 | set -v 17 | # install musl, build static slua 18 | sudo apt install musl musl-tools 19 | make clean 20 | make slua 21 | ./slua test/test_luazen.lua 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2022 Phil Leblanc 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without restriction, 6 | including without limitation the rights to use, copy, modify, merge, 7 | publish, distribute, sublicense, and/or sell copies of the Software, 8 | and to permit persons to whom the Software is furnished to do so, 9 | subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | # slua build makefile 3 | 4 | # To build musl libc-based executables on your current platform, 5 | # with the default gcc compiler, use instructions for 6 | # the _musl-gcc wrapper_ at https://www.musl-libc.org/how.html 7 | # or simply install musl libc: with a recent Debian (or Ubuntu, Mint, ...) 8 | # do: 9 | # sudo apt install musl musl-dev musl-tools 10 | # 11 | # To build a complete gcc-based cross-compile toolchain, the easiest 12 | # solution is to use 'musl-cross-make' by Rich Felker at 13 | # https://github.com/richfelker/musl-cross-make 14 | # 15 | # note: to build with glibc (or uClibc), must add " -lpthread -lm " at 16 | # the end of the link lines for slua and sluac (see the sglua target) 17 | 18 | 19 | # ---------------------------------------------------------------------- 20 | 21 | 22 | # This makefile uses the standard gcc compiler and the musl-gcc wrapper 23 | 24 | CC= musl-gcc 25 | AR= ar 26 | LD= ld 27 | STRIP= strip 28 | 29 | # Directories where the sources can be found 30 | LUA= lua-5.4.3 31 | LUAZEN= luazen-2.1 32 | LUALINUX=lualinux-0.3 33 | SRLUA=srlua-102 34 | 35 | CFLAGS= -Os -Isrc/$(LUA)/src -DLUA_USE_LINUX 36 | LDFLAGS= 37 | 38 | # ---------------------------------------------------------------------- 39 | 40 | default: smoketest sluac srlua 41 | 42 | smoketest: ./slua 43 | ./slua test/smoketest.lua 44 | 45 | slua: 46 | $(CC) -c $(CFLAGS) src/$(LUA)/src/*.c 47 | $(CC) -c $(CFLAGS) src/lsccore.c src/linenoise.c 48 | $(CC) -c $(CFLAGS) src/$(LUALINUX)/*.c 49 | $(CC) -c $(CFLAGS) src/$(LUAZEN)/*.c 50 | $(CC) -c $(CFLAGS) -D_7ZIP_ST src/$(LUAZEN)/lzma/*.c 51 | $(CC) -c $(CFLAGS) src/$(LUAZEN)/mono/*.c 52 | $(AR) rc slua.a *.o 53 | $(CC) -static -o slua $(CFLAGS) $(LDFLAGS) src/slua.c slua.a 54 | $(STRIP) slua 55 | rm -f *.o 56 | 57 | sluac: slua 58 | $(CC) -static -o sluac $(CFLAGS) $(LDFLAGS) src/$(LUA)/luac.c slua.a 59 | $(STRIP) sluac 60 | 61 | srlua: slua 62 | $(CC) -static -o srlua -Isrc/$(SRLUA) -Isrc $(CFLAGS) $(LDFLAGS) \ 63 | src/$(SRLUA)/srlua.c slua.a 64 | $(STRIP) srlua 65 | $(CC) -static -o srglue -Isrc/$(SRLUA) -Isrc $(CFLAGS) $(LDFLAGS) \ 66 | src/$(SRLUA)/srglue.c slua.a 67 | $(STRIP) srglue 68 | ./srglue ./srlua src/$(SRLUA)/test.lua srtest 69 | chmod +x ./srtest 70 | ./srtest arg1 arg2 arg3 71 | 72 | clean: 73 | rm -f slua sluac *.o *.a *.so 74 | rm -f srlua srglue srtest 75 | 76 | test: ./slua 77 | ./slua test/test_luazen.lua 78 | 79 | bin: ./slua 80 | cp ./slua ./bin/slua 81 | 82 | .PHONY: clean smoketest default 83 | 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![CI](https://github.com/philanc/slua/workflows/CI/badge.svg) 2 | 3 | # slua 4 | 5 | A static build of [Lua](http://www.lua.org/) 5.4 for Linux, with a few extension libraries. 6 | 7 | For convenience, a slua binary is provided for x86_64. 8 | 9 | ### Preloaded libraries 10 | 11 | Additional libraries are *pre-loaded*. They must be require()'d before use. 12 | 13 | - [luazen](https://github.com/philanc/luazen), a small library with LZMA compression and various crypto functions. 14 | - [lualinux](https://github.com/philanc/lualinux), a minimal binding to common Linux/Posix functions. -- see a list of [lualinux available functions](https://github.com/philanc/lualinux#available-functions)) 15 | - [linenoise](src/linenoise.md) - slua is built on Linux with linenoise to replace readline. A limited Lua binding to linenoise is also provided to allow usage of linenoise in applications. 16 | 17 | 18 | ### Extension mechanism 19 | 20 | [srlua](https://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/#srlua) by Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br) is included. As put by its author, this is a self-running Lua interpreter. It is meant to be combined with a Lua program into a single, stand-alone program that will execute the given Lua program when it is run. 21 | 22 | It has been slightly modified: 23 | 24 | * `srlua` and its companion program `srglue` are statically built (do `make srlua`). They are used exactly as the original `srlua` and `srglue`. See for example the target `srlua` in the Makefile. 25 | 26 | * The additional slua libraries are also built in `srlua`. 27 | 28 | * `srlua` uses `/proc/self/exe` instead of `argv[0]` to find the executable program and read the embedded Lua code. It allows the program to be placed somewhere in $PATH and called by just its name from anywhere. 29 | 30 | 31 | ### Static build 32 | 33 | slua is linked completely statically. It uses no dynamic library, not even libc. So it can be used anywhere, whatever the platform dynamic linker and libraries versions. It is built with the Musl C library which allows a very compact executable. 34 | 35 | It can be dropped and run from any directory, without interference with the local dynamic libraries and linker. 36 | 37 | On the other end, obviously, slua cannot load dynamic C libraries. It is *not* intended to be a replacement for a full fledged Lua installation. 38 | 39 | slua respects the environment variables LUA_PATH and LUA_CPATH. 40 | 41 | ### Installation 42 | 43 | The `src` directory includes all the required sources, so building `slua` doesn't require any external dependencies. 44 | 45 | There is no installation. The slua executable is static. It can be placed and executed anywhere. 46 | 47 | The default slua build procedure assumes that the C compiler is gcc and that musl libc is installed. It uses the `musl-gcc` wrapper. 48 | 49 | With a recent Debian (or Ubuntu, Mint, ...) musl libc can be installed with 50 | ``` 51 | sudo apt install musl musl-dev musl-tools 52 | ``` 53 | 54 | In that case, the makefile can be used as-is. 55 | 56 | Other distributions may include musl packages. Alternatively musl libc can easily be built form source. See instructions at https://www.musl-libc.org/ 57 | 58 | If the `musl-gcc` wrapper is not accessible in `$PATH`, or if another toolchain is used, the makefile must be adjusted accordingly. 59 | 60 | Then run 'make' at the root of the project tree: 61 | ``` 62 | make 63 | ``` 64 | 65 | The default target buils the `slua` executable, the associated `sluac` Lua compiler, and `srlua` and its companion `srglue` (See "extension mechanism" above). All these programs are statically linked. 66 | 67 | ### Pre-built binary 68 | 69 | A binary version of slua is provided here for convenience. This is a standalone executable, statically compiled with musl-1.2.2 for Linux x86_64. 70 | 71 | ### Package versions 72 | 73 | lua-5.4.3 - http://www.lua.org/ftp/lua-5.4.3.tar.gz 74 | 75 | luazen-2.1 - https://github.com/philanc/luazen 76 | 77 | lualinux-0.3 - https://github.com/philanc/lualinux 78 | 79 | linenoise - The full *readline* library is not used. It is replaced by the much smaller *linenoise* library. The linenoise implementation included here has been simplified to keep only functions used by the Lua REPL and extended to include a Lua binding. It is derived from Linenoise v1.0 - commit 027dbce - https://github.com/antirez/linenoise 80 | 81 | ### Modifications of the vanilla Lua 82 | 83 | lua.c is not used. It is replaced with src/slua.c. The only differences between lua.c and slua.c are the replacement of the readline interface with linenoise, and the addition of the preloaded libraries (look for "preloaded libraries", close to the end of slua.c) 84 | 85 | ### License and credits 86 | 87 | Lua and all extension libraries are distributed under the terms of their respective licenses (MIT or equivalent - see in the ./src directory). 88 | 89 | I am of course grateful to the PUC-Rio team for the great [Lua](http://www.lua.org/) language. 90 | 91 | The built-in libraries include some code from various authors: 92 | - linenoise by Salvatore Sanfilippo - https://github.com/antirez/linenoise 93 | - lzma compression by Igor Pavlov - https://www.7-zip.org/sdk.html 94 | - xchacha20/poly1305, blake2b, argon2i, x25519 DH key exchange and ed25519 signature from Loup Vaillant's Monocypher - https://monocypher.org/ 95 | - srlua by Luiz Henrique de Figueiredo (public domain) 96 | 97 | slua itslef and the library bindings are distributed under the MIT License (see file LICENSE) 98 | 99 | Copyright (c) 2022 Phil Leblanc 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /bin/slua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philanc/slua/9fee8e040b13522845d3dbca5c718a9580ecb3d0/bin/slua -------------------------------------------------------------------------------- /src/linenoise.h: -------------------------------------------------------------------------------- 1 | /// 151024 edited for slua 2 | /// 3 | /* linenoise.h -- VERSION 1.0 4 | * 5 | * Guerrilla line editing library against the idea that a line editing lib 6 | * needs to be 20,000 lines of C code. 7 | * 8 | * See linenoise.c for more information. 9 | * 10 | * ------------------------------------------------------------------------ 11 | * 12 | * Copyright (c) 2010-2014, Salvatore Sanfilippo 13 | * Copyright (c) 2010-2013, Pieter Noordhuis 14 | * 15 | * All rights reserved. 16 | * 17 | * Redistribution and use in source and binary forms, with or without 18 | * modification, are permitted provided that the following conditions are 19 | * met: 20 | * 21 | * * Redistributions of source code must retain the above copyright 22 | * notice, this list of conditions and the following disclaimer. 23 | * 24 | * * Redistributions in binary form must reproduce the above copyright 25 | * notice, this list of conditions and the following disclaimer in the 26 | * documentation and/or other materials provided with the distribution. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 | */ 40 | 41 | #ifndef __LINENOISE_H 42 | #define __LINENOISE_H 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | typedef struct linenoiseCompletions { 49 | size_t len; 50 | char **cvec; 51 | } linenoiseCompletions; 52 | 53 | typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *); 54 | void linenoiseSetCompletionCallback(linenoiseCompletionCallback *); 55 | void linenoiseAddCompletion(linenoiseCompletions *, const char *); 56 | 57 | char *linenoise(const char *prompt); 58 | int linenoiseHistoryAdd(const char *line); 59 | int linenoiseHistorySetMaxLen(int len); 60 | //~ int linenoiseHistorySave(const char *filename); 61 | //~ int linenoiseHistoryLoad(const char *filename); 62 | void linenoiseClearScreen(void); 63 | //~ void linenoiseSetMultiLine(int ml); 64 | void linenoisePrintKeyCodes(void); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif /* __LINENOISE_H */ 71 | -------------------------------------------------------------------------------- /src/linenoise.md: -------------------------------------------------------------------------------- 1 | # linenoise 2 | 3 | Linenoise is *"A minimal, zero-config, BSD licensed, readline replacement used in Redis, MongoDB, and Android."* - https://github.com/antirez/linenoise 4 | 5 | This is a Lua wrapper for the Linenoise library. It also includes some tty-related functions (get and set tty mode, test if a file descriptor is a tty, test if a key has been pressed). 6 | 7 | The version of linenoise included here has been both simplified (no support for multi-line edition and tab completion) and extended with a small Lua binding. It includes all the functions used by the regular Lua interpreter (see slua.c) 8 | 9 | ### API 10 | 11 | ``` 12 | --- Linenoise functions 13 | 14 | linenoise(prompt) 15 | display prompt, read a line (with all the linenoise line 16 | editing facilities). return read line or (nil, error message) 17 | 18 | addhistory(line) 19 | add a line to the current history (history is created if this is 20 | the first call). return true on success or (nil, error message) 21 | 22 | gethistory() 23 | return a table with the list of history entries. if the history 24 | is empty or has not been created yet, an empty table is returned 25 | 26 | clearhistory() 27 | empties the current history. return nothing 28 | 29 | 30 | --- Other tty functions 31 | 32 | isatty(fd) 33 | wraps the unix isatty() function 34 | return true if the file descriptor fd is a tty, or else false 35 | 36 | getmode(mode) 37 | get the current terminal mode 38 | return the current mode as a binary string 39 | (a copy of the termios structure) 40 | in case of error, the function returns (nil, error message) 41 | (usually fails because stdin is not a tty) 42 | 43 | setmode(mode) 44 | set the terminal mode 45 | 'mode' is a binary string obtained by getmode() 46 | return true on success or (nil, error message) 47 | (can fail because either stdin is not a tty or mode is not valid) 48 | 49 | setrawmode([opost]) 50 | set the current tty in raw mode 51 | if the optional parameter opost is 1, output post processing 52 | is not disabled. By default, output post processing is disabled. 53 | return true on success or (nil, error message) 54 | (usually fails because stdin is not a tty) 55 | 56 | kbhit() 57 | this is the old conio kbhit() function 58 | It is intended to be used in raw mode. 59 | return true if a key has been hit or else false 60 | in case of poll error, the function returns nil, error msg. 61 | 62 | 63 | ``` 64 | -------------------------------------------------------------------------------- /src/lsccore.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 Phil Leblanc -- see LICENSE file 2 | // --------------------------------------------------------------------- 3 | /* 4 | 5 | LuaLinuxSC - a minimal Lua binding to the Linux syscall interface. 6 | 7 | This is for Lua 5.3+ only, built with default 64-bit integers 8 | 9 | CAVEAT 10 | At the moment, this is tested and should work only for x86_64 11 | 12 | Among other things, it assumes that: 13 | sizeof(char *) == sizeof(long) == sizeof(lua_Integer) == 8 14 | 15 | 201223 - added (long) casts to allow x86: 16 | sizeof(char *) == sizeof(long) == 4 ; sizeof(lua_Integer) == 8 17 | 18 | */ 19 | 20 | 21 | #define VERSION "lsc-0.3" 22 | 23 | 24 | #include "lua.h" 25 | #include "lauxlib.h" 26 | 27 | #include 28 | #include 29 | #include 30 | #include // errno 31 | 32 | //~ #include /* For SYS_xxx definitions */ 33 | 34 | 35 | #define LERR(msg) return luaL_error(L, msg) 36 | #define RET_TRUE return (lua_pushboolean(L, 1), 1) 37 | #define RET_INT(i) return (lua_pushinteger(L, (i)), 1) 38 | #define RET_ERRNO return (lua_pushnil(L), lua_pushinteger(L, errno), 2) 39 | 40 | 41 | static int ll_syscall(lua_State *L) { 42 | long number = (long) luaL_checkinteger(L, 1); 43 | long p1 = luaL_optinteger(L, 2, 0); 44 | long p2 = luaL_optinteger(L, 3, 0); 45 | long p3 = luaL_optinteger(L, 4, 0); 46 | long p4 = luaL_optinteger(L, 5, 0); 47 | long p5 = luaL_optinteger(L, 6, 0); 48 | long p6 = luaL_optinteger(L, 7, 0); 49 | long r = syscall(number, p1, p2, p3, p4, p5, p6); 50 | if (r == -1) RET_ERRNO; // return nil, errno 51 | lua_pushinteger(L, r); 52 | return 1; 53 | } 54 | 55 | //---------------------------------------------------------------------- 56 | // memory / buffer minimal API 57 | // ...one big step towards the perfect footgun... :-) 58 | 59 | // FIXME: how to use a userdata automatically GC'd by Lua for buffer? 60 | 61 | static int ll_newbuffer(lua_State *L) { 62 | // lua API: newbuffer(size) => addr 63 | // return the address of a block of allocated memory 64 | size_t size = (long) luaL_checkinteger(L, 1); 65 | char *mb = (char *) malloc(size); 66 | if (mb == NULL) LERR("buffer: allocation failed"); 67 | memset(mb, 0, size); // ?? is it needed or already done by lua? 68 | lua_pushinteger(L, (lua_Integer) (long)mb); 69 | return 1; 70 | } 71 | 72 | static int ll_freebuffer(lua_State *L) { 73 | // lua API: freebuffer(addr) 74 | // free a buffer allocated with newbuffer() 75 | char *p = (char *) (long) luaL_checkinteger(L, 1); 76 | free(p); 77 | RET_TRUE; 78 | } 79 | 80 | static int ll_zero(lua_State *L) { 81 | // lua API: zero(addr, size) 82 | // write `size` null bytes at address `addr` 83 | char *p = (char *) (long) luaL_checkinteger(L, 1); 84 | size_t n = (long) luaL_checkinteger(L, 2); 85 | memset(p, 0, n); 86 | RET_TRUE; 87 | } 88 | 89 | static int ll_getstr(lua_State *L) { 90 | // lua API: getstr(addr [, size]) => string 91 | // if size=-1 (default), string is null-terminated 92 | char *p = (char *) (long) luaL_checkinteger(L, 1); 93 | long size = (long) luaL_optinteger(L, 2, -1); 94 | if (size < 0) { 95 | lua_pushstring (L, p); 96 | } else { 97 | lua_pushlstring (L, p, size); 98 | } 99 | return 1; 100 | } 101 | 102 | static int ll_putbin(lua_State *L) { 103 | // lua API: putbin(addr, str) 104 | // copy binary string `str` at address `addr` 105 | // return addr 106 | size_t len; 107 | char *ptr = (char *) (long) luaL_checkinteger(L, 1); 108 | const char *str = luaL_checklstring(L, 2, &len); 109 | memcpy(ptr, str, len); 110 | RET_INT( (lua_Integer) (long)ptr ); 111 | } 112 | 113 | static int ll_putstr(lua_State *L) { 114 | // lua API: putstr(addr, str) 115 | // same as putbin, but append a null terminator ('\0') 116 | // at the end of the written string. 117 | size_t len; 118 | char *ptr = (char *) (long) luaL_checkinteger(L, 1); 119 | const char *str = luaL_checklstring(L, 2, &len); 120 | memcpy(ptr, str, len); 121 | ptr[len] = '\0'; 122 | RET_INT( (lua_Integer) (long)ptr ); 123 | } 124 | 125 | static int ll_getuint(lua_State *L) { 126 | // lua API: getuint(addr, isize) => i 127 | // get unsigned integer i at address addr 128 | // isize is i size in bytes. can be 1, 2, 4 or 8 129 | char *p = (char *) (long) luaL_checkinteger(L, 1); 130 | int sz = (long) luaL_checkinteger(L, 2); 131 | long i; 132 | switch (sz) { 133 | case 1: i = *((uint8_t *) p); break; 134 | case 2: i = *((uint16_t *) p); break; 135 | case 4: i = *((uint32_t *) p); break; 136 | case 8: i = *((uint64_t *) p); break; 137 | default: LERR("lsc.getint: invalid parameter"); break; 138 | } 139 | RET_INT(i); 140 | } 141 | 142 | static int ll_putuint(lua_State *L) { 143 | // lua API: putuint(addr, i, isize) 144 | // put integer i at address addr. 145 | // isize is i size in bytes. can be 1, 2, 4 or 8 146 | char *p = (char *) (long) luaL_checkinteger(L, 1); 147 | long i = (long) luaL_checkinteger(L, 2); 148 | int sz = (long) luaL_checkinteger(L, 3); 149 | switch (sz) { 150 | case 1: *((uint8_t *) p) = i & 0xff; break; 151 | case 2: *((uint16_t *) p) = i & 0xffff; break; 152 | case 4: *((uint32_t *) p) = i & 0xffffffff; break; 153 | case 8: *((uint64_t *) p) = i; break; 154 | default: LERR("lsc.putint: invalid parameter"); break; 155 | } 156 | RET_INT( (lua_Integer) (long)p ); 157 | } 158 | 159 | // access to global variables: errno, environ 160 | // how could they be found? 161 | 162 | static int ll_errno(lua_State *L) { 163 | // lua api: errno() => errno value 164 | // errno(n): set errno to n 165 | int r = luaL_optinteger(L, 1, -1); 166 | if (r != -1) errno = r; 167 | RET_INT(errno); 168 | } 169 | 170 | static int ll_environ(lua_State *L) { 171 | // lua api: environ() => 'environ' value as a Lua integer 172 | // (the address of a list of strings "name=value") 173 | extern char **environ; 174 | RET_INT( (lua_Integer) (long)environ ); 175 | } 176 | 177 | 178 | 179 | 180 | //---------------------------------------------------------------------- 181 | // lua library declaration 182 | // 183 | 184 | // l5 function table 185 | static const struct luaL_Reg lsclib[] = { 186 | // 187 | {"syscall", ll_syscall}, 188 | {"newbuffer", ll_newbuffer}, 189 | {"freebuffer", ll_freebuffer}, 190 | {"zero", ll_zero}, 191 | {"getstr", ll_getstr}, 192 | {"putstr", ll_putstr}, 193 | {"putbin", ll_putbin}, 194 | {"getuint", ll_getuint}, 195 | {"putuint", ll_putuint}, 196 | {"errno", ll_errno}, 197 | {"environ", ll_environ}, 198 | // 199 | {NULL, NULL}, 200 | }; 201 | 202 | int luaopen_lsccore (lua_State *L) { 203 | 204 | // register main library functions 205 | //~ luaL_register (L, "l5", l5lib); 206 | luaL_newlib (L, lsclib); 207 | lua_pushliteral (L, "VERSION"); 208 | lua_pushliteral (L, VERSION); 209 | lua_settable (L, -3); 210 | return 1; 211 | } 212 | 213 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for building Lua 2 | # See ../doc/readme.html for installation and customization instructions. 3 | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 5 | 6 | # Your platform. See PLATS for possible values. 7 | PLAT= guess 8 | 9 | CC= gcc -std=gnu99 10 | CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_3 $(SYSCFLAGS) $(MYCFLAGS) 11 | LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) 12 | LIBS= -lm $(SYSLIBS) $(MYLIBS) 13 | 14 | AR= ar rcu 15 | RANLIB= ranlib 16 | RM= rm -f 17 | UNAME= uname 18 | 19 | SYSCFLAGS= 20 | SYSLDFLAGS= 21 | SYSLIBS= 22 | 23 | MYCFLAGS= 24 | MYLDFLAGS= 25 | MYLIBS= 26 | MYOBJS= 27 | 28 | # Special flags for compiler modules; -Os reduces code size. 29 | CMCFLAGS= 30 | 31 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= 32 | 33 | PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris 34 | 35 | LUA_A= liblua.a 36 | CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o 37 | LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o 38 | BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) 39 | 40 | LUA_T= lua 41 | LUA_O= lua.o 42 | 43 | LUAC_T= luac 44 | LUAC_O= luac.o 45 | 46 | ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) 47 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) 48 | ALL_A= $(LUA_A) 49 | 50 | # Targets start here. 51 | default: $(PLAT) 52 | 53 | all: $(ALL_T) 54 | 55 | o: $(ALL_O) 56 | 57 | a: $(ALL_A) 58 | 59 | $(LUA_A): $(BASE_O) 60 | $(AR) $@ $(BASE_O) 61 | $(RANLIB) $@ 62 | 63 | $(LUA_T): $(LUA_O) $(LUA_A) 64 | $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) 65 | 66 | $(LUAC_T): $(LUAC_O) $(LUA_A) 67 | $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) 68 | 69 | test: 70 | ./$(LUA_T) -v 71 | 72 | clean: 73 | $(RM) $(ALL_T) $(ALL_O) 74 | 75 | depend: 76 | @$(CC) $(CFLAGS) -MM l*.c 77 | 78 | echo: 79 | @echo "PLAT= $(PLAT)" 80 | @echo "CC= $(CC)" 81 | @echo "CFLAGS= $(CFLAGS)" 82 | @echo "LDFLAGS= $(SYSLDFLAGS)" 83 | @echo "LIBS= $(LIBS)" 84 | @echo "AR= $(AR)" 85 | @echo "RANLIB= $(RANLIB)" 86 | @echo "RM= $(RM)" 87 | @echo "UNAME= $(UNAME)" 88 | 89 | # Convenience targets for popular platforms. 90 | ALL= all 91 | 92 | help: 93 | @echo "Do 'make PLATFORM' where PLATFORM is one of these:" 94 | @echo " $(PLATS)" 95 | @echo "See doc/readme.html for complete instructions." 96 | 97 | guess: 98 | @echo Guessing `$(UNAME)` 99 | @$(MAKE) `$(UNAME)` 100 | 101 | AIX aix: 102 | $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" 103 | 104 | bsd: 105 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" 106 | 107 | c89: 108 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" 109 | @echo '' 110 | @echo '*** C89 does not guarantee 64-bit integers for Lua.' 111 | @echo '*** Make sure to compile all external Lua libraries' 112 | @echo '*** with LUA_USE_C89 to ensure consistency' 113 | @echo '' 114 | 115 | FreeBSD NetBSD OpenBSD freebsd: 116 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" 117 | 118 | generic: $(ALL) 119 | 120 | Linux linux: linux-noreadline 121 | 122 | linux-noreadline: 123 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" 124 | 125 | linux-readline: 126 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" 127 | 128 | Darwin macos macosx: 129 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" 130 | 131 | mingw: 132 | $(MAKE) "LUA_A=lua54.dll" "LUA_T=lua.exe" \ 133 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ 134 | "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe 135 | $(MAKE) "LUAC_T=luac.exe" luac.exe 136 | 137 | posix: 138 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" 139 | 140 | SunOS solaris: 141 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" 142 | 143 | # Targets that do not create files (not all makes understand .PHONY). 144 | .PHONY: all $(PLATS) help test clean default o a depend echo 145 | 146 | # Compiler modules may use special flags. 147 | llex.o: 148 | $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c 149 | 150 | lparser.o: 151 | $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c 152 | 153 | lcode.o: 154 | $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c 155 | 156 | # DO NOT DELETE 157 | 158 | lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ 159 | lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ 160 | ltable.h lundump.h lvm.h 161 | lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h 162 | lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 163 | lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ 164 | llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ 165 | ldo.h lgc.h lstring.h ltable.h lvm.h 166 | lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 167 | lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h 168 | ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 169 | ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ 170 | lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ 171 | ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h 172 | ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ 173 | lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ 174 | lparser.h lstring.h ltable.h lundump.h lvm.h 175 | ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ 176 | ltm.h lzio.h lmem.h lundump.h 177 | lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ 178 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h 179 | lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ 180 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h 181 | linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h 182 | liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 183 | llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ 184 | lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ 185 | lstring.h ltable.h 186 | lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 187 | lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ 188 | llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h 189 | loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 190 | lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ 191 | ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ 192 | lvm.h 193 | lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h 194 | loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 195 | lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ 196 | llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ 197 | ldo.h lfunc.h lstring.h lgc.h ltable.h 198 | lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ 199 | lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ 200 | lstring.h ltable.h 201 | lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ 202 | lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h 203 | lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 204 | ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ 205 | llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h 206 | ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 207 | ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ 208 | llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h 209 | lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 210 | luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h ldebug.h lstate.h \ 211 | lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.h 212 | lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ 213 | lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ 214 | lundump.h 215 | lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h 216 | lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ 217 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ 218 | ltable.h lvm.h ljumptab.h 219 | lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ 220 | lobject.h ltm.h lzio.h 221 | 222 | # (end of Makefile) 223 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | 15 | /* Increments 'L->top', checking for stack overflows */ 16 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 17 | "stack overflow");} 18 | 19 | 20 | /* 21 | ** If a call returns too many multiple returns, the callee may not have 22 | ** stack space to accommodate all results. In this case, this macro 23 | ** increases its stack space ('L->ci->top'). 24 | */ 25 | #define adjustresults(L,nres) \ 26 | { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 27 | 28 | 29 | /* Ensure the stack has at least 'n' elements */ 30 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 31 | "not enough elements in the stack") 32 | 33 | 34 | /* 35 | ** To reduce the overhead of returning from C functions, the presence of 36 | ** to-be-closed variables in these functions is coded in the CallInfo's 37 | ** field 'nresults', in a way that functions with no to-be-closed variables 38 | ** with zero, one, or "all" wanted results have no overhead. Functions 39 | ** with other number of wanted results, as well as functions with 40 | ** variables to be closed, have an extra check. 41 | */ 42 | 43 | #define hastocloseCfunc(n) ((n) < LUA_MULTRET) 44 | 45 | /* Map [-1, inf) (range of 'nresults') into (-inf, -2] */ 46 | #define codeNresults(n) (-(n) - 3) 47 | #define decodeNresults(n) (-(n) - 3) 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | /* arithmetic operators */ 28 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 29 | OPR_DIV, OPR_IDIV, 30 | /* bitwise operators */ 31 | OPR_BAND, OPR_BOR, OPR_BXOR, 32 | OPR_SHL, OPR_SHR, 33 | /* string operator */ 34 | OPR_CONCAT, 35 | /* comparison operators */ 36 | OPR_EQ, OPR_LT, OPR_LE, 37 | OPR_NE, OPR_GT, OPR_GE, 38 | /* logical operators */ 39 | OPR_AND, OPR_OR, 40 | OPR_NOBINOPR 41 | } BinOpr; 42 | 43 | 44 | /* true if operation is foldable (that is, it is arithmetic or bitwise) */ 45 | #define foldbinop(op) ((op) <= OPR_SHR) 46 | 47 | 48 | #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) 49 | 50 | 51 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 52 | 53 | 54 | /* get (pointer to) instruction of given 'expdesc' */ 55 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 56 | 57 | 58 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 59 | 60 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 61 | 62 | LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); 63 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 64 | LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); 65 | LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, 66 | int B, int C, int k); 67 | LUAI_FUNC int luaK_isKint (expdesc *e); 68 | LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); 69 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 70 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 71 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 72 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 73 | LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); 74 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 75 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 76 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 77 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 78 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 79 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 80 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 81 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 82 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 83 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 84 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 85 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 86 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 87 | LUAI_FUNC int luaK_jump (FuncState *fs); 88 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 89 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 90 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 91 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 92 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 93 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 94 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 95 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 96 | expdesc *v2, int line); 97 | LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, 98 | int ra, int asize, int hsize); 99 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 100 | LUAI_FUNC void luaK_finish (FuncState *fs); 101 | LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); 102 | 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco (lua_State *L) { 22 | lua_State *co = lua_tothread(L, 1); 23 | luaL_argexpected(L, co, 1, "thread"); 24 | return co; 25 | } 26 | 27 | 28 | /* 29 | ** Resumes a coroutine. Returns the number of results for non-error 30 | ** cases or -1 for errors. 31 | */ 32 | static int auxresume (lua_State *L, lua_State *co, int narg) { 33 | int status, nres; 34 | if (l_unlikely(!lua_checkstack(co, narg))) { 35 | lua_pushliteral(L, "too many arguments to resume"); 36 | return -1; /* error flag */ 37 | } 38 | lua_xmove(L, co, narg); 39 | status = lua_resume(co, L, narg, &nres); 40 | if (l_likely(status == LUA_OK || status == LUA_YIELD)) { 41 | if (l_unlikely(!lua_checkstack(L, nres + 1))) { 42 | lua_pop(co, nres); /* remove results anyway */ 43 | lua_pushliteral(L, "too many results to resume"); 44 | return -1; /* error flag */ 45 | } 46 | lua_xmove(co, L, nres); /* move yielded values */ 47 | return nres; 48 | } 49 | else { 50 | lua_xmove(co, L, 1); /* move error message */ 51 | return -1; /* error flag */ 52 | } 53 | } 54 | 55 | 56 | static int luaB_coresume (lua_State *L) { 57 | lua_State *co = getco(L); 58 | int r; 59 | r = auxresume(L, co, lua_gettop(L) - 1); 60 | if (l_unlikely(r < 0)) { 61 | lua_pushboolean(L, 0); 62 | lua_insert(L, -2); 63 | return 2; /* return false + error message */ 64 | } 65 | else { 66 | lua_pushboolean(L, 1); 67 | lua_insert(L, -(r + 1)); 68 | return r + 1; /* return true + 'resume' returns */ 69 | } 70 | } 71 | 72 | 73 | static int luaB_auxwrap (lua_State *L) { 74 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 75 | int r = auxresume(L, co, lua_gettop(L)); 76 | if (l_unlikely(r < 0)) { /* error? */ 77 | int stat = lua_status(co); 78 | if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ 79 | stat = lua_resetthread(co); /* close its tbc variables */ 80 | lua_assert(stat != LUA_OK); 81 | lua_xmove(co, L, 1); /* copy error message */ 82 | } 83 | if (stat != LUA_ERRMEM && /* not a memory error and ... */ 84 | lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ 85 | luaL_where(L, 1); /* add extra info, if available */ 86 | lua_insert(L, -2); 87 | lua_concat(L, 2); 88 | } 89 | return lua_error(L); /* propagate error */ 90 | } 91 | return r; 92 | } 93 | 94 | 95 | static int luaB_cocreate (lua_State *L) { 96 | lua_State *NL; 97 | luaL_checktype(L, 1, LUA_TFUNCTION); 98 | NL = lua_newthread(L); 99 | lua_pushvalue(L, 1); /* move function to top */ 100 | lua_xmove(L, NL, 1); /* move function from L to NL */ 101 | return 1; 102 | } 103 | 104 | 105 | static int luaB_cowrap (lua_State *L) { 106 | luaB_cocreate(L); 107 | lua_pushcclosure(L, luaB_auxwrap, 1); 108 | return 1; 109 | } 110 | 111 | 112 | static int luaB_yield (lua_State *L) { 113 | return lua_yield(L, lua_gettop(L)); 114 | } 115 | 116 | 117 | #define COS_RUN 0 118 | #define COS_DEAD 1 119 | #define COS_YIELD 2 120 | #define COS_NORM 3 121 | 122 | 123 | static const char *const statname[] = 124 | {"running", "dead", "suspended", "normal"}; 125 | 126 | 127 | static int auxstatus (lua_State *L, lua_State *co) { 128 | if (L == co) return COS_RUN; 129 | else { 130 | switch (lua_status(co)) { 131 | case LUA_YIELD: 132 | return COS_YIELD; 133 | case LUA_OK: { 134 | lua_Debug ar; 135 | if (lua_getstack(co, 0, &ar)) /* does it have frames? */ 136 | return COS_NORM; /* it is running */ 137 | else if (lua_gettop(co) == 0) 138 | return COS_DEAD; 139 | else 140 | return COS_YIELD; /* initial state */ 141 | } 142 | default: /* some error occurred */ 143 | return COS_DEAD; 144 | } 145 | } 146 | } 147 | 148 | 149 | static int luaB_costatus (lua_State *L) { 150 | lua_State *co = getco(L); 151 | lua_pushstring(L, statname[auxstatus(L, co)]); 152 | return 1; 153 | } 154 | 155 | 156 | static int luaB_yieldable (lua_State *L) { 157 | lua_State *co = lua_isnone(L, 1) ? L : getco(L); 158 | lua_pushboolean(L, lua_isyieldable(co)); 159 | return 1; 160 | } 161 | 162 | 163 | static int luaB_corunning (lua_State *L) { 164 | int ismain = lua_pushthread(L); 165 | lua_pushboolean(L, ismain); 166 | return 2; 167 | } 168 | 169 | 170 | static int luaB_close (lua_State *L) { 171 | lua_State *co = getco(L); 172 | int status = auxstatus(L, co); 173 | switch (status) { 174 | case COS_DEAD: case COS_YIELD: { 175 | status = lua_resetthread(co); 176 | if (status == LUA_OK) { 177 | lua_pushboolean(L, 1); 178 | return 1; 179 | } 180 | else { 181 | lua_pushboolean(L, 0); 182 | lua_xmove(co, L, 1); /* copy error message */ 183 | return 2; 184 | } 185 | } 186 | default: /* normal or running coroutine */ 187 | return luaL_error(L, "cannot close a %s coroutine", statname[status]); 188 | } 189 | } 190 | 191 | 192 | static const luaL_Reg co_funcs[] = { 193 | {"create", luaB_cocreate}, 194 | {"resume", luaB_coresume}, 195 | {"running", luaB_corunning}, 196 | {"status", luaB_costatus}, 197 | {"wrap", luaB_cowrap}, 198 | {"yield", luaB_yield}, 199 | {"isyieldable", luaB_yieldable}, 200 | {"close", luaB_close}, 201 | {NULL, NULL} 202 | }; 203 | 204 | 205 | 206 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 207 | luaL_newlib(L, co_funcs); 208 | return 1; 209 | } 210 | 211 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | 20 | #if defined (LUA_UCID) /* accept UniCode IDentifiers? */ 21 | /* consider all non-ascii codepoints to be alphabetic */ 22 | #define NONA 0x01 23 | #else 24 | #define NONA 0x00 /* default */ 25 | #endif 26 | 27 | 28 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 29 | 0x00, /* EOZ */ 30 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 31 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 32 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 33 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 34 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 35 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 36 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 37 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 38 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 39 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 40 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 41 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 42 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 43 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 44 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 45 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 46 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */ 47 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 48 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */ 49 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 50 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */ 51 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 52 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */ 53 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 54 | 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */ 55 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 56 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */ 57 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 58 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */ 59 | NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 60 | NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */ 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 62 | }; 63 | 64 | #endif /* } */ 65 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua. 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | 65 | /* 66 | ** In ASCII, this 'ltolower' is correct for alphabetic characters and 67 | ** for '.'. That is enough for Lua needs. ('check_exp' ensures that 68 | ** the character either is an upper-case letter or is unchanged by 69 | ** the transformation, which holds for lower-case letters and '.'.) 70 | */ 71 | #define ltolower(c) \ 72 | check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ 73 | (c) | ('A' ^ 'a')) 74 | 75 | 76 | /* one entry for each character and for -1 (EOZ) */ 77 | LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) 78 | 79 | 80 | #else /* }{ */ 81 | 82 | /* 83 | ** use standard C ctypes 84 | */ 85 | 86 | #include 87 | 88 | 89 | #define lislalpha(c) (isalpha(c) || (c) == '_') 90 | #define lislalnum(c) (isalnum(c) || (c) == '_') 91 | #define lisdigit(c) (isdigit(c)) 92 | #define lisspace(c) (isspace(c)) 93 | #define lisprint(c) (isprint(c)) 94 | #define lisxdigit(c) (isxdigit(c)) 95 | 96 | #define ltolower(c) (tolower(c)) 97 | 98 | #endif /* } */ 99 | 100 | #endif 101 | 102 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) 15 | 16 | 17 | /* Active Lua function (given call info) */ 18 | #define ci_func(ci) (clLvalue(s2v((ci)->func))) 19 | 20 | 21 | #define resethookcount(L) (L->hookcount = L->basehookcount) 22 | 23 | /* 24 | ** mark for entries in 'lineinfo' array that has absolute information in 25 | ** 'abslineinfo' array 26 | */ 27 | #define ABSLINEINFO (-0x80) 28 | 29 | 30 | /* 31 | ** MAXimum number of successive Instructions WiTHout ABSolute line 32 | ** information. (A power of two allows fast divisions.) 33 | */ 34 | #if !defined(MAXIWTHABS) 35 | #define MAXIWTHABS 128 36 | #endif 37 | 38 | 39 | LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); 40 | LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, 41 | StkId *pos); 42 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 43 | const char *opname); 44 | LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); 45 | LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, 46 | const char *what); 47 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 48 | const TValue *p2); 49 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 50 | const TValue *p2, 51 | const char *msg); 52 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 53 | const TValue *p2); 54 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 55 | const TValue *p2); 56 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 57 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 58 | TString *src, int line); 59 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 60 | LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); 61 | 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** It also allows the running of one GC step when the stack is 21 | ** reallocated. 22 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 23 | ** at every check. 24 | */ 25 | #define luaD_checkstackaux(L,n,pre,pos) \ 26 | if (l_unlikely(L->stack_last - L->top <= (n))) \ 27 | { pre; luaD_growstack(L, n, 1); pos; } \ 28 | else { condmovestack(L,pre,pos); } 29 | 30 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 31 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 32 | 33 | 34 | 35 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 36 | #define restorestack(L,n) ((StkId)((char *)L->stack + (n))) 37 | 38 | 39 | /* macro to check stack size, preserving 'p' */ 40 | #define checkstackGCp(L,n,p) \ 41 | luaD_checkstackaux(L, n, \ 42 | ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ 43 | luaC_checkGC(L), /* stack grow uses memory */ \ 44 | p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ 45 | 46 | 47 | /* macro to check stack size and GC */ 48 | #define checkstackGC(L,fsize) \ 49 | luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) 50 | 51 | 52 | /* type of protected functions, to be ran by 'runprotected' */ 53 | typedef void (*Pfunc) (lua_State *L, void *ud); 54 | 55 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 56 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 57 | const char *mode); 58 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, 59 | int fTransfer, int nTransfer); 60 | LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); 61 | LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); 62 | LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); 63 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 64 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 65 | LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); 66 | LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); 67 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 68 | ptrdiff_t oldtop, ptrdiff_t ef); 69 | LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); 70 | LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); 71 | LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); 72 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 73 | LUAI_FUNC void luaD_inctop (lua_State *L); 74 | 75 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 76 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 77 | 78 | #endif 79 | 80 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct { 23 | lua_State *L; 24 | lua_Writer writer; 25 | void *data; 26 | int strip; 27 | int status; 28 | } DumpState; 29 | 30 | 31 | /* 32 | ** All high-level dumps go through dumpVector; you can change it to 33 | ** change the endianness of the result 34 | */ 35 | #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) 36 | 37 | #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) 38 | 39 | 40 | static void dumpBlock (DumpState *D, const void *b, size_t size) { 41 | if (D->status == 0 && size > 0) { 42 | lua_unlock(D->L); 43 | D->status = (*D->writer)(D->L, b, size, D->data); 44 | lua_lock(D->L); 45 | } 46 | } 47 | 48 | 49 | #define dumpVar(D,x) dumpVector(D,&x,1) 50 | 51 | 52 | static void dumpByte (DumpState *D, int y) { 53 | lu_byte x = (lu_byte)y; 54 | dumpVar(D, x); 55 | } 56 | 57 | 58 | /* dumpInt Buff Size */ 59 | #define DIBS ((sizeof(size_t) * 8 / 7) + 1) 60 | 61 | static void dumpSize (DumpState *D, size_t x) { 62 | lu_byte buff[DIBS]; 63 | int n = 0; 64 | do { 65 | buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ 66 | x >>= 7; 67 | } while (x != 0); 68 | buff[DIBS - 1] |= 0x80; /* mark last byte */ 69 | dumpVector(D, buff + DIBS - n, n); 70 | } 71 | 72 | 73 | static void dumpInt (DumpState *D, int x) { 74 | dumpSize(D, x); 75 | } 76 | 77 | 78 | static void dumpNumber (DumpState *D, lua_Number x) { 79 | dumpVar(D, x); 80 | } 81 | 82 | 83 | static void dumpInteger (DumpState *D, lua_Integer x) { 84 | dumpVar(D, x); 85 | } 86 | 87 | 88 | static void dumpString (DumpState *D, const TString *s) { 89 | if (s == NULL) 90 | dumpSize(D, 0); 91 | else { 92 | size_t size = tsslen(s); 93 | const char *str = getstr(s); 94 | dumpSize(D, size + 1); 95 | dumpVector(D, str, size); 96 | } 97 | } 98 | 99 | 100 | static void dumpCode (DumpState *D, const Proto *f) { 101 | dumpInt(D, f->sizecode); 102 | dumpVector(D, f->code, f->sizecode); 103 | } 104 | 105 | 106 | static void dumpFunction(DumpState *D, const Proto *f, TString *psource); 107 | 108 | static void dumpConstants (DumpState *D, const Proto *f) { 109 | int i; 110 | int n = f->sizek; 111 | dumpInt(D, n); 112 | for (i = 0; i < n; i++) { 113 | const TValue *o = &f->k[i]; 114 | int tt = ttypetag(o); 115 | dumpByte(D, tt); 116 | switch (tt) { 117 | case LUA_VNUMFLT: 118 | dumpNumber(D, fltvalue(o)); 119 | break; 120 | case LUA_VNUMINT: 121 | dumpInteger(D, ivalue(o)); 122 | break; 123 | case LUA_VSHRSTR: 124 | case LUA_VLNGSTR: 125 | dumpString(D, tsvalue(o)); 126 | break; 127 | default: 128 | lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); 129 | } 130 | } 131 | } 132 | 133 | 134 | static void dumpProtos (DumpState *D, const Proto *f) { 135 | int i; 136 | int n = f->sizep; 137 | dumpInt(D, n); 138 | for (i = 0; i < n; i++) 139 | dumpFunction(D, f->p[i], f->source); 140 | } 141 | 142 | 143 | static void dumpUpvalues (DumpState *D, const Proto *f) { 144 | int i, n = f->sizeupvalues; 145 | dumpInt(D, n); 146 | for (i = 0; i < n; i++) { 147 | dumpByte(D, f->upvalues[i].instack); 148 | dumpByte(D, f->upvalues[i].idx); 149 | dumpByte(D, f->upvalues[i].kind); 150 | } 151 | } 152 | 153 | 154 | static void dumpDebug (DumpState *D, const Proto *f) { 155 | int i, n; 156 | n = (D->strip) ? 0 : f->sizelineinfo; 157 | dumpInt(D, n); 158 | dumpVector(D, f->lineinfo, n); 159 | n = (D->strip) ? 0 : f->sizeabslineinfo; 160 | dumpInt(D, n); 161 | for (i = 0; i < n; i++) { 162 | dumpInt(D, f->abslineinfo[i].pc); 163 | dumpInt(D, f->abslineinfo[i].line); 164 | } 165 | n = (D->strip) ? 0 : f->sizelocvars; 166 | dumpInt(D, n); 167 | for (i = 0; i < n; i++) { 168 | dumpString(D, f->locvars[i].varname); 169 | dumpInt(D, f->locvars[i].startpc); 170 | dumpInt(D, f->locvars[i].endpc); 171 | } 172 | n = (D->strip) ? 0 : f->sizeupvalues; 173 | dumpInt(D, n); 174 | for (i = 0; i < n; i++) 175 | dumpString(D, f->upvalues[i].name); 176 | } 177 | 178 | 179 | static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { 180 | if (D->strip || f->source == psource) 181 | dumpString(D, NULL); /* no debug info or same source as its parent */ 182 | else 183 | dumpString(D, f->source); 184 | dumpInt(D, f->linedefined); 185 | dumpInt(D, f->lastlinedefined); 186 | dumpByte(D, f->numparams); 187 | dumpByte(D, f->is_vararg); 188 | dumpByte(D, f->maxstacksize); 189 | dumpCode(D, f); 190 | dumpConstants(D, f); 191 | dumpUpvalues(D, f); 192 | dumpProtos(D, f); 193 | dumpDebug(D, f); 194 | } 195 | 196 | 197 | static void dumpHeader (DumpState *D) { 198 | dumpLiteral(D, LUA_SIGNATURE); 199 | dumpByte(D, LUAC_VERSION); 200 | dumpByte(D, LUAC_FORMAT); 201 | dumpLiteral(D, LUAC_DATA); 202 | dumpByte(D, sizeof(Instruction)); 203 | dumpByte(D, sizeof(lua_Integer)); 204 | dumpByte(D, sizeof(lua_Number)); 205 | dumpInteger(D, LUAC_INT); 206 | dumpNumber(D, LUAC_NUM); 207 | } 208 | 209 | 210 | /* 211 | ** dump Lua function as precompiled chunk 212 | */ 213 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 214 | int strip) { 215 | DumpState D; 216 | D.L = L; 217 | D.writer = w; 218 | D.data = data; 219 | D.strip = strip; 220 | D.status = 0; 221 | dumpHeader(&D); 222 | dumpByte(&D, f->sizeupvalues); 223 | dumpFunction(&D, f, NULL); 224 | return D.status; 225 | } 226 | 227 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lgc.h" 21 | #include "lmem.h" 22 | #include "lobject.h" 23 | #include "lstate.h" 24 | 25 | 26 | 27 | CClosure *luaF_newCclosure (lua_State *L, int nupvals) { 28 | GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); 29 | CClosure *c = gco2ccl(o); 30 | c->nupvalues = cast_byte(nupvals); 31 | return c; 32 | } 33 | 34 | 35 | LClosure *luaF_newLclosure (lua_State *L, int nupvals) { 36 | GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); 37 | LClosure *c = gco2lcl(o); 38 | c->p = NULL; 39 | c->nupvalues = cast_byte(nupvals); 40 | while (nupvals--) c->upvals[nupvals] = NULL; 41 | return c; 42 | } 43 | 44 | 45 | /* 46 | ** fill a closure with new closed upvalues 47 | */ 48 | void luaF_initupvals (lua_State *L, LClosure *cl) { 49 | int i; 50 | for (i = 0; i < cl->nupvalues; i++) { 51 | GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); 52 | UpVal *uv = gco2upv(o); 53 | uv->v = &uv->u.value; /* make it closed */ 54 | setnilvalue(uv->v); 55 | cl->upvals[i] = uv; 56 | luaC_objbarrier(L, cl, uv); 57 | } 58 | } 59 | 60 | 61 | /* 62 | ** Create a new upvalue at the given level, and link it to the list of 63 | ** open upvalues of 'L' after entry 'prev'. 64 | **/ 65 | static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { 66 | GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); 67 | UpVal *uv = gco2upv(o); 68 | UpVal *next = *prev; 69 | uv->v = s2v(level); /* current value lives in the stack */ 70 | uv->tbc = tbc; 71 | uv->u.open.next = next; /* link it to list of open upvalues */ 72 | uv->u.open.previous = prev; 73 | if (next) 74 | next->u.open.previous = &uv->u.open.next; 75 | *prev = uv; 76 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ 77 | L->twups = G(L)->twups; /* link it to the list */ 78 | G(L)->twups = L; 79 | } 80 | return uv; 81 | } 82 | 83 | 84 | /* 85 | ** Find and reuse, or create if it does not exist, an upvalue 86 | ** at the given level. 87 | */ 88 | UpVal *luaF_findupval (lua_State *L, StkId level) { 89 | UpVal **pp = &L->openupval; 90 | UpVal *p; 91 | lua_assert(isintwups(L) || L->openupval == NULL); 92 | while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ 93 | lua_assert(!isdead(G(L), p)); 94 | if (uplevel(p) == level) /* corresponding upvalue? */ 95 | return p; /* return it */ 96 | pp = &p->u.open.next; 97 | } 98 | /* not found: create a new upvalue after 'pp' */ 99 | return newupval(L, 0, level, pp); 100 | } 101 | 102 | 103 | /* 104 | ** Call closing method for object 'obj' with error message 'err'. The 105 | ** boolean 'yy' controls whether the call is yieldable. 106 | ** (This function assumes EXTRA_STACK.) 107 | */ 108 | static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { 109 | StkId top = L->top; 110 | const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); 111 | setobj2s(L, top, tm); /* will call metamethod... */ 112 | setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ 113 | setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ 114 | L->top = top + 3; /* add function and arguments */ 115 | if (yy) 116 | luaD_call(L, top, 0); 117 | else 118 | luaD_callnoyield(L, top, 0); 119 | } 120 | 121 | 122 | /* 123 | ** Check whether object at given level has a close metamethod and raise 124 | ** an error if not. 125 | */ 126 | static void checkclosemth (lua_State *L, StkId level) { 127 | const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); 128 | if (ttisnil(tm)) { /* no metamethod? */ 129 | int idx = cast_int(level - L->ci->func); /* variable index */ 130 | const char *vname = luaG_findlocal(L, L->ci, idx, NULL); 131 | if (vname == NULL) vname = "?"; 132 | luaG_runerror(L, "variable '%s' got a non-closable value", vname); 133 | } 134 | } 135 | 136 | 137 | /* 138 | ** Prepare and call a closing method. 139 | ** If status is CLOSEKTOP, the call to the closing method will be pushed 140 | ** at the top of the stack. Otherwise, values can be pushed right after 141 | ** the 'level' of the upvalue being closed, as everything after that 142 | ** won't be used again. 143 | */ 144 | static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { 145 | TValue *uv = s2v(level); /* value being closed */ 146 | TValue *errobj; 147 | if (status == CLOSEKTOP) 148 | errobj = &G(L)->nilvalue; /* error object is nil */ 149 | else { /* 'luaD_seterrorobj' will set top to level + 2 */ 150 | errobj = s2v(level + 1); /* error object goes after 'uv' */ 151 | luaD_seterrorobj(L, status, level + 1); /* set error object */ 152 | } 153 | callclosemethod(L, uv, errobj, yy); 154 | } 155 | 156 | 157 | /* 158 | ** Maximum value for deltas in 'tbclist', dependent on the type 159 | ** of delta. (This macro assumes that an 'L' is in scope where it 160 | ** is used.) 161 | */ 162 | #define MAXDELTA \ 163 | ((256ul << ((sizeof(L->stack->tbclist.delta) - 1) * 8)) - 1) 164 | 165 | 166 | /* 167 | ** Insert a variable in the list of to-be-closed variables. 168 | */ 169 | void luaF_newtbcupval (lua_State *L, StkId level) { 170 | lua_assert(level > L->tbclist); 171 | if (l_isfalse(s2v(level))) 172 | return; /* false doesn't need to be closed */ 173 | checkclosemth(L, level); /* value must have a close method */ 174 | while (cast_uint(level - L->tbclist) > MAXDELTA) { 175 | L->tbclist += MAXDELTA; /* create a dummy node at maximum delta */ 176 | L->tbclist->tbclist.delta = 0; 177 | } 178 | level->tbclist.delta = cast(unsigned short, level - L->tbclist); 179 | L->tbclist = level; 180 | } 181 | 182 | 183 | void luaF_unlinkupval (UpVal *uv) { 184 | lua_assert(upisopen(uv)); 185 | *uv->u.open.previous = uv->u.open.next; 186 | if (uv->u.open.next) 187 | uv->u.open.next->u.open.previous = uv->u.open.previous; 188 | } 189 | 190 | 191 | /* 192 | ** Close all upvalues up to the given stack level. 193 | */ 194 | void luaF_closeupval (lua_State *L, StkId level) { 195 | UpVal *uv; 196 | StkId upl; /* stack index pointed by 'uv' */ 197 | while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { 198 | TValue *slot = &uv->u.value; /* new position for value */ 199 | lua_assert(uplevel(uv) < L->top); 200 | luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ 201 | setobj(L, slot, uv->v); /* move value to upvalue slot */ 202 | uv->v = slot; /* now current value lives here */ 203 | if (!iswhite(uv)) { /* neither white nor dead? */ 204 | nw2black(uv); /* closed upvalues cannot be gray */ 205 | luaC_barrier(L, uv, slot); 206 | } 207 | } 208 | } 209 | 210 | 211 | /* 212 | ** Remove firt element from the tbclist plus its dummy nodes. 213 | */ 214 | static void poptbclist (lua_State *L) { 215 | StkId tbc = L->tbclist; 216 | lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ 217 | tbc -= tbc->tbclist.delta; 218 | while (tbc > L->stack && tbc->tbclist.delta == 0) 219 | tbc -= MAXDELTA; /* remove dummy nodes */ 220 | L->tbclist = tbc; 221 | } 222 | 223 | 224 | /* 225 | ** Close all upvalues and to-be-closed variables up to the given stack 226 | ** level. 227 | */ 228 | void luaF_close (lua_State *L, StkId level, int status, int yy) { 229 | ptrdiff_t levelrel = savestack(L, level); 230 | luaF_closeupval(L, level); /* first, close the upvalues */ 231 | while (L->tbclist >= level) { /* traverse tbc's down to that level */ 232 | StkId tbc = L->tbclist; /* get variable index */ 233 | poptbclist(L); /* remove it from list */ 234 | prepcallclosemth(L, tbc, status, yy); /* close variable */ 235 | level = restorestack(L, levelrel); 236 | } 237 | } 238 | 239 | 240 | Proto *luaF_newproto (lua_State *L) { 241 | GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); 242 | Proto *f = gco2p(o); 243 | f->k = NULL; 244 | f->sizek = 0; 245 | f->p = NULL; 246 | f->sizep = 0; 247 | f->code = NULL; 248 | f->sizecode = 0; 249 | f->lineinfo = NULL; 250 | f->sizelineinfo = 0; 251 | f->abslineinfo = NULL; 252 | f->sizeabslineinfo = 0; 253 | f->upvalues = NULL; 254 | f->sizeupvalues = 0; 255 | f->numparams = 0; 256 | f->is_vararg = 0; 257 | f->maxstacksize = 0; 258 | f->locvars = NULL; 259 | f->sizelocvars = 0; 260 | f->linedefined = 0; 261 | f->lastlinedefined = 0; 262 | f->source = NULL; 263 | return f; 264 | } 265 | 266 | 267 | void luaF_freeproto (lua_State *L, Proto *f) { 268 | luaM_freearray(L, f->code, f->sizecode); 269 | luaM_freearray(L, f->p, f->sizep); 270 | luaM_freearray(L, f->k, f->sizek); 271 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 272 | luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); 273 | luaM_freearray(L, f->locvars, f->sizelocvars); 274 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 275 | luaM_free(L, f); 276 | } 277 | 278 | 279 | /* 280 | ** Look for n-th local variable at line 'line' in function 'func'. 281 | ** Returns NULL if not found. 282 | */ 283 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 284 | int i; 285 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 286 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 287 | local_number--; 288 | if (local_number == 0) 289 | return getstr(f->locvars[i].varname); 290 | } 291 | } 292 | return NULL; /* not found */ 293 | } 294 | 295 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ 15 | cast_int(sizeof(TValue)) * (n)) 16 | 17 | #define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ 18 | cast_int(sizeof(TValue *)) * (n)) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | #define upisopen(up) ((up)->v != &(up)->u.value) 33 | 34 | 35 | #define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) 36 | 37 | 38 | /* 39 | ** maximum number of misses before giving up the cache of closures 40 | ** in prototypes 41 | */ 42 | #define MAXMISS 10 43 | 44 | 45 | 46 | /* special status to close upvalues preserving the top of the stack */ 47 | #define CLOSEKTOP (-1) 48 | 49 | 50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); 52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); 53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 55 | LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); 56 | LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); 57 | LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); 58 | LUAI_FUNC void luaF_unlinkupval (UpVal *uv); 59 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 60 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 61 | int pc); 62 | 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which means 16 | ** the object is not marked; gray, which means the object is marked, but 17 | ** its references may be not marked; and black, which means that the 18 | ** object and all its references are marked. The main invariant of the 19 | ** garbage collector, while marking objects, is that a black object can 20 | ** never point to a white one. Moreover, any gray object must be in a 21 | ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it 22 | ** can be visited again before finishing the collection cycle. (Open 23 | ** upvalues are an exception to this rule.) These lists have no meaning 24 | ** when the invariant is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | /* 29 | ** Possible states of the Garbage Collector 30 | */ 31 | #define GCSpropagate 0 32 | #define GCSenteratomic 1 33 | #define GCSatomic 2 34 | #define GCSswpallgc 3 35 | #define GCSswpfinobj 4 36 | #define GCSswptobefnz 5 37 | #define GCSswpend 6 38 | #define GCScallfin 7 39 | #define GCSpause 8 40 | 41 | 42 | #define issweepphase(g) \ 43 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 44 | 45 | 46 | /* 47 | ** macro to tell when main invariant (white objects cannot point to black 48 | ** ones) must be kept. During a collection, the sweep 49 | ** phase may break the invariant, as objects turned white may point to 50 | ** still-black objects. The invariant is restored when sweep ends and 51 | ** all objects are white again. 52 | */ 53 | 54 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 55 | 56 | 57 | /* 58 | ** some useful bit tricks 59 | */ 60 | #define resetbits(x,m) ((x) &= cast_byte(~(m))) 61 | #define setbits(x,m) ((x) |= (m)) 62 | #define testbits(x,m) ((x) & (m)) 63 | #define bitmask(b) (1<<(b)) 64 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 65 | #define l_setbit(x,b) setbits(x, bitmask(b)) 66 | #define resetbit(x,b) resetbits(x, bitmask(b)) 67 | #define testbit(x,b) testbits(x, bitmask(b)) 68 | 69 | 70 | /* 71 | ** Layout for bit use in 'marked' field. First three bits are 72 | ** used for object "age" in generational mode. Last bit is used 73 | ** by tests. 74 | */ 75 | #define WHITE0BIT 3 /* object is white (type 0) */ 76 | #define WHITE1BIT 4 /* object is white (type 1) */ 77 | #define BLACKBIT 5 /* object is black */ 78 | #define FINALIZEDBIT 6 /* object has been marked for finalization */ 79 | 80 | #define TESTBIT 7 81 | 82 | 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) ((m) & (ow)) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define nw2black(x) \ 100 | check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) 101 | 102 | #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) 103 | 104 | 105 | /* object age in generational mode */ 106 | #define G_NEW 0 /* created in current cycle */ 107 | #define G_SURVIVAL 1 /* created in previous cycle */ 108 | #define G_OLD0 2 /* marked old by frw. barrier in this cycle */ 109 | #define G_OLD1 3 /* first full cycle as old */ 110 | #define G_OLD 4 /* really old object (not to be visited) */ 111 | #define G_TOUCHED1 5 /* old object touched this cycle */ 112 | #define G_TOUCHED2 6 /* old object touched in previous cycle */ 113 | 114 | #define AGEBITS 7 /* all age bits (111) */ 115 | 116 | #define getage(o) ((o)->marked & AGEBITS) 117 | #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) 118 | #define isold(o) (getage(o) > G_SURVIVAL) 119 | 120 | #define changeage(o,f,t) \ 121 | check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) 122 | 123 | 124 | /* Default Values for GC parameters */ 125 | #define LUAI_GENMAJORMUL 100 126 | #define LUAI_GENMINORMUL 20 127 | 128 | /* wait memory to double before starting new cycle */ 129 | #define LUAI_GCPAUSE 200 130 | 131 | /* 132 | ** some gc parameters are stored divided by 4 to allow a maximum value 133 | ** up to 1023 in a 'lu_byte'. 134 | */ 135 | #define getgcparam(p) ((p) * 4) 136 | #define setgcparam(p,v) ((p) = (v) / 4) 137 | 138 | #define LUAI_GCMUL 100 139 | 140 | /* how much to allocate before next GC step (log2) */ 141 | #define LUAI_GCSTEPSIZE 13 /* 8 KB */ 142 | 143 | 144 | /* 145 | ** Check whether the declared GC mode is generational. While in 146 | ** generational mode, the collector can go temporarily to incremental 147 | ** mode to improve performance. This is signaled by 'g->lastatomic != 0'. 148 | */ 149 | #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) 150 | 151 | /* 152 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 153 | ** allows some adjustments to be done only when needed. macro 154 | ** 'condchangemem' is used only for heavy tests (forcing a full 155 | ** GC cycle on every opportunity) 156 | */ 157 | #define luaC_condGC(L,pre,pos) \ 158 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 159 | condchangemem(L,pre,pos); } 160 | 161 | /* more often than not, 'pre'/'pos' are empty */ 162 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 163 | 164 | 165 | #define luaC_barrier(L,p,v) ( \ 166 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 167 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 168 | 169 | #define luaC_barrierback(L,p,v) ( \ 170 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 171 | luaC_barrierback_(L,p) : cast_void(0)) 172 | 173 | #define luaC_objbarrier(L,p,o) ( \ 174 | (isblack(p) && iswhite(o)) ? \ 175 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 176 | 177 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 178 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 179 | LUAI_FUNC void luaC_step (lua_State *L); 180 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 181 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 182 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 183 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 184 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 185 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 186 | LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); 187 | 188 | 189 | #endif 190 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {LUA_GNAME, luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | {NULL, NULL} 54 | }; 55 | 56 | 57 | LUALIB_API void luaL_openlibs (lua_State *L) { 58 | const luaL_Reg *lib; 59 | /* "require" functions from 'loadedlibs' and set results to global table */ 60 | for (lib = loadedlibs; lib->func; lib++) { 61 | luaL_requiref(L, lib->name, lib->func, 1); 62 | lua_pop(L, 1); /* remove lib */ 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ljumptab.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ljumptab.h $ 3 | ** Jump Table for the Lua interpreter 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #undef vmdispatch 9 | #undef vmcase 10 | #undef vmbreak 11 | 12 | #define vmdispatch(x) goto *disptab[x]; 13 | 14 | #define vmcase(l) L_##l: 15 | 16 | #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); 17 | 18 | 19 | static const void *const disptab[NUM_OPCODES] = { 20 | 21 | #if 0 22 | ** you can update the following list with this command: 23 | ** 24 | ** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h 25 | ** 26 | #endif 27 | 28 | &&L_OP_MOVE, 29 | &&L_OP_LOADI, 30 | &&L_OP_LOADF, 31 | &&L_OP_LOADK, 32 | &&L_OP_LOADKX, 33 | &&L_OP_LOADFALSE, 34 | &&L_OP_LFALSESKIP, 35 | &&L_OP_LOADTRUE, 36 | &&L_OP_LOADNIL, 37 | &&L_OP_GETUPVAL, 38 | &&L_OP_SETUPVAL, 39 | &&L_OP_GETTABUP, 40 | &&L_OP_GETTABLE, 41 | &&L_OP_GETI, 42 | &&L_OP_GETFIELD, 43 | &&L_OP_SETTABUP, 44 | &&L_OP_SETTABLE, 45 | &&L_OP_SETI, 46 | &&L_OP_SETFIELD, 47 | &&L_OP_NEWTABLE, 48 | &&L_OP_SELF, 49 | &&L_OP_ADDI, 50 | &&L_OP_ADDK, 51 | &&L_OP_SUBK, 52 | &&L_OP_MULK, 53 | &&L_OP_MODK, 54 | &&L_OP_POWK, 55 | &&L_OP_DIVK, 56 | &&L_OP_IDIVK, 57 | &&L_OP_BANDK, 58 | &&L_OP_BORK, 59 | &&L_OP_BXORK, 60 | &&L_OP_SHRI, 61 | &&L_OP_SHLI, 62 | &&L_OP_ADD, 63 | &&L_OP_SUB, 64 | &&L_OP_MUL, 65 | &&L_OP_MOD, 66 | &&L_OP_POW, 67 | &&L_OP_DIV, 68 | &&L_OP_IDIV, 69 | &&L_OP_BAND, 70 | &&L_OP_BOR, 71 | &&L_OP_BXOR, 72 | &&L_OP_SHL, 73 | &&L_OP_SHR, 74 | &&L_OP_MMBIN, 75 | &&L_OP_MMBINI, 76 | &&L_OP_MMBINK, 77 | &&L_OP_UNM, 78 | &&L_OP_BNOT, 79 | &&L_OP_NOT, 80 | &&L_OP_LEN, 81 | &&L_OP_CONCAT, 82 | &&L_OP_CLOSE, 83 | &&L_OP_TBC, 84 | &&L_OP_JMP, 85 | &&L_OP_EQ, 86 | &&L_OP_LT, 87 | &&L_OP_LE, 88 | &&L_OP_EQK, 89 | &&L_OP_EQI, 90 | &&L_OP_LTI, 91 | &&L_OP_LEI, 92 | &&L_OP_GTI, 93 | &&L_OP_GEI, 94 | &&L_OP_TEST, 95 | &&L_OP_TESTSET, 96 | &&L_OP_CALL, 97 | &&L_OP_TAILCALL, 98 | &&L_OP_RETURN, 99 | &&L_OP_RETURN0, 100 | &&L_OP_RETURN1, 101 | &&L_OP_FORLOOP, 102 | &&L_OP_FORPREP, 103 | &&L_OP_TFORPREP, 104 | &&L_OP_TFORCALL, 105 | &&L_OP_TFORLOOP, 106 | &&L_OP_SETLIST, 107 | &&L_OP_CLOSURE, 108 | &&L_OP_VARARG, 109 | &&L_OP_VARARGPREP, 110 | &&L_OP_EXTRAARG 111 | 112 | }; 113 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include 11 | 12 | #include "lobject.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Single-char tokens (terminal symbols) are represented by their own 18 | ** numeric code. Other tokens start at the following value. 19 | */ 20 | #define FIRST_RESERVED (UCHAR_MAX + 1) 21 | 22 | 23 | #if !defined(LUA_ENV) 24 | #define LUA_ENV "_ENV" 25 | #endif 26 | 27 | 28 | /* 29 | * WARNING: if you change the order of this enumeration, 30 | * grep "ORDER RESERVED" 31 | */ 32 | enum RESERVED { 33 | /* terminal symbols denoted by reserved words */ 34 | TK_AND = FIRST_RESERVED, TK_BREAK, 35 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 36 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 37 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 38 | /* other terminal symbols */ 39 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 40 | TK_SHL, TK_SHR, 41 | TK_DBCOLON, TK_EOS, 42 | TK_FLT, TK_INT, TK_NAME, TK_STRING 43 | }; 44 | 45 | /* number of reserved words */ 46 | #define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) 47 | 48 | 49 | typedef union { 50 | lua_Number r; 51 | lua_Integer i; 52 | TString *ts; 53 | } SemInfo; /* semantics information */ 54 | 55 | 56 | typedef struct Token { 57 | int token; 58 | SemInfo seminfo; 59 | } Token; 60 | 61 | 62 | /* state of the lexer plus state of the parser when shared by all 63 | functions */ 64 | typedef struct LexState { 65 | int current; /* current character (charint) */ 66 | int linenumber; /* input line counter */ 67 | int lastline; /* line of last token 'consumed' */ 68 | Token t; /* current token */ 69 | Token lookahead; /* look ahead token */ 70 | struct FuncState *fs; /* current function (parser) */ 71 | struct lua_State *L; 72 | ZIO *z; /* input stream */ 73 | Mbuffer *buff; /* buffer for tokens */ 74 | Table *h; /* to avoid collection/reuse strings */ 75 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 76 | TString *source; /* current source name */ 77 | TString *envn; /* environment variable name */ 78 | } LexState; 79 | 80 | 81 | LUAI_FUNC void luaX_init (lua_State *L); 82 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 83 | TString *source, int firstchar); 84 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 85 | LUAI_FUNC void luaX_next (LexState *ls); 86 | LUAI_FUNC int luaX_lookahead (LexState *ls); 87 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 88 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 89 | 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | #if defined(EMERGENCYGCTESTS) 26 | /* 27 | ** First allocation will fail whenever not building initial state. 28 | ** (This fail will trigger 'tryagain' and a full GC cycle at every 29 | ** allocation.) 30 | */ 31 | static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { 32 | if (completestate(g) && ns > 0) /* frees never fail */ 33 | return NULL; /* fail */ 34 | else /* normal allocation */ 35 | return (*g->frealloc)(g->ud, block, os, ns); 36 | } 37 | #else 38 | #define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) 39 | #endif 40 | 41 | 42 | 43 | 44 | 45 | /* 46 | ** About the realloc function: 47 | ** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 48 | ** ('osize' is the old size, 'nsize' is the new size) 49 | ** 50 | ** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. 51 | ** Particularly, frealloc(ud, NULL, 0, 0) does nothing, 52 | ** which is equivalent to free(NULL) in ISO C. 53 | ** 54 | ** - frealloc(ud, NULL, x, s) creates a new block of size 's' 55 | ** (no matter 'x'). Returns NULL if it cannot create the new block. 56 | ** 57 | ** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from 58 | ** size 'x' to size 'y'. Returns NULL if it cannot reallocate the 59 | ** block to the new size. 60 | */ 61 | 62 | 63 | 64 | 65 | /* 66 | ** {================================================================== 67 | ** Functions to allocate/deallocate arrays for the Parser 68 | ** =================================================================== 69 | */ 70 | 71 | /* 72 | ** Minimum size for arrays during parsing, to avoid overhead of 73 | ** reallocating to size 1, then 2, and then 4. All these arrays 74 | ** will be reallocated to exact sizes or erased when parsing ends. 75 | */ 76 | #define MINSIZEARRAY 4 77 | 78 | 79 | void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, 80 | int size_elems, int limit, const char *what) { 81 | void *newblock; 82 | int size = *psize; 83 | if (nelems + 1 <= size) /* does one extra element still fit? */ 84 | return block; /* nothing to be done */ 85 | if (size >= limit / 2) { /* cannot double it? */ 86 | if (l_unlikely(size >= limit)) /* cannot grow even a little? */ 87 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 88 | size = limit; /* still have at least one free place */ 89 | } 90 | else { 91 | size *= 2; 92 | if (size < MINSIZEARRAY) 93 | size = MINSIZEARRAY; /* minimum size */ 94 | } 95 | lua_assert(nelems + 1 <= size && size <= limit); 96 | /* 'limit' ensures that multiplication will not overflow */ 97 | newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, 98 | cast_sizet(size) * size_elems); 99 | *psize = size; /* update only when everything else is OK */ 100 | return newblock; 101 | } 102 | 103 | 104 | /* 105 | ** In prototypes, the size of the array is also its number of 106 | ** elements (to save memory). So, if it cannot shrink an array 107 | ** to its number of elements, the only option is to raise an 108 | ** error. 109 | */ 110 | void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, 111 | int final_n, int size_elem) { 112 | void *newblock; 113 | size_t oldsize = cast_sizet((*size) * size_elem); 114 | size_t newsize = cast_sizet(final_n * size_elem); 115 | lua_assert(newsize <= oldsize); 116 | newblock = luaM_saferealloc_(L, block, oldsize, newsize); 117 | *size = final_n; 118 | return newblock; 119 | } 120 | 121 | /* }================================================================== */ 122 | 123 | 124 | l_noret luaM_toobig (lua_State *L) { 125 | luaG_runerror(L, "memory allocation error: block too big"); 126 | } 127 | 128 | 129 | /* 130 | ** Free memory 131 | */ 132 | void luaM_free_ (lua_State *L, void *block, size_t osize) { 133 | global_State *g = G(L); 134 | lua_assert((osize == 0) == (block == NULL)); 135 | (*g->frealloc)(g->ud, block, osize, 0); 136 | g->GCdebt -= osize; 137 | } 138 | 139 | 140 | /* 141 | ** In case of allocation fail, this function will do an emergency 142 | ** collection to free some memory and then try the allocation again. 143 | ** The GC should not be called while state is not fully built, as the 144 | ** collector is not yet fully initialized. Also, it should not be called 145 | ** when 'gcstopem' is true, because then the interpreter is in the 146 | ** middle of a collection step. 147 | */ 148 | static void *tryagain (lua_State *L, void *block, 149 | size_t osize, size_t nsize) { 150 | global_State *g = G(L); 151 | if (completestate(g) && !g->gcstopem) { 152 | luaC_fullgc(L, 1); /* try to free some memory... */ 153 | return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 154 | } 155 | else return NULL; /* cannot free any memory without a full state */ 156 | } 157 | 158 | 159 | /* 160 | ** Generic allocation routine. 161 | */ 162 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 163 | void *newblock; 164 | global_State *g = G(L); 165 | lua_assert((osize == 0) == (block == NULL)); 166 | newblock = firsttry(g, block, osize, nsize); 167 | if (l_unlikely(newblock == NULL && nsize > 0)) { 168 | newblock = tryagain(L, block, osize, nsize); 169 | if (newblock == NULL) /* still no memory? */ 170 | return NULL; /* do not update 'GCdebt' */ 171 | } 172 | lua_assert((nsize == 0) == (newblock == NULL)); 173 | g->GCdebt = (g->GCdebt + nsize) - osize; 174 | return newblock; 175 | } 176 | 177 | 178 | void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, 179 | size_t nsize) { 180 | void *newblock = luaM_realloc_(L, block, osize, nsize); 181 | if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ 182 | luaM_error(L); 183 | return newblock; 184 | } 185 | 186 | 187 | void *luaM_malloc_ (lua_State *L, size_t size, int tag) { 188 | if (size == 0) 189 | return NULL; /* that's all */ 190 | else { 191 | global_State *g = G(L); 192 | void *newblock = firsttry(g, NULL, tag, size); 193 | if (l_unlikely(newblock == NULL)) { 194 | newblock = tryagain(L, NULL, tag, size); 195 | if (newblock == NULL) 196 | luaM_error(L); 197 | } 198 | g->GCdebt += size; 199 | return newblock; 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lopcodes.h" 14 | 15 | 16 | /* ORDER OP */ 17 | 18 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 19 | /* MM OT IT T A mode opcode */ 20 | opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */ 21 | ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */ 22 | ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ 23 | ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ 24 | ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ 25 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ 26 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */ 27 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ 28 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ 29 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ 30 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ 31 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */ 32 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */ 33 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */ 34 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */ 35 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */ 36 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ 37 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ 38 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ 39 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */ 40 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ 41 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ 42 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ 43 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ 44 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ 45 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */ 46 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */ 47 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */ 48 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */ 49 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ 50 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ 51 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ 52 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ 53 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ 54 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ 55 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ 56 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ 57 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */ 58 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */ 59 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */ 60 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */ 61 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */ 62 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */ 63 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */ 64 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ 65 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ 66 | ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ 67 | ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/ 68 | ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/ 69 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ 70 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ 71 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ 72 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */ 73 | ,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */ 74 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */ 75 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */ 76 | ,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */ 77 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */ 78 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */ 79 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */ 80 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */ 81 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */ 82 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */ 83 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */ 84 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */ 85 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */ 86 | ,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */ 87 | ,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */ 88 | ,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */ 89 | ,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */ 90 | ,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */ 91 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */ 92 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */ 93 | ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */ 94 | ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */ 95 | ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ 96 | ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ 97 | ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ 98 | ,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */ 99 | ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ 100 | ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ 101 | ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ 102 | ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ 103 | }; 104 | 105 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lopnames.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopnames.h $ 3 | ** Opcode names 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #if !defined(lopnames_h) 8 | #define lopnames_h 9 | 10 | #include 11 | 12 | 13 | /* ORDER OP */ 14 | 15 | static const char *const opnames[] = { 16 | "MOVE", 17 | "LOADI", 18 | "LOADF", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADFALSE", 22 | "LFALSESKIP", 23 | "LOADTRUE", 24 | "LOADNIL", 25 | "GETUPVAL", 26 | "SETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "GETI", 30 | "GETFIELD", 31 | "SETTABUP", 32 | "SETTABLE", 33 | "SETI", 34 | "SETFIELD", 35 | "NEWTABLE", 36 | "SELF", 37 | "ADDI", 38 | "ADDK", 39 | "SUBK", 40 | "MULK", 41 | "MODK", 42 | "POWK", 43 | "DIVK", 44 | "IDIVK", 45 | "BANDK", 46 | "BORK", 47 | "BXORK", 48 | "SHRI", 49 | "SHLI", 50 | "ADD", 51 | "SUB", 52 | "MUL", 53 | "MOD", 54 | "POW", 55 | "DIV", 56 | "IDIV", 57 | "BAND", 58 | "BOR", 59 | "BXOR", 60 | "SHL", 61 | "SHR", 62 | "MMBIN", 63 | "MMBINI", 64 | "MMBINK", 65 | "UNM", 66 | "BNOT", 67 | "NOT", 68 | "LEN", 69 | "CONCAT", 70 | "CLOSE", 71 | "TBC", 72 | "JMP", 73 | "EQ", 74 | "LT", 75 | "LE", 76 | "EQK", 77 | "EQI", 78 | "LTI", 79 | "LEI", 80 | "GTI", 81 | "GEI", 82 | "TEST", 83 | "TESTSET", 84 | "CALL", 85 | "TAILCALL", 86 | "RETURN", 87 | "RETURN0", 88 | "RETURN1", 89 | "FORLOOP", 90 | "FORPREP", 91 | "TFORPREP", 92 | "TFORCALL", 93 | "TFORLOOP", 94 | "SETLIST", 95 | "CLOSURE", 96 | "VARARG", 97 | "VARARGPREP", 98 | "EXTRAARG", 99 | NULL 100 | }; 101 | 102 | #endif 103 | 104 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression and variable descriptor. 17 | ** Code generation for variables and expressions can be delayed to allow 18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed 19 | ** variable/expression. It has a description of its "main" value plus a 20 | ** list of conditional jumps that can also produce its value (generated 21 | ** by short-circuit operators 'and'/'or'). 22 | */ 23 | 24 | /* kinds of variables/expressions */ 25 | typedef enum { 26 | VVOID, /* when 'expdesc' describes the last expression of a list, 27 | this kind means an empty list (so, no expression) */ 28 | VNIL, /* constant nil */ 29 | VTRUE, /* constant true */ 30 | VFALSE, /* constant false */ 31 | VK, /* constant in 'k'; info = index of constant in 'k' */ 32 | VKFLT, /* floating constant; nval = numerical float value */ 33 | VKINT, /* integer constant; ival = numerical integer value */ 34 | VKSTR, /* string constant; strval = TString address; 35 | (string is fixed by the lexer) */ 36 | VNONRELOC, /* expression has its value in a fixed register; 37 | info = result register */ 38 | VLOCAL, /* local variable; var.ridx = register index; 39 | var.vidx = relative index in 'actvar.arr' */ 40 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 41 | VCONST, /* compile-time variable; 42 | info = absolute index in 'actvar.arr' */ 43 | VINDEXED, /* indexed variable; 44 | ind.t = table register; 45 | ind.idx = key's R index */ 46 | VINDEXUP, /* indexed upvalue; 47 | ind.t = table upvalue; 48 | ind.idx = key's K index */ 49 | VINDEXI, /* indexed variable with constant integer; 50 | ind.t = table register; 51 | ind.idx = key's value */ 52 | VINDEXSTR, /* indexed variable with literal string; 53 | ind.t = table register; 54 | ind.idx = key's K index */ 55 | VJMP, /* expression is a test/comparison; 56 | info = pc of corresponding jump instruction */ 57 | VRELOC, /* expression can put result in any register; 58 | info = instruction pc */ 59 | VCALL, /* expression is a function call; info = instruction pc */ 60 | VVARARG /* vararg expression; info = instruction pc */ 61 | } expkind; 62 | 63 | 64 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) 65 | #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) 66 | 67 | 68 | typedef struct expdesc { 69 | expkind k; 70 | union { 71 | lua_Integer ival; /* for VKINT */ 72 | lua_Number nval; /* for VKFLT */ 73 | TString *strval; /* for VKSTR */ 74 | int info; /* for generic use */ 75 | struct { /* for indexed variables */ 76 | short idx; /* index (R or "long" K) */ 77 | lu_byte t; /* table (register or upvalue) */ 78 | } ind; 79 | struct { /* for local variables */ 80 | lu_byte ridx; /* register holding the variable */ 81 | unsigned short vidx; /* compiler index (in 'actvar.arr') */ 82 | } var; 83 | } u; 84 | int t; /* patch list of 'exit when true' */ 85 | int f; /* patch list of 'exit when false' */ 86 | } expdesc; 87 | 88 | 89 | /* kinds of variables */ 90 | #define VDKREG 0 /* regular */ 91 | #define RDKCONST 1 /* constant */ 92 | #define RDKTOCLOSE 2 /* to-be-closed */ 93 | #define RDKCTC 3 /* compile-time constant */ 94 | 95 | /* description of an active local variable */ 96 | typedef union Vardesc { 97 | struct { 98 | TValuefields; /* constant value (if it is a compile-time constant) */ 99 | lu_byte kind; 100 | lu_byte ridx; /* register holding the variable */ 101 | short pidx; /* index of the variable in the Proto's 'locvars' array */ 102 | TString *name; /* variable name */ 103 | } vd; 104 | TValue k; /* constant value (if any) */ 105 | } Vardesc; 106 | 107 | 108 | 109 | /* description of pending goto statements and label statements */ 110 | typedef struct Labeldesc { 111 | TString *name; /* label identifier */ 112 | int pc; /* position in code */ 113 | int line; /* line where it appeared */ 114 | lu_byte nactvar; /* number of active variables in that position */ 115 | lu_byte close; /* goto that escapes upvalues */ 116 | } Labeldesc; 117 | 118 | 119 | /* list of labels or gotos */ 120 | typedef struct Labellist { 121 | Labeldesc *arr; /* array */ 122 | int n; /* number of entries in use */ 123 | int size; /* array size */ 124 | } Labellist; 125 | 126 | 127 | /* dynamic structures used by the parser */ 128 | typedef struct Dyndata { 129 | struct { /* list of all active local variables */ 130 | Vardesc *arr; 131 | int n; 132 | int size; 133 | } actvar; 134 | Labellist gt; /* list of pending gotos */ 135 | Labellist label; /* list of active labels */ 136 | } Dyndata; 137 | 138 | 139 | /* control of blocks */ 140 | struct BlockCnt; /* defined in lparser.c */ 141 | 142 | 143 | /* state needed to generate code for a given function */ 144 | typedef struct FuncState { 145 | Proto *f; /* current function header */ 146 | struct FuncState *prev; /* enclosing function */ 147 | struct LexState *ls; /* lexical state */ 148 | struct BlockCnt *bl; /* chain of current blocks */ 149 | int pc; /* next position to code (equivalent to 'ncode') */ 150 | int lasttarget; /* 'label' of last 'jump label' */ 151 | int previousline; /* last line that was saved in 'lineinfo' */ 152 | int nk; /* number of elements in 'k' */ 153 | int np; /* number of elements in 'p' */ 154 | int nabslineinfo; /* number of elements in 'abslineinfo' */ 155 | int firstlocal; /* index of first local var (in Dyndata array) */ 156 | int firstlabel; /* index of first label (in 'dyd->label->arr') */ 157 | short ndebugvars; /* number of elements in 'f->locvars' */ 158 | lu_byte nactvar; /* number of active local variables */ 159 | lu_byte nups; /* number of upvalues */ 160 | lu_byte freereg; /* first free register */ 161 | lu_byte iwthabs; /* instructions issued since last absolute line info */ 162 | lu_byte needclose; /* function needs to close upvalues when returning */ 163 | } FuncState; 164 | 165 | 166 | LUAI_FUNC int luaY_nvarstack (FuncState *fs); 167 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 168 | Dyndata *dyd, const char *name, int firstchar); 169 | 170 | 171 | #endif 172 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lstring_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | 24 | 25 | /* 26 | ** Maximum size for string table. 27 | */ 28 | #define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*)) 29 | 30 | 31 | /* 32 | ** equality for long strings 33 | */ 34 | int luaS_eqlngstr (TString *a, TString *b) { 35 | size_t len = a->u.lnglen; 36 | lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); 37 | return (a == b) || /* same instance or... */ 38 | ((len == b->u.lnglen) && /* equal length and ... */ 39 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 40 | } 41 | 42 | 43 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 44 | unsigned int h = seed ^ cast_uint(l); 45 | for (; l > 0; l--) 46 | h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); 47 | return h; 48 | } 49 | 50 | 51 | unsigned int luaS_hashlongstr (TString *ts) { 52 | lua_assert(ts->tt == LUA_VLNGSTR); 53 | if (ts->extra == 0) { /* no hash? */ 54 | size_t len = ts->u.lnglen; 55 | ts->hash = luaS_hash(getstr(ts), len, ts->hash); 56 | ts->extra = 1; /* now it has its hash */ 57 | } 58 | return ts->hash; 59 | } 60 | 61 | 62 | static void tablerehash (TString **vect, int osize, int nsize) { 63 | int i; 64 | for (i = osize; i < nsize; i++) /* clear new elements */ 65 | vect[i] = NULL; 66 | for (i = 0; i < osize; i++) { /* rehash old part of the array */ 67 | TString *p = vect[i]; 68 | vect[i] = NULL; 69 | while (p) { /* for each string in the list */ 70 | TString *hnext = p->u.hnext; /* save next */ 71 | unsigned int h = lmod(p->hash, nsize); /* new position */ 72 | p->u.hnext = vect[h]; /* chain it into array */ 73 | vect[h] = p; 74 | p = hnext; 75 | } 76 | } 77 | } 78 | 79 | 80 | /* 81 | ** Resize the string table. If allocation fails, keep the current size. 82 | ** (This can degrade performance, but any non-zero size should work 83 | ** correctly.) 84 | */ 85 | void luaS_resize (lua_State *L, int nsize) { 86 | stringtable *tb = &G(L)->strt; 87 | int osize = tb->size; 88 | TString **newvect; 89 | if (nsize < osize) /* shrinking table? */ 90 | tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ 91 | newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); 92 | if (l_unlikely(newvect == NULL)) { /* reallocation failed? */ 93 | if (nsize < osize) /* was it shrinking table? */ 94 | tablerehash(tb->hash, nsize, osize); /* restore to original size */ 95 | /* leave table as it was */ 96 | } 97 | else { /* allocation succeeded */ 98 | tb->hash = newvect; 99 | tb->size = nsize; 100 | if (nsize > osize) 101 | tablerehash(newvect, osize, nsize); /* rehash for new size */ 102 | } 103 | } 104 | 105 | 106 | /* 107 | ** Clear API string cache. (Entries cannot be empty, so fill them with 108 | ** a non-collectable string.) 109 | */ 110 | void luaS_clearcache (global_State *g) { 111 | int i, j; 112 | for (i = 0; i < STRCACHE_N; i++) 113 | for (j = 0; j < STRCACHE_M; j++) { 114 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ 115 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ 116 | } 117 | } 118 | 119 | 120 | /* 121 | ** Initialize the string table and the string cache 122 | */ 123 | void luaS_init (lua_State *L) { 124 | global_State *g = G(L); 125 | int i, j; 126 | stringtable *tb = &G(L)->strt; 127 | tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); 128 | tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ 129 | tb->size = MINSTRTABSIZE; 130 | /* pre-create memory-error message */ 131 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 132 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ 133 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ 134 | for (j = 0; j < STRCACHE_M; j++) 135 | g->strcache[i][j] = g->memerrmsg; 136 | } 137 | 138 | 139 | 140 | /* 141 | ** creates a new string object 142 | */ 143 | static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { 144 | TString *ts; 145 | GCObject *o; 146 | size_t totalsize; /* total size of TString object */ 147 | totalsize = sizelstring(l); 148 | o = luaC_newobj(L, tag, totalsize); 149 | ts = gco2ts(o); 150 | ts->hash = h; 151 | ts->extra = 0; 152 | getstr(ts)[l] = '\0'; /* ending 0 */ 153 | return ts; 154 | } 155 | 156 | 157 | TString *luaS_createlngstrobj (lua_State *L, size_t l) { 158 | TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed); 159 | ts->u.lnglen = l; 160 | return ts; 161 | } 162 | 163 | 164 | void luaS_remove (lua_State *L, TString *ts) { 165 | stringtable *tb = &G(L)->strt; 166 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 167 | while (*p != ts) /* find previous element */ 168 | p = &(*p)->u.hnext; 169 | *p = (*p)->u.hnext; /* remove element from its list */ 170 | tb->nuse--; 171 | } 172 | 173 | 174 | static void growstrtab (lua_State *L, stringtable *tb) { 175 | if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ 176 | luaC_fullgc(L, 1); /* try to free some... */ 177 | if (tb->nuse == MAX_INT) /* still too many? */ 178 | luaM_error(L); /* cannot even create a message... */ 179 | } 180 | if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ 181 | luaS_resize(L, tb->size * 2); 182 | } 183 | 184 | 185 | /* 186 | ** Checks whether short string exists and reuses it or creates a new one. 187 | */ 188 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 189 | TString *ts; 190 | global_State *g = G(L); 191 | stringtable *tb = &g->strt; 192 | unsigned int h = luaS_hash(str, l, g->seed); 193 | TString **list = &tb->hash[lmod(h, tb->size)]; 194 | lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ 195 | for (ts = *list; ts != NULL; ts = ts->u.hnext) { 196 | if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 197 | /* found! */ 198 | if (isdead(g, ts)) /* dead (but not collected yet)? */ 199 | changewhite(ts); /* resurrect it */ 200 | return ts; 201 | } 202 | } 203 | /* else must create a new string */ 204 | if (tb->nuse >= tb->size) { /* need to grow string table? */ 205 | growstrtab(L, tb); 206 | list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ 207 | } 208 | ts = createstrobj(L, l, LUA_VSHRSTR, h); 209 | memcpy(getstr(ts), str, l * sizeof(char)); 210 | ts->shrlen = cast_byte(l); 211 | ts->u.hnext = *list; 212 | *list = ts; 213 | tb->nuse++; 214 | return ts; 215 | } 216 | 217 | 218 | /* 219 | ** new string (with explicit length) 220 | */ 221 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 222 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 223 | return internshrstr(L, str, l); 224 | else { 225 | TString *ts; 226 | if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) 227 | luaM_toobig(L); 228 | ts = luaS_createlngstrobj(L, l); 229 | memcpy(getstr(ts), str, l * sizeof(char)); 230 | return ts; 231 | } 232 | } 233 | 234 | 235 | /* 236 | ** Create or reuse a zero-terminated string, first checking in the 237 | ** cache (using the string address as a key). The cache can contain 238 | ** only zero-terminated strings, so it is safe to use 'strcmp' to 239 | ** check hits. 240 | */ 241 | TString *luaS_new (lua_State *L, const char *str) { 242 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ 243 | int j; 244 | TString **p = G(L)->strcache[i]; 245 | for (j = 0; j < STRCACHE_M; j++) { 246 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */ 247 | return p[j]; /* that is it */ 248 | } 249 | /* normal route */ 250 | for (j = STRCACHE_M - 1; j > 0; j--) 251 | p[j] = p[j - 1]; /* move out last element */ 252 | /* new element is first in the list */ 253 | p[0] = luaS_newlstr(L, str, strlen(str)); 254 | return p[0]; 255 | } 256 | 257 | 258 | Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { 259 | Udata *u; 260 | int i; 261 | GCObject *o; 262 | if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) 263 | luaM_toobig(L); 264 | o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); 265 | u = gco2u(o); 266 | u->len = s; 267 | u->nuvalue = nuvalue; 268 | u->metatable = NULL; 269 | for (i = 0; i < nuvalue; i++) 270 | setnilvalue(&u->uv[i].uv); 271 | return u; 272 | } 273 | 274 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | /* 16 | ** Memory-allocation error message must be preallocated (it cannot 17 | ** be created after memory is exhausted) 18 | */ 19 | #define MEMERRMSG "not enough memory" 20 | 21 | 22 | /* 23 | ** Size of a TString: Size of the header plus space for the string 24 | ** itself (including final '\0'). 25 | */ 26 | #define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) 27 | 28 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 29 | (sizeof(s)/sizeof(char))-1)) 30 | 31 | 32 | /* 33 | ** test whether a string is a reserved word 34 | */ 35 | #define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) 36 | 37 | 38 | /* 39 | ** equality for short strings, which are always internalized 40 | */ 41 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) 42 | 43 | 44 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 45 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 46 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 47 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 48 | LUAI_FUNC void luaS_clearcache (global_State *g); 49 | LUAI_FUNC void luaS_init (lua_State *L); 50 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 51 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); 52 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 53 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 54 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 55 | 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->u.next) 16 | 17 | 18 | /* 19 | ** Clear all bits of fast-access metamethods, which means that the table 20 | ** may have any of these metamethods. (First access that fails after the 21 | ** clearing will set the bit again.) 22 | */ 23 | #define invalidateTMcache(t) ((t)->flags &= ~maskflags) 24 | 25 | 26 | /* true when 't' is using 'dummynode' as its hash part */ 27 | #define isdummy(t) ((t)->lastfree == NULL) 28 | 29 | 30 | /* allocated size for hash nodes */ 31 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 32 | 33 | 34 | /* returns the Node, given the value of a table entry */ 35 | #define nodefromval(v) cast(Node *, (v)) 36 | 37 | 38 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 39 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 40 | TValue *value); 41 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 42 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 43 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 44 | LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, 45 | TValue *value); 46 | LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, 47 | TValue *value); 48 | LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, 49 | const TValue *slot, TValue *value); 50 | LUAI_FUNC Table *luaH_new (lua_State *L); 51 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 52 | unsigned int nhsize); 53 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 54 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 55 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 56 | LUAI_FUNC lua_Unsigned luaH_getn (Table *t); 57 | LUAI_FUNC unsigned int luaH_realasize (const Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy (const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "ltable.h" 24 | #include "ltm.h" 25 | #include "lvm.h" 26 | 27 | 28 | static const char udatatypename[] = "userdata"; 29 | 30 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = { 31 | "no value", 32 | "nil", "boolean", udatatypename, "number", 33 | "string", "table", "function", udatatypename, "thread", 34 | "upvalue", "proto" /* these last cases are used for tests only */ 35 | }; 36 | 37 | 38 | void luaT_init (lua_State *L) { 39 | static const char *const luaT_eventname[] = { /* ORDER TM */ 40 | "__index", "__newindex", 41 | "__gc", "__mode", "__len", "__eq", 42 | "__add", "__sub", "__mul", "__mod", "__pow", 43 | "__div", "__idiv", 44 | "__band", "__bor", "__bxor", "__shl", "__shr", 45 | "__unm", "__bnot", "__lt", "__le", 46 | "__concat", "__call", "__close" 47 | }; 48 | int i; 49 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 51 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 52 | } 53 | } 54 | 55 | 56 | /* 57 | ** function to be used with macro "fasttm": optimized for absence of 58 | ** tag methods 59 | */ 60 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 61 | const TValue *tm = luaH_getshortstr(events, ename); 62 | lua_assert(event <= TM_EQ); 63 | if (notm(tm)) { /* no tag method? */ 64 | events->flags |= cast_byte(1u<metatable; 76 | break; 77 | case LUA_TUSERDATA: 78 | mt = uvalue(o)->metatable; 79 | break; 80 | default: 81 | mt = G(L)->mt[ttype(o)]; 82 | } 83 | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); 84 | } 85 | 86 | 87 | /* 88 | ** Return the name of the type of an object. For tables and userdata 89 | ** with metatable, use their '__name' metafield, if present. 90 | */ 91 | const char *luaT_objtypename (lua_State *L, const TValue *o) { 92 | Table *mt; 93 | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || 94 | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { 95 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); 96 | if (ttisstring(name)) /* is '__name' a string? */ 97 | return getstr(tsvalue(name)); /* use it as type name */ 98 | } 99 | return ttypename(ttype(o)); /* else use standard type name */ 100 | } 101 | 102 | 103 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 104 | const TValue *p2, const TValue *p3) { 105 | StkId func = L->top; 106 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 107 | setobj2s(L, func + 1, p1); /* 1st argument */ 108 | setobj2s(L, func + 2, p2); /* 2nd argument */ 109 | setobj2s(L, func + 3, p3); /* 3rd argument */ 110 | L->top = func + 4; 111 | /* metamethod may yield only when called from Lua code */ 112 | if (isLuacode(L->ci)) 113 | luaD_call(L, func, 0); 114 | else 115 | luaD_callnoyield(L, func, 0); 116 | } 117 | 118 | 119 | void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, 120 | const TValue *p2, StkId res) { 121 | ptrdiff_t result = savestack(L, res); 122 | StkId func = L->top; 123 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 124 | setobj2s(L, func + 1, p1); /* 1st argument */ 125 | setobj2s(L, func + 2, p2); /* 2nd argument */ 126 | L->top += 3; 127 | /* metamethod may yield only when called from Lua code */ 128 | if (isLuacode(L->ci)) 129 | luaD_call(L, func, 1); 130 | else 131 | luaD_callnoyield(L, func, 1); 132 | res = restorestack(L, result); 133 | setobjs2s(L, res, --L->top); /* move result to its place */ 134 | } 135 | 136 | 137 | static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 138 | StkId res, TMS event) { 139 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 140 | if (notm(tm)) 141 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 142 | if (notm(tm)) return 0; 143 | luaT_callTMres(L, tm, p1, p2, res); 144 | return 1; 145 | } 146 | 147 | 148 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 149 | StkId res, TMS event) { 150 | if (l_unlikely(!callbinTM(L, p1, p2, res, event))) { 151 | switch (event) { 152 | case TM_BAND: case TM_BOR: case TM_BXOR: 153 | case TM_SHL: case TM_SHR: case TM_BNOT: { 154 | if (ttisnumber(p1) && ttisnumber(p2)) 155 | luaG_tointerror(L, p1, p2); 156 | else 157 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 158 | } 159 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 160 | default: 161 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 162 | } 163 | } 164 | } 165 | 166 | 167 | void luaT_tryconcatTM (lua_State *L) { 168 | StkId top = L->top; 169 | if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, 170 | TM_CONCAT))) 171 | luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); 172 | } 173 | 174 | 175 | void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, 176 | int flip, StkId res, TMS event) { 177 | if (flip) 178 | luaT_trybinTM(L, p2, p1, res, event); 179 | else 180 | luaT_trybinTM(L, p1, p2, res, event); 181 | } 182 | 183 | 184 | void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 185 | int flip, StkId res, TMS event) { 186 | TValue aux; 187 | setivalue(&aux, i2); 188 | luaT_trybinassocTM(L, p1, &aux, flip, res, event); 189 | } 190 | 191 | 192 | /* 193 | ** Calls an order tag method. 194 | ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old 195 | ** behavior: if there is no '__le', try '__lt', based on l <= r iff 196 | ** !(r < l) (assuming a total order). If the metamethod yields during 197 | ** this substitution, the continuation has to know about it (to negate 198 | ** the result of rtop, event)) /* try original event */ 204 | return !l_isfalse(s2v(L->top)); 205 | #if defined(LUA_COMPAT_LT_LE) 206 | else if (event == TM_LE) { 207 | /* try '!(p2 < p1)' for '(p1 <= p2)' */ 208 | L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ 209 | if (callbinTM(L, p2, p1, L->top, TM_LT)) { 210 | L->ci->callstatus ^= CIST_LEQ; /* clear mark */ 211 | return l_isfalse(s2v(L->top)); 212 | } 213 | /* else error will remove this 'ci'; no need to clear mark */ 214 | } 215 | #endif 216 | luaG_ordererror(L, p1, p2); /* no metamethod found */ 217 | return 0; /* to avoid warnings */ 218 | } 219 | 220 | 221 | int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 222 | int flip, int isfloat, TMS event) { 223 | TValue aux; const TValue *p2; 224 | if (isfloat) { 225 | setfltvalue(&aux, cast_num(v2)); 226 | } 227 | else 228 | setivalue(&aux, v2); 229 | if (flip) { /* arguments were exchanged? */ 230 | p2 = p1; p1 = &aux; /* correct them */ 231 | } 232 | else 233 | p2 = &aux; 234 | return luaT_callorderTM(L, p1, p2, event); 235 | } 236 | 237 | 238 | void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, 239 | const Proto *p) { 240 | int i; 241 | int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ 242 | int nextra = actual - nfixparams; /* number of extra arguments */ 243 | ci->u.l.nextraargs = nextra; 244 | luaD_checkstack(L, p->maxstacksize + 1); 245 | /* copy function to the top of the stack */ 246 | setobjs2s(L, L->top++, ci->func); 247 | /* move fixed parameters to the top of the stack */ 248 | for (i = 1; i <= nfixparams; i++) { 249 | setobjs2s(L, L->top++, ci->func + i); 250 | setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ 251 | } 252 | ci->func += actual + 1; 253 | ci->top += actual + 1; 254 | lua_assert(L->top <= ci->top && ci->top <= L->stack_last); 255 | } 256 | 257 | 258 | void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { 259 | int i; 260 | int nextra = ci->u.l.nextraargs; 261 | if (wanted < 0) { 262 | wanted = nextra; /* get all extra arguments available */ 263 | checkstackGCp(L, nextra, where); /* ensure stack space */ 264 | L->top = where + nextra; /* next instruction will need top */ 265 | } 266 | for (i = 0; i < wanted && i < nextra; i++) 267 | setobjs2s(L, where + i, ci->func - nextra + i); 268 | for (; i < wanted; i++) /* complete required results with nil */ 269 | setnilvalue(s2v(where + i)); 270 | } 271 | 272 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Mask with 1 in all fast-access methods. A 1 in any of these bits 50 | ** in the flag of a (meta)table means the metatable does not have the 51 | ** corresponding metamethod field. (Bit 7 of the flag is used for 52 | ** 'isrealasize'.) 53 | */ 54 | #define maskflags (~(~0u << (TM_EQ + 1))) 55 | 56 | 57 | /* 58 | ** Test whether there is no tagmethod. 59 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 60 | */ 61 | #define notm(tm) ttisnil(tm) 62 | 63 | 64 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 65 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 66 | 67 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 68 | 69 | #define ttypename(x) luaT_typenames_[(x) + 1] 70 | 71 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) 72 | 73 | 74 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 75 | 76 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 77 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 78 | TMS event); 79 | LUAI_FUNC void luaT_init (lua_State *L); 80 | 81 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 82 | const TValue *p2, const TValue *p3); 83 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 84 | const TValue *p1, const TValue *p2, StkId p3); 85 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 86 | StkId res, TMS event); 87 | LUAI_FUNC void luaT_tryconcatTM (lua_State *L); 88 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 89 | const TValue *p2, int inv, StkId res, TMS event); 90 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 91 | int inv, StkId res, TMS event); 92 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 93 | const TValue *p2, TMS event); 94 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 95 | int inv, int isfloat, TMS event); 96 | 97 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 98 | struct CallInfo *ci, const Proto *p); 99 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 100 | StkId where, int wanted); 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_MATHLIBNAME "math" 39 | LUAMOD_API int (luaopen_math) (lua_State *L); 40 | 41 | #define LUA_DBLIBNAME "debug" 42 | LUAMOD_API int (luaopen_debug) (lua_State *L); 43 | 44 | #define LUA_LOADLIBNAME "package" 45 | LUAMOD_API int (luaopen_package) (lua_State *L); 46 | 47 | 48 | /* open all previous libraries */ 49 | LUALIB_API void (luaL_openlibs) (lua_State *L); 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | #include 15 | 16 | #include "lua.h" 17 | 18 | #include "ldebug.h" 19 | #include "ldo.h" 20 | #include "lfunc.h" 21 | #include "lmem.h" 22 | #include "lobject.h" 23 | #include "lstring.h" 24 | #include "lundump.h" 25 | #include "lzio.h" 26 | 27 | 28 | #if !defined(luai_verifycode) 29 | #define luai_verifycode(L,f) /* empty */ 30 | #endif 31 | 32 | 33 | typedef struct { 34 | lua_State *L; 35 | ZIO *Z; 36 | const char *name; 37 | } LoadState; 38 | 39 | 40 | static l_noret error (LoadState *S, const char *why) { 41 | luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); 42 | luaD_throw(S->L, LUA_ERRSYNTAX); 43 | } 44 | 45 | 46 | /* 47 | ** All high-level loads go through loadVector; you can change it to 48 | ** adapt to the endianness of the input 49 | */ 50 | #define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) 51 | 52 | static void loadBlock (LoadState *S, void *b, size_t size) { 53 | if (luaZ_read(S->Z, b, size) != 0) 54 | error(S, "truncated chunk"); 55 | } 56 | 57 | 58 | #define loadVar(S,x) loadVector(S,&x,1) 59 | 60 | 61 | static lu_byte loadByte (LoadState *S) { 62 | int b = zgetc(S->Z); 63 | if (b == EOZ) 64 | error(S, "truncated chunk"); 65 | return cast_byte(b); 66 | } 67 | 68 | 69 | static size_t loadUnsigned (LoadState *S, size_t limit) { 70 | size_t x = 0; 71 | int b; 72 | limit >>= 7; 73 | do { 74 | b = loadByte(S); 75 | if (x >= limit) 76 | error(S, "integer overflow"); 77 | x = (x << 7) | (b & 0x7f); 78 | } while ((b & 0x80) == 0); 79 | return x; 80 | } 81 | 82 | 83 | static size_t loadSize (LoadState *S) { 84 | return loadUnsigned(S, ~(size_t)0); 85 | } 86 | 87 | 88 | static int loadInt (LoadState *S) { 89 | return cast_int(loadUnsigned(S, INT_MAX)); 90 | } 91 | 92 | 93 | static lua_Number loadNumber (LoadState *S) { 94 | lua_Number x; 95 | loadVar(S, x); 96 | return x; 97 | } 98 | 99 | 100 | static lua_Integer loadInteger (LoadState *S) { 101 | lua_Integer x; 102 | loadVar(S, x); 103 | return x; 104 | } 105 | 106 | 107 | /* 108 | ** Load a nullable string into prototype 'p'. 109 | */ 110 | static TString *loadStringN (LoadState *S, Proto *p) { 111 | lua_State *L = S->L; 112 | TString *ts; 113 | size_t size = loadSize(S); 114 | if (size == 0) /* no string? */ 115 | return NULL; 116 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ 117 | char buff[LUAI_MAXSHORTLEN]; 118 | loadVector(S, buff, size); /* load string into buffer */ 119 | ts = luaS_newlstr(L, buff, size); /* create string */ 120 | } 121 | else { /* long string */ 122 | ts = luaS_createlngstrobj(L, size); /* create string */ 123 | setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ 124 | luaD_inctop(L); 125 | loadVector(S, getstr(ts), size); /* load directly in final place */ 126 | L->top--; /* pop string */ 127 | } 128 | luaC_objbarrier(L, p, ts); 129 | return ts; 130 | } 131 | 132 | 133 | /* 134 | ** Load a non-nullable string into prototype 'p'. 135 | */ 136 | static TString *loadString (LoadState *S, Proto *p) { 137 | TString *st = loadStringN(S, p); 138 | if (st == NULL) 139 | error(S, "bad format for constant string"); 140 | return st; 141 | } 142 | 143 | 144 | static void loadCode (LoadState *S, Proto *f) { 145 | int n = loadInt(S); 146 | f->code = luaM_newvectorchecked(S->L, n, Instruction); 147 | f->sizecode = n; 148 | loadVector(S, f->code, n); 149 | } 150 | 151 | 152 | static void loadFunction(LoadState *S, Proto *f, TString *psource); 153 | 154 | 155 | static void loadConstants (LoadState *S, Proto *f) { 156 | int i; 157 | int n = loadInt(S); 158 | f->k = luaM_newvectorchecked(S->L, n, TValue); 159 | f->sizek = n; 160 | for (i = 0; i < n; i++) 161 | setnilvalue(&f->k[i]); 162 | for (i = 0; i < n; i++) { 163 | TValue *o = &f->k[i]; 164 | int t = loadByte(S); 165 | switch (t) { 166 | case LUA_VNIL: 167 | setnilvalue(o); 168 | break; 169 | case LUA_VFALSE: 170 | setbfvalue(o); 171 | break; 172 | case LUA_VTRUE: 173 | setbtvalue(o); 174 | break; 175 | case LUA_VNUMFLT: 176 | setfltvalue(o, loadNumber(S)); 177 | break; 178 | case LUA_VNUMINT: 179 | setivalue(o, loadInteger(S)); 180 | break; 181 | case LUA_VSHRSTR: 182 | case LUA_VLNGSTR: 183 | setsvalue2n(S->L, o, loadString(S, f)); 184 | break; 185 | default: lua_assert(0); 186 | } 187 | } 188 | } 189 | 190 | 191 | static void loadProtos (LoadState *S, Proto *f) { 192 | int i; 193 | int n = loadInt(S); 194 | f->p = luaM_newvectorchecked(S->L, n, Proto *); 195 | f->sizep = n; 196 | for (i = 0; i < n; i++) 197 | f->p[i] = NULL; 198 | for (i = 0; i < n; i++) { 199 | f->p[i] = luaF_newproto(S->L); 200 | luaC_objbarrier(S->L, f, f->p[i]); 201 | loadFunction(S, f->p[i], f->source); 202 | } 203 | } 204 | 205 | 206 | /* 207 | ** Load the upvalues for a function. The names must be filled first, 208 | ** because the filling of the other fields can raise read errors and 209 | ** the creation of the error message can call an emergency collection; 210 | ** in that case all prototypes must be consistent for the GC. 211 | */ 212 | static void loadUpvalues (LoadState *S, Proto *f) { 213 | int i, n; 214 | n = loadInt(S); 215 | f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); 216 | f->sizeupvalues = n; 217 | for (i = 0; i < n; i++) /* make array valid for GC */ 218 | f->upvalues[i].name = NULL; 219 | for (i = 0; i < n; i++) { /* following calls can raise errors */ 220 | f->upvalues[i].instack = loadByte(S); 221 | f->upvalues[i].idx = loadByte(S); 222 | f->upvalues[i].kind = loadByte(S); 223 | } 224 | } 225 | 226 | 227 | static void loadDebug (LoadState *S, Proto *f) { 228 | int i, n; 229 | n = loadInt(S); 230 | f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); 231 | f->sizelineinfo = n; 232 | loadVector(S, f->lineinfo, n); 233 | n = loadInt(S); 234 | f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); 235 | f->sizeabslineinfo = n; 236 | for (i = 0; i < n; i++) { 237 | f->abslineinfo[i].pc = loadInt(S); 238 | f->abslineinfo[i].line = loadInt(S); 239 | } 240 | n = loadInt(S); 241 | f->locvars = luaM_newvectorchecked(S->L, n, LocVar); 242 | f->sizelocvars = n; 243 | for (i = 0; i < n; i++) 244 | f->locvars[i].varname = NULL; 245 | for (i = 0; i < n; i++) { 246 | f->locvars[i].varname = loadStringN(S, f); 247 | f->locvars[i].startpc = loadInt(S); 248 | f->locvars[i].endpc = loadInt(S); 249 | } 250 | n = loadInt(S); 251 | for (i = 0; i < n; i++) 252 | f->upvalues[i].name = loadStringN(S, f); 253 | } 254 | 255 | 256 | static void loadFunction (LoadState *S, Proto *f, TString *psource) { 257 | f->source = loadStringN(S, f); 258 | if (f->source == NULL) /* no source in dump? */ 259 | f->source = psource; /* reuse parent's source */ 260 | f->linedefined = loadInt(S); 261 | f->lastlinedefined = loadInt(S); 262 | f->numparams = loadByte(S); 263 | f->is_vararg = loadByte(S); 264 | f->maxstacksize = loadByte(S); 265 | loadCode(S, f); 266 | loadConstants(S, f); 267 | loadUpvalues(S, f); 268 | loadProtos(S, f); 269 | loadDebug(S, f); 270 | } 271 | 272 | 273 | static void checkliteral (LoadState *S, const char *s, const char *msg) { 274 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ 275 | size_t len = strlen(s); 276 | loadVector(S, buff, len); 277 | if (memcmp(s, buff, len) != 0) 278 | error(S, msg); 279 | } 280 | 281 | 282 | static void fchecksize (LoadState *S, size_t size, const char *tname) { 283 | if (loadByte(S) != size) 284 | error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); 285 | } 286 | 287 | 288 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) 289 | 290 | static void checkHeader (LoadState *S) { 291 | /* skip 1st char (already read and checked) */ 292 | checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); 293 | if (loadByte(S) != LUAC_VERSION) 294 | error(S, "version mismatch"); 295 | if (loadByte(S) != LUAC_FORMAT) 296 | error(S, "format mismatch"); 297 | checkliteral(S, LUAC_DATA, "corrupted chunk"); 298 | checksize(S, Instruction); 299 | checksize(S, lua_Integer); 300 | checksize(S, lua_Number); 301 | if (loadInteger(S) != LUAC_INT) 302 | error(S, "integer format mismatch"); 303 | if (loadNumber(S) != LUAC_NUM) 304 | error(S, "float format mismatch"); 305 | } 306 | 307 | 308 | /* 309 | ** Load precompiled chunk. 310 | */ 311 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { 312 | LoadState S; 313 | LClosure *cl; 314 | if (*name == '@' || *name == '=') 315 | S.name = name + 1; 316 | else if (*name == LUA_SIGNATURE[0]) 317 | S.name = "binary string"; 318 | else 319 | S.name = name; 320 | S.L = L; 321 | S.Z = Z; 322 | checkHeader(&S); 323 | cl = luaF_newLclosure(L, loadByte(&S)); 324 | setclLvalue2s(L, L->top, cl); 325 | luaD_inctop(L); 326 | cl->p = luaF_newproto(L); 327 | luaC_objbarrier(L, cl, cl->p); 328 | loadFunction(&S, cl->p, NULL); 329 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); 330 | luai_verifycode(L, cl->p); 331 | return cl; 332 | } 333 | 334 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | /* 22 | ** Encode major-minor version in one byte, one nibble for each 23 | */ 24 | #define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ 25 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 26 | 27 | #define LUAC_FORMAT 0 /* this is the official format */ 28 | 29 | /* load one chunk; from lundump.c */ 30 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 31 | 32 | /* dump one chunk; from ldump.c */ 33 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 34 | void* data, int strip); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lutf8lib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lutf8lib.c $ 3 | ** Standard library for UTF-8 manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lutf8lib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "lua.h" 19 | 20 | #include "lauxlib.h" 21 | #include "lualib.h" 22 | 23 | 24 | #define MAXUNICODE 0x10FFFFu 25 | 26 | #define MAXUTF 0x7FFFFFFFu 27 | 28 | /* 29 | ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. 30 | */ 31 | #if (UINT_MAX >> 30) >= 1 32 | typedef unsigned int utfint; 33 | #else 34 | typedef unsigned long utfint; 35 | #endif 36 | 37 | 38 | #define iscont(p) ((*(p) & 0xC0) == 0x80) 39 | 40 | 41 | /* from strlib */ 42 | /* translate a relative string position: negative means back from end */ 43 | static lua_Integer u_posrelat (lua_Integer pos, size_t len) { 44 | if (pos >= 0) return pos; 45 | else if (0u - (size_t)pos > len) return 0; 46 | else return (lua_Integer)len + pos + 1; 47 | } 48 | 49 | 50 | /* 51 | ** Decode one UTF-8 sequence, returning NULL if byte sequence is 52 | ** invalid. The array 'limits' stores the minimum value for each 53 | ** sequence length, to check for overlong representations. Its first 54 | ** entry forces an error for non-ascii bytes with no continuation 55 | ** bytes (count == 0). 56 | */ 57 | static const char *utf8_decode (const char *s, utfint *val, int strict) { 58 | static const utfint limits[] = 59 | {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; 60 | unsigned int c = (unsigned char)s[0]; 61 | utfint res = 0; /* final result */ 62 | if (c < 0x80) /* ascii? */ 63 | res = c; 64 | else { 65 | int count = 0; /* to count number of continuation bytes */ 66 | for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ 67 | unsigned int cc = (unsigned char)s[++count]; /* read next byte */ 68 | if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ 69 | return NULL; /* invalid byte sequence */ 70 | res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ 71 | } 72 | res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ 73 | if (count > 5 || res > MAXUTF || res < limits[count]) 74 | return NULL; /* invalid byte sequence */ 75 | s += count; /* skip continuation bytes read */ 76 | } 77 | if (strict) { 78 | /* check for invalid code points; too large or surrogates */ 79 | if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) 80 | return NULL; 81 | } 82 | if (val) *val = res; 83 | return s + 1; /* +1 to include first byte */ 84 | } 85 | 86 | 87 | /* 88 | ** utf8len(s [, i [, j [, lax]]]) --> number of characters that 89 | ** start in the range [i,j], or nil + current position if 's' is not 90 | ** well formed in that interval 91 | */ 92 | static int utflen (lua_State *L) { 93 | lua_Integer n = 0; /* counter for the number of characters */ 94 | size_t len; /* string length in bytes */ 95 | const char *s = luaL_checklstring(L, 1, &len); 96 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 97 | lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); 98 | int lax = lua_toboolean(L, 4); 99 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, 100 | "initial position out of bounds"); 101 | luaL_argcheck(L, --posj < (lua_Integer)len, 3, 102 | "final position out of bounds"); 103 | while (posi <= posj) { 104 | const char *s1 = utf8_decode(s + posi, NULL, !lax); 105 | if (s1 == NULL) { /* conversion error? */ 106 | luaL_pushfail(L); /* return fail ... */ 107 | lua_pushinteger(L, posi + 1); /* ... and current position */ 108 | return 2; 109 | } 110 | posi = s1 - s; 111 | n++; 112 | } 113 | lua_pushinteger(L, n); 114 | return 1; 115 | } 116 | 117 | 118 | /* 119 | ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all 120 | ** characters that start in the range [i,j] 121 | */ 122 | static int codepoint (lua_State *L) { 123 | size_t len; 124 | const char *s = luaL_checklstring(L, 1, &len); 125 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 126 | lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); 127 | int lax = lua_toboolean(L, 4); 128 | int n; 129 | const char *se; 130 | luaL_argcheck(L, posi >= 1, 2, "out of bounds"); 131 | luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); 132 | if (posi > pose) return 0; /* empty interval; return no values */ 133 | if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ 134 | return luaL_error(L, "string slice too long"); 135 | n = (int)(pose - posi) + 1; /* upper bound for number of returns */ 136 | luaL_checkstack(L, n, "string slice too long"); 137 | n = 0; /* count the number of returns */ 138 | se = s + pose; /* string end */ 139 | for (s += posi - 1; s < se;) { 140 | utfint code; 141 | s = utf8_decode(s, &code, !lax); 142 | if (s == NULL) 143 | return luaL_error(L, "invalid UTF-8 code"); 144 | lua_pushinteger(L, code); 145 | n++; 146 | } 147 | return n; 148 | } 149 | 150 | 151 | static void pushutfchar (lua_State *L, int arg) { 152 | lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); 153 | luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); 154 | lua_pushfstring(L, "%U", (long)code); 155 | } 156 | 157 | 158 | /* 159 | ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... 160 | */ 161 | static int utfchar (lua_State *L) { 162 | int n = lua_gettop(L); /* number of arguments */ 163 | if (n == 1) /* optimize common case of single char */ 164 | pushutfchar(L, 1); 165 | else { 166 | int i; 167 | luaL_Buffer b; 168 | luaL_buffinit(L, &b); 169 | for (i = 1; i <= n; i++) { 170 | pushutfchar(L, i); 171 | luaL_addvalue(&b); 172 | } 173 | luaL_pushresult(&b); 174 | } 175 | return 1; 176 | } 177 | 178 | 179 | /* 180 | ** offset(s, n, [i]) -> index where n-th character counting from 181 | ** position 'i' starts; 0 means character at 'i'. 182 | */ 183 | static int byteoffset (lua_State *L) { 184 | size_t len; 185 | const char *s = luaL_checklstring(L, 1, &len); 186 | lua_Integer n = luaL_checkinteger(L, 2); 187 | lua_Integer posi = (n >= 0) ? 1 : len + 1; 188 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len); 189 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, 190 | "position out of bounds"); 191 | if (n == 0) { 192 | /* find beginning of current byte sequence */ 193 | while (posi > 0 && iscont(s + posi)) posi--; 194 | } 195 | else { 196 | if (iscont(s + posi)) 197 | return luaL_error(L, "initial position is a continuation byte"); 198 | if (n < 0) { 199 | while (n < 0 && posi > 0) { /* move back */ 200 | do { /* find beginning of previous character */ 201 | posi--; 202 | } while (posi > 0 && iscont(s + posi)); 203 | n++; 204 | } 205 | } 206 | else { 207 | n--; /* do not move for 1st character */ 208 | while (n > 0 && posi < (lua_Integer)len) { 209 | do { /* find beginning of next character */ 210 | posi++; 211 | } while (iscont(s + posi)); /* (cannot pass final '\0') */ 212 | n--; 213 | } 214 | } 215 | } 216 | if (n == 0) /* did it find given character? */ 217 | lua_pushinteger(L, posi + 1); 218 | else /* no such character */ 219 | luaL_pushfail(L); 220 | return 1; 221 | } 222 | 223 | 224 | static int iter_aux (lua_State *L, int strict) { 225 | size_t len; 226 | const char *s = luaL_checklstring(L, 1, &len); 227 | lua_Integer n = lua_tointeger(L, 2) - 1; 228 | if (n < 0) /* first iteration? */ 229 | n = 0; /* start from here */ 230 | else if (n < (lua_Integer)len) { 231 | n++; /* skip current byte */ 232 | while (iscont(s + n)) n++; /* and its continuations */ 233 | } 234 | if (n >= (lua_Integer)len) 235 | return 0; /* no more codepoints */ 236 | else { 237 | utfint code; 238 | const char *next = utf8_decode(s + n, &code, strict); 239 | if (next == NULL) 240 | return luaL_error(L, "invalid UTF-8 code"); 241 | lua_pushinteger(L, n + 1); 242 | lua_pushinteger(L, code); 243 | return 2; 244 | } 245 | } 246 | 247 | 248 | static int iter_auxstrict (lua_State *L) { 249 | return iter_aux(L, 1); 250 | } 251 | 252 | static int iter_auxlax (lua_State *L) { 253 | return iter_aux(L, 0); 254 | } 255 | 256 | 257 | static int iter_codes (lua_State *L) { 258 | int lax = lua_toboolean(L, 2); 259 | luaL_checkstring(L, 1); 260 | lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); 261 | lua_pushvalue(L, 1); 262 | lua_pushinteger(L, 0); 263 | return 3; 264 | } 265 | 266 | 267 | /* pattern to match a single UTF-8 character */ 268 | #define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" 269 | 270 | 271 | static const luaL_Reg funcs[] = { 272 | {"offset", byteoffset}, 273 | {"codepoint", codepoint}, 274 | {"char", utfchar}, 275 | {"len", utflen}, 276 | {"codes", iter_codes}, 277 | /* placeholders */ 278 | {"charpattern", NULL}, 279 | {NULL, NULL} 280 | }; 281 | 282 | 283 | LUAMOD_API int luaopen_utf8 (lua_State *L) { 284 | luaL_newlib(L, funcs); 285 | lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); 286 | lua_setfield(L, -2, "charpattern"); 287 | return 1; 288 | } 289 | 290 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I F2Ieq 37 | #endif 38 | 39 | 40 | /* 41 | ** Rounding modes for float->integer coercion 42 | */ 43 | typedef enum { 44 | F2Ieq, /* no rounding; accepts only integral values */ 45 | F2Ifloor, /* takes the floor of the number */ 46 | F2Iceil /* takes the ceil of the number */ 47 | } F2Imod; 48 | 49 | 50 | /* convert an object to a float (including string coercion) */ 51 | #define tonumber(o,n) \ 52 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 53 | 54 | 55 | /* convert an object to a float (without string coercion) */ 56 | #define tonumberns(o,n) \ 57 | (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ 58 | (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) 59 | 60 | 61 | /* convert an object to an integer (including string coercion) */ 62 | #define tointeger(o,i) \ 63 | (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ 64 | : luaV_tointeger(o,i,LUA_FLOORN2I)) 65 | 66 | 67 | /* convert an object to an integer (without string coercion) */ 68 | #define tointegerns(o,i) \ 69 | (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ 70 | : luaV_tointegerns(o,i,LUA_FLOORN2I)) 71 | 72 | 73 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 74 | 75 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 76 | 77 | 78 | /* 79 | ** fast track for 'gettable': if 't' is a table and 't[k]' is present, 80 | ** return 1 with 'slot' pointing to 't[k]' (position of final result). 81 | ** Otherwise, return 0 (meaning it will have to check metamethod) 82 | ** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL 83 | ** (otherwise). 'f' is the raw get function to use. 84 | */ 85 | #define luaV_fastget(L,t,k,slot,f) \ 86 | (!ttistable(t) \ 87 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 88 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 89 | !isempty(slot))) /* result not empty? */ 90 | 91 | 92 | /* 93 | ** Special case of 'luaV_fastget' for integers, inlining the fast case 94 | ** of 'luaH_getint'. 95 | */ 96 | #define luaV_fastgeti(L,t,k,slot) \ 97 | (!ttistable(t) \ 98 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 99 | : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ 100 | ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ 101 | !isempty(slot))) /* result not empty? */ 102 | 103 | 104 | /* 105 | ** Finish a fast set operation (when fast get succeeds). In that case, 106 | ** 'slot' points to the place to put the value. 107 | */ 108 | #define luaV_finishfastset(L,t,slot,v) \ 109 | { setobj2t(L, cast(TValue *,slot), v); \ 110 | luaC_barrierback(L, gcvalue(t), v); } 111 | 112 | 113 | 114 | 115 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 116 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 117 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 118 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 119 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); 120 | LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, 121 | F2Imod mode); 122 | LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); 123 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 124 | StkId val, const TValue *slot); 125 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 126 | TValue *val, const TValue *slot); 127 | LUAI_FUNC void luaV_finishOp (lua_State *L); 128 | LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); 129 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 130 | LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); 131 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 132 | LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); 133 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 134 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 135 | 136 | #endif 137 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /src/lua-5.4.3/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /src/luazen-2.1/base64.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 Phil Leblanc -- see LICENSE file 2 | // --------------------------------------------------------------------- 3 | // base64 encoding 4 | 5 | 6 | // --------------------------------------------------------------------- 7 | // lua binding 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "lua.h" 14 | #include "lauxlib.h" 15 | 16 | # define LERR(msg) return luaL_error(L, msg) 17 | 18 | // (exported functions are prefixed with 'll_') 19 | 20 | // base64 encode, decode 21 | // public domain, by Luiz Henrique de Figueiredo, 2010 22 | 23 | // encode(): added an optional 'linelength' parameter 24 | // decode(): modified to allow decoding of non well-formed 25 | // encoded strings (ie. strings with no '=' padding) 26 | 27 | #define uint unsigned int 28 | #define B64LINELENGTH 72 29 | 30 | static const char code[]= 31 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 32 | 33 | static void b64encode(luaL_Buffer *b, uint c1, uint c2, uint c3, int n) { 34 | unsigned long tuple=c3+256UL*(c2+256UL*c1); 35 | int i; 36 | char s[4]; 37 | for (i=0; i<4; i++) { 38 | s[3-i] = code[tuple % 64]; 39 | tuple /= 64; 40 | } 41 | for (i=n+1; i<4; i++) s[i]='='; 42 | luaL_addlstring(b,s,4); 43 | } 44 | 45 | int ll_b64encode(lua_State *L) { 46 | // Lua: 47 | // b64encode(str) or b64encode(str, linelen) 48 | // str is the tring to enccode 49 | // linelen is an optional output line length 50 | // must be multiple of 4 51 | // default is 72, (must be <= 76 for Mime) 52 | // if 0, no '\n' is inserted 53 | size_t l; 54 | const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l); 55 | int linelength = ( 56 | lua_isnoneornil(L, 2) ? B64LINELENGTH : luaL_checkinteger(L, 2) 57 | ); 58 | luaL_Buffer b; 59 | int n; 60 | int cn = 0; 61 | luaL_buffinit(L,&b); 62 | for (n=l/3; n--; s+=3) { 63 | b64encode(&b,s[0],s[1],s[2],3); 64 | cn += 4; 65 | if ( linelength && cn >= linelength) { 66 | cn = 0; 67 | luaL_addlstring(&b,"\n",1); 68 | } 69 | } 70 | switch (l%3) 71 | { 72 | case 1: b64encode(&b,s[0],0,0,1); break; 73 | case 2: b64encode(&b,s[0],s[1],0,2); break; 74 | } 75 | luaL_pushresult(&b); 76 | return 1; 77 | } 78 | 79 | static void b64decode(luaL_Buffer *b, 80 | int c1, int c2, int c3, int c4, int n) { 81 | unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1)); 82 | char s[3]; 83 | switch (--n) 84 | { 85 | case 3: s[2]=tuple; 86 | case 2: s[1]=tuple >> 8; 87 | case 1: s[0]=tuple >> 16; 88 | } 89 | luaL_addlstring(b,s,n); 90 | } 91 | 92 | int ll_b64decode(lua_State *L) { 93 | // Lua api: b64decode(str) 94 | // str is the base64-encoded string to decode 95 | // return the decoded string or nil if str contains 96 | // an invalid character (whitespaces and newlines are ignored) 97 | // 98 | size_t l; 99 | const char *s=luaL_checklstring(L,1,&l); 100 | luaL_Buffer b; 101 | int n=0; 102 | char t[4]; 103 | luaL_buffinit(L,&b); 104 | for (;;) { 105 | int c=*s++; 106 | switch (c) { 107 | const char *p; 108 | case '=': 109 | //ph: added 'case 0:' here to allow decoding of non well-formed 110 | // encoded strings (ie. strings with no padding) 111 | case 0: 112 | switch (n) { 113 | case 1: b64decode(&b,t[0],0,0,0,1); break; 114 | case 2: b64decode(&b,t[0],t[1],0,0,2); break; 115 | case 3: b64decode(&b,t[0],t[1],t[2],0,3); break; 116 | } 117 | luaL_pushresult(&b); 118 | return 1; 119 | case '\n': 120 | case '\r': 121 | case '\t': 122 | case ' ': 123 | case '\f': 124 | case '\b': 125 | break; 126 | default: 127 | p=strchr(code,c); if (p==NULL) return 0; 128 | t[n++]= p-code; 129 | if (n==4) { 130 | b64decode(&b,t[0],t[1],t[2],t[3],4); 131 | n=0; 132 | } 133 | break; 134 | } //switch(c) 135 | } //for(;;) 136 | return 0; 137 | } 138 | -------------------------------------------------------------------------------- /src/luazen-2.1/luazen.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022 Phil Leblanc -- see LICENSE file 2 | // --------------------------------------------------------------------- 3 | /* 4 | luazen crypto, encoding and compression library 5 | 6 | */ 7 | 8 | 9 | 10 | // --------------------------------------------------------------------- 11 | // lua binding 12 | 13 | #define LIBNAME luazen 14 | #define VERSION "luazen-2.1" 15 | 16 | #include 17 | 18 | #include "lua.h" 19 | #include "lauxlib.h" 20 | 21 | // compatibility with Lua 5.2 --and lua 5.3, added 150621 22 | // (from roberto's lpeg 0.10.1 dated 101203) 23 | // 24 | #if (LUA_VERSION_NUM >= 502) 25 | 26 | #undef lua_equal 27 | #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 28 | 29 | #undef lua_getfenv 30 | #define lua_getfenv lua_getuservalue 31 | #undef lua_setfenv 32 | #define lua_setfenv lua_setuservalue 33 | 34 | #undef lua_objlen 35 | #define lua_objlen lua_rawlen 36 | 37 | #undef luaL_register 38 | #define luaL_register(L,n,f) \ 39 | { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } 40 | 41 | #endif 42 | 43 | //---------------------------------------------------------------------- 44 | // library table 45 | 46 | // max number of registered functions + 1 47 | #define LT_SIZE 100 48 | 49 | static struct luaL_Reg llib[LT_SIZE]; 50 | static int llib_top = 0; 51 | static luaL_Reg regnull = {NULL, NULL}; 52 | 53 | static int llib_append(const char *fname, lua_CFunction func) { 54 | // append a function registration to the function table 55 | luaL_Reg reg; 56 | reg.name = fname; 57 | reg.func = func; 58 | llib[llib_top] = reg; 59 | llib_top++; 60 | assert(llib_top < LT_SIZE); 61 | llib[llib_top] = regnull; 62 | }// llib_append 63 | 64 | // APPEND macro: declare and register a Lua function in one place. eg: 65 | // APPEND(lzf) 66 | // is expanded to: 67 | // int ll_lzf(lua_State *L); 68 | // llib_append("lzf", ll_lzf); 69 | // 70 | // it assumes that 71 | // - all library Lua C functions are named as 'll_xyz' 72 | // - the Lua name for the ll_xyz C function is 'xyz' 73 | 74 | #define APPEND(NAME) \ 75 | int ll_##NAME(lua_State *L); \ 76 | llib_append(#NAME, ll_ ## NAME); 77 | 78 | static void llib_init() { 79 | // must reinitialize llib_top each time llib_init is called 80 | llib_top = 0; 81 | 82 | // luazen function declarations - comment APPEND lines to 83 | // remove functions from the luazen build 84 | // 85 | //from lzma 86 | APPEND(lzma) 87 | APPEND(unlzma) 88 | // 89 | // from random, base64, md5 90 | APPEND(randombytes) 91 | APPEND(b64encode) 92 | APPEND(b64decode) 93 | APPEND(md5) 94 | // 95 | // from mono 96 | APPEND(encrypt) 97 | APPEND(decrypt) 98 | APPEND(blake2b) 99 | APPEND(argon2i) 100 | APPEND(x25519_public_key) 101 | APPEND(key_exchange) 102 | APPEND(x25519) 103 | APPEND(ed25519_public_key) 104 | APPEND(ed25519_sign) 105 | APPEND(ed25519_check) 106 | APPEND(sha512) 107 | // 108 | } //llib_init() 109 | 110 | //---------------------------------------------------------------------- 111 | // library registration 112 | 113 | int luaopen_luazen (lua_State *L) { 114 | llib_init(); // fill the library table 115 | luaL_register (L, "luazen", llib); 116 | // 117 | lua_pushliteral (L, "VERSION"); 118 | lua_pushliteral (L, VERSION); 119 | lua_settable (L, -3); 120 | return 1; 121 | } 122 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/Alloc.h: -------------------------------------------------------------------------------- 1 | /* Alloc.h -- Memory allocation functions 2 | 2018-02-19 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __COMMON_ALLOC_H 5 | #define __COMMON_ALLOC_H 6 | 7 | #include "7zTypes.h" 8 | 9 | EXTERN_C_BEGIN 10 | 11 | void *MyAlloc(size_t size); 12 | void MyFree(void *address); 13 | 14 | #ifdef _WIN32 15 | 16 | void SetLargePageSize(); 17 | 18 | void *MidAlloc(size_t size); 19 | void MidFree(void *address); 20 | void *BigAlloc(size_t size); 21 | void BigFree(void *address); 22 | 23 | #else 24 | 25 | #define MidAlloc(size) MyAlloc(size) 26 | #define MidFree(address) MyFree(address) 27 | #define BigAlloc(size) MyAlloc(size) 28 | #define BigFree(address) MyFree(address) 29 | 30 | #endif 31 | 32 | extern const ISzAlloc g_Alloc; 33 | extern const ISzAlloc g_BigAlloc; 34 | extern const ISzAlloc g_MidAlloc; 35 | extern const ISzAlloc g_AlignedAlloc; 36 | 37 | 38 | typedef struct 39 | { 40 | ISzAlloc vt; 41 | ISzAllocPtr baseAlloc; 42 | unsigned numAlignBits; /* ((1 << numAlignBits) >= sizeof(void *)) */ 43 | size_t offset; /* (offset == (k * sizeof(void *)) && offset < (1 << numAlignBits) */ 44 | } CAlignOffsetAlloc; 45 | 46 | void AlignOffsetAlloc_CreateVTable(CAlignOffsetAlloc *p); 47 | 48 | 49 | EXTERN_C_END 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/Compiler.h: -------------------------------------------------------------------------------- 1 | /* Compiler.h 2 | 2017-04-03 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __7Z_COMPILER_H 5 | #define __7Z_COMPILER_H 6 | 7 | #ifdef _MSC_VER 8 | 9 | #ifdef UNDER_CE 10 | #define RPC_NO_WINDOWS_H 11 | /* #pragma warning(disable : 4115) // '_RPC_ASYNC_STATE' : named type definition in parentheses */ 12 | #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union 13 | #pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int 14 | #endif 15 | 16 | #if _MSC_VER >= 1300 17 | #pragma warning(disable : 4996) // This function or variable may be unsafe 18 | #else 19 | #pragma warning(disable : 4511) // copy constructor could not be generated 20 | #pragma warning(disable : 4512) // assignment operator could not be generated 21 | #pragma warning(disable : 4514) // unreferenced inline function has been removed 22 | #pragma warning(disable : 4702) // unreachable code 23 | #pragma warning(disable : 4710) // not inlined 24 | #pragma warning(disable : 4714) // function marked as __forceinline not inlined 25 | #pragma warning(disable : 4786) // identifier was truncated to '255' characters in the debug information 26 | #endif 27 | 28 | #endif 29 | 30 | #define UNUSED_VAR(x) (void)x; 31 | /* #define UNUSED_VAR(x) x=x; */ 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/LzFind.h: -------------------------------------------------------------------------------- 1 | /* LzFind.h -- Match finder for LZ algorithms 2 | 2017-06-10 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZ_FIND_H 5 | #define __LZ_FIND_H 6 | 7 | #include "7zTypes.h" 8 | 9 | EXTERN_C_BEGIN 10 | 11 | typedef UInt32 CLzRef; 12 | 13 | typedef struct _CMatchFinder 14 | { 15 | Byte *buffer; 16 | UInt32 pos; 17 | UInt32 posLimit; 18 | UInt32 streamPos; 19 | UInt32 lenLimit; 20 | 21 | UInt32 cyclicBufferPos; 22 | UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ 23 | 24 | Byte streamEndWasReached; 25 | Byte btMode; 26 | Byte bigHash; 27 | Byte directInput; 28 | 29 | UInt32 matchMaxLen; 30 | CLzRef *hash; 31 | CLzRef *son; 32 | UInt32 hashMask; 33 | UInt32 cutValue; 34 | 35 | Byte *bufferBase; 36 | ISeqInStream *stream; 37 | 38 | UInt32 blockSize; 39 | UInt32 keepSizeBefore; 40 | UInt32 keepSizeAfter; 41 | 42 | UInt32 numHashBytes; 43 | size_t directInputRem; 44 | UInt32 historySize; 45 | UInt32 fixedHashSize; 46 | UInt32 hashSizeSum; 47 | SRes result; 48 | UInt32 crc[256]; 49 | size_t numRefs; 50 | 51 | UInt64 expectedDataSize; 52 | } CMatchFinder; 53 | 54 | #define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer) 55 | 56 | #define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos) 57 | 58 | #define Inline_MatchFinder_IsFinishedOK(p) \ 59 | ((p)->streamEndWasReached \ 60 | && (p)->streamPos == (p)->pos \ 61 | && (!(p)->directInput || (p)->directInputRem == 0)) 62 | 63 | int MatchFinder_NeedMove(CMatchFinder *p); 64 | Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p); 65 | void MatchFinder_MoveBlock(CMatchFinder *p); 66 | void MatchFinder_ReadIfRequired(CMatchFinder *p); 67 | 68 | void MatchFinder_Construct(CMatchFinder *p); 69 | 70 | /* Conditions: 71 | historySize <= 3 GB 72 | keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB 73 | */ 74 | int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, 75 | UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, 76 | ISzAllocPtr alloc); 77 | void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc); 78 | void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems); 79 | void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue); 80 | 81 | UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son, 82 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, 83 | UInt32 *distances, UInt32 maxLen); 84 | 85 | /* 86 | Conditions: 87 | Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. 88 | Mf_GetPointerToCurrentPos_Func's result must be used only before any other function 89 | */ 90 | 91 | typedef void (*Mf_Init_Func)(void *object); 92 | typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object); 93 | typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object); 94 | typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances); 95 | typedef void (*Mf_Skip_Func)(void *object, UInt32); 96 | 97 | typedef struct _IMatchFinder 98 | { 99 | Mf_Init_Func Init; 100 | Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; 101 | Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; 102 | Mf_GetMatches_Func GetMatches; 103 | Mf_Skip_Func Skip; 104 | } IMatchFinder; 105 | 106 | void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable); 107 | 108 | void MatchFinder_Init_LowHash(CMatchFinder *p); 109 | void MatchFinder_Init_HighHash(CMatchFinder *p); 110 | void MatchFinder_Init_3(CMatchFinder *p, int readData); 111 | void MatchFinder_Init(CMatchFinder *p); 112 | 113 | UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); 114 | UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); 115 | 116 | void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); 117 | void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); 118 | 119 | EXTERN_C_END 120 | 121 | #endif 122 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/LzHash.h: -------------------------------------------------------------------------------- 1 | /* LzHash.h -- HASH functions for LZ algorithms 2 | 2015-04-12 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZ_HASH_H 5 | #define __LZ_HASH_H 6 | 7 | #define kHash2Size (1 << 10) 8 | #define kHash3Size (1 << 16) 9 | #define kHash4Size (1 << 20) 10 | 11 | #define kFix3HashSize (kHash2Size) 12 | #define kFix4HashSize (kHash2Size + kHash3Size) 13 | #define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size) 14 | 15 | #define HASH2_CALC hv = cur[0] | ((UInt32)cur[1] << 8); 16 | 17 | #define HASH3_CALC { \ 18 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 19 | h2 = temp & (kHash2Size - 1); \ 20 | hv = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; } 21 | 22 | #define HASH4_CALC { \ 23 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 24 | h2 = temp & (kHash2Size - 1); \ 25 | temp ^= ((UInt32)cur[2] << 8); \ 26 | h3 = temp & (kHash3Size - 1); \ 27 | hv = (temp ^ (p->crc[cur[3]] << 5)) & p->hashMask; } 28 | 29 | #define HASH5_CALC { \ 30 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 31 | h2 = temp & (kHash2Size - 1); \ 32 | temp ^= ((UInt32)cur[2] << 8); \ 33 | h3 = temp & (kHash3Size - 1); \ 34 | temp ^= (p->crc[cur[3]] << 5); \ 35 | h4 = temp & (kHash4Size - 1); \ 36 | hv = (temp ^ (p->crc[cur[4]] << 3)) & p->hashMask; } 37 | 38 | /* #define HASH_ZIP_CALC hv = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */ 39 | #define HASH_ZIP_CALC hv = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF; 40 | 41 | 42 | #define MT_HASH2_CALC \ 43 | h2 = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1); 44 | 45 | #define MT_HASH3_CALC { \ 46 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 47 | h2 = temp & (kHash2Size - 1); \ 48 | h3 = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); } 49 | 50 | #define MT_HASH4_CALC { \ 51 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 52 | h2 = temp & (kHash2Size - 1); \ 53 | temp ^= ((UInt32)cur[2] << 8); \ 54 | h3 = temp & (kHash3Size - 1); \ 55 | h4 = (temp ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/LzmaDec.h: -------------------------------------------------------------------------------- 1 | /* LzmaDec.h -- LZMA Decoder 2 | 2018-04-21 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZMA_DEC_H 5 | #define __LZMA_DEC_H 6 | 7 | #include "7zTypes.h" 8 | 9 | EXTERN_C_BEGIN 10 | 11 | /* #define _LZMA_PROB32 */ 12 | /* _LZMA_PROB32 can increase the speed on some CPUs, 13 | but memory usage for CLzmaDec::probs will be doubled in that case */ 14 | 15 | typedef 16 | #ifdef _LZMA_PROB32 17 | UInt32 18 | #else 19 | UInt16 20 | #endif 21 | CLzmaProb; 22 | 23 | 24 | /* ---------- LZMA Properties ---------- */ 25 | 26 | #define LZMA_PROPS_SIZE 5 27 | 28 | typedef struct _CLzmaProps 29 | { 30 | Byte lc; 31 | Byte lp; 32 | Byte pb; 33 | Byte _pad_; 34 | UInt32 dicSize; 35 | } CLzmaProps; 36 | 37 | /* LzmaProps_Decode - decodes properties 38 | Returns: 39 | SZ_OK 40 | SZ_ERROR_UNSUPPORTED - Unsupported properties 41 | */ 42 | 43 | SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); 44 | 45 | 46 | /* ---------- LZMA Decoder state ---------- */ 47 | 48 | /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. 49 | Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ 50 | 51 | #define LZMA_REQUIRED_INPUT_MAX 20 52 | 53 | typedef struct 54 | { 55 | /* Don't change this structure. ASM code can use it. */ 56 | CLzmaProps prop; 57 | CLzmaProb *probs; 58 | CLzmaProb *probs_1664; 59 | Byte *dic; 60 | SizeT dicBufSize; 61 | SizeT dicPos; 62 | const Byte *buf; 63 | UInt32 range; 64 | UInt32 code; 65 | UInt32 processedPos; 66 | UInt32 checkDicSize; 67 | UInt32 reps[4]; 68 | UInt32 state; 69 | UInt32 remainLen; 70 | 71 | UInt32 numProbs; 72 | unsigned tempBufSize; 73 | Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; 74 | } CLzmaDec; 75 | 76 | #define LzmaDec_Construct(p) { (p)->dic = NULL; (p)->probs = NULL; } 77 | 78 | void LzmaDec_Init(CLzmaDec *p); 79 | 80 | /* There are two types of LZMA streams: 81 | - Stream with end mark. That end mark adds about 6 bytes to compressed size. 82 | - Stream without end mark. You must know exact uncompressed size to decompress such stream. */ 83 | 84 | typedef enum 85 | { 86 | LZMA_FINISH_ANY, /* finish at any point */ 87 | LZMA_FINISH_END /* block must be finished at the end */ 88 | } ELzmaFinishMode; 89 | 90 | /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! 91 | 92 | You must use LZMA_FINISH_END, when you know that current output buffer 93 | covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. 94 | 95 | If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, 96 | and output value of destLen will be less than output buffer size limit. 97 | You can check status result also. 98 | 99 | You can use multiple checks to test data integrity after full decompression: 100 | 1) Check Result and "status" variable. 101 | 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. 102 | 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. 103 | You must use correct finish mode in that case. */ 104 | 105 | typedef enum 106 | { 107 | LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ 108 | LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ 109 | LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ 110 | LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ 111 | LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ 112 | } ELzmaStatus; 113 | 114 | /* ELzmaStatus is used only as output value for function call */ 115 | 116 | 117 | /* ---------- Interfaces ---------- */ 118 | 119 | /* There are 3 levels of interfaces: 120 | 1) Dictionary Interface 121 | 2) Buffer Interface 122 | 3) One Call Interface 123 | You can select any of these interfaces, but don't mix functions from different 124 | groups for same object. */ 125 | 126 | 127 | /* There are two variants to allocate state for Dictionary Interface: 128 | 1) LzmaDec_Allocate / LzmaDec_Free 129 | 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs 130 | You can use variant 2, if you set dictionary buffer manually. 131 | For Buffer Interface you must always use variant 1. 132 | 133 | LzmaDec_Allocate* can return: 134 | SZ_OK 135 | SZ_ERROR_MEM - Memory allocation error 136 | SZ_ERROR_UNSUPPORTED - Unsupported properties 137 | */ 138 | 139 | SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc); 140 | void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc); 141 | 142 | SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc); 143 | void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc); 144 | 145 | /* ---------- Dictionary Interface ---------- */ 146 | 147 | /* You can use it, if you want to eliminate the overhead for data copying from 148 | dictionary to some other external buffer. 149 | You must work with CLzmaDec variables directly in this interface. 150 | 151 | STEPS: 152 | LzmaDec_Construct() 153 | LzmaDec_Allocate() 154 | for (each new stream) 155 | { 156 | LzmaDec_Init() 157 | while (it needs more decompression) 158 | { 159 | LzmaDec_DecodeToDic() 160 | use data from CLzmaDec::dic and update CLzmaDec::dicPos 161 | } 162 | } 163 | LzmaDec_Free() 164 | */ 165 | 166 | /* LzmaDec_DecodeToDic 167 | 168 | The decoding to internal dictionary buffer (CLzmaDec::dic). 169 | You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! 170 | 171 | finishMode: 172 | It has meaning only if the decoding reaches output limit (dicLimit). 173 | LZMA_FINISH_ANY - Decode just dicLimit bytes. 174 | LZMA_FINISH_END - Stream must be finished after dicLimit. 175 | 176 | Returns: 177 | SZ_OK 178 | status: 179 | LZMA_STATUS_FINISHED_WITH_MARK 180 | LZMA_STATUS_NOT_FINISHED 181 | LZMA_STATUS_NEEDS_MORE_INPUT 182 | LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 183 | SZ_ERROR_DATA - Data error 184 | */ 185 | 186 | SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, 187 | const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 188 | 189 | 190 | /* ---------- Buffer Interface ---------- */ 191 | 192 | /* It's zlib-like interface. 193 | See LzmaDec_DecodeToDic description for information about STEPS and return results, 194 | but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need 195 | to work with CLzmaDec variables manually. 196 | 197 | finishMode: 198 | It has meaning only if the decoding reaches output limit (*destLen). 199 | LZMA_FINISH_ANY - Decode just destLen bytes. 200 | LZMA_FINISH_END - Stream must be finished after (*destLen). 201 | */ 202 | 203 | SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, 204 | const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 205 | 206 | 207 | /* ---------- One Call Interface ---------- */ 208 | 209 | /* LzmaDecode 210 | 211 | finishMode: 212 | It has meaning only if the decoding reaches output limit (*destLen). 213 | LZMA_FINISH_ANY - Decode just destLen bytes. 214 | LZMA_FINISH_END - Stream must be finished after (*destLen). 215 | 216 | Returns: 217 | SZ_OK 218 | status: 219 | LZMA_STATUS_FINISHED_WITH_MARK 220 | LZMA_STATUS_NOT_FINISHED 221 | LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 222 | SZ_ERROR_DATA - Data error 223 | SZ_ERROR_MEM - Memory allocation error 224 | SZ_ERROR_UNSUPPORTED - Unsupported properties 225 | SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). 226 | */ 227 | 228 | SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, 229 | const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, 230 | ELzmaStatus *status, ISzAllocPtr alloc); 231 | 232 | EXTERN_C_END 233 | 234 | #endif 235 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/LzmaEnc.h: -------------------------------------------------------------------------------- 1 | /* LzmaEnc.h -- LZMA Encoder 2 | 2017-07-27 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZMA_ENC_H 5 | #define __LZMA_ENC_H 6 | 7 | #include "7zTypes.h" 8 | 9 | EXTERN_C_BEGIN 10 | 11 | #define LZMA_PROPS_SIZE 5 12 | 13 | typedef struct _CLzmaEncProps 14 | { 15 | int level; /* 0 <= level <= 9 */ 16 | UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version 17 | (1 << 12) <= dictSize <= (3 << 29) for 64-bit version 18 | default = (1 << 24) */ 19 | int lc; /* 0 <= lc <= 8, default = 3 */ 20 | int lp; /* 0 <= lp <= 4, default = 0 */ 21 | int pb; /* 0 <= pb <= 4, default = 2 */ 22 | int algo; /* 0 - fast, 1 - normal, default = 1 */ 23 | int fb; /* 5 <= fb <= 273, default = 32 */ 24 | int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ 25 | int numHashBytes; /* 2, 3 or 4, default = 4 */ 26 | UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ 27 | unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ 28 | int numThreads; /* 1 or 2, default = 2 */ 29 | 30 | UInt64 reduceSize; /* estimated size of data that will be compressed. default = (UInt64)(Int64)-1. 31 | Encoder uses this value to reduce dictionary size */ 32 | } CLzmaEncProps; 33 | 34 | void LzmaEncProps_Init(CLzmaEncProps *p); 35 | void LzmaEncProps_Normalize(CLzmaEncProps *p); 36 | UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2); 37 | 38 | 39 | /* ---------- CLzmaEncHandle Interface ---------- */ 40 | 41 | /* LzmaEnc* functions can return the following exit codes: 42 | SRes: 43 | SZ_OK - OK 44 | SZ_ERROR_MEM - Memory allocation error 45 | SZ_ERROR_PARAM - Incorrect paramater in props 46 | SZ_ERROR_WRITE - ISeqOutStream write callback error 47 | SZ_ERROR_OUTPUT_EOF - output buffer overflow - version with (Byte *) output 48 | SZ_ERROR_PROGRESS - some break from progress callback 49 | SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) 50 | */ 51 | 52 | typedef void * CLzmaEncHandle; 53 | 54 | CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc); 55 | void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig); 56 | 57 | SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props); 58 | void LzmaEnc_SetDataSize(CLzmaEncHandle p, UInt64 expectedDataSiize); 59 | SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size); 60 | unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p); 61 | 62 | SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, 63 | ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); 64 | SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 65 | int writeEndMark, ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); 66 | 67 | 68 | /* ---------- One Call Interface ---------- */ 69 | 70 | SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 71 | const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 72 | ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); 73 | 74 | EXTERN_C_END 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/LzmaLib.c: -------------------------------------------------------------------------------- 1 | /* LzmaLib.c -- LZMA library wrapper 2 | 2015-06-13 : Igor Pavlov : Public domain */ 3 | 4 | #include "Alloc.h" 5 | #include "LzmaDec.h" 6 | #include "LzmaEnc.h" 7 | #include "LzmaLib.h" 8 | 9 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 10 | unsigned char *outProps, size_t *outPropsSize, 11 | int level, /* 0 <= level <= 9, default = 5 */ 12 | unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */ 13 | int lc, /* 0 <= lc <= 8, default = 3 */ 14 | int lp, /* 0 <= lp <= 4, default = 0 */ 15 | int pb, /* 0 <= pb <= 4, default = 2 */ 16 | int fb, /* 5 <= fb <= 273, default = 32 */ 17 | int numThreads /* 1 or 2, default = 2 */ 18 | ) 19 | { 20 | CLzmaEncProps props; 21 | LzmaEncProps_Init(&props); 22 | props.level = level; 23 | props.dictSize = dictSize; 24 | props.lc = lc; 25 | props.lp = lp; 26 | props.pb = pb; 27 | props.fb = fb; 28 | props.numThreads = numThreads; 29 | 30 | return LzmaEncode(dest, destLen, src, srcLen, &props, outProps, outPropsSize, 0, 31 | NULL, &g_Alloc, &g_Alloc); 32 | } 33 | 34 | 35 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen, 36 | const unsigned char *props, size_t propsSize) 37 | { 38 | ELzmaStatus status; 39 | return LzmaDecode(dest, destLen, src, srcLen, props, (unsigned)propsSize, LZMA_FINISH_ANY, &status, &g_Alloc); 40 | } 41 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/LzmaLib.h: -------------------------------------------------------------------------------- 1 | /* LzmaLib.h -- LZMA library interface 2 | 2013-01-18 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZMA_LIB_H 5 | #define __LZMA_LIB_H 6 | 7 | #include "7zTypes.h" 8 | 9 | EXTERN_C_BEGIN 10 | 11 | #define MY_STDAPI int MY_STD_CALL 12 | 13 | #define LZMA_PROPS_SIZE 5 14 | 15 | /* 16 | RAM requirements for LZMA: 17 | for compression: (dictSize * 11.5 + 6 MB) + state_size 18 | for decompression: dictSize + state_size 19 | state_size = (4 + (1.5 << (lc + lp))) KB 20 | by default (lc=3, lp=0), state_size = 16 KB. 21 | 22 | LZMA properties (5 bytes) format 23 | Offset Size Description 24 | 0 1 lc, lp and pb in encoded form. 25 | 1 4 dictSize (little endian). 26 | */ 27 | 28 | /* 29 | LzmaCompress 30 | ------------ 31 | 32 | outPropsSize - 33 | In: the pointer to the size of outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. 34 | Out: the pointer to the size of written properties in outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. 35 | 36 | LZMA Encoder will use defult values for any parameter, if it is 37 | -1 for any from: level, loc, lp, pb, fb, numThreads 38 | 0 for dictSize 39 | 40 | level - compression level: 0 <= level <= 9; 41 | 42 | level dictSize algo fb 43 | 0: 16 KB 0 32 44 | 1: 64 KB 0 32 45 | 2: 256 KB 0 32 46 | 3: 1 MB 0 32 47 | 4: 4 MB 0 32 48 | 5: 16 MB 1 32 49 | 6: 32 MB 1 32 50 | 7+: 64 MB 1 64 51 | 52 | The default value for "level" is 5. 53 | 54 | algo = 0 means fast method 55 | algo = 1 means normal method 56 | 57 | dictSize - The dictionary size in bytes. The maximum value is 58 | 128 MB = (1 << 27) bytes for 32-bit version 59 | 1 GB = (1 << 30) bytes for 64-bit version 60 | The default value is 16 MB = (1 << 24) bytes. 61 | It's recommended to use the dictionary that is larger than 4 KB and 62 | that can be calculated as (1 << N) or (3 << N) sizes. 63 | 64 | lc - The number of literal context bits (high bits of previous literal). 65 | It can be in the range from 0 to 8. The default value is 3. 66 | Sometimes lc=4 gives the gain for big files. 67 | 68 | lp - The number of literal pos bits (low bits of current position for literals). 69 | It can be in the range from 0 to 4. The default value is 0. 70 | The lp switch is intended for periodical data when the period is equal to 2^lp. 71 | For example, for 32-bit (4 bytes) periodical data you can use lp=2. Often it's 72 | better to set lc=0, if you change lp switch. 73 | 74 | pb - The number of pos bits (low bits of current position). 75 | It can be in the range from 0 to 4. The default value is 2. 76 | The pb switch is intended for periodical data when the period is equal 2^pb. 77 | 78 | fb - Word size (the number of fast bytes). 79 | It can be in the range from 5 to 273. The default value is 32. 80 | Usually, a big number gives a little bit better compression ratio and 81 | slower compression process. 82 | 83 | numThreads - The number of thereads. 1 or 2. The default value is 2. 84 | Fast mode (algo = 0) can use only 1 thread. 85 | 86 | Out: 87 | destLen - processed output size 88 | Returns: 89 | SZ_OK - OK 90 | SZ_ERROR_MEM - Memory allocation error 91 | SZ_ERROR_PARAM - Incorrect paramater 92 | SZ_ERROR_OUTPUT_EOF - output buffer overflow 93 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 94 | */ 95 | 96 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 97 | unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */ 98 | int level, /* 0 <= level <= 9, default = 5 */ 99 | unsigned dictSize, /* default = (1 << 24) */ 100 | int lc, /* 0 <= lc <= 8, default = 3 */ 101 | int lp, /* 0 <= lp <= 4, default = 0 */ 102 | int pb, /* 0 <= pb <= 4, default = 2 */ 103 | int fb, /* 5 <= fb <= 273, default = 32 */ 104 | int numThreads /* 1 or 2, default = 2 */ 105 | ); 106 | 107 | /* 108 | LzmaUncompress 109 | -------------- 110 | In: 111 | dest - output data 112 | destLen - output data size 113 | src - input data 114 | srcLen - input data size 115 | Out: 116 | destLen - processed output size 117 | srcLen - processed input size 118 | Returns: 119 | SZ_OK - OK 120 | SZ_ERROR_DATA - Data error 121 | SZ_ERROR_MEM - Memory allocation arror 122 | SZ_ERROR_UNSUPPORTED - Unsupported properties 123 | SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) 124 | */ 125 | 126 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen, 127 | const unsigned char *props, size_t propsSize); 128 | 129 | EXTERN_C_END 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/Precomp.h: -------------------------------------------------------------------------------- 1 | /* Precomp.h -- StdAfx 2 | 2013-11-12 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __7Z_PRECOMP_H 5 | #define __7Z_PRECOMP_H 6 | 7 | #include "Compiler.h" 8 | /* #include "7zTypes.h" */ 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/luazen-2.1/lzma/lualzma.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 Phil Leblanc -- see LICENSE file 2 | // --------------------------------------------------------------------- 3 | // lzma - a Lua binding to LZMA by Igor Pavlov (7z) 4 | 5 | // all the LZMA code included here is Public Domain 6 | 7 | // this binding has been taken from luazen-0.16 8 | // https://github.com/philanc/luazen 9 | 10 | 11 | #define VERSION "lualzma-0.16" 12 | 13 | //---------------------------------------------------------------------- 14 | // lua binding 15 | 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "lua.h" 24 | #include "lauxlib.h" 25 | 26 | // single thread (no multi-thread support) 27 | //~ #define _7ZIP_ST --- defined in the makefile 28 | 29 | #include "LzmaLib.h" 30 | 31 | 32 | //---------------------------------------------------------------------- 33 | // compatibility with Lua 5.2 --and lua 5.3, added 150621 34 | // (from roberto's lpeg 0.10.1 dated 101203) 35 | // 36 | #if (LUA_VERSION_NUM >= 502) 37 | 38 | #undef lua_equal 39 | #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 40 | 41 | #undef lua_getfenv 42 | #define lua_getfenv lua_getuservalue 43 | #undef lua_setfenv 44 | #define lua_setfenv lua_setuservalue 45 | 46 | #undef lua_objlen 47 | #define lua_objlen lua_rawlen 48 | 49 | #undef luaL_register 50 | #define luaL_register(L,n,f) \ 51 | { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } 52 | 53 | #endif 54 | 55 | 56 | //---------------------------------------------------------------------- 57 | //-- lua binding 58 | 59 | 60 | static uint32_t load32_le(const uint8_t s[4]) { 61 | return (uint32_t)s[0] 62 | | ((uint32_t)s[1] << 8) 63 | | ((uint32_t)s[2] << 16) 64 | | ((uint32_t)s[3] << 24); 65 | } 66 | 67 | static void store32_le(uint8_t out[4], uint32_t in) { 68 | out[0] = in & 0xff; 69 | out[1] = (in >> 8) & 0xff; 70 | out[2] = (in >> 16) & 0xff; 71 | out[3] = (in >> 24) & 0xff; 72 | } 73 | 74 | static uint64_t load64_le(const uint8_t s[8]) { 75 | return (uint64_t)s[0] 76 | | ((uint64_t)s[1] << 8) 77 | | ((uint64_t)s[2] << 16) 78 | | ((uint64_t)s[3] << 24) 79 | | ((uint64_t)s[4] << 32) 80 | | ((uint64_t)s[5] << 40) 81 | | ((uint64_t)s[6] << 48) 82 | | ((uint64_t)s[7] << 56); 83 | } 84 | 85 | static void store64_le(uint8_t out[8], uint64_t in) { 86 | out[0] = in & 0xff; 87 | out[1] = (in >> 8) & 0xff; 88 | out[2] = (in >> 16) & 0xff; 89 | out[3] = (in >> 24) & 0xff; 90 | out[4] = (in >> 32) & 0xff; 91 | out[5] = (in >> 40) & 0xff; 92 | out[6] = (in >> 48) & 0xff; 93 | out[7] = (in >> 56) & 0xff; 94 | } 95 | 96 | int ll_lzma(lua_State *L) { 97 | // Lua API: compress(s) => c 98 | // compress string s, return compressed string c 99 | // or nil, error msg, lzma error number -- in case of error 100 | // 101 | size_t sln, cln, bufln, propssize; 102 | int r; 103 | const char *s = luaL_checklstring(L, 1, &sln); 104 | assert(sln < 0xffffffff); // fit a uint32 105 | 106 | // allocate compression buffer: 107 | // bufln is buffer length. suggested value is input size + 11% +16kb 108 | // (we use 'sln + sln>>3', ie input length +12.5%) 109 | bufln = sln + (sln >> 3) + 16384; 110 | unsigned char * buf = lua_newuserdata(L, bufln); 111 | 112 | // buffer format: 113 | // 2020-10-24 - use a format as can be uncompressed by the 114 | // linux lzma/unlzma commands: 115 | // - LZMA props: LZMA_PROPS_SIZE bytes (ie 5 bytes) 116 | // - uncompressed string length stored little endian (8 bytes) 117 | // - compressed output (at offset = LZMA_PROPS_SIZE + 8) 118 | // 119 | 120 | // cln, propssize _MUST_ be initialized before calling LzmaCompress 121 | propssize = LZMA_PROPS_SIZE; // = 5 122 | cln = bufln - LZMA_PROPS_SIZE - 8; // max available space in buf 123 | 124 | r = LzmaCompress( 125 | buf + LZMA_PROPS_SIZE + 8, &cln, // dest, destlen 126 | s, sln, // src, srclen 127 | buf, &propssize, // props, propslen 128 | 129 | // !! DO NOT CHANGE THE FOLLOWING PARAMETERS !! 130 | // (they are used to recognize lzma standard format 131 | // vs. the luazen lzma legacy format) 132 | 133 | 5, // level 134 | (1<<24), // dict size 135 | 3, // lc 136 | 0, // lp 137 | 2, // pb 138 | 32, // fb 139 | 1 // numthreads 140 | ); 141 | 142 | if (r != 0) { 143 | lua_pushnil (L); 144 | lua_pushliteral(L, "lzma error"); 145 | lua_pushinteger(L, r); 146 | return 3; 147 | } 148 | 149 | // store uncompressed string length (little endian) 150 | store64_le(buf+LZMA_PROPS_SIZE, sln); 151 | lua_pushlstring (L, buf, LZMA_PROPS_SIZE + 8 + cln); 152 | return 1; 153 | } //lzma() 154 | 155 | int ll_unlzma(lua_State *L) { 156 | // Lua API: uncompress(c) => s | nil, error msg 157 | // decompress string c, return original string s 158 | // or nil, error msg in case of decompression error 159 | // 160 | size_t sln, cln, bufln, dln; 161 | int r; 162 | const char *c = luaL_checklstring(L, 1, &cln); 163 | uint64_t sln64; 164 | 165 | // LzmaUncompress parameters 166 | unsigned char *dest; 167 | size_t *destLen; 168 | const unsigned char *src; 169 | size_t *srcLen; 170 | const unsigned char *props; 171 | size_t propsSize; 172 | 173 | // try to guess compressed string format (legacy or standard) 174 | const char default_props[5] = {0x5d, 0, 0, 0, 1}; 175 | if (strncmp(c, default_props, 5) == 0) { 176 | // standard format - set LzmaUncompress parameters 177 | sln64 = load64_le(c + LZMA_PROPS_SIZE); 178 | if (sln64 >= 1LL<<32) { 179 | lua_pushnil (L); 180 | lua_pushliteral(L, "uncompressed string too large"); 181 | return 2; 182 | } 183 | dln = (size_t) sln64; 184 | destLen = &dln; 185 | src = c + LZMA_PROPS_SIZE + 8; 186 | cln = cln - (LZMA_PROPS_SIZE + 8); 187 | srcLen = &cln; 188 | props = c; 189 | propsSize = LZMA_PROPS_SIZE; 190 | } else { 191 | // assume legacy format - set LzmaUncompress parameters 192 | dln = load32_le(c); 193 | destLen = &dln; 194 | src = c + 4 + LZMA_PROPS_SIZE; 195 | cln = cln - (4 + LZMA_PROPS_SIZE); 196 | srcLen = &cln; 197 | props = c + 4; 198 | propsSize = LZMA_PROPS_SIZE; 199 | } 200 | dest = lua_newuserdata(L, dln); // allocate buffer 201 | r = LzmaUncompress(dest, destLen, src, srcLen, props, propsSize); 202 | if (r != 0) { 203 | lua_pushnil (L); 204 | lua_pushliteral(L, "unlzma error"); 205 | lua_pushinteger(L, r); 206 | return 3; 207 | } 208 | lua_pushlstring (L, dest, dln); 209 | return 1; 210 | } //unlzma() 211 | 212 | -------------------------------------------------------------------------------- /src/luazen-2.1/mono/monocypher-ed25519.h: -------------------------------------------------------------------------------- 1 | // Monocypher version 3.1.3 2 | // 3 | // This file is dual-licensed. Choose whichever licence you want from 4 | // the two licences listed below. 5 | // 6 | // The first licence is a regular 2-clause BSD licence. The second licence 7 | // is the CC-0 from Creative Commons. It is intended to release Monocypher 8 | // to the public domain. The BSD licence serves as a fallback option. 9 | // 10 | // SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0 11 | // 12 | // ------------------------------------------------------------------------ 13 | // 14 | // Copyright (c) 2017-2019, Loup Vaillant 15 | // All rights reserved. 16 | // 17 | // 18 | // Redistribution and use in source and binary forms, with or without 19 | // modification, are permitted provided that the following conditions are 20 | // met: 21 | // 22 | // 1. Redistributions of source code must retain the above copyright 23 | // notice, this list of conditions and the following disclaimer. 24 | // 25 | // 2. Redistributions in binary form must reproduce the above copyright 26 | // notice, this list of conditions and the following disclaimer in the 27 | // documentation and/or other materials provided with the 28 | // distribution. 29 | // 30 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 | // 42 | // ------------------------------------------------------------------------ 43 | // 44 | // Written in 2017-2019 by Loup Vaillant 45 | // 46 | // To the extent possible under law, the author(s) have dedicated all copyright 47 | // and related neighboring rights to this software to the public domain 48 | // worldwide. This software is distributed without any warranty. 49 | // 50 | // You should have received a copy of the CC0 Public Domain Dedication along 51 | // with this software. If not, see 52 | // 53 | 54 | #ifndef ED25519_H 55 | #define ED25519_H 56 | 57 | #include "monocypher.h" 58 | 59 | #ifdef MONOCYPHER_CPP_NAMESPACE 60 | namespace MONOCYPHER_CPP_NAMESPACE { 61 | #elif defined(__cplusplus) 62 | extern "C" { 63 | #endif 64 | 65 | //////////////////////// 66 | /// Type definitions /// 67 | //////////////////////// 68 | 69 | // Do not rely on the size or content on any of those types, 70 | // they may change without notice. 71 | typedef struct { 72 | uint64_t hash[8]; 73 | uint64_t input[16]; 74 | uint64_t input_size[2]; 75 | size_t input_idx; 76 | } crypto_sha512_ctx; 77 | 78 | typedef struct { 79 | uint8_t key[128]; 80 | crypto_sha512_ctx ctx; 81 | } crypto_hmac_sha512_ctx; 82 | 83 | typedef struct { 84 | crypto_sign_ctx_abstract ctx; 85 | crypto_sha512_ctx hash; 86 | } crypto_sign_ed25519_ctx; 87 | typedef crypto_sign_ed25519_ctx crypto_check_ed25519_ctx; 88 | 89 | // SHA 512 90 | // ------- 91 | void crypto_sha512_init (crypto_sha512_ctx *ctx); 92 | void crypto_sha512_update(crypto_sha512_ctx *ctx, 93 | const uint8_t *message, size_t message_size); 94 | void crypto_sha512_final (crypto_sha512_ctx *ctx, uint8_t hash[64]); 95 | void crypto_sha512(uint8_t hash[64], const uint8_t *message, size_t message_size); 96 | 97 | // vtable for signatures 98 | extern const crypto_sign_vtable crypto_sha512_vtable; 99 | 100 | 101 | // HMAC SHA 512 102 | // ------------ 103 | void crypto_hmac_sha512_init(crypto_hmac_sha512_ctx *ctx, 104 | const uint8_t *key, size_t key_size); 105 | void crypto_hmac_sha512_update(crypto_hmac_sha512_ctx *ctx, 106 | const uint8_t *message, size_t message_size); 107 | void crypto_hmac_sha512_final(crypto_hmac_sha512_ctx *ctx, uint8_t hmac[64]); 108 | void crypto_hmac_sha512(uint8_t hmac[64], 109 | const uint8_t *key , size_t key_size, 110 | const uint8_t *message, size_t message_size); 111 | 112 | 113 | // Ed25519 114 | // ------- 115 | 116 | // Generate public key 117 | void crypto_ed25519_public_key(uint8_t public_key[32], 118 | const uint8_t secret_key[32]); 119 | 120 | // Direct interface 121 | void crypto_ed25519_sign(uint8_t signature [64], 122 | const uint8_t secret_key[32], 123 | const uint8_t public_key[32], // optional, may be 0 124 | const uint8_t *message, size_t message_size); 125 | int crypto_ed25519_check(const uint8_t signature [64], 126 | const uint8_t public_key[32], 127 | const uint8_t *message, size_t message_size); 128 | 129 | // Incremental interface 130 | void crypto_ed25519_sign_init_first_pass(crypto_sign_ctx_abstract *ctx, 131 | const uint8_t secret_key[32], 132 | const uint8_t public_key[32]); 133 | #define crypto_ed25519_sign_update crypto_sign_update 134 | #define crypto_ed25519_sign_init_second_pass crypto_sign_init_second_pass 135 | // use crypto_ed25519_sign_update() again. 136 | #define crypto_ed25519_sign_final crypto_sign_final 137 | 138 | void crypto_ed25519_check_init(crypto_check_ctx_abstract *ctx, 139 | const uint8_t signature[64], 140 | const uint8_t public_key[32]); 141 | #define crypto_ed25519_check_update crypto_check_update 142 | #define crypto_ed25519_check_final crypto_check_final 143 | 144 | void crypto_from_ed25519_private(uint8_t x25519[32], const uint8_t eddsa[32]); 145 | #define crypto_from_ed25519_public crypto_from_eddsa_public 146 | 147 | #ifdef __cplusplus 148 | } 149 | #endif 150 | 151 | #endif // ED25519_H 152 | -------------------------------------------------------------------------------- /src/luazen-2.1/random.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 Phil Leblanc -- see LICENSE file 2 | // --------------------------------------------------------------------- 3 | // Lua binding to utility functions: randombytes 4 | // interface to the OS Random Number Generator 5 | 6 | #ifdef _WIN32 7 | // ------------------------------ 8 | // randombytes() for windows 9 | // Use the Windows RNG (CryptGenRandom) 10 | // tested with MinGW (2016-07-31) 11 | 12 | #include /// for exit() 13 | 14 | #include 15 | #include /* CryptAcquireContext, CryptGenRandom */ 16 | 17 | int randombytes(unsigned char *x,unsigned long long xlen) 18 | { 19 | 20 | 21 | HCRYPTPROV p; 22 | ULONG i; 23 | 24 | if (xlen > 4096) { 25 | xlen = 4096; 26 | } 27 | 28 | if (CryptAcquireContext(&p, NULL, NULL, 29 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) { 30 | return(-1); 31 | } 32 | if (CryptGenRandom(p, xlen, (BYTE *)x) == FALSE) { 33 | return(-1); 34 | } 35 | CryptReleaseContext(p, 0); 36 | return 0; 37 | } 38 | 39 | #else // unix 40 | // ------------------------------- 41 | // use getrandom() or /dev/urandom 42 | 43 | #include 44 | #include 45 | 46 | #if defined __GLIBC_PREREQ && !defined __UCLIBC__ 47 | #define GLIBC_PREREQ(M, m) (__GLIBC_PREREQ(M, m)) 48 | #else 49 | #define GLIBC_PREREQ(M, m) 0 50 | #endif 51 | 52 | // the getrandom() detection code below has been provided by Daurnimator 53 | // (https://github.com/daurnimator) 54 | #ifndef HAVE_GETRANDOM 55 | #define HAVE_GETRANDOM (GLIBC_PREREQ(2,25) && __linux__) 56 | #endif 57 | #if HAVE_GETRANDOM 58 | #include 59 | #endif 60 | 61 | int randombytes(unsigned char *x, unsigned long long xlen) { 62 | int fd, i; 63 | size_t count = (size_t) xlen; 64 | 65 | #if HAVE_GETRANDOM 66 | i = getrandom(x, count, 0); 67 | #else 68 | fd = open("/dev/urandom",O_RDONLY); 69 | if (fd == -1) { 70 | return -1; 71 | } 72 | i = read(fd, x, count); 73 | close(fd); 74 | #endif 75 | if ((i < 0) || (i < count)) { 76 | return -1; 77 | } 78 | return 0; 79 | } 80 | 81 | #endif 82 | 83 | //---------------------------------------------------------------------- 84 | //-- lua binding functions 85 | 86 | #include "lua.h" 87 | #include "lauxlib.h" 88 | 89 | 90 | 91 | int ll_randombytes(lua_State *L) { 92 | // Lua API: randombytes(n) returns a string with n random bytes 93 | // n must be 256 or less. 94 | // randombytes return nil, error msg if the RNG fails or if n > 256 95 | // 96 | size_t bufln; 97 | unsigned char buf[256]; 98 | lua_Integer li = luaL_checkinteger(L, 1); // 1st arg 99 | if ((li > 256 ) || (li < 0)) { 100 | lua_pushnil (L); 101 | lua_pushliteral(L, "invalid byte number"); 102 | return 2; 103 | } 104 | int r = randombytes(buf, li); 105 | if (r != 0) { 106 | lua_pushnil (L); 107 | lua_pushliteral(L, "random generator error"); 108 | return 2; 109 | } 110 | lua_pushlstring (L, buf, li); 111 | return 1; 112 | } //randombytes() 113 | 114 | -------------------------------------------------------------------------------- /src/slualibs.h: -------------------------------------------------------------------------------- 1 | 2 | /// This should be included after 3 | /// luaL_openlibs(L); /* open standard libraries */ 4 | /// in the main program 5 | 6 | /// slua: preloaded libraries 7 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 8 | /// lualinux 9 | int luaopen_lualinux(lua_State *L); 10 | lua_pushcfunction(L, luaopen_lualinux); 11 | lua_setfield(L, -2, "lualinux"); 12 | /// lsccore 13 | int luaopen_lsccore(lua_State *L); 14 | lua_pushcfunction(L, luaopen_lsccore); 15 | lua_setfield(L, -2, "lsccore"); 16 | /// luazen 17 | int luaopen_luazen(lua_State *L); 18 | lua_pushcfunction(L, luaopen_luazen); 19 | lua_setfield(L, -2, "luazen"); 20 | /// linenoise 21 | int luaopen_linenoise(lua_State *L); 22 | lua_pushcfunction(L, luaopen_linenoise); 23 | lua_setfield(L, -2, "linenoise"); 24 | /// 25 | /// remove _PRELOAD table 26 | lua_pop(L, 1); 27 | 28 | /// slua: end of preloaded libraries -------------------------------------------------------------------------------- /src/sluaversion.h: -------------------------------------------------------------------------------- 1 | // added 230122 2 | #define SLUA_VERSION "slua-1.1" 3 | -------------------------------------------------------------------------------- /src/srglue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * srglue.h 3 | * glue exe and script for srlua 4 | * Luiz Henrique de Figueiredo 5 | * 14 Aug 2019 08:46:54 6 | * This code is hereby placed in the public domain and also under the MIT license 7 | */ 8 | 9 | #define GLUESIG "%%srglue" 10 | #define GLUELEN (sizeof(GLUESIG)-1) 11 | 12 | typedef struct { char sig[GLUELEN]; long size1, size2; } Glue; 13 | -------------------------------------------------------------------------------- /src/srlua-102/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for srlua 2 | 3 | LUA_TOPDIR= /usr/local 4 | LUA_INCDIR= $(LUA_TOPDIR)/include 5 | LUA_LIBDIR= $(LUA_TOPDIR)/lib 6 | INSTALL_DIR= /usr/local/bin 7 | 8 | CC= gcc -std=c99 9 | CFLAGS= -Wall -Wextra -Wfatal-errors -O2 10 | MYCFLAGS= $(CFLAGS) -I$(LUA_INCDIR) 11 | MYLFLAGS= -Wl,-E 12 | MYLIBS= -L$(LUA_LIBDIR) -llua -lm -ldl 13 | 14 | all: bin test 15 | 16 | bin: 17 | @$(MAKE) `uname` 18 | 19 | test: 20 | ./srglue srlua test.lua a.out ; chmod +x a.out 21 | ./a.out * 22 | 23 | install: 24 | cp srglue srlua $(INSTALL_DIR) 25 | 26 | clean: 27 | rm -f srglue srlua a.out 28 | 29 | Linux build: 30 | $(CC) $(CFLAGS) -o srglue srglue.c 31 | $(CC) $(MYCFLAGS) -o srlua srlua.c $(MYLIBS) $(MYLFLAGS) 32 | 33 | Darwin: 34 | $(MAKE) build MYLFLAGS= 35 | 36 | .PHONY: all bin test install clean Linux Darwin build 37 | -------------------------------------------------------------------------------- /src/srlua-102/README: -------------------------------------------------------------------------------- 1 | This is a self-running Lua interpreter. It is meant to be combined with 2 | a Lua program into a single, stand-alone program that will execute the 3 | given Lua program when it is run. 4 | 5 | The command-line arguments will be available to the Lua program in a 6 | table called "arg" and also as '...'. All standard Lua libraries will be 7 | available too. If you want to use a different set of libraries, just 8 | copy linit.c from the Lua source, add it to srlua.c, and edit loadedlibs 9 | and preloadedlibs to suit your needs (lualibs in Lua 5.1). 10 | 11 | For each Lua program that you want to turn into a stand-alone program, do 12 | srglue srlua prog.lua a.out ; chmod +x a.out 13 | Of course, you can use any name instead of a.out. 14 | 15 | For Windows, you need to create srlua.exe and srglue.exe first. Then, 16 | for each Lua program that you want to turn into a stand-alone program, do 17 | srglue srlua.exe prog.lua prog.exe 18 | Of course, you can use any name instead of prog.exe. 19 | 20 | To build srlua and srglue and run a simple test, just do make. 21 | If Lua is not installed in /usr/local, tell make: 22 | make LUA_TOPDIR=/var/tmp/lhf/lua-5.3.5/install 23 | 24 | To install srlua and srglue where you can find it, use a variant of these: 25 | make install 26 | sudo make install 27 | sudo make install INSTALL_DIR=/usr/local/bin 28 | 29 | For detailed installation instructions, see 30 | http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html 31 | 32 | This code is hereby placed in the public domain and also under the MIT license. 33 | 34 | Please send comments, suggestions, and bug reports to lhf@tecgraf.puc-rio.br . 35 | -------------------------------------------------------------------------------- /src/srlua-102/srglue.c: -------------------------------------------------------------------------------- 1 | /* 2 | * srglue.c 3 | * glue exe and script for srlua 4 | * Luiz Henrique de Figueiredo 5 | * 13 Aug 2019 08:44:57 6 | * This code is hereby placed in the public domain and also under the MIT license 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "srglue.h" 14 | 15 | static const char* progname="srglue"; 16 | 17 | static void cannot(const char* what, const char* name) 18 | { 19 | fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,name,strerror(errno)); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | static FILE* open(const char* name, const char* mode, const char* outname) 24 | { 25 | if (outname!=NULL && strcmp(name,outname)==0) 26 | { 27 | errno=EPERM; 28 | cannot("overwrite input file",name); 29 | return NULL; 30 | } 31 | else 32 | { 33 | FILE* f=fopen(name,mode); 34 | if (f==NULL) cannot("open",name); 35 | return f; 36 | } 37 | } 38 | 39 | static long copy(FILE* in, const char* name, FILE* out, const char* outname) 40 | { 41 | long size; 42 | if (fseek(in,0,SEEK_END)!=0) cannot("seek",name); 43 | size=ftell(in); 44 | if (fseek(in,0,SEEK_SET)!=0) cannot("seek",name); 45 | for (;;) 46 | { 47 | char b[BUFSIZ]; 48 | int n=fread(&b,1,sizeof(b),in); 49 | if (n==0) { if (ferror(in)) cannot("read",name); else break; } 50 | if (fwrite(&b,n,1,out)!=1) cannot("write",outname); 51 | } 52 | fclose(in); 53 | return size; 54 | } 55 | 56 | int main(int argc, char* argv[]) 57 | { 58 | if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; 59 | if (argc!=4) 60 | { 61 | fprintf(stderr,"usage: %s in.exe in.lua out.exe\n",progname); 62 | return 1; 63 | } 64 | else 65 | { 66 | FILE* in1=open(argv[1],"rb",argv[3]); 67 | FILE* in2=open(argv[2],"rb",argv[3]); 68 | FILE* out=open(argv[3],"wb",NULL); 69 | Glue t={GLUESIG,0,0}; 70 | t.size1=copy(in1,argv[1],out,argv[3]); 71 | t.size2=copy(in2,argv[2],out,argv[3]); 72 | if (fwrite(&t,sizeof(t),1,out)!=1) cannot("write",argv[3]); 73 | if (fclose(out)!=0) cannot("close",argv[3]); 74 | return 0; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/srlua-102/srglue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * srglue.h 3 | * glue exe and script for srlua 4 | * Luiz Henrique de Figueiredo 5 | * 14 Aug 2019 08:46:54 6 | * This code is hereby placed in the public domain and also under the MIT license 7 | */ 8 | 9 | #define GLUESIG "%%srglue" 10 | #define GLUELEN (sizeof(GLUESIG)-1) 11 | 12 | typedef struct { char sig[GLUELEN]; long size1, size2; } Glue; 13 | -------------------------------------------------------------------------------- /src/srlua-102/srlua.c: -------------------------------------------------------------------------------- 1 | /* 2 | * srlua.c 3 | * Lua interpreter for self-running programs 4 | * Luiz Henrique de Figueiredo 5 | * 14 Aug 2019 14:16:21 6 | * This code is hereby placed in the public domain and also under the MIT license 7 | */ 8 | 9 | // slua: 10 | // added slua preloaded libraries 11 | // ignore argv[0] - use /proc/self/exe to find the exe pathname 12 | // added SLUA_VERSION 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "srglue.h" 20 | #include "lua.h" 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | #if LUA_VERSION_NUM <= 501 25 | 26 | #define lua_load(L,r,d,n,m) (lua_load)(L,r,d,n) 27 | #define luaL_traceback(L,M,m,l) traceback(L,m) 28 | 29 | static void traceback(lua_State *L, const char *message) 30 | { 31 | lua_getglobal(L,"debug"); 32 | if (!lua_istable(L,-1)) { lua_pop(L,1); return; } 33 | lua_getfield(L,-1,"traceback"); 34 | if (!lua_isfunction(L,-1)) { lua_pop(L,2); return; } 35 | lua_pushstring(L,message); 36 | lua_pushinteger(L,2); 37 | lua_call(L,2,1); 38 | } 39 | 40 | #endif 41 | 42 | static const char* progname="srlua"; 43 | 44 | static void fatal(const char* message) 45 | { 46 | fprintf(stderr,"%s: %s\n",progname,message); 47 | exit(EXIT_FAILURE); 48 | } 49 | 50 | static void cannot(lua_State *L, const char* what, const char* name) 51 | { 52 | const char* why= memcmp(what,"find",4)==0 ? "no glue" : strerror(errno); 53 | const char* message=lua_pushfstring(L,"cannot %s %s: %s",what,name,why); 54 | fatal(message); 55 | } 56 | 57 | typedef struct { FILE *f; size_t size; char buff[512]; } State; 58 | 59 | static const char *myget(lua_State *L, void *data, size_t *size) 60 | { 61 | State* s=data; 62 | size_t n; 63 | (void)L; 64 | n=(sizeof(s->buff)<=s->size)? sizeof(s->buff) : s->size; 65 | n=fread(s->buff,1,n,s->f); 66 | s->size-=n; 67 | *size=n; 68 | return (n>0) ? s->buff : NULL; 69 | } 70 | 71 | static void load(lua_State *L, const char *name) 72 | { 73 | Glue t; 74 | State S; 75 | /// 220317 ignore argv[0] - force exe name 76 | /// it allows the executable to be located anywhere 77 | /// (in the PATH) and called by name (Linux only!) 78 | name = "/proc/self/exe"; 79 | FILE *f=fopen(name,"rb"); 80 | int c; 81 | 82 | if (f==NULL) cannot(L,"open",name); 83 | if (fseek(f,-sizeof(t),SEEK_END)!=0) cannot(L,"seek",name); 84 | if (fread(&t,sizeof(t),1,f)!=1) cannot(L,"read",name); 85 | if (memcmp(t.sig,GLUESIG,GLUELEN)!=0) cannot(L,"find a Lua program in",name); 86 | if (fseek(f,t.size1,SEEK_SET)!=0) cannot(L,"seek",name); 87 | S.f=f; S.size=t.size2; 88 | c=getc(f); 89 | if (c=='#') while (--S.size>0 && c!='\n') c=getc(f); 90 | ungetc(c,f); 91 | if (lua_load(L,myget,&S,"=",NULL)!=0) fatal(lua_tostring(L,-1)); 92 | fclose(f); 93 | } 94 | 95 | static int pmain(lua_State *L) 96 | { 97 | int argc=lua_tointeger(L,1); 98 | char** argv=lua_touserdata(L,2); 99 | int i; 100 | luaL_openlibs(L); 101 | 102 | /// ================================================================== 103 | /// slua: preloaded libraries 104 | 105 | #include "slualibs.h" 106 | #include "sluaversion.h" 107 | 108 | /// 230122 added SLUA_VERSION global 109 | lua_pushliteral(L, SLUA_VERSION); 110 | lua_setglobal(L, "SLUA_VERSION"); 111 | 112 | /// slua: end of preloaded libraries 113 | /// ================================================================== 114 | 115 | load(L,argv[0]); 116 | lua_createtable(L,argc,0); 117 | for (i=0; i