From 975f249a76f8a95a7eab03bda3ae1d4963d6fb0c Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Sat, 14 Jan 2006 15:29:41 +0000 Subject: [PATCH 01/21] initial release --- LICENSE | 19 ++++ Makefile | 123 ++++++++++++++++++++++++++ README | 67 ++++++++++++++ doc/luacrypto.html | 82 ++++++++++++++++++ src/crypto.c | 61 +++++++++++++ src/crypto.h | 29 +++++++ src/crypto.lua | 28 ++++++ src/evp.c | 211 +++++++++++++++++++++++++++++++++++++++++++++ src/evp.h | 30 +++++++ src/evp.lua | 26 ++++++ src/hmac.c | 204 +++++++++++++++++++++++++++++++++++++++++++ src/hmac.h | 30 +++++++ src/hmac.lua | 26 ++++++ tests/message | 1 + tests/test.lua | 83 ++++++++++++++++++ 15 files changed, 1020 insertions(+) create mode 100755 LICENSE create mode 100755 Makefile create mode 100755 README create mode 100755 doc/luacrypto.html create mode 100755 src/crypto.c create mode 100755 src/crypto.h create mode 100755 src/crypto.lua create mode 100755 src/evp.c create mode 100755 src/evp.h create mode 100755 src/evp.lua create mode 100755 src/hmac.c create mode 100755 src/hmac.h create mode 100755 src/hmac.lua create mode 100755 tests/message create mode 100755 tests/test.lua diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..fc89efc --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2006 Keith Howe + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..1c2eee7 --- /dev/null +++ b/Makefile @@ -0,0 +1,123 @@ +# Change these to reflect your Lua installation locations. The system +# independent Lua files (LUA_PATH), the system dependent Lua files (LUA_CPATH), +# and the Lua include files (LUAINC) are required to compile and install. +LUA_PATH = /usr/share/lua +LUA_CPATH = /usr/local/lua +LUAINC = /usr/include/lua + +# The location of the Lua interpreter and Lua compiler are required to make the +# tests and luadocs, and to generate compiled Lua libraries instead of source +# Lua libraries for install. +LUA = /usr/bin/lua +LUAC = /usr/bin/luac + +# This provides the necessary flags for linking against your OpenSSL libcrypto +# installation. Change it to suit your system if necessary. +LDFLAGS = -lcrypto +#CFLAGS = + +# Set this to lc to install the precompiled Lua libraries (compiled with luac), +# or to lua to install the source Lua libraries. +LUATYPE = lc + +# You shouldn't need to change anything below here. +.SUFFIXES: +srcdir := ./src +outdir := ./obj +tstdir := ./tests +INSTALL = install +SHELL = /bin/sh +MODULES = evp hmac +CFLAGS = -I$(LUAINC) -ansi -pedantic -Wall -O2 +SOOBJS = $(outdir)/crypto.so $(foreach module,$(MODULES),$(outdir)/$(module).so) +LCOBJS = $(outdir)/crypto.$(LUATYPE) $(foreach module,$(MODULES),$(outdir)/$(module).$(LUATYPE)) + +.PHONY: all $(outdir) +all: $(outdir) $(SOOBJS) $(LCOBJS) + +$(outdir): + @if [ ! -d $(outdir) ]; then $(INSTALL) -d $(outdir); fi + +$(outdir)/%.so: $(outdir)/%.o + $(CC) $(LDFLAGS) -shared $< -o $@ + +$(outdir)/%.o: $(srcdir)/%.c + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ + +$(outdir)/%.lc: $(srcdir)/%.lua + $(LUAC) $(LUACFLAGS) -o $@ $< + +$(outdir)/%.lua: $(srcdir)/%.lua + cp $< $@ + +luadoc: $(srcdir)/crypto.lua $(foreach module,$(MODULES),$(srcdir)/$(module).lua) + @if [ -x $(LUA) ]; then \ + echo "Building luadoc..."; \ + LUA_PATH="$(LUA_PATH)/?.$(LUATYPE)"; \ + LUA_CPATH="$(LUA_CPATH)/?.so"; \ + export LUA_PATH LUA_CPATH; \ + $(LUA) $(srcdir)/test.lua $(srcdir)/crypto.lua; \ + else \ + echo "Lua interpreter not found; not building luadoc."; \ + echo "Set LUA to your interpreter location and execute"; \ + echo "'make tests' to run the test suite."; \ + fi + +.PHONY: clean distclean mostlyclean +clean: + rm -fr $(outdir) + rm -f ./core ./core.* + +distclean: clean ; + +mostlyclean: + rm -fr $(outdir)/* + rm -f ./core ./core.* + +define INSTALL_TEMPLATE +$(1)_install: $(outdir)/$(1).so $(outdir)/$(1).$(LUATYPE) + $(INSTALL) -d $(LUA_CPATH)/crypto/$(1) + $(INSTALL) -D $(outdir)/$(1).so $(LUA_CPATH)/crypto/$(1)/core.so + $(INSTALL) -D $(outdir)/$(1).$(LUATYPE) $(LUA_PATH)/crypto/$(1).$(LUATYPE) + +$(1)_uninstall: + -rm $(LUA_CPATH)/crypto/$(1)/core.so + -rm -r $(LUA_CPATH)/crypto/$(1)/ + -rm $(LUA_PATH)/crypto/$(1).$(LUATYPE) +endef + +$(foreach module,$(MODULES),$(eval $(call INSTALL_TEMPLATE,$(module)))) + +.PHONY: install crypto_install uninstall crypto_uninstall + +crypto_install: + $(INSTALL) -d $(LUA_PATH)/crypto/ + $(INSTALL) -d $(LUA_CPATH)/crypto/ + $(INSTALL) -D $(outdir)/crypto.so $(LUA_CPATH)/crypto/core.so + $(INSTALL) -D $(outdir)/crypto.$(LUATYPE) $(LUA_CPATH)/crypto.$(LUATYPE) + +install: crypto_install $(foreach module,$(MODULES),$(module)_install) tests ; + +crypto_uninstall: + -rm $(LUA_CPATH)/crypto/core.so + -rm -r $(LUA_CPATH)/crypto/ + -rm -r $(LUA_PATH)/crypto/ + -rm $(LUA_PATH)/crypto.$(LUATYPE) + +uninstall: $(foreach module,$(MODULES),$(module)_uninstall) crypto_uninstall + +.PHONY: tests +tests: + @echo "" + @if [ -x $(LUA) ]; then \ + echo "Running tests..."; \ + LUA_PATH="$(LUA_PATH)/?.$(LUATYPE)"; \ + LUA_CPATH="$(LUA_CPATH)/?.so"; \ + export LUA_PATH LUA_CPATH; \ + $(LUA) $(tstdir)/test.lua $(tstdir)/message; \ + else \ + echo "Lua interpreter not found; not running tests."; \ + echo "Set LUA to your interpreter location and execute"; \ + echo "'make tests' to run the test suite."; \ + fi + @echo "" diff --git a/README b/README new file mode 100755 index 0000000..bda5407 --- /dev/null +++ b/README @@ -0,0 +1,67 @@ +1. INTRODUCTION + +This project provides Lua bindings for the OpenSSL libcrypto libraries. +libcrypto provides digest functions, such as MD5, SHA-1, and HMAC; cipher +functions, such as RC5 and Blowfish; public key crypto, such as RSA and +Diffie-Hellman; and some other assorted tools (i.e. random number generation). + +Currently only the digest functions are mapped by this Lua binding. + +http://luacrypto.luaforge.net/ + by Keith Howe + +2. REQUIREMENTS + + - Lua 5.1 - http://www.lua.org/ + + luacrypto requires Lua 5.1 since it uses the new Lua package system. + It should also work with Compat-5.1, though you will have to update the + build and libraries in order to support this. + + - libcrypto 0.9.7+ - http://www.openssl.org/ + + luacrypto has only been built against and tested with OpenSSL versions + 0.9.7 and above. It depends on additional API methods introduced in 0.9.7. + +2. INSTALLATION + +Edit the Makefile and set the following fields to the correct values for your +environment: + + LUAINC: The location of your Lua 5.1 include headers. This is required to + build the code. + + LUA_PATH: The installation path for your system independent Lua files. + LUA_CPATH: The installation path for your system dependent Lua files. These + two settings are required to install the libraries properly. + + LUA_TYPE: Set this to the type of Lua libraries you would like to install; + either "lc" for precompiled Lua libraries (using luac), or "lua" for + regular text Lua libraries. + + LUA: The location of your Lua 5.1 interpreter; only needed to run the tests + and to generate the luadoc. + LUAC: The location of your Lua 5.1 compiler; only needed to build the + precompiled Lua libraries. + + LDFLAGS: On most systems, the default value will be fine. However, if your + OpenSSL libcrypto library is not in the standard library paths, you may + need to point this at your OpenSSL install. + + CFLAGS: On most systems, the default value will be fine. However, if your + OpenSSL libcrypto headers are not in the standard include paths, you may + need to point this at your OpenSSL install. + +Once configured, run "make" to build the libraries. This will also build the +documentation using luadoc if you have set the location of your Lua +interpreter. Run "make install" to install the libraries in the correct +location. The install will run a test suite if you have set the location of +your Lua interpreter. You can run the tests independently with "make tests", +but you cannot run the tests until after you have run "make install". + +3. CREDITS + +Much of this code was heavily inspired by and/or lifted directly from the +lmd5 project (http://luaforge.net/projects/lmd5/), written by +Luiz Henrique de Figueiredo . + diff --git a/doc/luacrypto.html b/doc/luacrypto.html new file mode 100755 index 0000000..6fc1bff --- /dev/null +++ b/doc/luacrypto.html @@ -0,0 +1,82 @@ + + + + luacrypto documentation + + + +

Table of Contents

+
    +
  1. Quick & Easy
  2. +
  3. EVP
  4. +
  5. HMAC
  6. +
+ +

Quick & Easy

+

This module is written to use the new Lua 5.1 package system. To use this module, you need to do:

+

+    local evp = require("crypto.evp")
+    local hmac = require("crypto.hmac")
+    
+

This will put the EVP and HMAC package namespaces into the evp and hmac tables respectively. The simplest usage of these packages are:

+

+    assert(io.input(some_file))
+    local md5_of_some_file = evp.digest("md5", io.read("*all"))
+    
+    assert(io.input(some_file))
+    local hmac_of_some_file = hmac.digest("sha1", io.read("*all"), "hmackey")
+    
+ +

crypto.evp

+

The crypto.evp library is a wrapper over the EVP_Digest* calls available in libcrypto. These calls handle all message digest creation operations for a wide variety of algorithms (SHA-1, MD5, etc). The luacrypto bindings provide two interfaces into this functionality. The first is a single function call interface that has no setup and no side-effects. The second is an object based interface that may be useful for some scenarios.

+ +

Functional Interface

+ +

output = evp.digest(type, string, [raw])

+

This function generates the message digest of the input string and returns it as output. The hashing algorithm to use is specified as a string by type, and this can be any one of the types allowed by OpenSSL (see their documentation for a full list). Examples include "sha1" and "md5". The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the message digest.

+ +

Object Interface

+

The object interface is mainly useful in cases where you want to increase efficiency. For instance, if you will be hashing a lot of different items using the same algorithm, you can create a new message digest object of the type required and reset it between each digest call, which will save you a little bit of setup overhead each time. Alternately, if you are hashing two or more items that have mostly the same content up until the end, you can load an object with the identical data, then clone off multiple copes to tack on the remaining, differing pieces of data for each.

+ +

d = evp.new(type)

+

Creates a new EVP message digest object of the type specified. This is the string name of the hash algorithm to use, which can be any one of the allowed OpenSSL types (see their documentation for a full list). Example types include "sha1" and "md5".

+ +

d:reset()

+

Resets the internals of the EVP message digest object to a clean slate.

+ +

d2 = d:clone()

+

Clones the message digest object and its current state, including data loaded to this point.

+ +

d:update(string)

+

Appends the data in string to the current internal data set to be hashed.

+ +

output = d:digest([string], [raw])

+

Generates the message digest for the internal data set, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the message digest.

+ +

crypto.hmac

+

The crypto.hmac library is a wrapper over the HMAC_* calls available in libcrypto. These calls implement Keyed-Hashing for Message Authentication (HMAC). HMAC can use any message digest type (SHA-1, MD5, etc). The luacrypto bindings provide two interfaces into this functionality. The first is a single function call interface that has no setup and no side-effects. The second is an object based interface that may be useful for some scenarios.

+ +

Functional Interface

+ +

output = hmac.digest(type, string, key, [raw])

+

This function generates the HMAC of the input string and returns it as output. The hashing algorithm to use is specified as a string by type, and this can be any one of the types allowed by OpenSSL (see their documentation for a full list). Examples include "sha1" and "md5". The string provided in key will be used as the seed for the HMAC generation. The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the HMAC.

+ +

Object Interface

+

The object interface is mainly useful in cases where you want to increase efficiency. For instance, if you will be generating HMACs for a lot of different items using the same algorithm, you can create a new HMAC object of the type required and reset it between each digest call, which will save you a little bit of setup overhead each time.

+ +

d = hmac.new(type, key)

+

Creates a new HMAC object of the type specified. This is the string name of the hash algorithm to use, which can be any one of the allowed OpenSSL types (see their documentation for a full list). Example types include "sha1" and "md5". The HMAC key to use is provided as a string in key.

+ +

d:reset()

+

Resets the internals of the HMAC object to a clean slate.

+ +

d2 = d:clone()

+

Clones the HMAC object and its current state, including data loaded to this point. DOES NOT WORK YET. Just returns a new pointer to the same object.

+ +

d:update(string)

+

Appends the data in string to the current internal data set to be hashed.

+ +

output = d:digest([string], [raw])

+

Generates the HMAC for the internal data set, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the message digest. Note that you can only run this method once on an object; running it a second time will product a bogus HMAC because the internal state is irrecovably destroyed after the first call.

+ + diff --git a/src/crypto.c b/src/crypto.c new file mode 100755 index 0000000..cdb90bb --- /dev/null +++ b/src/crypto.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2006 Keith Howe + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include + +#include "crypto.h" + +#include "lua.h" +#include "lauxlib.h" + +/* code registered as functions */ +static const luaL_reg f[] = +{ + { NULL, NULL } +}; + +/* code registered as methods */ +static const luaL_reg m[] = +{ + { NULL, NULL } +}; + +extern int luaopen_crypto_core(lua_State *L) +{ + OpenSSL_add_all_digests(); + + luaL_newmetatable(L, CRYPTO_TYPE); + + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + + luaL_openlib(L, NULL, m, 0); + luaL_openlib(L, CRYPTO_NAME, f, 0); + + lua_pushliteral(L, "version"); + lua_pushliteral(L, CRYPTO_VERSION); + lua_settable(L, -3); + + return 1; +} diff --git a/src/crypto.h b/src/crypto.h new file mode 100755 index 0000000..b1754ff --- /dev/null +++ b/src/crypto.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2006 Keith Howe + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include + +#define CRYPTO_TAG " library for " LUA_VERSION " / 0.1.0 / using openssl" +#define CRYPTO_NAME "crypto" +#define CRYPTO_VERSION CRYPTO_NAME CRYPTO_TAG +#define CRYPTO_TYPE CRYPTO_NAME " context" diff --git a/src/crypto.lua b/src/crypto.lua new file mode 100755 index 0000000..7abcdc6 --- /dev/null +++ b/src/crypto.lua @@ -0,0 +1,28 @@ +--[[ +-- Copyright (c) 2006 Keith Howe +-- +-- Permission is hereby granted, free of charge, to any person obtaining a +-- copy of this software and associated documentation files (the "Software"), +-- to deal in the Software without restriction, including without limitation +-- the rights to use, copy, modify, merge, publish, distribute, sublicense, +-- and/or sell copies of the Software, and to permit persons to whom the +-- Software is furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included +-- in all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +-- DEALINGS IN THE SOFTWARE. +-- +--]] + +--- luacrypto module +-- This module does stuff. + +local crypto = require("crypto.core") +module("crypto") diff --git a/src/evp.c b/src/evp.c new file mode 100755 index 0000000..44eab2b --- /dev/null +++ b/src/evp.c @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2006 Keith Howe + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include + +#include "evp.h" + +#include "lua.h" +#include "lauxlib.h" + +static EVP_MD_CTX *Pget(lua_State *L, int i) +{ + if (luaL_checkudata(L, i, EVP_TYPE) == NULL) luaL_typerror(L, i, EVP_TYPE); + return lua_touserdata(L, i); +} + +static EVP_MD_CTX *Pnew(lua_State *L) +{ + EVP_MD_CTX *c = lua_newuserdata(L, sizeof(EVP_MD_CTX)); + luaL_getmetatable(L, EVP_TYPE); + lua_setmetatable(L, -2); + return c; +} + +static int Lnew(lua_State *L) /** new(type) */ +{ + EVP_MD_CTX *c = NULL; + const char *s = luaL_checkstring(L, 1); + const EVP_MD *type = EVP_get_digestbyname(s); + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + c = Pnew(L); + EVP_MD_CTX_init(c); + EVP_DigestInit_ex(c, type, NULL); + + return 1; +} + +static int Lclone(lua_State *L) /** clone(c) */ +{ + EVP_MD_CTX *c = Pget(L, 1); + EVP_MD_CTX *d = Pnew(L); + EVP_MD_CTX_init(d); + EVP_MD_CTX_copy_ex(d, c); + return 1; +} + +static int Lreset(lua_State *L) /** reset(c) */ +{ + EVP_MD_CTX *c = Pget(L, 1); + const EVP_MD *t = EVP_MD_CTX_md(c); + EVP_MD_CTX_cleanup(c); + EVP_MD_CTX_init(c); + EVP_DigestInit_ex(c, t, NULL); + return 0; +} + +static int Lupdate(lua_State *L) /** update(c, s) */ +{ + EVP_MD_CTX *c = Pget(L, 1); + const char *s = luaL_checkstring(L, 2); + + EVP_DigestUpdate(c, s, lua_strlen(L, 2)); + + return 0; +} + +static int Ldigest(lua_State *L) /** digest(c, s, [raw]) */ +{ + EVP_MD_CTX *c = Pget(L, 1); + EVP_MD_CTX *d = NULL; + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + + if (lua_isstring(L, 2)) + { + const char *s = luaL_checkstring(L, 2); + EVP_DigestUpdate(c, s, lua_strlen(L, 2)); + } + + d = EVP_MD_CTX_create(); + EVP_MD_CTX_copy_ex(d, c); + EVP_DigestFinal_ex(d, digest, &written); + EVP_MD_CTX_destroy(d); + + if (lua_toboolean(L, 3)) + lua_pushlstring(L, (char *)digest, written); + else + { + int i; + char *hex; + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static int Ltostring(lua_State *L) /** tostring(c) */ +{ + EVP_MD_CTX *c = Pget(L, 1); + char s[64]; + sprintf(s, "%s %p", EVP_NAME, (void *)c); + lua_pushstring(L, s); + return 1; +} + +static int Lgc(lua_State *L) +{ + EVP_MD_CTX *c = Pget(L, 1); + EVP_MD_CTX_cleanup(c); + return 1; +} + +static int Lfdigest(lua_State *L) /** digest(type, s, [raw]) */ +{ + EVP_MD_CTX *c = NULL; + const char *type_name = luaL_checkstring(L, 1); + const char *s = luaL_checkstring(L, 2); + const EVP_MD *type = EVP_get_digestbyname(type_name); + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + c = EVP_MD_CTX_create(); + EVP_DigestInit_ex(c, type, NULL); + EVP_DigestUpdate(c, s, lua_strlen(L, 2)); + EVP_DigestFinal_ex(c, digest, &written); + + if (lua_toboolean(L, 3)) + lua_pushlstring(L, (char *)digest, written); + else + { + int i; + char *hex; + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static const luaL_reg f[] = +{ + { "digest", Lfdigest }, + { "new", Lnew }, + { NULL, NULL } +}; + +static const luaL_reg m[] = +{ + { "__tostring", Ltostring }, + { "__gc", Lgc }, + { "clone", Lclone }, + { "digest", Ldigest }, + { "reset", Lreset }, + { "tostring", Ltostring }, + { "update", Lupdate }, + { NULL, NULL } +}; + +extern int luaopen_crypto_evp_core(lua_State *L) +{ + luaL_newmetatable(L, EVP_TYPE); + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + + luaL_openlib(L, NULL, m, 0); + luaL_openlib(L, EVP_NAME, f, 0); + + lua_pushliteral(L, "version"); + lua_pushliteral(L, EVP_VERSION); + lua_settable(L, -3); + + return 1; +} diff --git a/src/evp.h b/src/evp.h new file mode 100755 index 0000000..432bddd --- /dev/null +++ b/src/evp.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2006 Keith Howe + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include + +#include "crypto.h" + +#define EVP_NAME "crypto.evp" +#define EVP_VERSION EVP_NAME CRYPTO_TAG +#define EVP_TYPE EVP_NAME " context" diff --git a/src/evp.lua b/src/evp.lua new file mode 100755 index 0000000..576aefb --- /dev/null +++ b/src/evp.lua @@ -0,0 +1,26 @@ +--[[ +-- Copyright (c) 2006 Keith Howe +-- +-- Permission is hereby granted, free of charge, to any person obtaining a +-- copy of this software and associated documentation files (the "Software"), +-- to deal in the Software without restriction, including without limitation +-- the rights to use, copy, modify, merge, publish, distribute, sublicense, +-- and/or sell copies of the Software, and to permit persons to whom the +-- Software is furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included +-- in all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +-- DEALINGS IN THE SOFTWARE. +-- +--]] + +local evp = require("crypto.evp.core") +local crypto = require("crypto") +module("crypto.evp") diff --git a/src/hmac.c b/src/hmac.c new file mode 100755 index 0000000..7d36071 --- /dev/null +++ b/src/hmac.c @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2006 Keith Howe + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include + +#include "hmac.h" + +#include "lua.h" +#include "lauxlib.h" + +static HMAC_CTX *Pget(lua_State *L, int i) +{ + if (luaL_checkudata(L, i, HMAC_TYPE) == NULL) luaL_typerror(L, i, HMAC_TYPE); + return lua_touserdata(L, i); +} + +static HMAC_CTX *Pnew(lua_State *L) +{ + HMAC_CTX *c = lua_newuserdata(L, sizeof(HMAC_CTX)); + luaL_getmetatable(L, HMAC_TYPE); + lua_setmetatable(L, -2); + return c; +} + +static int Lnew(lua_State *L) /** new(type, key) */ +{ + HMAC_CTX *c = Pnew(L); + const char *s = luaL_checkstring(L, 1); + const char *k = luaL_checkstring(L, 2); + const EVP_MD *type = EVP_get_digestbyname(s); + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + HMAC_CTX_init(c); + HMAC_Init_ex(c, k, lua_strlen(L, 2), type, NULL); + + return 1; +} + +static int Lclone(lua_State *L) /** clone(c) */ +{ + HMAC_CTX *c = Pget(L, 1); + HMAC_CTX *d = Pnew(L); + *d = *c; + return 1; +} + +static int Lreset(lua_State *L) /** reset(c) */ +{ + HMAC_CTX *c = Pget(L, 1); + HMAC_Init_ex(c, NULL, 0, NULL, NULL); + return 0; +} + +static int Lupdate(lua_State *L) /** update(c, s) */ +{ + HMAC_CTX *c = Pget(L, 1); + const char *s = luaL_checkstring(L, 2); + + HMAC_Update(c, (unsigned char *)s, lua_strlen(L, 2)); + + return 0; +} + +static int Ldigest(lua_State *L) /** digest(c, s, [raw]) */ +{ + HMAC_CTX *c = Pget(L, 1); + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + + if (lua_isstring(L, 2)) + { + const char *s = luaL_checkstring(L, 2); + HMAC_Update(c, (unsigned char *)s, lua_strlen(L, 2)); + } + + HMAC_Final(c, digest, &written); + + if (lua_toboolean(L, 3)) + lua_pushlstring(L, (char *)digest, written); + else + { + int i; + char *hex; + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static int Ltostring(lua_State *L) /** tostring(c) */ +{ + HMAC_CTX *c = Pget(L, 1); + char s[64]; + sprintf(s, "%s %p", HMAC_NAME, (void *)c); + lua_pushstring(L, s); + return 1; +} + +static int Lgc(lua_State *L) +{ + HMAC_CTX *c = Pget(L, 1); + HMAC_CTX_cleanup(c); + return 1; +} + +static int Lfdigest(lua_State *L) /** digest(type, s, key, [raw]) */ +{ + HMAC_CTX c; + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + const char *t = luaL_checkstring(L, 1); + const char *s = luaL_checkstring(L, 2); + const char *k = luaL_checkstring(L, 3); + const EVP_MD *type = EVP_get_digestbyname(t); + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + HMAC_CTX_init(&c); + HMAC_Init_ex(&c, k, lua_strlen(L, 3), type, NULL); + HMAC_Update(&c, (unsigned char *)s, lua_strlen(L, 2)); + HMAC_Final(&c, digest, &written); + + if (lua_toboolean(L, 4)) + lua_pushlstring(L, (char *)digest, written); + else + { + int i; + char *hex; + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static const luaL_reg f[] = +{ + { "new", Lnew }, + { "digest", Lfdigest }, + { NULL, NULL } +}; + +static const luaL_reg m[] = +{ + { "__tostring", Ltostring }, + { "__gc", Lgc }, + { "clone", Lclone }, + { "digest", Ldigest }, + { "reset", Lreset }, + { "tostring", Ltostring }, + { "update", Lupdate }, + { NULL, NULL } +}; + +extern int luaopen_crypto_hmac_core(lua_State *L) +{ + luaL_newmetatable(L, HMAC_TYPE); + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + + luaL_openlib(L, NULL, m, 0); + luaL_openlib(L, HMAC_NAME, f, 0); + + lua_pushliteral(L, "version"); + lua_pushliteral(L, HMAC_VERSION); + lua_settable(L, -3); + + return 1; +} diff --git a/src/hmac.h b/src/hmac.h new file mode 100755 index 0000000..05cb6cb --- /dev/null +++ b/src/hmac.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2006 Keith Howe + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include + +#include "crypto.h" + +#define HMAC_NAME "crypto.hmac" +#define HMAC_VERSION HMAC_NAME CRYPTO_TAG +#define HMAC_TYPE HMAC_NAME " context" diff --git a/src/hmac.lua b/src/hmac.lua new file mode 100755 index 0000000..788c9b8 --- /dev/null +++ b/src/hmac.lua @@ -0,0 +1,26 @@ +--[[ +-- Copyright (c) 2006 Keith Howe +-- +-- Permission is hereby granted, free of charge, to any person obtaining a +-- copy of this software and associated documentation files (the "Software"), +-- to deal in the Software without restriction, including without limitation +-- the rights to use, copy, modify, merge, publish, distribute, sublicense, +-- and/or sell copies of the Software, and to permit persons to whom the +-- Software is furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included +-- in all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +-- DEALINGS IN THE SOFTWARE. +-- +--]] + +local hmac = require("crypto.hmac.core") +local crypto = require("crypto") +module("crypto.hmac") diff --git a/tests/message b/tests/message new file mode 100755 index 0000000..c36bbdd --- /dev/null +++ b/tests/message @@ -0,0 +1 @@ +This is a sample message to use for hashing tests. diff --git a/tests/test.lua b/tests/test.lua new file mode 100755 index 0000000..4cf8413 --- /dev/null +++ b/tests/test.lua @@ -0,0 +1,83 @@ +--[[ +-- Copyright (c) 2006 Keith Howe +-- +-- Permission is hereby granted, free of charge, to any person obtaining a +-- copy of this software and associated documentation files (the "Software"), +-- to deal in the Software without restriction, including without limitation +-- the rights to use, copy, modify, merge, publish, distribute, sublicense, +-- and/or sell copies of the Software, and to permit persons to whom the +-- Software is furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included +-- in all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +-- DEALINGS IN THE SOFTWARE. +-- +--]] + +local evp = require("crypto.evp") +local hmac = require("crypto.hmac") + +md5_KNOWN = "09920f6f666f8e7b09a8d00bd4d06873" +sha1_KNOWN = "d6ed6e26ebeb37ba0792ec75a3d0b4dcec279d25" +hmac_KNOWN = "70a7ea81a287d094c534cdd67be82e85066e13be" + +print("EVP version: " .. evp.version) +print("HMAC version: " .. hmac.version) +print("") + +function report(w, s, F, t) + print(w, s .. " " .. F) + assert(s == _G[t .. "_KNOWN"]) +end + +F = arg[1] +for i, t in ipairs({"sha1", "md5", "sha1", "hmac"}) do + print("testing " .. t) + local d + if (t == "hmac") then + d = hmac.new("sha1", "luacrypto") + else + d = evp.new(t) + end + + assert(io.input(F)) + report("all", d:digest(io.read("*all")), F, t) + + d:reset(d) + + assert(io.input(F)) + while true do + local c = io.read(1) + if c == nil then break end + d:update(c) + end + report("loop", d:digest(), F, t) + if (t ~= "hmac") then + report("again", d:digest(), F, t) + assert(io.input(F)) + report("alone", evp.digest(t, io.read("*all")), F, t) + else + assert(io.input(F)) + report("alone", hmac.digest("sha1", io.read("*all"), "luacrypto"), F, t); + end + + assert(io.input(F)) + d:reset() + while true do + local c = io.read(math.random(1, 16)) + if c == nil then break end + d:update(c) + end + report("reset", d:digest(d), F, t) + report("known", _G[t .. "_KNOWN"], F, t) + print("") +end + +print("all tests passed") From 0953f6f6001a50447b05ad678a0e1c1f5675ea78 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Sun, 22 Jan 2006 21:54:20 +0000 Subject: [PATCH 02/21] separated user build settings into a config file --- Makefile | 93 +++++++++++++++++++++++++++----------------------------- config | 21 +++++++++++++ 2 files changed, 66 insertions(+), 48 deletions(-) create mode 100755 config diff --git a/Makefile b/Makefile index 1c2eee7..637af41 100755 --- a/Makefile +++ b/Makefile @@ -1,26 +1,14 @@ -# Change these to reflect your Lua installation locations. The system -# independent Lua files (LUA_PATH), the system dependent Lua files (LUA_CPATH), -# and the Lua include files (LUAINC) are required to compile and install. -LUA_PATH = /usr/share/lua -LUA_CPATH = /usr/local/lua -LUAINC = /usr/include/lua - -# The location of the Lua interpreter and Lua compiler are required to make the -# tests and luadocs, and to generate compiled Lua libraries instead of source -# Lua libraries for install. -LUA = /usr/bin/lua -LUAC = /usr/bin/luac - -# This provides the necessary flags for linking against your OpenSSL libcrypto -# installation. Change it to suit your system if necessary. -LDFLAGS = -lcrypto -#CFLAGS = - -# Set this to lc to install the precompiled Lua libraries (compiled with luac), -# or to lua to install the source Lua libraries. -LUATYPE = lc - -# You shouldn't need to change anything below here. +# include in user config or use default values instead +-include ./config +LUA_PATH ?= /usr/share/lua +LUA_CPATH ?= /usr/local/lua +LUAINC ?= /usr/include/lua +LUA ?= /usr/bin/lua +LUAC ?= /usr/bin/luac +CRYPTOLIB ?= -lcrypto +LUATYPE ?= lc + +# other default stuff that generally won't change .SUFFIXES: srcdir := ./src outdir := ./obj @@ -28,41 +16,46 @@ tstdir := ./tests INSTALL = install SHELL = /bin/sh MODULES = evp hmac -CFLAGS = -I$(LUAINC) -ansi -pedantic -Wall -O2 -SOOBJS = $(outdir)/crypto.so $(foreach module,$(MODULES),$(outdir)/$(module).so) -LCOBJS = $(outdir)/crypto.$(LUATYPE) $(foreach module,$(MODULES),$(outdir)/$(module).$(LUATYPE)) +CFLAGS = $(CRYPTOINC) $(LUAINC) -ansi -pedantic -Wall -O2 +SOOBJS = $(outdir)/crypto/core.so $(foreach module,$(MODULES),$(outdir)/crypto/$(module)/core.so) +LCOBJS = $(outdir)/crypto.$(LUATYPE) $(foreach module,$(MODULES),$(outdir)/crypto/$(module).$(LUATYPE)) +LDFLAGS = $(CRYPTOLIB) +# default target .PHONY: all $(outdir) all: $(outdir) $(SOOBJS) $(LCOBJS) +# rule to create build directory $(outdir): @if [ ! -d $(outdir) ]; then $(INSTALL) -d $(outdir); fi + @if [ ! -d $(outdir)/crypto ]; then $(INSTALL) -d $(outdir); fi + @$(foreach module,$(MODULES),if [ ! -d $(outdir)/crypto/$(module) ]; then $(INSTALL) -d $(outdir)/crypto/$(module); fi ; ) -$(outdir)/%.so: $(outdir)/%.o +# rules for building the final so's +$(outdir)/%/core.so: $(outdir)/%.o $(CC) $(LDFLAGS) -shared $< -o $@ -$(outdir)/%.o: $(srcdir)/%.c +$(outdir)/crypto/%/core.so: $(outdir)/%.o + $(CC) $(LDFLAGS) -shared $< -o $@ + +# rules for building intermediary objects +$(outdir)/%.o: $(srcdir)/%.c $(srcdir)/%.h $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ -$(outdir)/%.lc: $(srcdir)/%.lua +# rules for building the final lua/lc files +$(outdir)/%.lc: $(srcdir)/%.lua + $(LUAC) $(LUACFLAGS) -o $@ $< + +$(outdir)/crypto/%.lc: $(srcdir)/%.lua $(LUAC) $(LUACFLAGS) -o $@ $< $(outdir)/%.lua: $(srcdir)/%.lua cp $< $@ -luadoc: $(srcdir)/crypto.lua $(foreach module,$(MODULES),$(srcdir)/$(module).lua) - @if [ -x $(LUA) ]; then \ - echo "Building luadoc..."; \ - LUA_PATH="$(LUA_PATH)/?.$(LUATYPE)"; \ - LUA_CPATH="$(LUA_CPATH)/?.so"; \ - export LUA_PATH LUA_CPATH; \ - $(LUA) $(srcdir)/test.lua $(srcdir)/crypto.lua; \ - else \ - echo "Lua interpreter not found; not building luadoc."; \ - echo "Set LUA to your interpreter location and execute"; \ - echo "'make tests' to run the test suite."; \ - fi +$(outdir)/crypto/%.lua: $(srcdir)/%.lua + cp $< $@ +# cleanup rules .PHONY: clean distclean mostlyclean clean: rm -fr $(outdir) @@ -74,11 +67,12 @@ mostlyclean: rm -fr $(outdir)/* rm -f ./core ./core.* +# template for generating un/install rules define INSTALL_TEMPLATE -$(1)_install: $(outdir)/$(1).so $(outdir)/$(1).$(LUATYPE) +$(1)_install: $(outdir)/crypto/$(1)/core.so $(outdir)/crypto/$(1).$(LUATYPE) $(INSTALL) -d $(LUA_CPATH)/crypto/$(1) - $(INSTALL) -D $(outdir)/$(1).so $(LUA_CPATH)/crypto/$(1)/core.so - $(INSTALL) -D $(outdir)/$(1).$(LUATYPE) $(LUA_PATH)/crypto/$(1).$(LUATYPE) + $(INSTALL) -D $(outdir)/crypto/$(1)/core.so $(LUA_CPATH)/crypto/$(1)/core.so + $(INSTALL) -D $(outdir)/crypto/$(1).$(LUATYPE) $(LUA_PATH)/crypto/$(1).$(LUATYPE) $(1)_uninstall: -rm $(LUA_CPATH)/crypto/$(1)/core.so @@ -86,6 +80,7 @@ $(1)_uninstall: -rm $(LUA_PATH)/crypto/$(1).$(LUATYPE) endef +# install rules $(foreach module,$(MODULES),$(eval $(call INSTALL_TEMPLATE,$(module)))) .PHONY: install crypto_install uninstall crypto_uninstall @@ -93,11 +88,12 @@ $(foreach module,$(MODULES),$(eval $(call INSTALL_TEMPLATE,$(module)))) crypto_install: $(INSTALL) -d $(LUA_PATH)/crypto/ $(INSTALL) -d $(LUA_CPATH)/crypto/ - $(INSTALL) -D $(outdir)/crypto.so $(LUA_CPATH)/crypto/core.so + $(INSTALL) -D $(outdir)/crypto/core.so $(LUA_CPATH)/crypto/core.so $(INSTALL) -D $(outdir)/crypto.$(LUATYPE) $(LUA_CPATH)/crypto.$(LUATYPE) -install: crypto_install $(foreach module,$(MODULES),$(module)_install) tests ; +install: crypto_install $(foreach module,$(MODULES),$(module)_install) ; +# uninstall rules crypto_uninstall: -rm $(LUA_CPATH)/crypto/core.so -rm -r $(LUA_CPATH)/crypto/ @@ -106,13 +102,14 @@ crypto_uninstall: uninstall: $(foreach module,$(MODULES),$(module)_uninstall) crypto_uninstall +# rule to run the test suite .PHONY: tests tests: @echo "" @if [ -x $(LUA) ]; then \ echo "Running tests..."; \ - LUA_PATH="$(LUA_PATH)/?.$(LUATYPE)"; \ - LUA_CPATH="$(LUA_CPATH)/?.so"; \ + LUA_PATH="$(outdir)/?.$(LUATYPE)"; \ + LUA_CPATH="$(outdir)/?.so"; \ export LUA_PATH LUA_CPATH; \ $(LUA) $(tstdir)/test.lua $(tstdir)/message; \ else \ diff --git a/config b/config new file mode 100755 index 0000000..0e6cfc2 --- /dev/null +++ b/config @@ -0,0 +1,21 @@ +# Change these to reflect your Lua installation locations. The system +# independent Lua files (LUA_PATH), the system dependent Lua files (LUA_CPATH), +# and the Lua include files (LUAINC) are required to compile and install. +LUA_PATH = /usr/share/lua +LUA_CPATH = /usr/local/lua +LUAINC = -I/usr/include/lua + +# The location of the Lua interpreter and Lua compiler are required to run the +# test suite and to generate precompiled Lua libraries instead of source Lua +# libraries for install. +LUA = /usr/bin/lua +LUAC = /usr/bin/luac + +# This provides the necessary flags for linking against your OpenSSL libcrypto +# installation. Change it to suit your system if necessary. +CRYPTOLIB = -lcrypto +CRYPTOINC = + +# Set this to lc to install the precompiled Lua libraries (compiled with luac), +# or to lua to install the source Lua libraries. +LUATYPE = lc From 746d235ba9c02334352f93264eb439259499dc12 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Sun, 22 Jan 2006 23:01:32 +0000 Subject: [PATCH 03/21] added support for Lua 5.0 with Compat-5.1 --- Makefile | 21 ++- README | 44 +++--- config | 13 +- contrib/compat-5.1r4/LICENSE | 7 + contrib/compat-5.1r4/compat-5.1.c | 97 ++++++++++++ contrib/compat-5.1r4/compat-5.1.h | 13 ++ contrib/compat-5.1r4/compat-5.1.lua | 235 ++++++++++++++++++++++++++++ src/crypto.c | 3 + src/evp.c | 3 + src/hmac.c | 3 + 10 files changed, 414 insertions(+), 25 deletions(-) create mode 100755 contrib/compat-5.1r4/LICENSE create mode 100755 contrib/compat-5.1r4/compat-5.1.c create mode 100755 contrib/compat-5.1r4/compat-5.1.h create mode 100755 contrib/compat-5.1r4/compat-5.1.lua diff --git a/Makefile b/Makefile index 637af41..71d55d7 100755 --- a/Makefile +++ b/Makefile @@ -16,7 +16,9 @@ tstdir := ./tests INSTALL = install SHELL = /bin/sh MODULES = evp hmac -CFLAGS = $(CRYPTOINC) $(LUAINC) -ansi -pedantic -Wall -O2 +COMPAT_DIR = contrib/compat-5.1r4 +COMPAT_O = $(if $(USE_COMPAT),$(outdir)/compat-5.1.o) +CFLAGS = $(CRYPTOINC) $(LUAINC) $(if $(USE_COMPAT),-I$(COMPAT_DIR) -DUSE_COMPAT) -ansi -pedantic -Wall -O2 SOOBJS = $(outdir)/crypto/core.so $(foreach module,$(MODULES),$(outdir)/crypto/$(module)/core.so) LCOBJS = $(outdir)/crypto.$(LUATYPE) $(foreach module,$(MODULES),$(outdir)/crypto/$(module).$(LUATYPE)) LDFLAGS = $(CRYPTOLIB) @@ -32,16 +34,19 @@ $(outdir): @$(foreach module,$(MODULES),if [ ! -d $(outdir)/crypto/$(module) ]; then $(INSTALL) -d $(outdir)/crypto/$(module); fi ; ) # rules for building the final so's -$(outdir)/%/core.so: $(outdir)/%.o - $(CC) $(LDFLAGS) -shared $< -o $@ +$(outdir)/%/core.so: $(outdir)/%.o $(COMPAT_O) + $(CC) $(LDFLAGS) -shared $< $(COMPAT_O) -o $@ -$(outdir)/crypto/%/core.so: $(outdir)/%.o - $(CC) $(LDFLAGS) -shared $< -o $@ +$(outdir)/crypto/%/core.so: $(outdir)/%.o $(COMPAT_O) + $(CC) $(LDFLAGS) -shared $< $(COMPAT_O) -o $@ # rules for building intermediary objects $(outdir)/%.o: $(srcdir)/%.c $(srcdir)/%.h $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +$(outdir)/compat-5.1.o: $(COMPAT_DIR)/compat-5.1.c + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ + # rules for building the final lua/lc files $(outdir)/%.lc: $(srcdir)/%.lua $(LUAC) $(LUACFLAGS) -o $@ $< @@ -91,7 +96,10 @@ crypto_install: $(INSTALL) -D $(outdir)/crypto/core.so $(LUA_CPATH)/crypto/core.so $(INSTALL) -D $(outdir)/crypto.$(LUATYPE) $(LUA_CPATH)/crypto.$(LUATYPE) -install: crypto_install $(foreach module,$(MODULES),$(module)_install) ; +compat_install: + $(INSTALL) -D $(COMPAT_DIR)/compat-5.1.lua $(LUA_PATH)/compat-5.1.lua + +install: crypto_install $(foreach module,$(MODULES),$(module)_install) $(if $(INSTALL_COMPAT),compat_install) ; # uninstall rules crypto_uninstall: @@ -110,6 +118,7 @@ tests: echo "Running tests..."; \ LUA_PATH="$(outdir)/?.$(LUATYPE)"; \ LUA_CPATH="$(outdir)/?.so"; \ + $(if $(USE_COMPAT),LUA_INIT="@$(COMPAT_DIR)/compat-5.1.lua"; export LUA_INIT;) \ export LUA_PATH LUA_CPATH; \ $(LUA) $(tstdir)/test.lua $(tstdir)/message; \ else \ diff --git a/README b/README index bda5407..e4322a0 100755 --- a/README +++ b/README @@ -12,20 +12,19 @@ http://luacrypto.luaforge.net/ 2. REQUIREMENTS - - Lua 5.1 - http://www.lua.org/ + - Lua 5.x - http://www.lua.org/ - luacrypto requires Lua 5.1 since it uses the new Lua package system. - It should also work with Compat-5.1, though you will have to update the - build and libraries in order to support this. + luacrypto uses the new Lua 5.1 package system. It will also work with + Lua 5.0 using the included Compat-5.1 emulation system. - libcrypto 0.9.7+ - http://www.openssl.org/ luacrypto has only been built against and tested with OpenSSL versions 0.9.7 and above. It depends on additional API methods introduced in 0.9.7. -2. INSTALLATION +3. INSTALLATION -Edit the Makefile and set the following fields to the correct values for your +Edit the "config" file and set the fields to the correct values for your environment: LUAINC: The location of your Lua 5.1 include headers. This is required to @@ -34,34 +33,43 @@ environment: LUA_PATH: The installation path for your system independent Lua files. LUA_CPATH: The installation path for your system dependent Lua files. These two settings are required to install the libraries properly. + + USE_COMPAT: Set this if your target environment is Lua 5.0. If your target is + Lua 5.1, leave this blank. + INSTALL_COMPAT: If you have set USE_COMPAT, you should also set this value to + install the compat-5.1.lua module during "make install". However, if you + already have the module installed (and possibly customised), leave this + unset to leave it unmolested during "make install". + (Note that the compat-5.1.lua module must be loaded before any wrapped + modules will work. See http://www.keplerproject.org/compat for details). + LUA_TYPE: Set this to the type of Lua libraries you would like to install; either "lc" for precompiled Lua libraries (using luac), or "lua" for - regular text Lua libraries. + regular Lua libraries. - LUA: The location of your Lua 5.1 interpreter; only needed to run the tests - and to generate the luadoc. + LUA: The location of your Lua 5.1 interpreter; only needed to run the tests. LUAC: The location of your Lua 5.1 compiler; only needed to build the precompiled Lua libraries. - LDFLAGS: On most systems, the default value will be fine. However, if your + CRYPTOLIB: On most systems, the default value will be fine. However, if your OpenSSL libcrypto library is not in the standard library paths, you may need to point this at your OpenSSL install. - - CFLAGS: On most systems, the default value will be fine. However, if your + CRYPTOINC: On most systems, the default value will be fine. However, if your OpenSSL libcrypto headers are not in the standard include paths, you may need to point this at your OpenSSL install. -Once configured, run "make" to build the libraries. This will also build the -documentation using luadoc if you have set the location of your Lua +Once configured, run "make" to build the libraries. You can then run +"make tests" to run a test suite if you have set the location of your Lua interpreter. Run "make install" to install the libraries in the correct -location. The install will run a test suite if you have set the location of -your Lua interpreter. You can run the tests independently with "make tests", -but you cannot run the tests until after you have run "make install". +location. -3. CREDITS +4. CREDITS Much of this code was heavily inspired by and/or lifted directly from the lmd5 project (http://luaforge.net/projects/lmd5/), written by Luiz Henrique de Figueiredo . +The Compat-5.1 library (http://www.keplerproject.org/compat/), written by +the folks at the Kepler Project, is also included for Lua 5.0 support. + diff --git a/config b/config index 0e6cfc2..fecbb29 100755 --- a/config +++ b/config @@ -1,10 +1,21 @@ # Change these to reflect your Lua installation locations. The system # independent Lua files (LUA_PATH), the system dependent Lua files (LUA_CPATH), -# and the Lua include files (LUAINC) are required to compile and install. +# and the Lua include files (LUAINC) are required to compile and install. Set +# USE_COMPAT to 1 if you building for a Lua 5.0 environment, or leave it blank +# if you are building for a Lua 5.1 environment. LUA_PATH = /usr/share/lua LUA_CPATH = /usr/local/lua LUAINC = -I/usr/include/lua +# If your target environment is Lua 5.1, leave both of these COMPAT flags unset. +# If your target environment is Lua 5.0, the USE_COMPAT flag must be set to 1 +# to use the Compat-5.1 module emulation. If you already have Compat-5.1 +# installed in your LUA_PATH and don't want to overwrite it, set INSTALL_COMPAT +# to a blank value; otherwise, it should be set to 1 if USE_COMPAT is set. See +# http://www.keplerproject.org/compat/ for more details about Compat-5.1. +USE_COMPAT = +INSTALL_COMPAT = + # The location of the Lua interpreter and Lua compiler are required to run the # test suite and to generate precompiled Lua libraries instead of source Lua # libraries for install. diff --git a/contrib/compat-5.1r4/LICENSE b/contrib/compat-5.1r4/LICENSE new file mode 100755 index 0000000..2c46100 --- /dev/null +++ b/contrib/compat-5.1r4/LICENSE @@ -0,0 +1,7 @@ +Copyright � 2004-2005 The Kepler Project. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/contrib/compat-5.1r4/compat-5.1.c b/contrib/compat-5.1r4/compat-5.1.c new file mode 100755 index 0000000..fafdaa0 --- /dev/null +++ b/contrib/compat-5.1r4/compat-5.1.c @@ -0,0 +1,97 @@ +/* +** Compat-5.1 +** Copyright Kepler Project 2004-2005 (http://www.keplerproject.org/compat) +** $Id: compat-5.1.c,v 1.1 2006-01-22 23:01:32 nezroy Exp $ +*/ + +#include +#include +#include "lua.h" +#include "lauxlib.h" +#include "compat-5.1.h" + +static void getfield(lua_State *L, int idx, const char *name) { + const char *end = strchr(name, '.'); + lua_pushvalue(L, idx); + while (end) { + lua_pushlstring(L, name, end - name); + lua_gettable(L, -2); + lua_remove(L, -2); + if (lua_isnil(L, -1)) return; + name = end+1; + end = strchr(name, '.'); + } + lua_pushstring(L, name); + lua_gettable(L, -2); + lua_remove(L, -2); +} + +static void setfield(lua_State *L, int idx, const char *name) { + const char *end = strchr(name, '.'); + lua_pushvalue(L, idx); + while (end) { + lua_pushlstring(L, name, end - name); + lua_gettable(L, -2); + /* create table if not found */ + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + lua_newtable(L); + lua_pushlstring(L, name, end - name); + lua_pushvalue(L, -2); + lua_settable(L, -4); + } + lua_remove(L, -2); + name = end+1; + end = strchr(name, '.'); + } + lua_pushstring(L, name); + lua_pushvalue(L, -3); + lua_settable(L, -3); + lua_pop(L, 2); +} + +LUALIB_API void luaL_module(lua_State *L, const char *libname, + const luaL_reg *l, int nup) { + if (libname) { + getfield(L, LUA_GLOBALSINDEX, libname); /* check whether lib already exists */ + if (lua_isnil(L, -1)) { + int env, ns; + lua_pop(L, 1); /* get rid of nil */ + lua_pushliteral(L, "require"); + lua_gettable(L, LUA_GLOBALSINDEX); /* look for require */ + lua_getfenv(L, -1); /* getfenv(require) */ + lua_remove(L, -2); /* remove function require */ + env = lua_gettop(L); + + lua_newtable(L); /* create namespace for lib */ + ns = lua_gettop(L); + getfield(L, env, "package.loaded"); /* get package.loaded table */ + if (lua_isnil(L, -1)) { /* create package.loaded table */ + lua_pop(L, 1); /* remove previous result */ + lua_newtable(L); + lua_pushvalue(L, -1); + setfield(L, env, "package.loaded"); + } + else if (!lua_istable(L, -1)) + luaL_error(L, "name conflict for library `%s'", libname); + lua_pushstring(L, libname); + lua_pushvalue(L, ns); + lua_settable(L, -3); /* package.loaded[libname] = ns */ + lua_pop(L, 1); /* get rid of package.loaded table */ + lua_pushvalue(L, ns); /* copy namespace */ + setfield(L, LUA_GLOBALSINDEX, libname); + lua_remove (L, env); /* remove env */ + } + lua_insert(L, -(nup+1)); /* move library table to below upvalues */ + } + for (; l->name; l++) { + int i; + lua_pushstring(L, l->name); + for (i=0; ifunc, nup); + lua_settable(L, -(nup+3)); + } + lua_pop(L, nup); /* remove upvalues */ +} + diff --git a/contrib/compat-5.1r4/compat-5.1.h b/contrib/compat-5.1r4/compat-5.1.h new file mode 100755 index 0000000..7e66378 --- /dev/null +++ b/contrib/compat-5.1r4/compat-5.1.h @@ -0,0 +1,13 @@ +/* +** Compat-5.1 +** Copyright Kepler Project 2004-2005 (http://www.keplerproject.org/compat/) +** $Id: compat-5.1.h,v 1.1 2006-01-22 23:01:32 nezroy Exp $ +*/ + +#ifndef COMPAT_H + +LUALIB_API void luaL_module(lua_State *L, const char *libname, + const luaL_reg *l, int nup); +#define luaL_openlib luaL_module + +#endif diff --git a/contrib/compat-5.1r4/compat-5.1.lua b/contrib/compat-5.1r4/compat-5.1.lua new file mode 100755 index 0000000..e68572c --- /dev/null +++ b/contrib/compat-5.1r4/compat-5.1.lua @@ -0,0 +1,235 @@ +-- +-- Compat-5.1 +-- Copyright Kepler Project 2004-2005 (http://www.keplerproject.org/compat) +-- According to Lua 5.1 +-- $Id: compat-5.1.lua,v 1.1 2006-01-22 23:01:32 nezroy Exp $ +-- + +_COMPAT51 = "Compat-5.1 R4" + +local LUA_DIRSEP = '/' +local LUA_OFSEP = '_' +local OLD_LUA_OFSEP = '' +local POF = 'luaopen_' + +local assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type = assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type +local format, gfind, gsub = string.format, string.gfind, string.gsub + +-- +-- avoid overwriting the package table if it's already there +-- +package = package or {} + +package.path = LUA_PATH or os.getenv("LUA_PATH") or + ("./?.lua;" .. + "/usr/local/share/lua/5.0/?.lua;" .. + "/usr/local/share/lua/5.0/?/?.lua;" .. + "/usr/local/share/lua/5.0/?/init.lua" ) + +package.cpath = os.getenv("LUA_CPATH") or + "./?.so;" .. + "./l?.so;" .. + "/usr/local/lib/lua/5.0/?.so;" .. + "/usr/local/lib/lua/5.0/l?.so" + +-- +-- make sure require works with standard libraries +-- +package.loaded = package.loaded or {} +package.loaded.string = string +package.loaded.math = math +package.loaded.io = io +package.loaded.os = os +package.loaded.table = table +package.loaded.base = _G +package.loaded.coroutine = coroutine + +-- +-- avoid overwriting the package.preload table if it's already there +-- +package.preload = package.preload or {} + + +-- +-- auxiliar function to read "nested globals" +-- +local function getfield (t, f) + assert (type(f)=="string", "not a valid field name ("..tostring(f)..")") + for w in gfind(f, "[%w_]+") do + if not t then return nil end + t = rawget(t, w) + end + return t +end + + +-- +-- auxiliar function to write "nested globals" +-- +local function setfield (t, f, v) + for w in gfind(f, "([%w_]+)%.") do + t[w] = t[w] or {} -- create table if absent + t = t[w] -- get the table + end + local w = gsub(f, "[%w_]+%.", "") -- get last field name + t[w] = v -- do the assignment +end + + +-- +-- looks for a file `name' in given path +-- +local function search (path, name) + for c in gfind(path, "[^;]+") do + c = gsub(c, "%?", name) + local f = io.open(c) + if f then -- file exist? + f:close() + return c + end + end + return nil -- file not found +end + + +-- +-- check whether library is already loaded +-- +local function loader_preload (name) + assert (type(name) == "string", format ( + "bad argument #1 to `require' (string expected, got %s)", type(name))) + if type(package.preload) ~= "table" then + error ("`package.preload' must be a table") + end + return package.preload[name] +end + + +-- +-- C library loader +-- +local function loader_C (name) + assert (type(name) == "string", format ( + "bad argument #1 to `require' (string expected, got %s)", type(name))) + local fname = gsub (name, "%.", LUA_DIRSEP) + fname = search (package.cpath, fname) + if not fname then + return false + end + local funcname = POF .. gsub (name, "%.", LUA_OFSEP) + local f, err = loadlib (fname, funcname) + if not f then + funcname = POF .. gsub (name, "%.", OLD_LUA_OFSEP) + f, err = loadlib (fname, funcname) + if not f then + error (format ("error loading package `%s' (%s)", name, err)) + end + end + return f +end + + +-- +-- Lua library loader +-- +local function loader_Lua (name) + assert (type(name) == "string", format ( + "bad argument #1 to `require' (string expected, got %s)", type(name))) + local path = LUA_PATH + if not path then + path = assert (package.path, "`package.path' must be a string") + end + local fname = gsub (name, "%.", LUA_DIRSEP) + fname = search (path, fname) + if not fname then + return false + end + local f, err = loadfile (fname) + if not f then + error (format ("error loading package `%s' (%s)", name, err)) + end + return f +end + + +-- create `loaders' table +package.loaders = package.loaders or { loader_preload, loader_C, loader_Lua, } + + +-- +-- iterate over available loaders +-- +local function load (name, loaders) + -- iterate over available loaders + assert (type (loaders) == "table", "`package.loaders' must be a table") + for i, loader in ipairs (loaders) do + local f = loader (name) + if f then + return f + end + end + error (format ("package `%s' not found", name)) +end + + +-- +-- new require +-- +function _G.require (name) + assert (type(name) == "string", format ( + "bad argument #1 to `require' (string expected, got %s)", type(name))) + local p = loaded[name] -- is it there? + if p then + return p + end + -- first mark it as loaded + loaded[name] = true + -- load and run init function + local actual_arg = _G.arg + _G.arg = { name } + local res = load(name, loaders)(name) + if res then + loaded[name] = res -- store result + end + _G.arg = actual_arg + -- return value should be in loaded[name] + return loaded[name] +end + + +-- +-- new module function +-- +function _G.module (name) + local _G = getfenv(0) -- simulate C function environment + local ns = getfield(_G, name) -- search for namespace + if not ns then + ns = {} -- create new namespace + setfield(_G, name, ns) + elseif type(ns) ~= "table" then + error("name conflict for module `"..name.."'") + end + if not ns._NAME then + ns._NAME = name + ns._M = ns + ns._PACKAGE = gsub(name, "[^.]*$", "") + end + setmetatable(ns, {__index = _G}) + loaded[name] = ns + setfenv(2, ns) + return ns +end + + +-- +-- define functions' environments +-- +local env = { + loaded = package.loaded, + loaders = package.loaders, + package = package, + _G = _G, +} +for i, f in ipairs { _G.module, _G.require, load, loader_preload, loader_C, loader_Lua, } do + setfenv (f, env) +end diff --git a/src/crypto.c b/src/crypto.c index cdb90bb..0303a91 100755 --- a/src/crypto.c +++ b/src/crypto.c @@ -27,6 +27,9 @@ #include "lua.h" #include "lauxlib.h" +#ifdef USE_COMPAT + #include "compat-5.1.h" +#endif /* code registered as functions */ static const luaL_reg f[] = diff --git a/src/evp.c b/src/evp.c index 44eab2b..2973907 100755 --- a/src/evp.c +++ b/src/evp.c @@ -27,6 +27,9 @@ #include "lua.h" #include "lauxlib.h" +#ifdef USE_COMPAT + #include "compat-5.1.h" +#endif static EVP_MD_CTX *Pget(lua_State *L, int i) { diff --git a/src/hmac.c b/src/hmac.c index 7d36071..7e8596a 100755 --- a/src/hmac.c +++ b/src/hmac.c @@ -27,6 +27,9 @@ #include "lua.h" #include "lauxlib.h" +#ifdef USE_COMPAT + #include "compat-5.1.h" +#endif static HMAC_CTX *Pget(lua_State *L, int i) { From 7b7501417d1dec2670b70a3eeb0b1834dbf41ef9 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Sun, 22 Jan 2006 23:12:01 +0000 Subject: [PATCH 04/21] grammar and such --- README | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/README b/README index e4322a0..e3ea1a8 100755 --- a/README +++ b/README @@ -39,8 +39,8 @@ environment: INSTALL_COMPAT: If you have set USE_COMPAT, you should also set this value to install the compat-5.1.lua module during "make install". However, if you already have the module installed (and possibly customised), leave this - unset to leave it unmolested during "make install". - + unset to keep it from being clobbered during "make install". + (Note that the compat-5.1.lua module must be loaded before any wrapped modules will work. See http://www.keplerproject.org/compat for details). @@ -48,8 +48,8 @@ environment: either "lc" for precompiled Lua libraries (using luac), or "lua" for regular Lua libraries. - LUA: The location of your Lua 5.1 interpreter; only needed to run the tests. - LUAC: The location of your Lua 5.1 compiler; only needed to build the + LUA: The location of your Lua interpreter; only needed to run the tests. + LUAC: The location of your Lua compiler; only needed to build the precompiled Lua libraries. CRYPTOLIB: On most systems, the default value will be fine. However, if your @@ -70,6 +70,5 @@ Much of this code was heavily inspired by and/or lifted directly from the lmd5 project (http://luaforge.net/projects/lmd5/), written by Luiz Henrique de Figueiredo . -The Compat-5.1 library (http://www.keplerproject.org/compat/), written by -the folks at the Kepler Project, is also included for Lua 5.0 support. - +The Compat-5.1 library (http://www.keplerproject.org/compat/) written by +the folks at the Kepler Project is also included for Lua 5.0 support. From eb32724fb9a6b4e8fcf6b9184f96170b692924b1 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Sun, 22 Jan 2006 23:12:42 +0000 Subject: [PATCH 05/21] removed Lua 5.1 restrictions --- README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README b/README index e3ea1a8..c870e77 100755 --- a/README +++ b/README @@ -27,8 +27,8 @@ http://luacrypto.luaforge.net/ Edit the "config" file and set the fields to the correct values for your environment: - LUAINC: The location of your Lua 5.1 include headers. This is required to - build the code. + LUAINC: The location of your Lua include headers. This is required to build + the code. LUA_PATH: The installation path for your system independent Lua files. LUA_CPATH: The installation path for your system dependent Lua files. These From 005b9c92147a4ffa5917d903083f854a7825b1d2 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Tue, 22 Aug 2006 00:50:08 +0000 Subject: [PATCH 06/21] removing contrib area --- contrib/compat-5.1r4/LICENSE | 7 - contrib/compat-5.1r4/compat-5.1.c | 97 ------------ contrib/compat-5.1r4/compat-5.1.h | 13 -- contrib/compat-5.1r4/compat-5.1.lua | 235 ---------------------------- 4 files changed, 352 deletions(-) delete mode 100755 contrib/compat-5.1r4/LICENSE delete mode 100755 contrib/compat-5.1r4/compat-5.1.c delete mode 100755 contrib/compat-5.1r4/compat-5.1.h delete mode 100755 contrib/compat-5.1r4/compat-5.1.lua diff --git a/contrib/compat-5.1r4/LICENSE b/contrib/compat-5.1r4/LICENSE deleted file mode 100755 index 2c46100..0000000 --- a/contrib/compat-5.1r4/LICENSE +++ /dev/null @@ -1,7 +0,0 @@ -Copyright � 2004-2005 The Kepler Project. - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/contrib/compat-5.1r4/compat-5.1.c b/contrib/compat-5.1r4/compat-5.1.c deleted file mode 100755 index fafdaa0..0000000 --- a/contrib/compat-5.1r4/compat-5.1.c +++ /dev/null @@ -1,97 +0,0 @@ -/* -** Compat-5.1 -** Copyright Kepler Project 2004-2005 (http://www.keplerproject.org/compat) -** $Id: compat-5.1.c,v 1.1 2006-01-22 23:01:32 nezroy Exp $ -*/ - -#include -#include -#include "lua.h" -#include "lauxlib.h" -#include "compat-5.1.h" - -static void getfield(lua_State *L, int idx, const char *name) { - const char *end = strchr(name, '.'); - lua_pushvalue(L, idx); - while (end) { - lua_pushlstring(L, name, end - name); - lua_gettable(L, -2); - lua_remove(L, -2); - if (lua_isnil(L, -1)) return; - name = end+1; - end = strchr(name, '.'); - } - lua_pushstring(L, name); - lua_gettable(L, -2); - lua_remove(L, -2); -} - -static void setfield(lua_State *L, int idx, const char *name) { - const char *end = strchr(name, '.'); - lua_pushvalue(L, idx); - while (end) { - lua_pushlstring(L, name, end - name); - lua_gettable(L, -2); - /* create table if not found */ - if (lua_isnil(L, -1)) { - lua_pop(L, 1); - lua_newtable(L); - lua_pushlstring(L, name, end - name); - lua_pushvalue(L, -2); - lua_settable(L, -4); - } - lua_remove(L, -2); - name = end+1; - end = strchr(name, '.'); - } - lua_pushstring(L, name); - lua_pushvalue(L, -3); - lua_settable(L, -3); - lua_pop(L, 2); -} - -LUALIB_API void luaL_module(lua_State *L, const char *libname, - const luaL_reg *l, int nup) { - if (libname) { - getfield(L, LUA_GLOBALSINDEX, libname); /* check whether lib already exists */ - if (lua_isnil(L, -1)) { - int env, ns; - lua_pop(L, 1); /* get rid of nil */ - lua_pushliteral(L, "require"); - lua_gettable(L, LUA_GLOBALSINDEX); /* look for require */ - lua_getfenv(L, -1); /* getfenv(require) */ - lua_remove(L, -2); /* remove function require */ - env = lua_gettop(L); - - lua_newtable(L); /* create namespace for lib */ - ns = lua_gettop(L); - getfield(L, env, "package.loaded"); /* get package.loaded table */ - if (lua_isnil(L, -1)) { /* create package.loaded table */ - lua_pop(L, 1); /* remove previous result */ - lua_newtable(L); - lua_pushvalue(L, -1); - setfield(L, env, "package.loaded"); - } - else if (!lua_istable(L, -1)) - luaL_error(L, "name conflict for library `%s'", libname); - lua_pushstring(L, libname); - lua_pushvalue(L, ns); - lua_settable(L, -3); /* package.loaded[libname] = ns */ - lua_pop(L, 1); /* get rid of package.loaded table */ - lua_pushvalue(L, ns); /* copy namespace */ - setfield(L, LUA_GLOBALSINDEX, libname); - lua_remove (L, env); /* remove env */ - } - lua_insert(L, -(nup+1)); /* move library table to below upvalues */ - } - for (; l->name; l++) { - int i; - lua_pushstring(L, l->name); - for (i=0; ifunc, nup); - lua_settable(L, -(nup+3)); - } - lua_pop(L, nup); /* remove upvalues */ -} - diff --git a/contrib/compat-5.1r4/compat-5.1.h b/contrib/compat-5.1r4/compat-5.1.h deleted file mode 100755 index 7e66378..0000000 --- a/contrib/compat-5.1r4/compat-5.1.h +++ /dev/null @@ -1,13 +0,0 @@ -/* -** Compat-5.1 -** Copyright Kepler Project 2004-2005 (http://www.keplerproject.org/compat/) -** $Id: compat-5.1.h,v 1.1 2006-01-22 23:01:32 nezroy Exp $ -*/ - -#ifndef COMPAT_H - -LUALIB_API void luaL_module(lua_State *L, const char *libname, - const luaL_reg *l, int nup); -#define luaL_openlib luaL_module - -#endif diff --git a/contrib/compat-5.1r4/compat-5.1.lua b/contrib/compat-5.1r4/compat-5.1.lua deleted file mode 100755 index e68572c..0000000 --- a/contrib/compat-5.1r4/compat-5.1.lua +++ /dev/null @@ -1,235 +0,0 @@ --- --- Compat-5.1 --- Copyright Kepler Project 2004-2005 (http://www.keplerproject.org/compat) --- According to Lua 5.1 --- $Id: compat-5.1.lua,v 1.1 2006-01-22 23:01:32 nezroy Exp $ --- - -_COMPAT51 = "Compat-5.1 R4" - -local LUA_DIRSEP = '/' -local LUA_OFSEP = '_' -local OLD_LUA_OFSEP = '' -local POF = 'luaopen_' - -local assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type = assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type -local format, gfind, gsub = string.format, string.gfind, string.gsub - --- --- avoid overwriting the package table if it's already there --- -package = package or {} - -package.path = LUA_PATH or os.getenv("LUA_PATH") or - ("./?.lua;" .. - "/usr/local/share/lua/5.0/?.lua;" .. - "/usr/local/share/lua/5.0/?/?.lua;" .. - "/usr/local/share/lua/5.0/?/init.lua" ) - -package.cpath = os.getenv("LUA_CPATH") or - "./?.so;" .. - "./l?.so;" .. - "/usr/local/lib/lua/5.0/?.so;" .. - "/usr/local/lib/lua/5.0/l?.so" - --- --- make sure require works with standard libraries --- -package.loaded = package.loaded or {} -package.loaded.string = string -package.loaded.math = math -package.loaded.io = io -package.loaded.os = os -package.loaded.table = table -package.loaded.base = _G -package.loaded.coroutine = coroutine - --- --- avoid overwriting the package.preload table if it's already there --- -package.preload = package.preload or {} - - --- --- auxiliar function to read "nested globals" --- -local function getfield (t, f) - assert (type(f)=="string", "not a valid field name ("..tostring(f)..")") - for w in gfind(f, "[%w_]+") do - if not t then return nil end - t = rawget(t, w) - end - return t -end - - --- --- auxiliar function to write "nested globals" --- -local function setfield (t, f, v) - for w in gfind(f, "([%w_]+)%.") do - t[w] = t[w] or {} -- create table if absent - t = t[w] -- get the table - end - local w = gsub(f, "[%w_]+%.", "") -- get last field name - t[w] = v -- do the assignment -end - - --- --- looks for a file `name' in given path --- -local function search (path, name) - for c in gfind(path, "[^;]+") do - c = gsub(c, "%?", name) - local f = io.open(c) - if f then -- file exist? - f:close() - return c - end - end - return nil -- file not found -end - - --- --- check whether library is already loaded --- -local function loader_preload (name) - assert (type(name) == "string", format ( - "bad argument #1 to `require' (string expected, got %s)", type(name))) - if type(package.preload) ~= "table" then - error ("`package.preload' must be a table") - end - return package.preload[name] -end - - --- --- C library loader --- -local function loader_C (name) - assert (type(name) == "string", format ( - "bad argument #1 to `require' (string expected, got %s)", type(name))) - local fname = gsub (name, "%.", LUA_DIRSEP) - fname = search (package.cpath, fname) - if not fname then - return false - end - local funcname = POF .. gsub (name, "%.", LUA_OFSEP) - local f, err = loadlib (fname, funcname) - if not f then - funcname = POF .. gsub (name, "%.", OLD_LUA_OFSEP) - f, err = loadlib (fname, funcname) - if not f then - error (format ("error loading package `%s' (%s)", name, err)) - end - end - return f -end - - --- --- Lua library loader --- -local function loader_Lua (name) - assert (type(name) == "string", format ( - "bad argument #1 to `require' (string expected, got %s)", type(name))) - local path = LUA_PATH - if not path then - path = assert (package.path, "`package.path' must be a string") - end - local fname = gsub (name, "%.", LUA_DIRSEP) - fname = search (path, fname) - if not fname then - return false - end - local f, err = loadfile (fname) - if not f then - error (format ("error loading package `%s' (%s)", name, err)) - end - return f -end - - --- create `loaders' table -package.loaders = package.loaders or { loader_preload, loader_C, loader_Lua, } - - --- --- iterate over available loaders --- -local function load (name, loaders) - -- iterate over available loaders - assert (type (loaders) == "table", "`package.loaders' must be a table") - for i, loader in ipairs (loaders) do - local f = loader (name) - if f then - return f - end - end - error (format ("package `%s' not found", name)) -end - - --- --- new require --- -function _G.require (name) - assert (type(name) == "string", format ( - "bad argument #1 to `require' (string expected, got %s)", type(name))) - local p = loaded[name] -- is it there? - if p then - return p - end - -- first mark it as loaded - loaded[name] = true - -- load and run init function - local actual_arg = _G.arg - _G.arg = { name } - local res = load(name, loaders)(name) - if res then - loaded[name] = res -- store result - end - _G.arg = actual_arg - -- return value should be in loaded[name] - return loaded[name] -end - - --- --- new module function --- -function _G.module (name) - local _G = getfenv(0) -- simulate C function environment - local ns = getfield(_G, name) -- search for namespace - if not ns then - ns = {} -- create new namespace - setfield(_G, name, ns) - elseif type(ns) ~= "table" then - error("name conflict for module `"..name.."'") - end - if not ns._NAME then - ns._NAME = name - ns._M = ns - ns._PACKAGE = gsub(name, "[^.]*$", "") - end - setmetatable(ns, {__index = _G}) - loaded[name] = ns - setfenv(2, ns) - return ns -end - - --- --- define functions' environments --- -local env = { - loaded = package.loaded, - loaders = package.loaders, - package = package, - _G = _G, -} -for i, f in ipairs { _G.module, _G.require, load, loader_preload, loader_C, loader_Lua, } do - setfenv (f, env) -end From d8388053be3ad8e198c18e7dd88da6ca60d21e0f Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Tue, 22 Aug 2006 00:55:02 +0000 Subject: [PATCH 07/21] cleaning out files --- LICENSE | 19 --------------- README | 74 --------------------------------------------------------- 2 files changed, 93 deletions(-) delete mode 100755 LICENSE delete mode 100755 README diff --git a/LICENSE b/LICENSE deleted file mode 100755 index fc89efc..0000000 --- a/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2006 Keith Howe - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/README b/README deleted file mode 100755 index c870e77..0000000 --- a/README +++ /dev/null @@ -1,74 +0,0 @@ -1. INTRODUCTION - -This project provides Lua bindings for the OpenSSL libcrypto libraries. -libcrypto provides digest functions, such as MD5, SHA-1, and HMAC; cipher -functions, such as RC5 and Blowfish; public key crypto, such as RSA and -Diffie-Hellman; and some other assorted tools (i.e. random number generation). - -Currently only the digest functions are mapped by this Lua binding. - -http://luacrypto.luaforge.net/ - by Keith Howe - -2. REQUIREMENTS - - - Lua 5.x - http://www.lua.org/ - - luacrypto uses the new Lua 5.1 package system. It will also work with - Lua 5.0 using the included Compat-5.1 emulation system. - - - libcrypto 0.9.7+ - http://www.openssl.org/ - - luacrypto has only been built against and tested with OpenSSL versions - 0.9.7 and above. It depends on additional API methods introduced in 0.9.7. - -3. INSTALLATION - -Edit the "config" file and set the fields to the correct values for your -environment: - - LUAINC: The location of your Lua include headers. This is required to build - the code. - - LUA_PATH: The installation path for your system independent Lua files. - LUA_CPATH: The installation path for your system dependent Lua files. These - two settings are required to install the libraries properly. - - USE_COMPAT: Set this if your target environment is Lua 5.0. If your target is - Lua 5.1, leave this blank. - INSTALL_COMPAT: If you have set USE_COMPAT, you should also set this value to - install the compat-5.1.lua module during "make install". However, if you - already have the module installed (and possibly customised), leave this - unset to keep it from being clobbered during "make install". - - (Note that the compat-5.1.lua module must be loaded before any wrapped - modules will work. See http://www.keplerproject.org/compat for details). - - LUA_TYPE: Set this to the type of Lua libraries you would like to install; - either "lc" for precompiled Lua libraries (using luac), or "lua" for - regular Lua libraries. - - LUA: The location of your Lua interpreter; only needed to run the tests. - LUAC: The location of your Lua compiler; only needed to build the - precompiled Lua libraries. - - CRYPTOLIB: On most systems, the default value will be fine. However, if your - OpenSSL libcrypto library is not in the standard library paths, you may - need to point this at your OpenSSL install. - CRYPTOINC: On most systems, the default value will be fine. However, if your - OpenSSL libcrypto headers are not in the standard include paths, you may - need to point this at your OpenSSL install. - -Once configured, run "make" to build the libraries. You can then run -"make tests" to run a test suite if you have set the location of your Lua -interpreter. Run "make install" to install the libraries in the correct -location. - -4. CREDITS - -Much of this code was heavily inspired by and/or lifted directly from the -lmd5 project (http://luaforge.net/projects/lmd5/), written by -Luiz Henrique de Figueiredo . - -The Compat-5.1 library (http://www.keplerproject.org/compat/) written by -the folks at the Kepler Project is also included for Lua 5.0 support. From 9ac35f0078ea27897b704e647d72f28b521d6dfa Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Tue, 22 Aug 2006 19:18:42 +0000 Subject: [PATCH 08/21] adding rand support and collapsing into a kepler-ized format --- Makefile | 136 ++----------- config | 45 ++--- src/crypto.c | 64 ------- src/crypto.h | 29 --- src/crypto.lua | 28 --- src/evp.c | 214 --------------------- src/evp.h | 30 --- src/evp.lua | 26 --- src/hmac.c | 207 -------------------- src/hmac.h | 30 --- src/hmac.lua | 26 --- src/lcrypto.c | 508 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lcrypto.h | 24 +++ tests/test.lua | 9 +- 14 files changed, 570 insertions(+), 806 deletions(-) delete mode 100755 src/crypto.c delete mode 100755 src/crypto.h delete mode 100755 src/crypto.lua delete mode 100755 src/evp.c delete mode 100755 src/evp.h delete mode 100755 src/evp.lua delete mode 100755 src/hmac.c delete mode 100755 src/hmac.h delete mode 100755 src/hmac.lua create mode 100755 src/lcrypto.c create mode 100755 src/lcrypto.h diff --git a/Makefile b/Makefile index 71d55d7..1239aca 100755 --- a/Makefile +++ b/Makefile @@ -1,129 +1,25 @@ -# include in user config or use default values instead --include ./config -LUA_PATH ?= /usr/share/lua -LUA_CPATH ?= /usr/local/lua -LUAINC ?= /usr/include/lua -LUA ?= /usr/bin/lua -LUAC ?= /usr/bin/luac -CRYPTOLIB ?= -lcrypto -LUATYPE ?= lc +T= lcrypto +V= 0.2.0 +CONFIG= ./config -# other default stuff that generally won't change -.SUFFIXES: -srcdir := ./src -outdir := ./obj -tstdir := ./tests -INSTALL = install -SHELL = /bin/sh -MODULES = evp hmac -COMPAT_DIR = contrib/compat-5.1r4 -COMPAT_O = $(if $(USE_COMPAT),$(outdir)/compat-5.1.o) -CFLAGS = $(CRYPTOINC) $(LUAINC) $(if $(USE_COMPAT),-I$(COMPAT_DIR) -DUSE_COMPAT) -ansi -pedantic -Wall -O2 -SOOBJS = $(outdir)/crypto/core.so $(foreach module,$(MODULES),$(outdir)/crypto/$(module)/core.so) -LCOBJS = $(outdir)/crypto.$(LUATYPE) $(foreach module,$(MODULES),$(outdir)/crypto/$(module).$(LUATYPE)) -LDFLAGS = $(CRYPTOLIB) +include $(CONFIG) -# default target -.PHONY: all $(outdir) -all: $(outdir) $(SOOBJS) $(LCOBJS) +OBJS= $(COMPAT_DIR)/compat-5.1.o src/$T.o +SRCS= src/$T.h src/$T.c -# rule to create build directory -$(outdir): - @if [ ! -d $(outdir) ]; then $(INSTALL) -d $(outdir); fi - @if [ ! -d $(outdir)/crypto ]; then $(INSTALL) -d $(outdir); fi - @$(foreach module,$(MODULES),if [ ! -d $(outdir)/crypto/$(module) ]; then $(INSTALL) -d $(outdir)/crypto/$(module); fi ; ) -# rules for building the final so's -$(outdir)/%/core.so: $(outdir)/%.o $(COMPAT_O) - $(CC) $(LDFLAGS) -shared $< $(COMPAT_O) -o $@ +lib: src/$(LIBNAME) -$(outdir)/crypto/%/core.so: $(outdir)/%.o $(COMPAT_O) - $(CC) $(LDFLAGS) -shared $< $(COMPAT_O) -o $@ +src/$(LIBNAME): $(OBJS) + export MACOSX_DEPLOYMENT_TARGET="10.3"; $(CC) $(CFLAGS) $(LIB_OPTION) -o src/$(LIBNAME) $(OBJS) $(OPENSSL_LIBS) -# rules for building intermediary objects -$(outdir)/%.o: $(srcdir)/%.c $(srcdir)/%.h - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +$(COMPAT_DIR)/compat-5.1.o: $(COMPAT_DIR)/compat-5.1.c + $(CC) -c $(CFLAGS) -o $@ $(COMPAT_DIR)/compat-5.1.c -$(outdir)/compat-5.1.o: $(COMPAT_DIR)/compat-5.1.c - $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +install: src/$(LIBNAME) + mkdir -p $(LUA_LIBDIR) + cp src/$(LIBNAME) $(LUA_LIBDIR) + cd $(LUA_LIBDIR); ln -f -s $(LIBNAME) $T.so -# rules for building the final lua/lc files -$(outdir)/%.lc: $(srcdir)/%.lua - $(LUAC) $(LUACFLAGS) -o $@ $< - -$(outdir)/crypto/%.lc: $(srcdir)/%.lua - $(LUAC) $(LUACFLAGS) -o $@ $< - -$(outdir)/%.lua: $(srcdir)/%.lua - cp $< $@ - -$(outdir)/crypto/%.lua: $(srcdir)/%.lua - cp $< $@ - -# cleanup rules -.PHONY: clean distclean mostlyclean clean: - rm -fr $(outdir) - rm -f ./core ./core.* - -distclean: clean ; - -mostlyclean: - rm -fr $(outdir)/* - rm -f ./core ./core.* - -# template for generating un/install rules -define INSTALL_TEMPLATE -$(1)_install: $(outdir)/crypto/$(1)/core.so $(outdir)/crypto/$(1).$(LUATYPE) - $(INSTALL) -d $(LUA_CPATH)/crypto/$(1) - $(INSTALL) -D $(outdir)/crypto/$(1)/core.so $(LUA_CPATH)/crypto/$(1)/core.so - $(INSTALL) -D $(outdir)/crypto/$(1).$(LUATYPE) $(LUA_PATH)/crypto/$(1).$(LUATYPE) - -$(1)_uninstall: - -rm $(LUA_CPATH)/crypto/$(1)/core.so - -rm -r $(LUA_CPATH)/crypto/$(1)/ - -rm $(LUA_PATH)/crypto/$(1).$(LUATYPE) -endef - -# install rules -$(foreach module,$(MODULES),$(eval $(call INSTALL_TEMPLATE,$(module)))) - -.PHONY: install crypto_install uninstall crypto_uninstall - -crypto_install: - $(INSTALL) -d $(LUA_PATH)/crypto/ - $(INSTALL) -d $(LUA_CPATH)/crypto/ - $(INSTALL) -D $(outdir)/crypto/core.so $(LUA_CPATH)/crypto/core.so - $(INSTALL) -D $(outdir)/crypto.$(LUATYPE) $(LUA_CPATH)/crypto.$(LUATYPE) - -compat_install: - $(INSTALL) -D $(COMPAT_DIR)/compat-5.1.lua $(LUA_PATH)/compat-5.1.lua - -install: crypto_install $(foreach module,$(MODULES),$(module)_install) $(if $(INSTALL_COMPAT),compat_install) ; - -# uninstall rules -crypto_uninstall: - -rm $(LUA_CPATH)/crypto/core.so - -rm -r $(LUA_CPATH)/crypto/ - -rm -r $(LUA_PATH)/crypto/ - -rm $(LUA_PATH)/crypto.$(LUATYPE) - -uninstall: $(foreach module,$(MODULES),$(module)_uninstall) crypto_uninstall - -# rule to run the test suite -.PHONY: tests -tests: - @echo "" - @if [ -x $(LUA) ]; then \ - echo "Running tests..."; \ - LUA_PATH="$(outdir)/?.$(LUATYPE)"; \ - LUA_CPATH="$(outdir)/?.so"; \ - $(if $(USE_COMPAT),LUA_INIT="@$(COMPAT_DIR)/compat-5.1.lua"; export LUA_INIT;) \ - export LUA_PATH LUA_CPATH; \ - $(LUA) $(tstdir)/test.lua $(tstdir)/message; \ - else \ - echo "Lua interpreter not found; not running tests."; \ - echo "Set LUA to your interpreter location and execute"; \ - echo "'make tests' to run the test suite."; \ - fi - @echo "" + rm -f src/$(LIBNAME) $(OBJS) $(COMPAT_O) diff --git a/config b/config index fecbb29..5c446e0 100755 --- a/config +++ b/config @@ -1,32 +1,21 @@ -# Change these to reflect your Lua installation locations. The system -# independent Lua files (LUA_PATH), the system dependent Lua files (LUA_CPATH), -# and the Lua include files (LUAINC) are required to compile and install. Set -# USE_COMPAT to 1 if you building for a Lua 5.0 environment, or leave it blank -# if you are building for a Lua 5.1 environment. -LUA_PATH = /usr/share/lua -LUA_CPATH = /usr/local/lua -LUAINC = -I/usr/include/lua +# Installation directories +# System's libraries directory (where binary libraries are installed) +LUA_LIBDIR= /usr/local/lib/lua/5.0 +# Lua includes directory +LUA_INC= /usr/local/include -# If your target environment is Lua 5.1, leave both of these COMPAT flags unset. -# If your target environment is Lua 5.0, the USE_COMPAT flag must be set to 1 -# to use the Compat-5.1 module emulation. If you already have Compat-5.1 -# installed in your LUA_PATH and don't want to overwrite it, set INSTALL_COMPAT -# to a blank value; otherwise, it should be set to 1 if USE_COMPAT is set. See -# http://www.keplerproject.org/compat/ for more details about Compat-5.1. -USE_COMPAT = -INSTALL_COMPAT = +# OS dependent +LIB_OPTION= -shared #for Linux +#LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X -# The location of the Lua interpreter and Lua compiler are required to run the -# test suite and to generate precompiled Lua libraries instead of source Lua -# libraries for install. -LUA = /usr/bin/lua -LUAC = /usr/bin/luac +LIBNAME= $T.so.$V +COMPAT_DIR= ../compat/src -# This provides the necessary flags for linking against your OpenSSL libcrypto -# installation. Change it to suit your system if necessary. -CRYPTOLIB = -lcrypto -CRYPTOINC = +OPENSSL_LIBS= -L/usr/local/openssl/lib -lcrypto -lssl +OPENSSL_INCS= -I/usr/local/openssl/include -# Set this to lc to install the precompiled Lua libraries (compiled with luac), -# or to lua to install the source Lua libraries. -LUATYPE = lc +# Compilation directives +WARN= -O2 -Wall -fPIC -W -Waggregate-return -Wcast-align -Wmissing-prototypes -Wnested-externs -Wshadow -Wwrite-strings +INCS= -I$(LUA_INC) -I$(COMPAT_DIR) +CFLAGS= $(WARN) $(OPENSSL_INCS) $(INCS) +CC= gcc diff --git a/src/crypto.c b/src/crypto.c deleted file mode 100755 index 0303a91..0000000 --- a/src/crypto.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2006 Keith Howe - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - */ - -#include - -#include "crypto.h" - -#include "lua.h" -#include "lauxlib.h" -#ifdef USE_COMPAT - #include "compat-5.1.h" -#endif - -/* code registered as functions */ -static const luaL_reg f[] = -{ - { NULL, NULL } -}; - -/* code registered as methods */ -static const luaL_reg m[] = -{ - { NULL, NULL } -}; - -extern int luaopen_crypto_core(lua_State *L) -{ - OpenSSL_add_all_digests(); - - luaL_newmetatable(L, CRYPTO_TYPE); - - lua_pushstring(L, "__index"); - lua_pushvalue(L, -2); - lua_settable(L, -3); - - luaL_openlib(L, NULL, m, 0); - luaL_openlib(L, CRYPTO_NAME, f, 0); - - lua_pushliteral(L, "version"); - lua_pushliteral(L, CRYPTO_VERSION); - lua_settable(L, -3); - - return 1; -} diff --git a/src/crypto.h b/src/crypto.h deleted file mode 100755 index b1754ff..0000000 --- a/src/crypto.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2006 Keith Howe - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - */ - -#include - -#define CRYPTO_TAG " library for " LUA_VERSION " / 0.1.0 / using openssl" -#define CRYPTO_NAME "crypto" -#define CRYPTO_VERSION CRYPTO_NAME CRYPTO_TAG -#define CRYPTO_TYPE CRYPTO_NAME " context" diff --git a/src/crypto.lua b/src/crypto.lua deleted file mode 100755 index 7abcdc6..0000000 --- a/src/crypto.lua +++ /dev/null @@ -1,28 +0,0 @@ ---[[ --- Copyright (c) 2006 Keith Howe --- --- Permission is hereby granted, free of charge, to any person obtaining a --- copy of this software and associated documentation files (the "Software"), --- to deal in the Software without restriction, including without limitation --- the rights to use, copy, modify, merge, publish, distribute, sublicense, --- and/or sell copies of the Software, and to permit persons to whom the --- Software is furnished to do so, subject to the following conditions: --- --- The above copyright notice and this permission notice shall be included --- in all copies or substantial portions of the Software. --- --- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING --- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER --- DEALINGS IN THE SOFTWARE. --- ---]] - ---- luacrypto module --- This module does stuff. - -local crypto = require("crypto.core") -module("crypto") diff --git a/src/evp.c b/src/evp.c deleted file mode 100755 index 2973907..0000000 --- a/src/evp.c +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (c) 2006 Keith Howe - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - */ - -#include - -#include "evp.h" - -#include "lua.h" -#include "lauxlib.h" -#ifdef USE_COMPAT - #include "compat-5.1.h" -#endif - -static EVP_MD_CTX *Pget(lua_State *L, int i) -{ - if (luaL_checkudata(L, i, EVP_TYPE) == NULL) luaL_typerror(L, i, EVP_TYPE); - return lua_touserdata(L, i); -} - -static EVP_MD_CTX *Pnew(lua_State *L) -{ - EVP_MD_CTX *c = lua_newuserdata(L, sizeof(EVP_MD_CTX)); - luaL_getmetatable(L, EVP_TYPE); - lua_setmetatable(L, -2); - return c; -} - -static int Lnew(lua_State *L) /** new(type) */ -{ - EVP_MD_CTX *c = NULL; - const char *s = luaL_checkstring(L, 1); - const EVP_MD *type = EVP_get_digestbyname(s); - - if (type == NULL) { - luaL_argerror(L, 1, "invalid digest type"); - return 0; - } - - c = Pnew(L); - EVP_MD_CTX_init(c); - EVP_DigestInit_ex(c, type, NULL); - - return 1; -} - -static int Lclone(lua_State *L) /** clone(c) */ -{ - EVP_MD_CTX *c = Pget(L, 1); - EVP_MD_CTX *d = Pnew(L); - EVP_MD_CTX_init(d); - EVP_MD_CTX_copy_ex(d, c); - return 1; -} - -static int Lreset(lua_State *L) /** reset(c) */ -{ - EVP_MD_CTX *c = Pget(L, 1); - const EVP_MD *t = EVP_MD_CTX_md(c); - EVP_MD_CTX_cleanup(c); - EVP_MD_CTX_init(c); - EVP_DigestInit_ex(c, t, NULL); - return 0; -} - -static int Lupdate(lua_State *L) /** update(c, s) */ -{ - EVP_MD_CTX *c = Pget(L, 1); - const char *s = luaL_checkstring(L, 2); - - EVP_DigestUpdate(c, s, lua_strlen(L, 2)); - - return 0; -} - -static int Ldigest(lua_State *L) /** digest(c, s, [raw]) */ -{ - EVP_MD_CTX *c = Pget(L, 1); - EVP_MD_CTX *d = NULL; - unsigned char digest[EVP_MAX_MD_SIZE]; - size_t written = 0; - - if (lua_isstring(L, 2)) - { - const char *s = luaL_checkstring(L, 2); - EVP_DigestUpdate(c, s, lua_strlen(L, 2)); - } - - d = EVP_MD_CTX_create(); - EVP_MD_CTX_copy_ex(d, c); - EVP_DigestFinal_ex(d, digest, &written); - EVP_MD_CTX_destroy(d); - - if (lua_toboolean(L, 3)) - lua_pushlstring(L, (char *)digest, written); - else - { - int i; - char *hex; - hex = calloc(sizeof(char), written*2 + 1); - for (i = 0; i < written; i++) - sprintf(hex + 2*i, "%02x", digest[i]); - lua_pushlstring(L, hex, written*2); - free(hex); - } - - return 1; -} - -static int Ltostring(lua_State *L) /** tostring(c) */ -{ - EVP_MD_CTX *c = Pget(L, 1); - char s[64]; - sprintf(s, "%s %p", EVP_NAME, (void *)c); - lua_pushstring(L, s); - return 1; -} - -static int Lgc(lua_State *L) -{ - EVP_MD_CTX *c = Pget(L, 1); - EVP_MD_CTX_cleanup(c); - return 1; -} - -static int Lfdigest(lua_State *L) /** digest(type, s, [raw]) */ -{ - EVP_MD_CTX *c = NULL; - const char *type_name = luaL_checkstring(L, 1); - const char *s = luaL_checkstring(L, 2); - const EVP_MD *type = EVP_get_digestbyname(type_name); - unsigned char digest[EVP_MAX_MD_SIZE]; - size_t written = 0; - - if (type == NULL) { - luaL_argerror(L, 1, "invalid digest type"); - return 0; - } - - c = EVP_MD_CTX_create(); - EVP_DigestInit_ex(c, type, NULL); - EVP_DigestUpdate(c, s, lua_strlen(L, 2)); - EVP_DigestFinal_ex(c, digest, &written); - - if (lua_toboolean(L, 3)) - lua_pushlstring(L, (char *)digest, written); - else - { - int i; - char *hex; - hex = calloc(sizeof(char), written*2 + 1); - for (i = 0; i < written; i++) - sprintf(hex + 2*i, "%02x", digest[i]); - lua_pushlstring(L, hex, written*2); - free(hex); - } - - return 1; -} - -static const luaL_reg f[] = -{ - { "digest", Lfdigest }, - { "new", Lnew }, - { NULL, NULL } -}; - -static const luaL_reg m[] = -{ - { "__tostring", Ltostring }, - { "__gc", Lgc }, - { "clone", Lclone }, - { "digest", Ldigest }, - { "reset", Lreset }, - { "tostring", Ltostring }, - { "update", Lupdate }, - { NULL, NULL } -}; - -extern int luaopen_crypto_evp_core(lua_State *L) -{ - luaL_newmetatable(L, EVP_TYPE); - lua_pushstring(L, "__index"); - lua_pushvalue(L, -2); - lua_settable(L, -3); - - luaL_openlib(L, NULL, m, 0); - luaL_openlib(L, EVP_NAME, f, 0); - - lua_pushliteral(L, "version"); - lua_pushliteral(L, EVP_VERSION); - lua_settable(L, -3); - - return 1; -} diff --git a/src/evp.h b/src/evp.h deleted file mode 100755 index 432bddd..0000000 --- a/src/evp.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2006 Keith Howe - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - */ - -#include - -#include "crypto.h" - -#define EVP_NAME "crypto.evp" -#define EVP_VERSION EVP_NAME CRYPTO_TAG -#define EVP_TYPE EVP_NAME " context" diff --git a/src/evp.lua b/src/evp.lua deleted file mode 100755 index 576aefb..0000000 --- a/src/evp.lua +++ /dev/null @@ -1,26 +0,0 @@ ---[[ --- Copyright (c) 2006 Keith Howe --- --- Permission is hereby granted, free of charge, to any person obtaining a --- copy of this software and associated documentation files (the "Software"), --- to deal in the Software without restriction, including without limitation --- the rights to use, copy, modify, merge, publish, distribute, sublicense, --- and/or sell copies of the Software, and to permit persons to whom the --- Software is furnished to do so, subject to the following conditions: --- --- The above copyright notice and this permission notice shall be included --- in all copies or substantial portions of the Software. --- --- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING --- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER --- DEALINGS IN THE SOFTWARE. --- ---]] - -local evp = require("crypto.evp.core") -local crypto = require("crypto") -module("crypto.evp") diff --git a/src/hmac.c b/src/hmac.c deleted file mode 100755 index 7e8596a..0000000 --- a/src/hmac.c +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (c) 2006 Keith Howe - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - */ - -#include - -#include "hmac.h" - -#include "lua.h" -#include "lauxlib.h" -#ifdef USE_COMPAT - #include "compat-5.1.h" -#endif - -static HMAC_CTX *Pget(lua_State *L, int i) -{ - if (luaL_checkudata(L, i, HMAC_TYPE) == NULL) luaL_typerror(L, i, HMAC_TYPE); - return lua_touserdata(L, i); -} - -static HMAC_CTX *Pnew(lua_State *L) -{ - HMAC_CTX *c = lua_newuserdata(L, sizeof(HMAC_CTX)); - luaL_getmetatable(L, HMAC_TYPE); - lua_setmetatable(L, -2); - return c; -} - -static int Lnew(lua_State *L) /** new(type, key) */ -{ - HMAC_CTX *c = Pnew(L); - const char *s = luaL_checkstring(L, 1); - const char *k = luaL_checkstring(L, 2); - const EVP_MD *type = EVP_get_digestbyname(s); - - if (type == NULL) { - luaL_argerror(L, 1, "invalid digest type"); - return 0; - } - - HMAC_CTX_init(c); - HMAC_Init_ex(c, k, lua_strlen(L, 2), type, NULL); - - return 1; -} - -static int Lclone(lua_State *L) /** clone(c) */ -{ - HMAC_CTX *c = Pget(L, 1); - HMAC_CTX *d = Pnew(L); - *d = *c; - return 1; -} - -static int Lreset(lua_State *L) /** reset(c) */ -{ - HMAC_CTX *c = Pget(L, 1); - HMAC_Init_ex(c, NULL, 0, NULL, NULL); - return 0; -} - -static int Lupdate(lua_State *L) /** update(c, s) */ -{ - HMAC_CTX *c = Pget(L, 1); - const char *s = luaL_checkstring(L, 2); - - HMAC_Update(c, (unsigned char *)s, lua_strlen(L, 2)); - - return 0; -} - -static int Ldigest(lua_State *L) /** digest(c, s, [raw]) */ -{ - HMAC_CTX *c = Pget(L, 1); - unsigned char digest[EVP_MAX_MD_SIZE]; - size_t written = 0; - - if (lua_isstring(L, 2)) - { - const char *s = luaL_checkstring(L, 2); - HMAC_Update(c, (unsigned char *)s, lua_strlen(L, 2)); - } - - HMAC_Final(c, digest, &written); - - if (lua_toboolean(L, 3)) - lua_pushlstring(L, (char *)digest, written); - else - { - int i; - char *hex; - hex = calloc(sizeof(char), written*2 + 1); - for (i = 0; i < written; i++) - sprintf(hex + 2*i, "%02x", digest[i]); - lua_pushlstring(L, hex, written*2); - free(hex); - } - - return 1; -} - -static int Ltostring(lua_State *L) /** tostring(c) */ -{ - HMAC_CTX *c = Pget(L, 1); - char s[64]; - sprintf(s, "%s %p", HMAC_NAME, (void *)c); - lua_pushstring(L, s); - return 1; -} - -static int Lgc(lua_State *L) -{ - HMAC_CTX *c = Pget(L, 1); - HMAC_CTX_cleanup(c); - return 1; -} - -static int Lfdigest(lua_State *L) /** digest(type, s, key, [raw]) */ -{ - HMAC_CTX c; - unsigned char digest[EVP_MAX_MD_SIZE]; - size_t written = 0; - const char *t = luaL_checkstring(L, 1); - const char *s = luaL_checkstring(L, 2); - const char *k = luaL_checkstring(L, 3); - const EVP_MD *type = EVP_get_digestbyname(t); - - if (type == NULL) { - luaL_argerror(L, 1, "invalid digest type"); - return 0; - } - - HMAC_CTX_init(&c); - HMAC_Init_ex(&c, k, lua_strlen(L, 3), type, NULL); - HMAC_Update(&c, (unsigned char *)s, lua_strlen(L, 2)); - HMAC_Final(&c, digest, &written); - - if (lua_toboolean(L, 4)) - lua_pushlstring(L, (char *)digest, written); - else - { - int i; - char *hex; - hex = calloc(sizeof(char), written*2 + 1); - for (i = 0; i < written; i++) - sprintf(hex + 2*i, "%02x", digest[i]); - lua_pushlstring(L, hex, written*2); - free(hex); - } - - return 1; -} - -static const luaL_reg f[] = -{ - { "new", Lnew }, - { "digest", Lfdigest }, - { NULL, NULL } -}; - -static const luaL_reg m[] = -{ - { "__tostring", Ltostring }, - { "__gc", Lgc }, - { "clone", Lclone }, - { "digest", Ldigest }, - { "reset", Lreset }, - { "tostring", Ltostring }, - { "update", Lupdate }, - { NULL, NULL } -}; - -extern int luaopen_crypto_hmac_core(lua_State *L) -{ - luaL_newmetatable(L, HMAC_TYPE); - lua_pushstring(L, "__index"); - lua_pushvalue(L, -2); - lua_settable(L, -3); - - luaL_openlib(L, NULL, m, 0); - luaL_openlib(L, HMAC_NAME, f, 0); - - lua_pushliteral(L, "version"); - lua_pushliteral(L, HMAC_VERSION); - lua_settable(L, -3); - - return 1; -} diff --git a/src/hmac.h b/src/hmac.h deleted file mode 100755 index 05cb6cb..0000000 --- a/src/hmac.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2006 Keith Howe - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - */ - -#include - -#include "crypto.h" - -#define HMAC_NAME "crypto.hmac" -#define HMAC_VERSION HMAC_NAME CRYPTO_TAG -#define HMAC_TYPE HMAC_NAME " context" diff --git a/src/hmac.lua b/src/hmac.lua deleted file mode 100755 index 788c9b8..0000000 --- a/src/hmac.lua +++ /dev/null @@ -1,26 +0,0 @@ ---[[ --- Copyright (c) 2006 Keith Howe --- --- Permission is hereby granted, free of charge, to any person obtaining a --- copy of this software and associated documentation files (the "Software"), --- to deal in the Software without restriction, including without limitation --- the rights to use, copy, modify, merge, publish, distribute, sublicense, --- and/or sell copies of the Software, and to permit persons to whom the --- Software is furnished to do so, subject to the following conditions: --- --- The above copyright notice and this permission notice shall be included --- in all copies or substantial portions of the Software. --- --- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING --- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER --- DEALINGS IN THE SOFTWARE. --- ---]] - -local hmac = require("crypto.hmac.core") -local crypto = require("crypto") -module("crypto.hmac") diff --git a/src/lcrypto.c b/src/lcrypto.c new file mode 100755 index 0000000..2ebce5b --- /dev/null +++ b/src/lcrypto.c @@ -0,0 +1,508 @@ +/* +** $Id: lcrypto.c,v 1.1 2006-08-22 19:18:42 nezroy Exp $ +** See Copyright Notice in license.html +*/ + +#include +#include +#include +#include +#include + +#include "lua.h" +#include "lauxlib.h" +#include "compat-5.1.h" + +#include "lcrypto.h" + +LUACRYPTO_API int luaopen_crypto(lua_State *L); + +static int crypto_error(lua_State *L) +{ + char buf[120]; + unsigned long e = ERR_get_error(); + ERR_load_crypto_strings(); + lua_pushnil(L); + lua_pushstring(L, ERR_error_string(e, buf)); + return 2; +} + +static EVP_MD_CTX *evp_pget(lua_State *L, int i) +{ + if (luaL_checkudata(L, i, LUACRYPTO_EVPNAME) == NULL) luaL_typerror(L, i, LUACRYPTO_EVPNAME); + return lua_touserdata(L, i); +} + +static EVP_MD_CTX *evp_pnew(lua_State *L) +{ + EVP_MD_CTX *c = lua_newuserdata(L, sizeof(EVP_MD_CTX)); + luaL_getmetatable(L, LUACRYPTO_EVPNAME); + lua_setmetatable(L, -2); + return c; +} + +static int evp_fnew(lua_State *L) +{ + EVP_MD_CTX *c = NULL; + const char *s = luaL_checkstring(L, 1); + const EVP_MD *type = EVP_get_digestbyname(s); + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + c = evp_pnew(L); + EVP_MD_CTX_init(c); + EVP_DigestInit_ex(c, type, NULL); + + return 1; +} + +static int evp_clone(lua_State *L) +{ + EVP_MD_CTX *c = evp_pget(L, 1); + EVP_MD_CTX *d = evp_pnew(L); + EVP_MD_CTX_init(d); + EVP_MD_CTX_copy_ex(d, c); + return 1; +} + +static int evp_reset(lua_State *L) +{ + EVP_MD_CTX *c = evp_pget(L, 1); + const EVP_MD *t = EVP_MD_CTX_md(c); + EVP_MD_CTX_cleanup(c); + EVP_MD_CTX_init(c); + EVP_DigestInit_ex(c, t, NULL); + return 0; +} + +static int evp_update(lua_State *L) +{ + EVP_MD_CTX *c = evp_pget(L, 1); + const char *s = luaL_checkstring(L, 2); + + EVP_DigestUpdate(c, s, lua_strlen(L, 2)); + + lua_settop(L, 1); + return 1; +} + +static int evp_digest(lua_State *L) +{ + EVP_MD_CTX *c = evp_pget(L, 1); + EVP_MD_CTX *d = NULL; + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + unsigned int i; + char *hex; + + if (lua_isstring(L, 2)) + { + const char *s = luaL_checkstring(L, 2); + EVP_DigestUpdate(c, s, lua_strlen(L, 2)); + } + + d = EVP_MD_CTX_create(); + EVP_MD_CTX_copy_ex(d, c); + EVP_DigestFinal_ex(d, digest, &written); + EVP_MD_CTX_destroy(d); + + if (lua_toboolean(L, 3)) + lua_pushlstring(L, (char *)digest, written); + else + { + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static int evp_tostring(lua_State *L) +{ + EVP_MD_CTX *c = evp_pget(L, 1); + char s[64]; + sprintf(s, "%s %p", LUACRYPTO_EVPNAME, (void *)c); + lua_pushstring(L, s); + return 1; +} + +static int evp_gc(lua_State *L) +{ + EVP_MD_CTX *c = evp_pget(L, 1); + EVP_MD_CTX_cleanup(c); + return 1; +} + +static int evp_fdigest(lua_State *L) +{ + EVP_MD_CTX *c = NULL; + const char *type_name = luaL_checkstring(L, 1); + const char *s = luaL_checkstring(L, 2); + const EVP_MD *type = EVP_get_digestbyname(type_name); + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + unsigned int i; + char *hex; + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + c = EVP_MD_CTX_create(); + EVP_DigestInit_ex(c, type, NULL); + EVP_DigestUpdate(c, s, lua_strlen(L, 2)); + EVP_DigestFinal_ex(c, digest, &written); + + if (lua_toboolean(L, 3)) + lua_pushlstring(L, (char *)digest, written); + else + { + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static HMAC_CTX *hmac_pget(lua_State *L, int i) +{ + if (luaL_checkudata(L, i, LUACRYPTO_HMACNAME) == NULL) luaL_typerror(L, i, LUACRYPTO_HMACNAME); + return lua_touserdata(L, i); +} + +static HMAC_CTX *hmac_pnew(lua_State *L) +{ + HMAC_CTX *c = lua_newuserdata(L, sizeof(HMAC_CTX)); + luaL_getmetatable(L, LUACRYPTO_HMACNAME); + lua_setmetatable(L, -2); + return c; +} + +static int hmac_fnew(lua_State *L) +{ + HMAC_CTX *c = hmac_pnew(L); + const char *s = luaL_checkstring(L, 1); + const char *k = luaL_checkstring(L, 2); + const EVP_MD *type = EVP_get_digestbyname(s); + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + HMAC_CTX_init(c); + HMAC_Init_ex(c, k, lua_strlen(L, 2), type, NULL); + + return 1; +} + +static int hmac_clone(lua_State *L) +{ + HMAC_CTX *c = hmac_pget(L, 1); + HMAC_CTX *d = hmac_pnew(L); + *d = *c; + return 1; +} + +static int hmac_reset(lua_State *L) +{ + HMAC_CTX *c = hmac_pget(L, 1); + HMAC_Init_ex(c, NULL, 0, NULL, NULL); + return 0; +} + +static int hmac_update(lua_State *L) +{ + HMAC_CTX *c = hmac_pget(L, 1); + const char *s = luaL_checkstring(L, 2); + + HMAC_Update(c, (unsigned char *)s, lua_strlen(L, 2)); + + lua_settop(L, 1); + return 1; +} + +static int hmac_digest(lua_State *L) +{ + HMAC_CTX *c = hmac_pget(L, 1); + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + unsigned int i; + char *hex; + + if (lua_isstring(L, 2)) + { + const char *s = luaL_checkstring(L, 2); + HMAC_Update(c, (unsigned char *)s, lua_strlen(L, 2)); + } + + HMAC_Final(c, digest, &written); + + if (lua_toboolean(L, 3)) + lua_pushlstring(L, (char *)digest, written); + else + { + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static int hmac_tostring(lua_State *L) +{ + HMAC_CTX *c = hmac_pget(L, 1); + char s[64]; + sprintf(s, "%s %p", LUACRYPTO_HMACNAME, (void *)c); + lua_pushstring(L, s); + return 1; +} + +static int hmac_gc(lua_State *L) +{ + HMAC_CTX *c = hmac_pget(L, 1); + HMAC_CTX_cleanup(c); + return 1; +} + +static int hmac_fdigest(lua_State *L) +{ + HMAC_CTX c; + unsigned char digest[EVP_MAX_MD_SIZE]; + size_t written = 0; + unsigned int i; + char *hex; + const char *t = luaL_checkstring(L, 1); + const char *s = luaL_checkstring(L, 2); + const char *k = luaL_checkstring(L, 3); + const EVP_MD *type = EVP_get_digestbyname(t); + + if (type == NULL) { + luaL_argerror(L, 1, "invalid digest type"); + return 0; + } + + HMAC_CTX_init(&c); + HMAC_Init_ex(&c, k, lua_strlen(L, 3), type, NULL); + HMAC_Update(&c, (unsigned char *)s, lua_strlen(L, 2)); + HMAC_Final(&c, digest, &written); + + if (lua_toboolean(L, 4)) + lua_pushlstring(L, (char *)digest, written); + else + { + hex = calloc(sizeof(char), written*2 + 1); + for (i = 0; i < written; i++) + sprintf(hex + 2*i, "%02x", digest[i]); + lua_pushlstring(L, hex, written*2); + free(hex); + } + + return 1; +} + +static int rand_do_bytes(lua_State *L, int (*bytes)(unsigned char *, int)) +{ + size_t count = luaL_checkint(L, 1); + unsigned char tmp[256], *buf = tmp; + if (count > sizeof tmp) + buf = malloc(count); + if (!buf) + return luaL_error(L, "out of memory"); + else if (!bytes(buf, count)) + return crypto_error(L); + lua_pushlstring(L, (char *)buf, count); + if (buf != tmp) + free(buf); + return 1; +} + +static int rand_bytes(lua_State *L) +{ + return rand_do_bytes(L, RAND_bytes); +} + +static int rand_pseudo_bytes(lua_State *L) +{ + return rand_do_bytes(L, RAND_pseudo_bytes); +} + +static int rand_add(lua_State *L) +{ + size_t num; + const void *buf = luaL_checklstring(L, 1, &num); + double entropy = luaL_optnumber(L, 2, num); + RAND_add(buf, num, entropy); + return 0; +} + +static int rand_status(lua_State *L) +{ + lua_pushboolean(L, RAND_status()); + return 1; +} + +enum { WRITE_FILE_COUNT = 1024 }; +static int rand_load(lua_State *L) +{ + const char *name = luaL_optstring(L, 1, 0); + char tmp[256]; + int n; + if (!name && !(name = RAND_file_name(tmp, sizeof tmp))) + return crypto_error(L); + n = RAND_load_file(name, WRITE_FILE_COUNT); + if (n == 0) + return crypto_error(L); + lua_pushnumber(L, n); + return 1; +} + +static int rand_write(lua_State *L) +{ + const char *name = luaL_optstring(L, 1, 0); + char tmp[256]; + int n; + if (!name && !(name = RAND_file_name(tmp, sizeof tmp))) + return crypto_error(L); + n = RAND_write_file(name); + if (n == 0) + return crypto_error(L); + lua_pushnumber(L, n); + return 1; +} + +static int rand_cleanup(lua_State *L) +{ + RAND_cleanup(); + return 0; +} + +/* +** Create a metatable and leave it on top of the stack. +*/ +LUACRYPTO_API int luacrypto_createmeta (lua_State *L, const char *name, const luaL_reg *methods) { + if (!luaL_newmetatable (L, name)) + return 0; + + /* define methods */ + luaL_openlib (L, NULL, methods, 0); + + /* define metamethods */ + lua_pushliteral (L, "__index"); + lua_pushvalue (L, -2); + lua_settable (L, -3); + + lua_pushliteral (L, "__metatable"); + lua_pushliteral (L, LUACRYPTO_PREFIX"you're not allowed to get this metatable"); + lua_settable (L, -3); + + return 1; +} + +/* +** Create metatables for each class of object. +*/ +static void create_metatables (lua_State *L) +{ + struct luaL_reg evp_functions[] = { + { "digest", evp_fdigest }, + { "new", evp_fnew }, + {NULL, NULL}, + }; + struct luaL_reg evp_methods[] = { + { "__tostring", evp_tostring }, + { "__gc", evp_gc }, + { "clone", evp_clone }, + { "digest", evp_digest }, + { "reset", evp_reset }, + { "tostring", evp_tostring }, + { "update", evp_update }, + {NULL, NULL}, + }; + struct luaL_reg hmac_functions[] = { + { "digest", hmac_fdigest }, + { "new", hmac_fnew }, + { NULL, NULL } + }; + struct luaL_reg hmac_methods[] = { + { "__tostring", hmac_tostring }, + { "__gc", hmac_gc }, + { "clone", hmac_clone }, + { "digest", hmac_digest }, + { "reset", hmac_reset }, + { "tostring", hmac_tostring }, + { "update", hmac_update }, + { NULL, NULL } + }; + struct luaL_reg rand_functions[] = { + { "bytes", rand_bytes }, + { "pseudo_bytes", rand_pseudo_bytes }, + { "add", rand_add }, + { "seed", rand_add }, + { "status", rand_status }, + { "load", rand_load }, + { "write", rand_write }, + { "cleanup", rand_cleanup }, + { NULL, NULL } + }; + + luaL_openlib (L, LUACRYPTO_EVPNAME, evp_functions, 0); + luacrypto_createmeta(L, LUACRYPTO_EVPNAME, evp_methods); + luaL_openlib (L, LUACRYPTO_HMACNAME, hmac_functions, 0); + luacrypto_createmeta(L, LUACRYPTO_HMACNAME, hmac_methods); + luaL_openlib (L, LUACRYPTO_RANDNAME, rand_functions, 0); + lua_pop (L, 3); +} + +/* +** Define the metatable for the object on top of the stack +*/ +LUACRYPTO_API void luacrypto_setmeta (lua_State *L, const char *name) { + luaL_getmetatable (L, name); + lua_setmetatable (L, -2); +} + +/* +** Assumes the table is on top of the stack. +*/ +LUACRYPTO_API void luacrypto_set_info (lua_State *L) { + lua_pushliteral (L, "_COPYRIGHT"); + lua_pushliteral (L, "Copyright (C) 2005-2006 Keith Howe"); + lua_settable (L, -3); + lua_pushliteral (L, "_DESCRIPTION"); + lua_pushliteral (L, "LuaCrypto is a Lua wrapper for OpenSSL"); + lua_settable (L, -3); + lua_pushliteral (L, "_VERSION"); + lua_pushliteral (L, "LuaCrypto 0.2.0"); + lua_settable (L, -3); +} + +/* +** Creates the metatables for the objects and registers the +** driver open method. +*/ +LUACRYPTO_API int luaopen_crypto(lua_State *L) +{ + OpenSSL_add_all_digests(); + + struct luaL_reg core[] = { + {NULL, NULL}, + }; + create_metatables (L); + luaL_openlib (L, LUACRYPTO_CORENAME, core, 0); + luacrypto_set_info (L); + return 1; +} diff --git a/src/lcrypto.h b/src/lcrypto.h new file mode 100755 index 0000000..31d9473 --- /dev/null +++ b/src/lcrypto.h @@ -0,0 +1,24 @@ +/* +** $Id: lcrypto.h,v 1.1 2006-08-22 19:18:42 nezroy Exp $ +** See Copyright Notice in license.html +*/ + +#ifndef _LUACRYPTO_ +#define _LUACRYPTO_ + +#ifndef LUACRYPTO_API +#define LUACRYPTO_API +#endif + +#define LUACRYPTO_PREFIX "LuaCrypto: " +#define LUACRYPTO_CORENAME "crypto" +#define LUACRYPTO_EVPNAME "crypto.evp" +#define LUACRYPTO_HMACNAME "crypto.hmac" +#define LUACRYPTO_RANDNAME "crypto.rand" + +LUACRYPTO_API int luacrypto_createmeta (lua_State *L, const char *name, const luaL_reg *methods); +LUACRYPTO_API void luacrypto_setmeta (lua_State *L, const char *name); +LUACRYPTO_API void luacrypto_set_info (lua_State *L); + + +#endif diff --git a/tests/test.lua b/tests/test.lua index 4cf8413..0b0e347 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -1,3 +1,4 @@ +#!/usr/local/bin/lua50 --[[ -- Copyright (c) 2006 Keith Howe -- @@ -21,15 +22,15 @@ -- --]] -local evp = require("crypto.evp") -local hmac = require("crypto.hmac") +require("crypto") +local evp = crypto.evp +local hmac = crypto.hmac md5_KNOWN = "09920f6f666f8e7b09a8d00bd4d06873" sha1_KNOWN = "d6ed6e26ebeb37ba0792ec75a3d0b4dcec279d25" hmac_KNOWN = "70a7ea81a287d094c534cdd67be82e85066e13be" -print("EVP version: " .. evp.version) -print("HMAC version: " .. hmac.version) +print("LuaCrypto version: " .. crypto._VERSION) print("") function report(w, s, F, t) From 3d690d7c9ab8445f66cb3641ae9b80e438c924d4 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Fri, 25 Aug 2006 03:24:17 +0000 Subject: [PATCH 09/21] adding new documentation and tweaking build --- Makefile | 8 ++- config | 4 +- doc/luacrypto.html | 82 ---------------------- doc/us/examples.html | 101 +++++++++++++++++++++++++++ doc/us/index.html | 115 ++++++++++++++++++++++++++++++ doc/us/license.html | 116 ++++++++++++++++++++++++++++++ doc/us/luacrypto-128.png | Bin 0 -> 11784 bytes doc/us/manual.html | 147 +++++++++++++++++++++++++++++++++++++++ src/lcrypto.c | 8 ++- tests/rand.lua | 59 ++++++++++++++++ tests/test.lua | 27 ++----- 11 files changed, 556 insertions(+), 111 deletions(-) delete mode 100755 doc/luacrypto.html create mode 100755 doc/us/examples.html create mode 100755 doc/us/index.html create mode 100755 doc/us/license.html create mode 100755 doc/us/luacrypto-128.png create mode 100755 doc/us/manual.html create mode 100755 tests/rand.lua diff --git a/Makefile b/Makefile index 1239aca..95622f9 100755 --- a/Makefile +++ b/Makefile @@ -3,11 +3,13 @@ V= 0.2.0 CONFIG= ./config include $(CONFIG) - -OBJS= $(COMPAT_DIR)/compat-5.1.o src/$T.o + +ifeq "$(LUA_VERSION_NUM)" "500" +COMPAT_O= $(COMPAT_DIR)/compat-5.1.o +endif +OBJS= src/$T.o $(COMPAT_O) SRCS= src/$T.h src/$T.c - lib: src/$(LIBNAME) src/$(LIBNAME): $(OBJS) diff --git a/config b/config index 5c446e0..1f79243 100755 --- a/config +++ b/config @@ -7,7 +7,9 @@ LUA_INC= /usr/local/include # OS dependent LIB_OPTION= -shared #for Linux #LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X - + +# Lua version number (first and second digits of target version) +LUA_VERSION_NUM= 500 LIBNAME= $T.so.$V COMPAT_DIR= ../compat/src diff --git a/doc/luacrypto.html b/doc/luacrypto.html deleted file mode 100755 index 6fc1bff..0000000 --- a/doc/luacrypto.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - luacrypto documentation - - - -

Table of Contents

-
    -
  1. Quick & Easy
  2. -
  3. EVP
  4. -
  5. HMAC
  6. -
- -

Quick & Easy

-

This module is written to use the new Lua 5.1 package system. To use this module, you need to do:

-

-    local evp = require("crypto.evp")
-    local hmac = require("crypto.hmac")
-    
-

This will put the EVP and HMAC package namespaces into the evp and hmac tables respectively. The simplest usage of these packages are:

-

-    assert(io.input(some_file))
-    local md5_of_some_file = evp.digest("md5", io.read("*all"))
-    
-    assert(io.input(some_file))
-    local hmac_of_some_file = hmac.digest("sha1", io.read("*all"), "hmackey")
-    
- -

crypto.evp

-

The crypto.evp library is a wrapper over the EVP_Digest* calls available in libcrypto. These calls handle all message digest creation operations for a wide variety of algorithms (SHA-1, MD5, etc). The luacrypto bindings provide two interfaces into this functionality. The first is a single function call interface that has no setup and no side-effects. The second is an object based interface that may be useful for some scenarios.

- -

Functional Interface

- -

output = evp.digest(type, string, [raw])

-

This function generates the message digest of the input string and returns it as output. The hashing algorithm to use is specified as a string by type, and this can be any one of the types allowed by OpenSSL (see their documentation for a full list). Examples include "sha1" and "md5". The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the message digest.

- -

Object Interface

-

The object interface is mainly useful in cases where you want to increase efficiency. For instance, if you will be hashing a lot of different items using the same algorithm, you can create a new message digest object of the type required and reset it between each digest call, which will save you a little bit of setup overhead each time. Alternately, if you are hashing two or more items that have mostly the same content up until the end, you can load an object with the identical data, then clone off multiple copes to tack on the remaining, differing pieces of data for each.

- -

d = evp.new(type)

-

Creates a new EVP message digest object of the type specified. This is the string name of the hash algorithm to use, which can be any one of the allowed OpenSSL types (see their documentation for a full list). Example types include "sha1" and "md5".

- -

d:reset()

-

Resets the internals of the EVP message digest object to a clean slate.

- -

d2 = d:clone()

-

Clones the message digest object and its current state, including data loaded to this point.

- -

d:update(string)

-

Appends the data in string to the current internal data set to be hashed.

- -

output = d:digest([string], [raw])

-

Generates the message digest for the internal data set, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the message digest.

- -

crypto.hmac

-

The crypto.hmac library is a wrapper over the HMAC_* calls available in libcrypto. These calls implement Keyed-Hashing for Message Authentication (HMAC). HMAC can use any message digest type (SHA-1, MD5, etc). The luacrypto bindings provide two interfaces into this functionality. The first is a single function call interface that has no setup and no side-effects. The second is an object based interface that may be useful for some scenarios.

- -

Functional Interface

- -

output = hmac.digest(type, string, key, [raw])

-

This function generates the HMAC of the input string and returns it as output. The hashing algorithm to use is specified as a string by type, and this can be any one of the types allowed by OpenSSL (see their documentation for a full list). Examples include "sha1" and "md5". The string provided in key will be used as the seed for the HMAC generation. The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the HMAC.

- -

Object Interface

-

The object interface is mainly useful in cases where you want to increase efficiency. For instance, if you will be generating HMACs for a lot of different items using the same algorithm, you can create a new HMAC object of the type required and reset it between each digest call, which will save you a little bit of setup overhead each time.

- -

d = hmac.new(type, key)

-

Creates a new HMAC object of the type specified. This is the string name of the hash algorithm to use, which can be any one of the allowed OpenSSL types (see their documentation for a full list). Example types include "sha1" and "md5". The HMAC key to use is provided as a string in key.

- -

d:reset()

-

Resets the internals of the HMAC object to a clean slate.

- -

d2 = d:clone()

-

Clones the HMAC object and its current state, including data loaded to this point. DOES NOT WORK YET. Just returns a new pointer to the same object.

- -

d:update(string)

-

Appends the data in string to the current internal data set to be hashed.

- -

output = d:digest([string], [raw])

-

Generates the HMAC for the internal data set, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean value indicating whether the output should be formatted as a hexadecimal string (the default), or as a direct binary equivalent of the message digest. Note that you can only run this method once on an object; running it a second time will product a bogus HMAC because the internal state is irrecovably destroyed after the first call.

- - diff --git a/doc/us/examples.html b/doc/us/examples.html new file mode 100755 index 0000000..b94364a --- /dev/null +++ b/doc/us/examples.html @@ -0,0 +1,101 @@ + + + + LuaCrypto: A Lua frontend to OpenSSL + + + + + +
+ +
+ +
LuaCrypto
+
A Lua frontend to OpenSSL
+
+ +
+ + + +
+ + +

Example

+ +Below is a sample displaying the basic use of the library. + +
+local crypto = require("crypto")
+local evp = require("crypto.evp")
+local hmac = require("crypto.hmac")
+
+assert(io.input(some_file))
+local md5_of_some_file = evp.digest("md5", io.read("*all"))
+   
+assert(io.input(some_file))
+local hmac_of_some_file = hmac.digest("sha1", io.read("*all"), "hmackey")
+
+ +And here is a sample of the object interface to the code. + +
+require("crypto")
+
+local evp = crypto.evp.new("md5")
+for line in io.lines(some_file) do 
+    evp:update(line)
+end
+local md5_of_some_file = evp:digest()
+
+ +
+ +
+ +
+

Valid XHTML 1.0!

+

+ $Id: examples.html,v 1.1 2006-08-25 03:24:17 nezroy Exp $ +

+
+ +
+ + + diff --git a/doc/us/index.html b/doc/us/index.html new file mode 100755 index 0000000..a2e3d9b --- /dev/null +++ b/doc/us/index.html @@ -0,0 +1,115 @@ + + + + LuaCrypto: A Lua frontend to OpenSSL + + + + + +
+ +
+ +
LuaCrypto
+
A Lua frontend to OpenSSL
+
+ +
+ + + +
+

Overview

+ +

LuaCrypto provides a Lua frontend to the OpenSSL cryptographic library. The OpenSSL features that are currently exposed are digests (MD5, SHA-1, HMAC, and more) and crypto-grade random number generators.

+ +

+LuaCrypto is free software and uses the same license as Lua 5.0. It is currently a stand-alone component with the goal of eventually becoming part of the Kepler Project.

+ +

Status

+ +

Current version is 0.2.0. It was developed for Lua 5.1 but it is compatible with Lua 5.0 through the use of Compat-5.1.

+ +

Download

+ +

LuaCrypto can be downloaded from its Lua Forge page.

+ +

Dependencies

+ + + +

History

+ +
+
0.2.0 [24/Aug/2006]
+
Added random support.
+
Removed Lua stub files and collapsed modules.
+
Changed all supporting materials (documentation, build, etc.) to Kepler standards.
+ +
0.1.1 [22/Jan/2006]
+
Added Lua 5.0/Compat-5.1 support.
+ +
0.1.0 [13/Jan/2006]
+
Initial release.
+
+ +

Credits

+ +

Much of the original release was based on the lmd5 project, written by Luiz Henrique de Figueiredo. More recent versions were based on existing Kepler components and also incorporate changes contributed by Mark Edgar.

+ +

Contact

+ +

For more information please contact me. Comments are welcome!

+ +
+ +
+ +
+

+ Valid XHTML 1.0!

+

$Id: index.html,v 1.1 2006-08-25 03:24:17 nezroy Exp $

+
+ +
+ + + diff --git a/doc/us/license.html b/doc/us/license.html new file mode 100755 index 0000000..aa94a59 --- /dev/null +++ b/doc/us/license.html @@ -0,0 +1,116 @@ + + + + LuaCrypto: A Lua frontend to OpenSSL + + + + + +
+ +
+ +
LuaCrypto
+
A Lua frontend to OpenSSL
+
+ +
+ + + +
+ +

License

+ +

LuaCrypto is free software: it can be used for both academic and +commercial purposes at absolutely no cost. There are no royalties +or GNU-like "copyleft" restrictions. LuaCrypto qualifies as Open +Source software. Its licenses are compatible with GPL. LuaCrypto is not +in the public domain and Keith Howe keeps its copyright. The +legal details are below.

+ +

The spirit of the license is that you are free to use LuaCrypto +for any purpose at no cost without having to ask us. The only +requirement is that if you do use LuaCrypto, then you should give +us credit by including the appropriate copyright notice somewhere +in your product or its documentation.

+ +

The LuaCrypto library is designed and implemented by Keith Howe. The implementation is not derived from licensed software.

+ +
+

Copyright © 2006 Keith Howe.

+ +

Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, copy, +modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions:

+ +

The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software.

+ +

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.

+ +

 

+

 

+ +
+ +
+ +
+

+ Valid XHTML 1.0!

+

$Id: license.html,v 1.1 2006-08-25 03:24:17 nezroy Exp $

+
+ +
+ + + diff --git a/doc/us/luacrypto-128.png b/doc/us/luacrypto-128.png new file mode 100755 index 0000000000000000000000000000000000000000..ce902da358279b97c2c4c2be01fe9083b5c375c1 GIT binary patch literal 11784 zcmW-nby!qg7sW3j9fQ;$NJ~g7-Cfc!bdQ8|cMLEB!qA8)DWP;XD4>9Zbcb{|(jf5N z_nYS#?thni&z!UNUcbHGX=x}C;yu9w0Dw?MSzZUcBL4g0po4$aOfNFQ3$B~8ktcXn z{{L68&ruQpxI9$kW%T^!4uaqK=_yPd7ncME4ou-PP~$qveyU={loynN^2$@lj0``@ zl7T9?e;oDi`gq;rYrE+0w)oNA79i<)gr<#6w!oW@ryQjc?^ zR+P}sc>gIQXd`J^fGyLMh;s`l9L0CC{@^NOw40-0gzz@zF4-*qpUQsbTUht-|&fv^%y5ByPG<5#iN*rPe0=O+s zQ6cAyrE%PeLK-3iUWdh^=@}T<+1Lzbii}(G+QVmBeUDatMiFAk>FVlQTd%Hm$EvfW zTMl%K9swhoSdz# zt(%*hy}iAkpP#aFw4!CXcRB3?Wt6@ejEX@yx2YahUS#j!-~r?&Lg0FOxtyecB}%s+ zr@3NSLJg4kK>2X7JITSoV0C+OpkI3aIg0khlQ<(IqZC$6T2Zf}k`i}!_sgZwhk4g_ z3GdyBqN1XyDgBhbg2|uImmk#_7#N`Nls|le za8t6=Z@=p1{d^}h7M-j1zEqg<>jepvjOR99%^bM&W+vN=HgCMWquK1 zTKy1oRPbad2TpEzKu>!5)#^ZB3ag`oL#BqEvGJ!#2l8`g(TPf3yX?e@?;gUjc9cgJ&chRp_boriY)=O|?Q z&&+facF&AUqeYwF=!zQ>i9Fk#bo_U9@>Mej3xPypqTDq!;yW#?W}U)qk%^vLL&YVc zPBXm0M~}F=ZTEV@&q*^O*7e317#Q`eIG-aUBV#FfXhl5MM?TBh*x2CW;?{6FmiDJi zzvD@LA?v2DJ|J-!bi7+?Atb<#Oy^4jcoFs_X!J;41}x6s>;d8xLt{&LzjN_YGAoDf|ARajD|+s_IG;l?S*=V;Na^Q-M@b2h?Axf|6{KpbniWkaP_k_RG;oZbNTJ26&9TBcv)22BSrR~F&E)+4~rA9OKm zhyG|O0!O$y%-5Fs(#D-TYa568Fy6HuFnGoTuV`Zs08qD$NhZziLWLBd*@@#q-^C z^L)SB&QGbKDJUsf7JS-(1)%s;USx^r(oj$Y%{#lNdZ7L&jdEd*P#krGo5HjSLP<$U zMphU4a9>bRaCPO!hYjwU1E1T*r4TXq)vmgrM3+QNbYL(a6OrX~^3 z&FH%hbwo4C6NJmVZEzP@TU!SO1-ZB!f)Zn5Z0sbMVJWGUkj8289iM@zaDjND^)ot9 zK+4K3b=<*LI?2pdLDxI@S|(sT&hQ5%x2dq(G5|1xA6C{JsZd(41tv+!@72}S6&2iq zf<9NrD;YwrL;d}$nNg9E@pMx64f8Iw#^rh0F6iZ~pBlvNM{>XBganM{DO5a#0?Tv{ z!atfx;ro5kqE2C+8qr=Gs(*UoexXT6>jX6Q1tR7Zr2@{jH#axO#&m#m5l>qM1qJX( zHMGwj_yD|60-(yWGaOc`FLB&~A*)OdR5)L3K6yNM&~WkAQxe?bpbGRfF|WkL^bAW4 zWAp0ti;CSnB8091e=iRFQ(vffC|wOQAOtT-WXZ|N&(F_gQSaWpt2QpT8_s@&*xueg zS^E`w+=<=X+?+ZFel#VoUw1{5P{h9Fv*m>bobMJ^zd^g%*{Stzk_n8_Z_jOf)w2O4 zzV~jfs6c~{ZS;$50UqY&-sa}yew^VMERhbk^Wf_no0|Sxjv$_M5(GeL0RLAO9~=u% z%Mv}GDApD8*{?D#2cP9b_Z-rpU=Pdu~V076hAG^iR9llVU?S{Yk?fi8m6HVI1 zhD4P5C)Mie#ukm&=t+rXuiaT#!nsz@z|fKzPY&p&rHb8&R1$77d3Ttvo^nz z?a}<&nwpZ*(!k5Zr=S$P87siRCgL=%Q!Y~elv(mYQ=DLe-U)y*$?^h9IJb&Lxv7v9 z*n37~m&f1nzxg~q*B(FPGB`z3cD#$y@(a2xpN21X$LB11l?Oi{H51-5G*!FuQxOG6rABItKuSD85a` z&+URuQ~KwMT9w$NnnK$&Xm!^WtQZUhRaLw^JUsmTXYpc(0HFG2>3eYK^?=18SOMaM z79o$mfKMdyftj6)%Xx3AJYOjx{@uG#>cjI*MXMbJ942H*X+~|0>Wh~=bYl9Sazg9N z%93?oxF0b#!!f zbiTPR83B)>P<|h$oMumF&n=dVJv(xa-%;IA2IdRI{av|rx0gBAH!PGpuuz#pE?!3! z+ME#?jB*Es4QyLL@n>RU0&j#|D*5uo_F%65Ti~Vn_n_tqD@B~@`Ge5=)0}beFzV{E z7a|kE@*mNeD>RD3MFKBiJB#JP6Lk4K+uE30Uylo5^g>CkeveTtiDGr0b>H_vA87&U zqfoc4tO(jb&SzX)otE?{_a492ds3K9GVd^at*X{o{c&@Lnp=+SxPE1XqBT7oFtgI5G?*07zufM2g)KTIxl$02Arh%oG z0Ttu^Bu>$}BlQJ?yBN)GBq6IJ%FlTyvr+Pzxv?Jhj2QtTl}+cXC_#-49lW)^)Q7RK z&@-^eqSFxFTyK5d8V(Jdsq)^Ndg}T{n;AEjd~I{T3yRj!uYij{eNQYZw`9mW-~Du3=>ti8Zcot`lW9=!UDKo=)QJ;KA!X4z|>S-nZ5_Br>Qhw$m~`D9UtsA36YrKrtqGvx%`Eibz7FM zX0{rdPYZ`u8e(!7EYd=u+ci?YF<3Nkwc?kOlDkPt$!O9}B4!MT8Q0jD7#$rQpd&8H zeTajD!}oCEP2+riULLq8Ec%gbDT#^43!cLY^71%Fv~vq3O@#`TB8#I zBthyJHM~A67ZaYT6O$;fZ0T}XD0Vp06m%&`5w0ef3^c6OIembnlP!Nz>0 zS2{XVm1eCu5Gr^I{&}Nt12!L4stzg+2^Dm@vOU{Ak0Q26W>#aT+>eHIK_4b7S-4Eo z^Vs5QIGYpa(Jn}2Yf$XYLHE~h@F`ie`@7aXF+y=WpeZ&YjEt+`Lg${=*@rSQGJ-t> z$MfeM>b<8mXpQ@W%Tb`v zRz?PglXG}C{u8#Cv4w?&QUji*#owjBBta99#`CaV{IvnY5=EGbw^4;%m5|sP7OfLg zjRxOo-jWPD8_oI=zCQO@I<2q5>{9A>p8)2qH5UU>PS#qNxrKx?zoYMBckY4S-%LMp zoF1Z>x%^UpwUIcA{JQupu(7Ugf3BY6#S0zXe9RDH8ezAS)$YKfZ_}2%+Xo*4zBc@+ z7|LlsxVubH(Sa0>B*Bk1(XpkG^r$2^LxA@L0^g6Re9Qu2R_V#i3};e$zBxz@R)f6L zADaQwX1L>DSxwt!j_)o7Q1v)Agwj_}*tax#Xpqp&p{FNDp*-0yS+y}quB(E)WJ$)q zgAd~PaDSJt825?)jgGuLvZMqwI2OU@lRt^2#68xP)nPtALxO`H18&mfqQ0jTNW+|| z|CP&2z)_yq05&fsW;z$eJDtUuZLL&&fLyv~25_!mW>ztTWq>{5*{mJ$<*tp-C<34O znJk-F>ptjk>}<`Sb62=Jc+ewdWl<4WZ`D7Jh2965SX(!ZaElQn?Pao*bqi&Rc(O1t z<=51VS@LSB#wo7-yFPt%aeiCAEDp`@QQRss%6Rb-pP5}6wo{&YRbhzT%9-&+Lp@z; zDecn}u(Pb{X4LK9-8DWvb-Xy>nEQU`BqKv5`&bsMhYe7EtUb9oF@Y9%A#Pp&sY@D= z#(5b|i^HJsnTzYWtYvj?MN}@^k1rlvbw5lVM6K+-^U>gCH8p=hlf;t3&doja`h~x> zgK%x?cVvD?VlfdwyFY$@nU6sQ2eH+=mrnZ1Z4mi-h07 zL+{hmmn**;i`uA4DST#B)AggoB&^%UVbG5u8qN^HgkSGf4#PP;7b%QAZoEj~V_CjL z1*wj@=G2u~tJm*AnFEzR3FL^f&_IOYAi9-$G$O?HivNB5-8W8-UKp#d(RTL=faS?7((SM^u_W`}jy zY-&(q3%Gym>F>Y)?dJqkpvuB_$lmww>oq0Li|2VI!uX~@2$Gtb8Xg`_M14L@2LK;< zEX-?d2f+gQt)A;5xbxXk;L+K|!O~;vH$u_%VlvxC+m!9;x!!#bME3kzFu{zr3IL~TdFNbaHK!gnZB1fPNv5kI5IYB zbexfXf`CH*4=fn6ft?L4(^RrWO-)yKmrK4>!SiyJM;i7w3SdVZMf7x6?uvZ8y{>NM zpivVY8ra{(gZgA!D@^GV0Yj^Mbh=&5NfGA0H?^?3Dq4*bROP3V3|d}o9UXM@b!`hw zH+-RKYJYM3*ygG?<9%~;qOt`Uo2p05(Ex3f+$67G#K>Gpz;9-a=!uaLu*sQS4kp1d z?e6*Sv$?V1U)oM8X{Daxb*qBXO|BYOX>{s)lo&r- zK(G0y#k*veLlNe6wDcf}A^rAag_!mN+96PIcN+_ZOKyG%J(t#>+9);H<4Y6YgOHrh zHwJESefkG3{w;`{Kpa$OQnp5}rKd;s_%WkO@^Cqpkf7+re)CN2KWqTHhST>|^TSi& zV~ML&owhK?QVd|GpK6H2R(Z1*|TS$Rl}UF z<{3VWk)f3`H`dXKNL(174ep{ZhL9@QiSBtlwtMH{$zZ#2kTSg_BD1dAVmFEX>csq>qJDcd(vT(f7<8ul!W_SKDaZwVD0Vf7Y{h zmjS|uHx@&lv%Y2V-X@Q2VlZcPNQ`@*1CKPM;7{u*4&SYoz3>*W-kbE;?VXx=KLFqV z@fM#kan=GI#}woSlAxQ{FP#i*M}zB`%qM>^c(iQtZwsJTF$|isDHFm)D(JR&6!J*~ zpBBD456AkHG9_0qx_^Muap6t$-iv?O6rzG=1k({vP~XqiPEJk^R@hTk_69xNUB2br z?dhu?o)dFpvG2yicLe<%5S>89(xz83$1Iu2k(J*RIXmv zL46O=R}>A%o@XjU^C>Oxt+Qo0YKRI5Dy_6y>7;+2=H@q9)FHnN*Az8@mA; z*{d$n#!6!W_UF$>2GgG9t-p=E+N+Gs4h_-N(D=NWbflH}6RKU3DdxLzF@JkQt88d! z*jgfei#ZZSc5~qnb7I-wfU=@Zq6zu#K)ir_@Cgy+lu>)^z<$Vg_d0BeKu#RWdV9-gih#MPgf$3 zc^TRfhqSm*#^4W`%}5ms4gk0~TFOaGgiuq@AL5PBhCBp+lOo|}yfJU;ZB6w3?8v{I z=^l@RYwzUcw3u1%f2wWNaCZ3AQt%b(q(xN8*_jl9A^l?p2jHg6wg@I|Xb1J>INASI zMyFg9(d1U%E&@O&Q*hOG>@x{l)upGMtk&nvU-JI_&wYNuNx8vlNl=QvE!^8U-i=7dJ|`6Fx<6$sU_6jV6<3E2c1UC}Z}eV@*k}9`%mZCJ1?T7G zgM&UW7(f6W;EwC7l&&N4-v9LJ6AFc@tE&TlCkgz&&muvCyuSWgGp`w~M;RJ&$KULQ z29eDyJvkvs5PzWxGjetwkb3a{J+6#^teR5sG5VaWT9x$o_dhWYy+c-@IwW~*kH%T8 z`;xU^+7ZPh5aSaG_Px+2o0{2X<18EOZMxdbm<9U=UcuWB+%b;s36eM?3kyMi*Hw}P zzQEC&4yYx``QjljpvsLg{7@#iKlS z9u5M6-b14)DPnWJOGOb|MwdE@6BZU(RMde>dh`AG2l5x~{P|*_2s9%|tnuy2oF#qs zmZk)WO|@Yi^Ow0(A~L?l@yUZpd>k3KsuPOd)F(aNB)D{PKXXDfEPu@QLVDvs;lM^kE0zgojWU|o!`*{<<6&uke}KYgpsld` zV~)kigzi!M-+n8r=2sf=xI{!AZ@aI7#jl!)kug4_I6wq8@qd7av(>I_r{jsSg@uK= zIg$DAyt#_`%A9kGG6XdSQAM;!tit_f#wrTsnf!Vqv+2xAEt;gGE%=uGL{=-2fnFNx zxcd;vRTLV^;b(z{{>rx2s5ixB8Ad=p30iru+(cYTNQ*bl2B0OHytm2apx2>1oYmEW zO(G$pL05r5*)S&K?>wNX@a;|eCk0xH46X(082*xNUEQ6|Jr+)LNZ&xG7#fl#Gaag- z{DD+#5f3ty;yho|=$sEA64j!$aeQTCwldu-0DfZHOlHNEC2cY+OD~cR1C_xN;*jUo zd0JjxXCP6NPb>6kMAL?~hfIasmA!py&@%o#Gy(w#_pr!9dS-U?*y=HOx!R2k4K1CV zj_clFnEP3Uw+k>XXZCczC8B}iEjr5p9Z+xrg-1X@X1|Y#7D-5*mYTXwc#F&x*)zhJRX~dmt7l?;9W&Ip6Uw(q zBPkv*QDlGO+FJDZ3li*daa>U*c^t>A;%3^Zx?eQ}KviX9zq-#95uCP4K758q`1u78 z7Zn{jU}wPT;IsOH*crzb$(neb3{nUtkfB^0wkpNORIuSGD6@1is)PY7bhZK#E)ZFu z|Bo+g3~BIUYmD#~$bIA@3BoEi@I6Gcz)1}$k&UbN^SrshLzUzL7jcT>_ID*_AE^-- z%c-gMnw-Fh`Mt|0f1z7CrHpa6xH2Kctob%4|&05$V=9yi9`73 zaxt*72?s5(EH$2K4ILdFi-?9z=ONHhaeb3Ukn``57&ah$hpWYmiDs$tV^xFOm5$`c zhN4%kQG?;a0v)47_7hT4QRlf_9}`rxeJfdhQ`48vo$sod^XluKFymqfJ2#lSIX|3z zz7zhUwv&@dv-UA4uUs@?o@`EF`qo<-4i1(dJ$e+AcV(Cze#B!YsWT7w^v0Dq+8B*>s@p3a#;vvV^u&3P%)AKl%NhSVpm3o==e-zm3zA&1@Q64`r-96fK4zQOWb0c7 z7|LKl&|oDeEH@9HKHDMp_O6#bC#_}$DX^w~Mcbe*^HwItatiAJTE`AJz=*s%`&zs= zeFXpAk)9~Pww3tlNxRL!zpE?Wq-l44{pyVaXQY;s>&3&)QjW?_IrM#_Bz-X{+t8 zi{UHO7I`2E(ABjN^H${y7kq;AnTwsBg~#=g*K5&B=8iAwMm01=Y~@Uf@^r#g6Al9G zc;2tlm>CHkP7=x{ONrOFr0lADmqJYV0?tG`ZTS>Y^%_~9j=L#e5nwU4@!h!K>xZn$ z&&m-WTrEc^{ESi(rFq_?oYwbpc>n%(>5|m?b1JU$kZo*lpOKZ|s{`7(N!q!C3#t2) z1m8d2(_QaKzmaq-?eE+ensIUIx5k18s{QU@;w^roUt9dX&&G1+(VY+lUyrP;8acT- zkggM1y)sc;qmpu8Z}qe7WDPEmgle?ec@uKIf#En!8y$lu6?COzUvxcD+`qS#4-R_% zvFk3HwucP$CMCJNtZZuoS)f#lkMoy5i&Y_aC-siwHXow}z^>_;Z^LY8$Z1BgZSC|< zf@!m!r>Ccl5$#J`0zcxzBh~&F$2Cv*y)04V<3gbqM%mm(*uHlMKZ0>Ea}QFH=?I+9 z)$m)*?1e_xC5RD=LFJrgcG=b6fmm;EZ>R|rUZD>`_?Xi)wMS@9;=ym~p`p0B40qrV9fyIWc)Q%W6K^Q1R6{+uyxGWmg=k0T+J^|! z=$td%bEp|ZT35KMQc*Tq3%6ch{6}Ehw|F!Hq%tEXB|`y62O0sley2K1>ihF6aJ?jJ z!W^)g%1s)%O`CY1KkruxKnumC-#nixHz_SC$qv3SIwh+?-TH?X?y)^%w1eLCL?c-JPWyw{T{k6bEZWT?35jfK%${S= z`+d{Rg`M%Q2KxHhBAz`aIEjAT$zGC|3kS7!#0D@D6W<70`1*yJDFHo^>2?Lh@i=mJ zXs<=Tf*jHwbEO8WA5Kp?Ta8WWeoMUgqkAPle_!f$WTr@AJdWbdWRhAhk6l;Vfm|htZ1E!$z*OxR|7mxNY`r z6UUAom_|wCihcdD$;1EQ*QoSzsugO{-%cCs*;do2U9H5>8RFvNi~~}M;`+0gfGu%O zPN$9j_h4{Ca+myBH_50<%FVZv9&{k8m;Z!LA);Xr;rT0)@bsw76CrH8DyBTL;#Ou< z%fiZv{SQCw026=gNqltl2s-M_bhx&S*$P%jEW!VGI5b=CO|0_w3p!a0A_Oz8_T0Z; zQeW=&?fOdkUw`!Ky?OcK1(^bspcC|p_0;>*Bme9(ub1LGOMrcA#>R*Cw!8Lwj<_*f z71;;ewrD0KGj6fN=_{T=<9dghk>(&vMW^QO?rv;iTGJ}au-Sb(OVmQ0S!aRJB8O2ec%h6|amMHG~jCgmUp*p)l=h@fs6@&S`IzY^$%hlZwV^a8`D zyk)EMMr;O>eUI+n*cW{qqfmLQ@t&?%2ZDW#3+IxN=>aEPue)RP6h2EAs(x=7a`ps`1IPpq}-J zdW#NBaEiW{Cf^mp% zTiJmfHbna)wgcY2vuPiMzTXF2fCEn(n@Iq$gd`PbwRAUR$uPS8X_rIG1R&((-@V>| z$3*grW{xGD&|Jg39XR4(kEyX4NK;8+0pm@3Ji|Ly=wy_X?H6;7V6Y-^ISl*XPKw~4 zGedh9pO6cq`!DKChf5Z(8)H!)Umyxrl*P6V3IAC0Oi^Va8Cw_h7`lrsByKgf&_6aZN|D3O#;pWcng5kJDhx~UcLPnK!Z(Vt3B z(oRg$cDp*>7#kCv=Cb@D$1XVy+y9%zH?73>U#=|?mvq&32$du9Qc_UR&}%1PBI3vf zkCvqWe&46@6VU6u<7s9F0?EDPloY$cPlX2kckhrRU&@uPD4T4lyWspc0p(rul!}xSSG@91$|hOm-93}Ny{8~+ zC@(MR)ngc#d7TWF>CR5YgQB-nX-%!f@Um(aDUz?SWN_nvQ7WP*{PsIzg=Q(jtyw_8 zlWn`;57TA$72MUt_y!5bk+=Fum_no(Ve=anSx@8h*ue1NJQzBtdow;ZKJJ>h)fCq5 zE)x=No9>;zmn{*PoSbY8hrdM~ef^!Aa0{W1&@VTn%4V^k^pVa_v3vW}dO;n<&Biv< z9{P}8a^U;~L5Ru@3{P1ADk%yHPOc6@HdGqGvEn*V1SB*grvB*7>qxXH?BLNUdfSqy@O`!P0ar`F)@i#%%7r) z`WeQTW@F^)v2ENcC69j9p#eS7%3I%!~pcbh>v}X7ihCIG=>rxNY%*?up`T&m8%FDe)vtsh-kv%A?{6TvK zZ7^IF5gyJ-5qp2A(Ae?l3XKhM{F50l)x?MkzUA%cJTGvd&Ty2ww|XLlc{W44Og(2a z7%*a?T0jXqCq556Cnmi;jxa-e2waYoB@IPVM?GL4?r6|?h>Tr;F>G-!1w$`6S7&D% z7cY>CJ6qY=(A(L#*mziZ*}SH=_NQ0S)uLCjclGr0gnPVk^P*>aZR7I>ZX@C5;_PAN W3v#A$y?XFIfQo{Ke1)uK`2PSeGmKOK literal 0 HcmV?d00001 diff --git a/doc/us/manual.html b/doc/us/manual.html new file mode 100755 index 0000000..8a6ce09 --- /dev/null +++ b/doc/us/manual.html @@ -0,0 +1,147 @@ + + + + LuaCrypto: A Lua frontend to OpenSSL + + + + + +
+ +
+ +
LuaCrypto
+
A Lua frontend to OpenSSL
+
+ +
+ + + +
+

Introduction

+ +

LuaCrypto is a Lua frontend to the OpenSSL cryptographic library. The OpenSSL features that are currently exposed are digests (MD5, SHA-1, HMAC, and more) and crypto-grade random number generators.

+ +

The API tries to hide the OpenSSL setup and teardown, so in most cases it is not simply a pass-through to the existing OpenSSL API. Since this is still a very early version of the software, the API may undergo significant future changes! You have been warned.

+ +

Building

+ +

LuaCrypto could be built to Lua 5.0 or to Lua 5.1. In both cases, the language library and headers files for the target version must be installed properly.

+ +

LuaCrypto offers a Makefile and a separate configuration file, +config, which should be edited to suit your installation before runnig make. The file has some definitions like paths to the external libraries, compiler options and the like. In particular, you must set the correct path to your installed OpenSSL libraries. Another important setting is the version of Lua language, which is not obtained from the installed software.

+ +

Installation

+ +

The LuaCrypto compiled binary should be copied to a directory in your C path. Lua 5.0 users should install Compat-5.1 also.

+ +

Reference

+ +

Parameters

+
+
dtype
+
This parameter is always a string naming the hashing algorithm to use for a digest operation. The list of supported algorithms may change with each version of the OpenSSL library. Refer to the OpenSSL documentation for a complete and up to date list. As of 0.9.7, the supported types are: +
    +
  • md5
  • +
  • md4
  • +
  • md2
  • +
  • sha1
  • +
  • sha
  • +
  • mdc2
  • +
  • ripemd160
  • +
+
+ +

Message Digest (EVP) - crypto.evp

+
+
crypto.evp.digest(dtype, string [, raw])
+
This function generates the message digest of the input string and returns it. The hashing algorithm to use is specified by dtype. The optional raw flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the message digest, or formatted as a hexadecimal string (the default).
+ +
crypto.evp.new(dtype)
+
Creates a new EVP message digest object using the algorithm specified by dtype.
+ +
evp:reset()
+
Resets the EVP message digest object to a clean slate.
+ +
evp:clone()
+
Returns a new message digest object which is a clone of the object and its current state, including any data loaded to this point.
+ +
evp:update(string)
+
Appends the data in string to the current internal data set to be hashed. Returns the object so that it can be reused in nested calls.
+ +
evp:digest([string] [, raw])
+
Generates the message digest for the loaded data, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the message digest, or formatted as a hexadecimal string (the default).
+
+ +

HMAC - crypto.hmac

+
+
crypto.hmac.digest(dtype, string, key [, raw])
+
This function returns the HMAC of the string. The hashing algorithm to use is specified by dtype. The value provided in key will be used as the seed for the HMAC generation. The optional raw flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the HMAC or formatted as a hexadecimal string (the default).
+ +
crypto.hmac.new(dtype, key)
+
Creates a new HMAC object using the algorithm specified by type. The HMAC seed key to use is provided by key.
+ +
hmac:reset()
+
Resets the HMAC object to a clean slate.
+ +
hmac:clone()
+
Returns a new HMAC object which is a clone of the object and its current state, including data loaded to this point. DOES NOT WORK YET. Just returns a new pointer to the same object.
+ +
hmac:update(string)
+
Appends the data in string to the current internal data set to be hashed.
+ +
hmac:digest([string] [, raw])
+
Generates the HMAC for the loaded data, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the message digest or formatted as a hexadecimal string (the default). Note that you can only run this method once on an object; running it a second time will product a bogus HMAC because the internal state is irrecovably destroyed after the first call.
+
+ +
+ +
+ +
+

Valid XHTML 1.0!

+

+ $Id: manual.html,v 1.1 2006-08-25 03:24:17 nezroy Exp $ +

+
+ +
+ + + diff --git a/src/lcrypto.c b/src/lcrypto.c index 2ebce5b..972edbf 100755 --- a/src/lcrypto.c +++ b/src/lcrypto.c @@ -1,5 +1,5 @@ /* -** $Id: lcrypto.c,v 1.1 2006-08-22 19:18:42 nezroy Exp $ +** $Id: lcrypto.c,v 1.2 2006-08-25 03:24:17 nezroy Exp $ ** See Copyright Notice in license.html */ @@ -10,8 +10,10 @@ #include #include "lua.h" -#include "lauxlib.h" -#include "compat-5.1.h" +#include "lauxlib.h" +#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 +#include "compat-5.1.h" +#endif #include "lcrypto.h" diff --git a/tests/rand.lua b/tests/rand.lua new file mode 100755 index 0000000..156d97a --- /dev/null +++ b/tests/rand.lua @@ -0,0 +1,59 @@ +#!/usr/local/bin/lua50 + +--[[ +-- $Id: rand.lua,v 1.1 2006-08-25 03:24:17 nezroy Exp $ +-- See Copyright Notice in license.html +--]] + +require "crypto" +local rand = crypto.rand + +print("RAND version: " .. crypto._VERSION) +print("") + +local SEEDFILE = "tmp.rnd" + +if rand.load(SEEDFILE) then + print("loaded previous random seed") +end + +if rand.status() then + print("ready to generate") + print("") +else + print("The PRNG does not yet have enough data.") + local prompt = "Please type some random characters and press ENTER: " + repeat + io.write(prompt); io.flush() + local line = io.read("*l") + -- entropy of English is 1.1 bits per character + rand.add(line, string.len(line) * 1.1 / 8) + prompt = "More: " + until rand.status() +end + +local N = 20 +local S = 5 + +print(string.format("generating %d sets of %d random bytes using pseudo_bytes()", S, N)) +for i = 1, S do + local data = assert(rand.pseudo_bytes(N)) + print(table.concat({string.byte(data, 1, N)}, ",")) +end +print("") + +print(string.format("generating %d sets of %d random bytes using bytes()", S, N)) +for i = 1, S do + local data = assert(rand.bytes(N)) + print(table.concat({string.byte(data, 1, N)}, ",")) +end +print("") + +print("saving seed in " .. SEEDFILE) +print("") +rand.write(SEEDFILE) + +-- don't leave any sensitive data lying around in memory +print("cleaning up state") +print("") +rand.cleanup() diff --git a/tests/test.lua b/tests/test.lua index 0b0e347..a1663bb 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -1,26 +1,9 @@ #!/usr/local/bin/lua50 ---[[ --- Copyright (c) 2006 Keith Howe --- --- Permission is hereby granted, free of charge, to any person obtaining a --- copy of this software and associated documentation files (the "Software"), --- to deal in the Software without restriction, including without limitation --- the rights to use, copy, modify, merge, publish, distribute, sublicense, --- and/or sell copies of the Software, and to permit persons to whom the --- Software is furnished to do so, subject to the following conditions: --- --- The above copyright notice and this permission notice shall be included --- in all copies or substantial portions of the Software. --- --- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING --- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER --- DEALINGS IN THE SOFTWARE. --- ---]] + +--[[ +-- $Id: test.lua,v 1.3 2006-08-25 03:24:17 nezroy Exp $ +-- See Copyright Notice in license.html +--]] require("crypto") local evp = crypto.evp From 7dee258cb19daa26567005c829fc5ce3302d5cbc Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Fri, 25 Aug 2006 03:28:32 +0000 Subject: [PATCH 10/21] fixing API define --- src/lcrypto.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lcrypto.h b/src/lcrypto.h index 31d9473..4cd5bd1 100755 --- a/src/lcrypto.h +++ b/src/lcrypto.h @@ -1,5 +1,5 @@ /* -** $Id: lcrypto.h,v 1.1 2006-08-22 19:18:42 nezroy Exp $ +** $Id: lcrypto.h,v 1.2 2006-08-25 03:28:32 nezroy Exp $ ** See Copyright Notice in license.html */ @@ -7,7 +7,7 @@ #define _LUACRYPTO_ #ifndef LUACRYPTO_API -#define LUACRYPTO_API +#define LUACRYPTO_API LUA_API #endif #define LUACRYPTO_PREFIX "LuaCrypto: " From c568f290ed884aa64c0ddc014b0d7f9f7ec07818 Mon Sep 17 00:00:00 2001 From: Keith Howe Date: Fri, 25 Aug 2006 03:37:51 +0000 Subject: [PATCH 11/21] adding README and historical doc links --- README | 4 ++++ doc/us/index.html | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100755 README diff --git a/README b/README new file mode 100755 index 0000000..152dce9 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +LuaCrypto provides a Lua frontend to the OpenSSL cryptographic library. +The OpenSSL features that are currently exposed are digests (MD5, SHA-1, +HMAC, and more) and crypto-grade random number generators. Please see docs +at doc/us/index.html or http://luacrypto.luaforge.net/. diff --git a/doc/us/index.html b/doc/us/index.html index a2e3d9b..7a7f783 100755 --- a/doc/us/index.html +++ b/doc/us/index.html @@ -84,10 +84,10 @@

History

Removed Lua stub files and collapsed modules.
Changed all supporting materials (documentation, build, etc.) to Kepler standards.
-
0.1.1 [22/Jan/2006]
+
0.1.1 [22/Jan/2006]
Added Lua 5.0/Compat-5.1 support.
-
0.1.0 [13/Jan/2006]
+
0.1.0 [13/Jan/2006]
Initial release.
@@ -106,7 +106,7 @@

Contact

Valid XHTML 1.0!

-

$Id: index.html,v 1.1 2006-08-25 03:24:17 nezroy Exp $

+

$Id: index.html,v 1.2 2006-08-25 03:37:51 nezroy Exp $

From ac0bb2f354bc7e5bf26c040b08bc8df9c9633e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Draho=C5=A1?= Date: Thu, 7 Apr 2011 22:13:52 -0400 Subject: [PATCH 12/21] build: add CMakeLists.txt and related files --- CMakeLists.txt | 32 ++++++++++++ dist.cmake | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ dist.info | 15 ++++++ 3 files changed, 177 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 dist.cmake create mode 100644 dist.info diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8435e1c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (C) 2007-2009 LuaDist. +# Created by Peter Kapec +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Please note that the package source code is licensed under its own license. + +PROJECT(luacrypto C) +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +INCLUDE(dist.cmake) + +# Find libraries +FIND_PATH( OPENSSL_INCLUDE_DIR NAMES openssl/err.h) +INCLUDE_DIRECTORIES( ${OPENSSL_INCLUDE_DIR}) +FIND_LIBRARY ( CRYPTO_LIBRARY NAMES crypto ) +FIND_LIBRARY ( SSL_LIBRARY NAMES ssl) +IF(WIN32) + SET(LIBS gdi32) +ENDIF() +INCLUDE_DIRECTORIES(src) + +IF (WIN32) + ADD_DEFINITIONS("-DLUACRYPTO_API=__declspec(dllexport)") +ENDIF (WIN32) + +ADD_LUA_MODULE(crypto src/lcrypto.c) +TARGET_LINK_LIBRARIES(crypto ${CRYPTO_LIBRARY} ${SSL_LIBRARY} ${LIBS}) + +# Install all files and documentation +INSTALL (TARGETS crypto DESTINATION ${INSTALL_CMOD}) +INSTALL (FILES README DESTINATION ${INSTALL_DATA}) +INSTALL (DIRECTORY doc DESTINATION ${INSTALL_DATA}) +INSTALL (DIRECTORY tests/ DESTINATION ${INSTALL_TEST}) diff --git a/dist.cmake b/dist.cmake new file mode 100644 index 0000000..95928b2 --- /dev/null +++ b/dist.cmake @@ -0,0 +1,130 @@ +# LuaDist CMake utility library. +# Provides variables and utility functions common to LuaDist CMake builds. +# +# Copyright (C) 2007-2010 LuaDist. +# by David Manura, Peter Drahos +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Please note that the package source code is licensed under its own license. + +# Few convinence settings +SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true) +SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH}) + +# Where to install module parts: +set(INSTALL_BIN bin CACHE PATH "Where to install binaries to.") +set(INSTALL_LIB lib CACHE PATH "Where to install libraries to.") +set(INSTALL_INC include CACHE PATH "Where to install headers to.") +set(INSTALL_ETC etc CACHE PATH "Where to store configuration files") +set(INSTALL_LMOD share/lua/lmod CACHE PATH "Directory to install Lua modules.") +set(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules.") +set(INSTALL_DATA share/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.") +set(INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.") +set(INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.") +set(INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.") +set(INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files") + + +# In MSVC, prevent warnings that can occur when using standard libraries. +if(MSVC) + add_definitions(-D_CRT_SECURE_NO_WARNINGS) +endif(MSVC) + +# Adds Lua shared library module target `_target`. +# Additional sources to build the module are listed after `_target`. +macro(add_lua_module _target) + find_package(Lua51 REQUIRED) + include_directories(${LUA_INCLUDE_DIR}) #2DO: somehow apply only to _target? + + add_library(${_target} MODULE ${ARGN}) + set_target_properties(${_target} PROPERTIES PREFIX "") + target_link_libraries(${_target} ${LUA_LIBRARY}) + + IF(WIN32) + set_target_properties(${_target} PROPERTIES LINK_FLAGS "-Wl,--enable-auto-import") + ENDIF() + +endmacro(add_lua_module) + +# Runs Lua script `_testfile` under CTest tester. +# Optional argument `_testcurrentdir` is current working directory to run test under +# (defaults to ${CMAKE_CURRENT_BINARY_DIR}). +# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}. +# Under LuaDist, set test=true in config.lua to enable testing. +macro(add_lua_test _testfile) + include(CTest) + if(BUILD_TESTING) + find_program(LUA NAMES lua lua.bat) + get_filename_component(TESTFILEABS ${_testfile} ABSOLUTE) + get_filename_component(TESTFILENAME ${_testfile} NAME) + get_filename_component(TESTFILEBASE ${_testfile} NAME_WE) + + # Write wrapper script. + set(TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME}) + set(TESTWRAPPERSOURCE +"package.path = '${CMAKE_CURRENT_BINARY_DIR}/?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;' .. package.path +package.cpath = '${CMAKE_CURRENT_BINARY_DIR}/?.so\;${CMAKE_CURRENT_BINARY_DIR}/?.dll\;' .. package.cpath +return dofile '${TESTFILEABS}' +" ) + if(${ARGC} GREATER 1) + set(_testcurrentdir ${ARGV1}) + get_filename_component(TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE) + set(TESTWRAPPERSOURCE +"require 'lfs' +lfs.chdir('${TESTCURRENTDIRABS}') +${TESTWRAPPERSOURCE}") + endif() + FILE(WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) + + add_test(${TESTFILEBASE} ${LUA} ${TESTWRAPPER}) + endif(BUILD_TESTING) + + # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake +endmacro(add_lua_test) + +# Converts Lua source file `_source` to binary string embedded in C source +# file `_target`. Optionally compiles Lua source to byte code (not available +# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua +# versions of bin2c [1] and luac [2] may be passed respectively as additional +# arguments. +# +# [1] http://lua-users.org/wiki/BinToCee +# [2] http://lua-users.org/wiki/LuaCompilerInLua +function(add_lua_bin2c _target _source) + find_program(LUA NAMES lua lua.bat) + execute_process(COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET) + if (NOT ${_LUA_DUMP_RESULT}) + SET(HAVE_LUA_DUMP true) + endif() + message("-- string.dump=${HAVE_LUA_DUMP}") + + if (ARGV2) + get_filename_component(BIN2C ${ARGV2} ABSOLUTE) + set(BIN2C ${LUA} ${BIN2C}) + else() + find_program(BIN2C NAMES bin2c bin2c.bat) + endif() + if (HAVE_LUA_DUMP) + if (ARGV3) + get_filename_component(LUAC ${ARGV3} ABSOLUTE) + set(LUAC ${LUA} ${LUAC}) + else() + find_program(LUAC NAMES luac luac.bat) + endif() + endif (HAVE_LUA_DUMP) + message("-- bin2c=${BIN2C}") + message("-- luac=${LUAC}") + + get_filename_component(SOURCEABS ${_source} ABSOLUTE) + if (HAVE_LUA_DUMP) + get_filename_component(SOURCEBASE ${_source} NAME_WE) + add_custom_command( + OUTPUT ${_target} DEPENDS ${_source} + COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS} + COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" ) + else() + add_custom_command( + OUTPUT ${_target} DEPENDS ${SOURCEABS} + COMMAND ${BIN2C} ${_source} ">${_target}" ) + endif() +endfunction(add_lua_bin2c) diff --git a/dist.info b/dist.info new file mode 100644 index 0000000..7688483 --- /dev/null +++ b/dist.info @@ -0,0 +1,15 @@ +--- This file is part of LuaDist project + +name = "luacrypto" +version = "0.2.0" + +desc = "LuaCrypto provides a Lua frontend to the OpenSSL cryptographic library." +author = "Keith Howe" +license = "MIT" +url = "http://luacrypto.luaforge.net/" +maintainer = "Peter Kapec" + +depends = { + "lua ~> 5.1", + "openssl >=0.9.7" +} From a0596f43267218cfe1f4b198496456af49adf955 Mon Sep 17 00:00:00 2001 From: David Manura Date: Thu, 7 Apr 2011 22:18:24 -0400 Subject: [PATCH 13/21] build: update dist.cmake --- CMakeLists.txt | 45 ++--- dist.cmake | 508 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 438 insertions(+), 115 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8435e1c..cc7b7c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,32 +1,33 @@ -# Copyright (C) 2007-2009 LuaDist. +# Copyright (C) 2007-2011 LuaDist. # Created by Peter Kapec # Redistribution and use of this file is allowed according to the terms of the MIT license. # For details see the COPYRIGHT file distributed with LuaDist. # Please note that the package source code is licensed under its own license. -PROJECT(luacrypto C) -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -INCLUDE(dist.cmake) +project ( luacrypto C ) +cmake_minimum_required ( VERSION 2.6 ) +include ( dist.cmake ) # Find libraries -FIND_PATH( OPENSSL_INCLUDE_DIR NAMES openssl/err.h) -INCLUDE_DIRECTORIES( ${OPENSSL_INCLUDE_DIR}) -FIND_LIBRARY ( CRYPTO_LIBRARY NAMES crypto ) -FIND_LIBRARY ( SSL_LIBRARY NAMES ssl) -IF(WIN32) - SET(LIBS gdi32) -ENDIF() -INCLUDE_DIRECTORIES(src) +find_path ( OPENSSL_INCLUDE_DIR NAMES openssl/err.h ) +include_directories ( ${OPENSSL_INCLUDE_DIR} ) +find_library ( CRYPTO_LIBRARY NAMES crypto ) +find_library ( SSL_LIBRARY NAMES ssl ) +if ( WIN32 ) + set ( LIBS gdi32 ) +endif () +include_directories ( src ) -IF (WIN32) - ADD_DEFINITIONS("-DLUACRYPTO_API=__declspec(dllexport)") -ENDIF (WIN32) +if ( WIN32 ) + add_definitions ( "-DLUACRYPTO_API=__declspec(dllexport)" ) +endif ( WIN32 ) -ADD_LUA_MODULE(crypto src/lcrypto.c) -TARGET_LINK_LIBRARIES(crypto ${CRYPTO_LIBRARY} ${SSL_LIBRARY} ${LIBS}) +install_lua_module ( crypto src/lcrypto.c ) +target_link_libraries ( crypto ${CRYPTO_LIBRARY} ${SSL_LIBRARY} ${LIBS}) -# Install all files and documentation -INSTALL (TARGETS crypto DESTINATION ${INSTALL_CMOD}) -INSTALL (FILES README DESTINATION ${INSTALL_DATA}) -INSTALL (DIRECTORY doc DESTINATION ${INSTALL_DATA}) -INSTALL (DIRECTORY tests/ DESTINATION ${INSTALL_TEST}) +install_data ( README ) +install_doc ( doc/ ) +install_test ( tests/ ) + +add_lua_test ( tests/test.lua ) #FIX: arg[1] +add_lua_test ( tests/rand.lua ) diff --git a/dist.cmake b/dist.cmake index 95928b2..2184da6 100644 --- a/dist.cmake +++ b/dist.cmake @@ -1,86 +1,408 @@ # LuaDist CMake utility library. # Provides variables and utility functions common to LuaDist CMake builds. # -# Copyright (C) 2007-2010 LuaDist. +# Copyright (C) 2007-2011 LuaDist. # by David Manura, Peter Drahos # Redistribution and use of this file is allowed according to the terms of the MIT license. # For details see the COPYRIGHT file distributed with LuaDist. # Please note that the package source code is licensed under its own license. -# Few convinence settings -SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true) -SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH}) +## INSTALL DEFAULTS (Relative to CMAKE_INSTALL_PREFIX) +# Primary paths +set ( INSTALL_BIN bin CACHE PATH "Where to install binaries to." ) +set ( INSTALL_LIB lib CACHE PATH "Where to install libraries to." ) +set ( INSTALL_INC include CACHE PATH "Where to install headers to." ) +set ( INSTALL_ETC etc CACHE PATH "Where to store configuration files" ) +set ( INSTALL_LMOD ${INSTALL_LIB}/lua CACHE PATH "Directory to install Lua modules." ) +set ( INSTALL_CMOD ${INSTALL_LIB}/lua CACHE PATH "Directory to install Lua binary modules." ) +set ( INSTALL_SHARE share CACHE PATH "Directory for shared data." ) -# Where to install module parts: -set(INSTALL_BIN bin CACHE PATH "Where to install binaries to.") -set(INSTALL_LIB lib CACHE PATH "Where to install libraries to.") -set(INSTALL_INC include CACHE PATH "Where to install headers to.") -set(INSTALL_ETC etc CACHE PATH "Where to store configuration files") -set(INSTALL_LMOD share/lua/lmod CACHE PATH "Directory to install Lua modules.") -set(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules.") -set(INSTALL_DATA share/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.") -set(INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.") -set(INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.") -set(INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.") -set(INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files") +# Secondary paths +set ( INSTALL_DATA ${INSTALL_SHARE}/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.") +set ( INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.") +set ( INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.") +set ( INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.") +set ( INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files") +# Skipable content, headers, binaries and libraries are always required +option ( SKIP_TESTING "Do not add tests." OFF) +option ( SKIP_LUA_WRAPPER "Do not build and install Lua executable wrappers." OFF) +option ( SKIP_INSTALL_DATA "Skip installing all data." OFF ) +if ( NOT SKIP_INSTALL_DATA ) + option ( SKIP_INSTALL_DOC "Skip installation of documentation." OFF ) + option ( SKIP_INSTALL_EXAMPLE "Skip installation of documentation." OFF ) + option ( SKIP_INSTALL_TEST "Skip installation of tests." OFF) + option ( SKIP_INSTALL_FOO "Skip installation of optional package content." OFF) +endif () + +# TWEAKS +# Setting CMAKE to use loose block and search for find modules in source directory +set ( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true ) +set ( CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH} ) # In MSVC, prevent warnings that can occur when using standard libraries. -if(MSVC) - add_definitions(-D_CRT_SECURE_NO_WARNINGS) -endif(MSVC) - -# Adds Lua shared library module target `_target`. -# Additional sources to build the module are listed after `_target`. -macro(add_lua_module _target) - find_package(Lua51 REQUIRED) - include_directories(${LUA_INCLUDE_DIR}) #2DO: somehow apply only to _target? - - add_library(${_target} MODULE ${ARGN}) - set_target_properties(${_target} PROPERTIES PREFIX "") - target_link_libraries(${_target} ${LUA_LIBRARY}) - - IF(WIN32) - set_target_properties(${_target} PROPERTIES LINK_FLAGS "-Wl,--enable-auto-import") - ENDIF() - -endmacro(add_lua_module) +if ( MSVC ) + add_definitions ( -D_CRT_SECURE_NO_WARNINGS ) +endif () + +## MACROS +# Parser macro +macro ( parse_arguments prefix arg_names option_names) + set ( DEFAULT_ARGS ) + foreach ( arg_name ${arg_names} ) + set ( ${prefix}_${arg_name} ) + endforeach () + foreach ( option ${option_names} ) + set ( ${prefix}_${option} FALSE ) + endforeach () + + set ( current_arg_name DEFAULT_ARGS ) + set ( current_arg_list ) + foreach ( arg ${ARGN} ) + set ( larg_names ${arg_names} ) + list ( FIND larg_names "${arg}" is_arg_name ) + if ( is_arg_name GREATER -1 ) + set ( ${prefix}_${current_arg_name} ${current_arg_list} ) + set ( current_arg_name ${arg} ) + set ( current_arg_list ) + else () + set ( loption_names ${option_names} ) + list ( FIND loption_names "${arg}" is_option ) + if ( is_option GREATER -1 ) + set ( ${prefix}_${arg} TRUE ) + else () + set ( current_arg_list ${current_arg_list} ${arg} ) + endif () + endif () + endforeach () + set ( ${prefix}_${current_arg_name} ${current_arg_list} ) +endmacro () + +# INSTALL_LUA_EXECUTABLE ( target source ) +# Automatically generate a binary wrapper for lua application and install it +# The wrapper and the source of the application will be placed into /bin +# If the application source did not have .lua suffix then it will be added +# USE: lua_executable ( sputnik src/sputnik.lua ) +macro ( install_lua_executable _name _source ) + get_filename_component ( _source_name ${_source} NAME_WE ) + if ( NOT SKIP_LUA_WRAPPER ) + enable_language ( C ) + + find_package ( Lua51 REQUIRED ) + include_directories ( ${LUA_INCLUDE_DIR} ) + + set ( _wrapper ${CMAKE_CURRENT_BINARY_DIR}/${_name}.c ) + set ( _code +"// Not so simple executable wrapper for Lua apps +#include +#include +#include +#include +#include + +lua_State *L\; + +static int getargs (lua_State *L, char **argv, int n) { +int narg\; +int i\; +int argc = 0\; +while (argv[argc]) argc++\; +narg = argc - (n + 1)\; +luaL_checkstack(L, narg + 3, \"too many arguments to script\")\; +for (i=n+1\; i < argc\; i++) + lua_pushstring(L, argv[i])\; +lua_createtable(L, narg, n + 1)\; +for (i=0\; i < argc\; i++) { + lua_pushstring(L, argv[i])\; + lua_rawseti(L, -2, i - n)\; +} +return narg\; +} + +static void lstop (lua_State *L, lua_Debug *ar) { +(void)ar\; +lua_sethook(L, NULL, 0, 0)\; +luaL_error(L, \"interrupted!\")\; +} + +static void laction (int i) { +signal(i, SIG_DFL)\; +lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1)\; +} + +static void l_message (const char *pname, const char *msg) { +if (pname) fprintf(stderr, \"%s: \", pname)\; +fprintf(stderr, \"%s\\n\", msg)\; +fflush(stderr)\; +} + +static int report (lua_State *L, int status) { +if (status && !lua_isnil(L, -1)) { + const char *msg = lua_tostring(L, -1)\; + if (msg == NULL) msg = \"(error object is not a string)\"\; + l_message(\"${_source_name}\", msg)\; + lua_pop(L, 1)\; +} +return status\; +} + +static int traceback (lua_State *L) { +if (!lua_isstring(L, 1)) + return 1\; +lua_getfield(L, LUA_GLOBALSINDEX, \"debug\")\; +if (!lua_istable(L, -1)) { + lua_pop(L, 1)\; + return 1\; +} +lua_getfield(L, -1, \"traceback\")\; +if (!lua_isfunction(L, -1)) { + lua_pop(L, 2)\; + return 1\; +} +lua_pushvalue(L, 1)\; +lua_pushinteger(L, 2)\; +lua_call(L, 2, 1)\; +return 1\; +} + +static int docall (lua_State *L, int narg, int clear) { +int status\; +int base = lua_gettop(L) - narg\; +lua_pushcfunction(L, traceback)\; +lua_insert(L, base)\; +signal(SIGINT, laction)\; +status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base)\; +signal(SIGINT, SIG_DFL)\; +lua_remove(L, base)\; +if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0)\; +return status\; +} + +int main (int argc, char **argv) { +L=lua_open()\; +lua_gc(L, LUA_GCSTOP, 0)\; +luaL_openlibs(L)\; +lua_gc(L, LUA_GCRESTART, 0)\; +int narg = getargs(L, argv, 0)\; +lua_setglobal(L, \"arg\")\; + +// Script +char script[500] = \"./${_source_name}.lua\"\; +lua_getglobal(L, \"_PROGDIR\")\; +if (lua_isstring(L, -1)) { + sprintf( script, \"%s/${_source_name}.lua\", lua_tostring(L, -1))\; +} +lua_pop(L, 1)\; +// Run +int status = luaL_loadfile(L, script)\; +lua_insert(L, -(narg+1))\; +if (status == 0) + status = docall(L, narg, 0)\; +else + lua_pop(L, narg)\; + +report(L, status)\; +lua_close(L)\; +return status\; +}; +") + file ( WRITE ${_wrapper} ${_code} ) + add_executable ( ${_name} ${_wrapper} ) + target_link_libraries ( ${_name} ${LUA_LIBRARY} ) + install ( TARGETS ${_name} DESTINATION ${INSTALL_BIN} ) + endif() + install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN} RENAME ${_source_name}.lua ) +endmacro () + +# INSTALL_LIBRARY +# Installs any libraries generated using "add_library" into apropriate places. +# USE: install_library ( libexpat ) +macro ( install_library ) + foreach ( _file ${ARGN} ) + install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN} LIBRARY DESTINATION ${INSTALL_LIB} ARCHIVE DESTINATION ${INSTALL_LIB} ) + endforeach() +endmacro () + +# INSTALL_EXECUTABLE +# Installs any executables generated using "add_executable". +# USE: install_executable ( lua ) +macro ( install_executable ) + foreach ( _file ${ARGN} ) + install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN} ) + endforeach() +endmacro () + +# INSTALL_HEADER +# Install a directories or files into header destination. +# USE: install_header ( lua.h luaconf.h ) or install_header ( GL ) +# NOTE: If headers need to be installed into subdirectories use the INSTALL command directly +macro ( install_header ) + parse_arguments ( _ARG "INTO" "" ${ARGN} ) + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} ) + endif () + endforeach() +endmacro () + +# INSTALL_DATA ( files/directories ) +# This installs additional data files or directories. +# USE: install_data ( extra data.dat ) +macro ( install_data ) + if ( NOT SKIP_INSTALL_DATA ) + parse_arguments ( _ARG "INTO" "" ${ARGN} ) + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO} ) + endif () + endforeach() + endif() +endmacro () + +# INSTALL_DOC ( files/directories ) +# This installs documentation content +# USE: install_doc ( doc/ ) +macro ( install_doc ) + if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_DOC ) + parse_arguments ( _ARG "INTO" "" ${ARGN} ) + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} ) + endif () + endforeach() + endif() +endmacro () + +# INSTALL_EXAMPLE ( files/directories ) +# This installs additional data +# USE: install_example ( examples/ exampleA.lua ) +macro ( install_example ) + if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_EXAMPLE ) + parse_arguments ( _ARG "INTO" "" ${ARGN} ) + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} ) + endif () + endforeach() + endif() +endmacro () + +# INSTALL_TEST ( files/directories ) +# This installs tests +# USE: install_example ( examples/ exampleA.lua ) +macro ( install_test ) + if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_TEST ) + parse_arguments ( _ARG "INTO" "" ${ARGN} ) + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} ) + endif () + endforeach() + endif() +endmacro () + +# INSTALL_FOO ( files/directories ) +# This installs optional content +# USE: install_foo ( examples/ exampleA.lua ) +macro ( install_foo ) + if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_FOO ) + parse_arguments ( _ARG "INTO" "" ${ARGN} ) + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} ) + endif () + endforeach() + endif() +endmacro () + +# INSTALL_LUA_MODULE +# This macro installs a lua source module into destination given by lua require syntax. +# Binary modules are also supported where this funcion takes sources and libraries to compile separated by LINK keyword +# USE: install_lua_module ( socket.http src/http.lua ) +# USE2: install_lua_module ( mime.core src/mime.c ) +# USE3: install_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} ) +macro (install_lua_module _name ) + string ( REPLACE "." "/" _module "${_name}" ) + string ( REPLACE "." "_" _target "${_name}" ) + + set ( _lua_module "${_module}.lua" ) + set ( _bin_module "${_module}${CMAKE_SHARED_MODULE_SUFFIX}" ) + + parse_arguments ( _MODULE "LINK" "" ${ARGN} ) + get_filename_component ( _ext ${ARGV1} EXT ) + if ( _ext STREQUAL ".lua" ) + get_filename_component ( _path ${_lua_module} PATH ) + get_filename_component ( _filename ${_lua_module} NAME ) + install ( FILES ${ARGV1} DESTINATION ${INSTALL_LMOD}/${_path} RENAME ${_filename} ) + else () + enable_language ( C ) + get_filename_component ( _module_name ${_bin_module} NAME_WE ) + get_filename_component ( _module_path ${_bin_module} PATH ) + + find_package ( Lua51 REQUIRED ) + include_directories ( ${LUA_INCLUDE_DIR} ) + + add_library( ${_target} MODULE ${_MODULE_DEFAULT_ARGS}) + target_link_libraries ( ${_target} ${LUA_LIBRARY} ${_MODULE_LINK} ) + set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${_module_path}" PREFIX "" OUTPUT_NAME "${_module_name}" ) + + install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_path}) + endif () +endmacro () + +# ADD_LUA_TEST # Runs Lua script `_testfile` under CTest tester. # Optional argument `_testcurrentdir` is current working directory to run test under # (defaults to ${CMAKE_CURRENT_BINARY_DIR}). # Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}. # Under LuaDist, set test=true in config.lua to enable testing. -macro(add_lua_test _testfile) - include(CTest) - if(BUILD_TESTING) - find_program(LUA NAMES lua lua.bat) - get_filename_component(TESTFILEABS ${_testfile} ABSOLUTE) - get_filename_component(TESTFILENAME ${_testfile} NAME) - get_filename_component(TESTFILEBASE ${_testfile} NAME_WE) +# USE: add_lua_test ( test/test1.lua ) +macro ( add_lua_test _testfile ) + if ( NOT SKIP_TESTING ) + include ( CTest ) + find_program ( LUA NAMES lua lua.bat ) + get_filename_component ( TESTFILEABS ${_testfile} ABSOLUTE ) + get_filename_component ( TESTFILENAME ${_testfile} NAME ) + get_filename_component ( TESTFILEBASE ${_testfile} NAME_WE ) # Write wrapper script. - set(TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME}) - set(TESTWRAPPERSOURCE -"package.path = '${CMAKE_CURRENT_BINARY_DIR}/?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;' .. package.path -package.cpath = '${CMAKE_CURRENT_BINARY_DIR}/?.so\;${CMAKE_CURRENT_BINARY_DIR}/?.dll\;' .. package.cpath + set ( TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME} ) + set ( TESTWRAPPERSOURCE +"local configuration = ... +local sodir = '${CMAKE_CURRENT_BINARY_DIR}' .. (configuration == '' and '' or '/' .. configuration) +package.path = sodir .. '/?.lua\;' .. sodir .. '/?.lua\;' .. package.path +package.cpath = sodir .. '/?.so\;' .. sodir .. '/?.dll\;' .. package.cpath +arg[0] = '${TESTFILEABS}' return dofile '${TESTFILEABS}' " ) - if(${ARGC} GREATER 1) - set(_testcurrentdir ${ARGV1}) - get_filename_component(TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE) - set(TESTWRAPPERSOURCE -"require 'lfs' -lfs.chdir('${TESTCURRENTDIRABS}') -${TESTWRAPPERSOURCE}") - endif() - FILE(WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) - - add_test(${TESTFILEBASE} ${LUA} ${TESTWRAPPER}) - endif(BUILD_TESTING) - + if ( ${ARGC} GREATER 1 ) + set ( _testcurrentdir ${ARGV1} ) + get_filename_component ( TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE ) + # note: CMake 2.6 (unlike 2.8) lacks WORKING_DIRECTORY parameter. +#old: +# set ( TESTWRAPPERSOURCE +#"require 'lfs'; lfs.chdir('${TESTCURRENTDIRABS}' ) +#${TESTWRAPPERSOURCE}" ) + set ( _pre ${CMAKE_COMMAND} -E chdir "${TESTCURRENTDIRABS}" ) + endif () + file ( WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) + add_test ( NAME ${TESTFILEBASE} COMMAND ${_pre} ${LUA} ${TESTWRAPPER} $ ) + endif () # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake -endmacro(add_lua_test) +endmacro () # Converts Lua source file `_source` to binary string embedded in C source # file `_target`. Optionally compiles Lua source to byte code (not available @@ -90,41 +412,41 @@ endmacro(add_lua_test) # # [1] http://lua-users.org/wiki/BinToCee # [2] http://lua-users.org/wiki/LuaCompilerInLua -function(add_lua_bin2c _target _source) - find_program(LUA NAMES lua lua.bat) - execute_process(COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET) - if (NOT ${_LUA_DUMP_RESULT}) - SET(HAVE_LUA_DUMP true) - endif() - message("-- string.dump=${HAVE_LUA_DUMP}") - - if (ARGV2) - get_filename_component(BIN2C ${ARGV2} ABSOLUTE) - set(BIN2C ${LUA} ${BIN2C}) - else() - find_program(BIN2C NAMES bin2c bin2c.bat) - endif() - if (HAVE_LUA_DUMP) - if (ARGV3) - get_filename_component(LUAC ${ARGV3} ABSOLUTE) - set(LUAC ${LUA} ${LUAC}) - else() - find_program(LUAC NAMES luac luac.bat) - endif() - endif (HAVE_LUA_DUMP) - message("-- bin2c=${BIN2C}") - message("-- luac=${LUAC}") - - get_filename_component(SOURCEABS ${_source} ABSOLUTE) - if (HAVE_LUA_DUMP) - get_filename_component(SOURCEBASE ${_source} NAME_WE) - add_custom_command( +function ( add_lua_bin2c _target _source ) + find_program ( LUA NAMES lua lua.bat ) + execute_process ( COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET ) + if ( NOT ${_LUA_DUMP_RESULT} ) + SET ( HAVE_LUA_DUMP true ) + endif () + message ( "-- string.dump=${HAVE_LUA_DUMP}" ) + + if ( ARGV2 ) + get_filename_component ( BIN2C ${ARGV2} ABSOLUTE ) + set ( BIN2C ${LUA} ${BIN2C} ) + else () + find_program ( BIN2C NAMES bin2c bin2c.bat ) + endif () + if ( HAVE_LUA_DUMP ) + if ( ARGV3 ) + get_filename_component ( LUAC ${ARGV3} ABSOLUTE ) + set ( LUAC ${LUA} ${LUAC} ) + else () + find_program ( LUAC NAMES luac luac.bat ) + endif () + endif ( HAVE_LUA_DUMP ) + message ( "-- bin2c=${BIN2C}" ) + message ( "-- luac=${LUAC}" ) + + get_filename_component ( SOURCEABS ${_source} ABSOLUTE ) + if ( HAVE_LUA_DUMP ) + get_filename_component ( SOURCEBASE ${_source} NAME_WE ) + add_custom_command ( OUTPUT ${_target} DEPENDS ${_source} COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS} COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" ) - else() - add_custom_command( + else () + add_custom_command ( OUTPUT ${_target} DEPENDS ${SOURCEABS} COMMAND ${BIN2C} ${_source} ">${_target}" ) - endif() -endfunction(add_lua_bin2c) + endif () +endfunction() From 9c64e229cf96eef658f47eb321f4c9dbf2f3ba4b Mon Sep 17 00:00:00 2001 From: Michal Kottman Date: Thu, 1 Mar 2012 02:03:59 +0100 Subject: [PATCH 14/21] Update dist.info to version 0.3.0 --- dist.info | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dist.info b/dist.info index 7688483..6842a33 100644 --- a/dist.info +++ b/dist.info @@ -1,13 +1,13 @@ --- This file is part of LuaDist project name = "luacrypto" -version = "0.2.0" +version = "0.3.0" desc = "LuaCrypto provides a Lua frontend to the OpenSSL cryptographic library." author = "Keith Howe" license = "MIT" -url = "http://luacrypto.luaforge.net/" -maintainer = "Peter Kapec" +url = "http://mkottman.github.com/luacrypto/" +maintainer = "Peter Kapec, Michal Kottman" depends = { "lua ~> 5.1", From afc53009d821a0f610ec3618300961235a7b639a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Draho=C5=A1?= Date: Tue, 3 Apr 2012 02:07:52 +0200 Subject: [PATCH 15/21] Adding travis build --- .travis.yml | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4bba101 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,50 @@ +# +# LuaDist Travis-CI Hook +# + +# Since CMake is not directly supported we use erlang VMs +language: erlang + +# Try using multiple Lua Implementations +env: + - LUA="" # Use automatic dependencies + - LUA="luajit" # Try with LuaJIT +# - CMAKE="-DCMAKE_VARIABLE=value" +# - LUA="lua-5.1.5" + +# Allow luajit to fail +matrix: + allow_failures: + - env: LUA="luajit" + +# We need CMake and LuaDist +install: + - export MODULE=`basename $PWD` + - sudo apt-get install cmake >/dev/null 2>&1 + - git clone git://github.com/LuaDist/bootstrap.git _luadist >/dev/null 2>&1 + - cd _luadist + - git submodule update --init >/dev/null 2>&1 + - ./bootstrap >/dev/null 2>&1 + - export LUADIST=$PWD/_install/bin/luadist + - cd $HOME + +# Use LuaDist to deploy the module +script: + - $LUADIST _test install $LUA $MODULE-scm $CMAKE -verbose=true -test=true + +# Execute additional tests or commands +#after_script: +# - [run additional test commans] + +# Only watch the master branch +branches: + only: + - master + +# Notify the LuaDist Dev group if needed +notifications: + recipients: + - luadist-dev@googlegroups.com + email: + on_success: change + on_failure: always \ No newline at end of file From 1b170d72ab13173b3481989ec9d1f6bbf982eae3 Mon Sep 17 00:00:00 2001 From: David Manura Date: Mon, 23 Apr 2012 22:01:33 -0400 Subject: [PATCH 16/21] cmake: update dist.cmake --- dist.cmake | 452 ----------------------------------------------------- 1 file changed, 452 deletions(-) delete mode 100644 dist.cmake diff --git a/dist.cmake b/dist.cmake deleted file mode 100644 index 2184da6..0000000 --- a/dist.cmake +++ /dev/null @@ -1,452 +0,0 @@ -# LuaDist CMake utility library. -# Provides variables and utility functions common to LuaDist CMake builds. -# -# Copyright (C) 2007-2011 LuaDist. -# by David Manura, Peter Drahos -# Redistribution and use of this file is allowed according to the terms of the MIT license. -# For details see the COPYRIGHT file distributed with LuaDist. -# Please note that the package source code is licensed under its own license. - -## INSTALL DEFAULTS (Relative to CMAKE_INSTALL_PREFIX) -# Primary paths -set ( INSTALL_BIN bin CACHE PATH "Where to install binaries to." ) -set ( INSTALL_LIB lib CACHE PATH "Where to install libraries to." ) -set ( INSTALL_INC include CACHE PATH "Where to install headers to." ) -set ( INSTALL_ETC etc CACHE PATH "Where to store configuration files" ) -set ( INSTALL_LMOD ${INSTALL_LIB}/lua CACHE PATH "Directory to install Lua modules." ) -set ( INSTALL_CMOD ${INSTALL_LIB}/lua CACHE PATH "Directory to install Lua binary modules." ) -set ( INSTALL_SHARE share CACHE PATH "Directory for shared data." ) - -# Secondary paths -set ( INSTALL_DATA ${INSTALL_SHARE}/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.") -set ( INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.") -set ( INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.") -set ( INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.") -set ( INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files") - -# Skipable content, headers, binaries and libraries are always required -option ( SKIP_TESTING "Do not add tests." OFF) -option ( SKIP_LUA_WRAPPER "Do not build and install Lua executable wrappers." OFF) -option ( SKIP_INSTALL_DATA "Skip installing all data." OFF ) -if ( NOT SKIP_INSTALL_DATA ) - option ( SKIP_INSTALL_DOC "Skip installation of documentation." OFF ) - option ( SKIP_INSTALL_EXAMPLE "Skip installation of documentation." OFF ) - option ( SKIP_INSTALL_TEST "Skip installation of tests." OFF) - option ( SKIP_INSTALL_FOO "Skip installation of optional package content." OFF) -endif () - -# TWEAKS -# Setting CMAKE to use loose block and search for find modules in source directory -set ( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true ) -set ( CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH} ) - -# In MSVC, prevent warnings that can occur when using standard libraries. -if ( MSVC ) - add_definitions ( -D_CRT_SECURE_NO_WARNINGS ) -endif () - -## MACROS -# Parser macro -macro ( parse_arguments prefix arg_names option_names) - set ( DEFAULT_ARGS ) - foreach ( arg_name ${arg_names} ) - set ( ${prefix}_${arg_name} ) - endforeach () - foreach ( option ${option_names} ) - set ( ${prefix}_${option} FALSE ) - endforeach () - - set ( current_arg_name DEFAULT_ARGS ) - set ( current_arg_list ) - foreach ( arg ${ARGN} ) - set ( larg_names ${arg_names} ) - list ( FIND larg_names "${arg}" is_arg_name ) - if ( is_arg_name GREATER -1 ) - set ( ${prefix}_${current_arg_name} ${current_arg_list} ) - set ( current_arg_name ${arg} ) - set ( current_arg_list ) - else () - set ( loption_names ${option_names} ) - list ( FIND loption_names "${arg}" is_option ) - if ( is_option GREATER -1 ) - set ( ${prefix}_${arg} TRUE ) - else () - set ( current_arg_list ${current_arg_list} ${arg} ) - endif () - endif () - endforeach () - set ( ${prefix}_${current_arg_name} ${current_arg_list} ) -endmacro () - -# INSTALL_LUA_EXECUTABLE ( target source ) -# Automatically generate a binary wrapper for lua application and install it -# The wrapper and the source of the application will be placed into /bin -# If the application source did not have .lua suffix then it will be added -# USE: lua_executable ( sputnik src/sputnik.lua ) -macro ( install_lua_executable _name _source ) - get_filename_component ( _source_name ${_source} NAME_WE ) - if ( NOT SKIP_LUA_WRAPPER ) - enable_language ( C ) - - find_package ( Lua51 REQUIRED ) - include_directories ( ${LUA_INCLUDE_DIR} ) - - set ( _wrapper ${CMAKE_CURRENT_BINARY_DIR}/${_name}.c ) - set ( _code -"// Not so simple executable wrapper for Lua apps -#include -#include -#include -#include -#include - -lua_State *L\; - -static int getargs (lua_State *L, char **argv, int n) { -int narg\; -int i\; -int argc = 0\; -while (argv[argc]) argc++\; -narg = argc - (n + 1)\; -luaL_checkstack(L, narg + 3, \"too many arguments to script\")\; -for (i=n+1\; i < argc\; i++) - lua_pushstring(L, argv[i])\; -lua_createtable(L, narg, n + 1)\; -for (i=0\; i < argc\; i++) { - lua_pushstring(L, argv[i])\; - lua_rawseti(L, -2, i - n)\; -} -return narg\; -} - -static void lstop (lua_State *L, lua_Debug *ar) { -(void)ar\; -lua_sethook(L, NULL, 0, 0)\; -luaL_error(L, \"interrupted!\")\; -} - -static void laction (int i) { -signal(i, SIG_DFL)\; -lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1)\; -} - -static void l_message (const char *pname, const char *msg) { -if (pname) fprintf(stderr, \"%s: \", pname)\; -fprintf(stderr, \"%s\\n\", msg)\; -fflush(stderr)\; -} - -static int report (lua_State *L, int status) { -if (status && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1)\; - if (msg == NULL) msg = \"(error object is not a string)\"\; - l_message(\"${_source_name}\", msg)\; - lua_pop(L, 1)\; -} -return status\; -} - -static int traceback (lua_State *L) { -if (!lua_isstring(L, 1)) - return 1\; -lua_getfield(L, LUA_GLOBALSINDEX, \"debug\")\; -if (!lua_istable(L, -1)) { - lua_pop(L, 1)\; - return 1\; -} -lua_getfield(L, -1, \"traceback\")\; -if (!lua_isfunction(L, -1)) { - lua_pop(L, 2)\; - return 1\; -} -lua_pushvalue(L, 1)\; -lua_pushinteger(L, 2)\; -lua_call(L, 2, 1)\; -return 1\; -} - -static int docall (lua_State *L, int narg, int clear) { -int status\; -int base = lua_gettop(L) - narg\; -lua_pushcfunction(L, traceback)\; -lua_insert(L, base)\; -signal(SIGINT, laction)\; -status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base)\; -signal(SIGINT, SIG_DFL)\; -lua_remove(L, base)\; -if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0)\; -return status\; -} - -int main (int argc, char **argv) { -L=lua_open()\; -lua_gc(L, LUA_GCSTOP, 0)\; -luaL_openlibs(L)\; -lua_gc(L, LUA_GCRESTART, 0)\; -int narg = getargs(L, argv, 0)\; -lua_setglobal(L, \"arg\")\; - -// Script -char script[500] = \"./${_source_name}.lua\"\; -lua_getglobal(L, \"_PROGDIR\")\; -if (lua_isstring(L, -1)) { - sprintf( script, \"%s/${_source_name}.lua\", lua_tostring(L, -1))\; -} -lua_pop(L, 1)\; - -// Run -int status = luaL_loadfile(L, script)\; -lua_insert(L, -(narg+1))\; -if (status == 0) - status = docall(L, narg, 0)\; -else - lua_pop(L, narg)\; - -report(L, status)\; -lua_close(L)\; -return status\; -}; -") - file ( WRITE ${_wrapper} ${_code} ) - add_executable ( ${_name} ${_wrapper} ) - target_link_libraries ( ${_name} ${LUA_LIBRARY} ) - install ( TARGETS ${_name} DESTINATION ${INSTALL_BIN} ) - endif() - install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN} RENAME ${_source_name}.lua ) -endmacro () - -# INSTALL_LIBRARY -# Installs any libraries generated using "add_library" into apropriate places. -# USE: install_library ( libexpat ) -macro ( install_library ) - foreach ( _file ${ARGN} ) - install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN} LIBRARY DESTINATION ${INSTALL_LIB} ARCHIVE DESTINATION ${INSTALL_LIB} ) - endforeach() -endmacro () - -# INSTALL_EXECUTABLE -# Installs any executables generated using "add_executable". -# USE: install_executable ( lua ) -macro ( install_executable ) - foreach ( _file ${ARGN} ) - install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN} ) - endforeach() -endmacro () - -# INSTALL_HEADER -# Install a directories or files into header destination. -# USE: install_header ( lua.h luaconf.h ) or install_header ( GL ) -# NOTE: If headers need to be installed into subdirectories use the INSTALL command directly -macro ( install_header ) - parse_arguments ( _ARG "INTO" "" ${ARGN} ) - foreach ( _file ${_ARG_DEFAULT_ARGS} ) - if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) - install ( DIRECTORY ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} ) - else () - install ( FILES ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} ) - endif () - endforeach() -endmacro () - -# INSTALL_DATA ( files/directories ) -# This installs additional data files or directories. -# USE: install_data ( extra data.dat ) -macro ( install_data ) - if ( NOT SKIP_INSTALL_DATA ) - parse_arguments ( _ARG "INTO" "" ${ARGN} ) - foreach ( _file ${_ARG_DEFAULT_ARGS} ) - if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) - install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO} ) - else () - install ( FILES ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO} ) - endif () - endforeach() - endif() -endmacro () - -# INSTALL_DOC ( files/directories ) -# This installs documentation content -# USE: install_doc ( doc/ ) -macro ( install_doc ) - if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_DOC ) - parse_arguments ( _ARG "INTO" "" ${ARGN} ) - foreach ( _file ${_ARG_DEFAULT_ARGS} ) - if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) - install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} ) - else () - install ( FILES ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} ) - endif () - endforeach() - endif() -endmacro () - -# INSTALL_EXAMPLE ( files/directories ) -# This installs additional data -# USE: install_example ( examples/ exampleA.lua ) -macro ( install_example ) - if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_EXAMPLE ) - parse_arguments ( _ARG "INTO" "" ${ARGN} ) - foreach ( _file ${_ARG_DEFAULT_ARGS} ) - if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) - install ( DIRECTORY ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} ) - else () - install ( FILES ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} ) - endif () - endforeach() - endif() -endmacro () - -# INSTALL_TEST ( files/directories ) -# This installs tests -# USE: install_example ( examples/ exampleA.lua ) -macro ( install_test ) - if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_TEST ) - parse_arguments ( _ARG "INTO" "" ${ARGN} ) - foreach ( _file ${_ARG_DEFAULT_ARGS} ) - if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) - install ( DIRECTORY ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} ) - else () - install ( FILES ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} ) - endif () - endforeach() - endif() -endmacro () - -# INSTALL_FOO ( files/directories ) -# This installs optional content -# USE: install_foo ( examples/ exampleA.lua ) -macro ( install_foo ) - if ( NOT SKIP_INSTALL_DATA AND NOT SKIP_INSTALL_FOO ) - parse_arguments ( _ARG "INTO" "" ${ARGN} ) - foreach ( _file ${_ARG_DEFAULT_ARGS} ) - if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) - install ( DIRECTORY ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} ) - else () - install ( FILES ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} ) - endif () - endforeach() - endif() -endmacro () - -# INSTALL_LUA_MODULE -# This macro installs a lua source module into destination given by lua require syntax. -# Binary modules are also supported where this funcion takes sources and libraries to compile separated by LINK keyword -# USE: install_lua_module ( socket.http src/http.lua ) -# USE2: install_lua_module ( mime.core src/mime.c ) -# USE3: install_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} ) -macro (install_lua_module _name ) - string ( REPLACE "." "/" _module "${_name}" ) - string ( REPLACE "." "_" _target "${_name}" ) - - set ( _lua_module "${_module}.lua" ) - set ( _bin_module "${_module}${CMAKE_SHARED_MODULE_SUFFIX}" ) - - parse_arguments ( _MODULE "LINK" "" ${ARGN} ) - get_filename_component ( _ext ${ARGV1} EXT ) - if ( _ext STREQUAL ".lua" ) - get_filename_component ( _path ${_lua_module} PATH ) - get_filename_component ( _filename ${_lua_module} NAME ) - install ( FILES ${ARGV1} DESTINATION ${INSTALL_LMOD}/${_path} RENAME ${_filename} ) - else () - enable_language ( C ) - get_filename_component ( _module_name ${_bin_module} NAME_WE ) - get_filename_component ( _module_path ${_bin_module} PATH ) - - find_package ( Lua51 REQUIRED ) - include_directories ( ${LUA_INCLUDE_DIR} ) - - add_library( ${_target} MODULE ${_MODULE_DEFAULT_ARGS}) - target_link_libraries ( ${_target} ${LUA_LIBRARY} ${_MODULE_LINK} ) - set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${_module_path}" PREFIX "" OUTPUT_NAME "${_module_name}" ) - - install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_path}) - endif () -endmacro () - -# ADD_LUA_TEST -# Runs Lua script `_testfile` under CTest tester. -# Optional argument `_testcurrentdir` is current working directory to run test under -# (defaults to ${CMAKE_CURRENT_BINARY_DIR}). -# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}. -# Under LuaDist, set test=true in config.lua to enable testing. -# USE: add_lua_test ( test/test1.lua ) -macro ( add_lua_test _testfile ) - if ( NOT SKIP_TESTING ) - include ( CTest ) - find_program ( LUA NAMES lua lua.bat ) - get_filename_component ( TESTFILEABS ${_testfile} ABSOLUTE ) - get_filename_component ( TESTFILENAME ${_testfile} NAME ) - get_filename_component ( TESTFILEBASE ${_testfile} NAME_WE ) - - # Write wrapper script. - set ( TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME} ) - set ( TESTWRAPPERSOURCE -"local configuration = ... -local sodir = '${CMAKE_CURRENT_BINARY_DIR}' .. (configuration == '' and '' or '/' .. configuration) -package.path = sodir .. '/?.lua\;' .. sodir .. '/?.lua\;' .. package.path -package.cpath = sodir .. '/?.so\;' .. sodir .. '/?.dll\;' .. package.cpath -arg[0] = '${TESTFILEABS}' -return dofile '${TESTFILEABS}' -" ) - if ( ${ARGC} GREATER 1 ) - set ( _testcurrentdir ${ARGV1} ) - get_filename_component ( TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE ) - # note: CMake 2.6 (unlike 2.8) lacks WORKING_DIRECTORY parameter. -#old: -# set ( TESTWRAPPERSOURCE -#"require 'lfs'; lfs.chdir('${TESTCURRENTDIRABS}' ) -#${TESTWRAPPERSOURCE}" ) - set ( _pre ${CMAKE_COMMAND} -E chdir "${TESTCURRENTDIRABS}" ) - endif () - file ( WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) - add_test ( NAME ${TESTFILEBASE} COMMAND ${_pre} ${LUA} ${TESTWRAPPER} $ ) - endif () - # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake -endmacro () - -# Converts Lua source file `_source` to binary string embedded in C source -# file `_target`. Optionally compiles Lua source to byte code (not available -# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua -# versions of bin2c [1] and luac [2] may be passed respectively as additional -# arguments. -# -# [1] http://lua-users.org/wiki/BinToCee -# [2] http://lua-users.org/wiki/LuaCompilerInLua -function ( add_lua_bin2c _target _source ) - find_program ( LUA NAMES lua lua.bat ) - execute_process ( COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET ) - if ( NOT ${_LUA_DUMP_RESULT} ) - SET ( HAVE_LUA_DUMP true ) - endif () - message ( "-- string.dump=${HAVE_LUA_DUMP}" ) - - if ( ARGV2 ) - get_filename_component ( BIN2C ${ARGV2} ABSOLUTE ) - set ( BIN2C ${LUA} ${BIN2C} ) - else () - find_program ( BIN2C NAMES bin2c bin2c.bat ) - endif () - if ( HAVE_LUA_DUMP ) - if ( ARGV3 ) - get_filename_component ( LUAC ${ARGV3} ABSOLUTE ) - set ( LUAC ${LUA} ${LUAC} ) - else () - find_program ( LUAC NAMES luac luac.bat ) - endif () - endif ( HAVE_LUA_DUMP ) - message ( "-- bin2c=${BIN2C}" ) - message ( "-- luac=${LUAC}" ) - - get_filename_component ( SOURCEABS ${_source} ABSOLUTE ) - if ( HAVE_LUA_DUMP ) - get_filename_component ( SOURCEBASE ${_source} NAME_WE ) - add_custom_command ( - OUTPUT ${_target} DEPENDS ${_source} - COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS} - COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" ) - else () - add_custom_command ( - OUTPUT ${_target} DEPENDS ${SOURCEABS} - COMMAND ${BIN2C} ${_source} ">${_target}" ) - endif () -endfunction() From de987d7e51a1be821b83ca35c2578e3e1dba4c23 Mon Sep 17 00:00:00 2001 From: David Manura Date: Mon, 23 Apr 2012 22:14:59 -0400 Subject: [PATCH 17/21] cmake: update CMakeLists.txt and dist.info after merge --- CMakeLists.txt | 4 ++-- dist.info | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6abeb5b..c7c0a0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,11 +26,11 @@ endif ( WIN32 ) install_lua_module ( crypto src/lcrypto.c ) target_link_libraries ( crypto ${CRYPTO_LIBRARY} ${SSL_LIBRARY} ${LIBS}) -install_data ( README ) +install_data ( COPYING ChangeLog README NEWS ) install_doc ( doc/ PATTERN Makefile* EXCLUDE ) install_test ( tests/ PATTERN Makefile* EXCLUDE ) -add_lua_test ( tests/test.lua WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) +add_lua_test ( tests/test.lua WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests ) add_lua_test ( tests/rand.lua ) add_lua_test ( tests/encrypt.lua ) add_lua_test ( tests/pkeytest.lua ) diff --git a/dist.info b/dist.info index 6842a33..8e5bdb0 100644 --- a/dist.info +++ b/dist.info @@ -1,7 +1,7 @@ --- This file is part of LuaDist project name = "luacrypto" -version = "0.3.0" +version = "0.3.0.20120420" desc = "LuaCrypto provides a Lua frontend to the OpenSSL cryptographic library." author = "Keith Howe" From 8a89409cc3711b05dc8ab1781203d93d1ba1c148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Drahos=CC=8C?= Date: Sat, 25 Aug 2012 04:00:44 +0200 Subject: [PATCH 18/21] Updated cmake macros --- .travis.yml | 45 ++++--- CMakeLists.txt | 11 +- cmake/FindLua.cmake | 118 ++++++++++++++++ cmake/dist.cmake | 321 ++++++++++++++++++++++++++++++++++++++++++++ cmake/lua.cmake | 293 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 760 insertions(+), 28 deletions(-) create mode 100644 cmake/FindLua.cmake create mode 100644 cmake/dist.cmake create mode 100644 cmake/lua.cmake diff --git a/.travis.yml b/.travis.yml index 4bba101..049d304 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,39 +2,40 @@ # LuaDist Travis-CI Hook # -# Since CMake is not directly supported we use erlang VMs -language: erlang - +# We assume C build environments +language: C + # Try using multiple Lua Implementations env: - - LUA="" # Use automatic dependencies - - LUA="luajit" # Try with LuaJIT -# - CMAKE="-DCMAKE_VARIABLE=value" -# - LUA="lua-5.1.5" + - TOOL="" # Use native compiler (GCC usually) + - COMPILER="clang" # Use clang + - TOOL="i686-w64-mingw32" # 32bit MinGW + - TOOL="x86_64-w64-mingw32" # 64bit MinGW + - TOOL="arm-linux-gnueabihf" # ARM hard-float (hf), linux -# Allow luajit to fail +# Crosscompile builds may fail matrix: allow_failures: - - env: LUA="luajit" - -# We need CMake and LuaDist + - env: TOOL="i686-w64-mingw32" + - env: TOOL="x86_64-w64-mingw32" + - env: TOOL="arm-linux-gnueabihf" + +# Install dependencies install: - - export MODULE=`basename $PWD` - - sudo apt-get install cmake >/dev/null 2>&1 - - git clone git://github.com/LuaDist/bootstrap.git _luadist >/dev/null 2>&1 - - cd _luadist - - git submodule update --init >/dev/null 2>&1 - - ./bootstrap >/dev/null 2>&1 - - export LUADIST=$PWD/_install/bin/luadist - - cd $HOME + - git clone git://github.com/LuaDist/_util.git ~/_util + - ~/_util/travis install + +# Bootstap +before_script: + - ~/_util/travis bootstrap -# Use LuaDist to deploy the module +# Build the module script: - - $LUADIST _test install $LUA $MODULE-scm $CMAKE -verbose=true -test=true + - ~/_util/travis build # Execute additional tests or commands #after_script: -# - [run additional test commans] +# - ~/_util/travis test # Only watch the master branch branches: diff --git a/CMakeLists.txt b/CMakeLists.txt index c7c0a0d..d4a831a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,11 @@ -# Copyright (C) 2007-2011 LuaDist. +# Copyright (C) 2007-2012 LuaDist. # Created by Peter Kapec # Redistribution and use of this file is allowed according to the terms of the MIT license. # For details see the COPYRIGHT file distributed with LuaDist. # Please note that the package source code is licensed under its own license. project ( luacrypto C ) -cmake_minimum_required ( VERSION 2.6 ) +cmake_minimum_required ( VERSION 2.8 ) include ( cmake/dist.cmake ) include ( lua ) @@ -15,16 +15,15 @@ include_directories ( ${OPENSSL_INCLUDE_DIR} ) find_library ( CRYPTO_LIBRARY NAMES crypto ) find_library ( SSL_LIBRARY NAMES ssl ) if ( WIN32 ) - set ( LIBS gdi32 ) -endif () + set ( LIBS gdi32 ) +endif ( ) include_directories ( src ) if ( WIN32 ) add_definitions ( "-DLUACRYPTO_API=__declspec(dllexport)" ) endif ( WIN32 ) -install_lua_module ( crypto src/lcrypto.c ) -target_link_libraries ( crypto ${CRYPTO_LIBRARY} ${SSL_LIBRARY} ${LIBS}) +install_lua_module ( crypto src/lcrypto.c LINK ${CRYPTO_LIBRARY} ${SSL_LIBRARY} ${LIBS} ) install_data ( COPYING ChangeLog README NEWS ) install_doc ( doc/ PATTERN Makefile* EXCLUDE ) diff --git a/cmake/FindLua.cmake b/cmake/FindLua.cmake new file mode 100644 index 0000000..7fb7ca3 --- /dev/null +++ b/cmake/FindLua.cmake @@ -0,0 +1,118 @@ +# Locate Lua library +# This module defines +# LUA_EXECUTABLE, if found +# LUA_FOUND, if false, do not try to link to Lua +# LUA_LIBRARIES +# LUA_INCLUDE_DIR, where to find lua.h +# LUA_VERSION_STRING, the version of Lua found (since CMake 2.8.8) +# +# Note that the expected include convention is +# #include "lua.h" +# and not +# #include +# This is because, the lua location is not standardized and may exist +# in locations other than lua/ + +#============================================================================= +# Copyright 2007-2009 Kitware, Inc. +# Modified to support Lua 5.2 by LuaDist 2012 +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) +# +# The required version of Lua can be specified using the +# standard syntax, e.g. FIND_PACKAGE(Lua 5.1) +# Otherwise the module will search for any available Lua implementation + +# Always search for non-versioned lua first (recommended) +SET(_POSSIBLE_LUA_INCLUDE include include/lua) +SET(_POSSIBLE_LUA_EXECUTABLE lua) +SET(_POSSIBLE_LUA_LIBRARY lua) + +# Determine possible naming suffixes (there is no standard for this) +IF(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR) + SET(_POSSIBLE_SUFFIXES "${Lua_FIND_VERSION_MAJOR}${Lua_FIND_VERSION_MINOR}" "${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}" "-${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}") +ELSE(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR) + SET(_POSSIBLE_SUFFIXES "52" "5.2" "-5.2" "51" "5.1" "-5.1") +ENDIF(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR) + +# Set up possible search names and locations +FOREACH(_SUFFIX ${_POSSIBLE_SUFFIXES}) + LIST(APPEND _POSSIBLE_LUA_INCLUDE "include/lua${_SUFFIX}") + LIST(APPEND _POSSIBLE_LUA_EXECUTABLE "lua${_SUFFIX}") + LIST(APPEND _POSSIBLE_LUA_LIBRARY "lua${_SUFFIX}") +ENDFOREACH(_SUFFIX) + +# Find the lua executable +FIND_PROGRAM(LUA_EXECUTABLE + NAMES ${_POSSIBLE_LUA_EXECUTABLE} +) + +# Find the lua header +FIND_PATH(LUA_INCLUDE_DIR lua.h + HINTS + $ENV{LUA_DIR} + PATH_SUFFIXES ${_POSSIBLE_LUA_INCLUDE} + PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt +) + +# Find the lua library +FIND_LIBRARY(LUA_LIBRARY + NAMES ${_POSSIBLE_LUA_LIBRARY} + HINTS + $ENV{LUA_DIR} + PATH_SUFFIXES lib64 lib + PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt +) + +IF(LUA_LIBRARY) + # include the math library for Unix + IF(UNIX AND NOT APPLE) + FIND_LIBRARY(LUA_MATH_LIBRARY m) + SET( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries") + # For Windows and Mac, don't need to explicitly include the math library + ELSE(UNIX AND NOT APPLE) + SET( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries") + ENDIF(UNIX AND NOT APPLE) +ENDIF(LUA_LIBRARY) + +# Determine Lua version +IF(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") + FILE(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua .+\"") + + STRING(REGEX REPLACE "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([^\"]+)\".*" "\\1" LUA_VERSION_STRING "${lua_version_str}") + UNSET(lua_version_str) +ENDIF() + +INCLUDE(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if +# all listed variables are TRUE +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua + REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR + VERSION_VAR LUA_VERSION_STRING) + +MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY LUA_EXECUTABLE) + diff --git a/cmake/dist.cmake b/cmake/dist.cmake new file mode 100644 index 0000000..310ef94 --- /dev/null +++ b/cmake/dist.cmake @@ -0,0 +1,321 @@ +# LuaDist CMake utility library. +# Provides sane project defaults and macros common to LuaDist CMake builds. +# +# Copyright (C) 2007-2012 LuaDist. +# by David Manura, Peter Drahoš +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Please note that the package source code is licensed under its own license. + +## Extract information from dist.info +if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/dist.info ) + message ( FATAL_ERROR + "Missing dist.info file (${CMAKE_CURRENT_SOURCE_DIR}/dist.info)." ) +endif () +file ( READ ${CMAKE_CURRENT_SOURCE_DIR}/dist.info DIST_INFO ) +if ( "${DIST_INFO}" STREQUAL "" ) + message ( FATAL_ERROR "Failed to load dist.info." ) +endif () +# Reads field `name` from dist.info string `DIST_INFO` into variable `var`. +macro ( _parse_dist_field name var ) + string ( REGEX REPLACE ".*${name}[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1" + ${var} "${DIST_INFO}" ) + if ( ${var} STREQUAL DIST_INFO ) + message ( FATAL_ERROR "Failed to extract \"${var}\" from dist.info" ) + endif () +endmacro () +# +_parse_dist_field ( name DIST_NAME ) +_parse_dist_field ( version DIST_VERSION ) +_parse_dist_field ( license DIST_LICENSE ) +_parse_dist_field ( author DIST_AUTHOR ) +_parse_dist_field ( maintainer DIST_MAINTAINER ) +_parse_dist_field ( url DIST_URL ) +_parse_dist_field ( desc DIST_DESC ) +message ( "DIST_NAME: ${DIST_NAME}") +message ( "DIST_VERSION: ${DIST_VERSION}") +message ( "DIST_LICENSE: ${DIST_LICENSE}") +message ( "DIST_AUTHOR: ${DIST_AUTHOR}") +message ( "DIST_MAINTAINER: ${DIST_MAINTAINER}") +message ( "DIST_URL: ${DIST_URL}") +message ( "DIST_DESC: ${DIST_DESC}") +string ( REGEX REPLACE ".*depends[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1" + DIST_DEPENDS ${DIST_INFO} ) +if ( DIST_DEPENDS STREQUAL DIST_INFO ) + set ( DIST_DEPENDS "" ) +endif () +message ( "DIST_DEPENDS: ${DIST_DEPENDS}") +## 2DO: Parse DIST_DEPENDS and try to install Dependencies with automatically using externalproject_add + + +## INSTALL DEFAULTS (Relative to CMAKE_INSTALL_PREFIX) +# Primary paths +set ( INSTALL_BIN bin CACHE PATH "Where to install binaries to." ) +set ( INSTALL_LIB lib CACHE PATH "Where to install libraries to." ) +set ( INSTALL_INC include CACHE PATH "Where to install headers to." ) +set ( INSTALL_ETC etc CACHE PATH "Where to store configuration files" ) +set ( INSTALL_SHARE share CACHE PATH "Directory for shared data." ) + +# Secondary paths +option ( INSTALL_VERSION + "Install runtime libraries and executables with version information." OFF) +set ( INSTALL_DATA ${INSTALL_SHARE}/${DIST_NAME} CACHE PATH + "Directory the package can store documentation, tests or other data in.") +set ( INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH + "Recommended directory to install documentation into.") +set ( INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH + "Recommended directory to install examples into.") +set ( INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH + "Recommended directory to install tests into.") +set ( INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH + "Where to install additional files") + +# Tweaks and other defaults +# Setting CMAKE to use loose block and search for find modules in source directory +set ( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true ) +set ( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH} ) +option ( BUILD_SHARED_LIBS "Build shared libraries" ON ) + +# In MSVC, prevent warnings that can occur when using standard libraries. +if ( MSVC ) + add_definitions ( -D_CRT_SECURE_NO_WARNINGS ) +endif () + +# RPath and relative linking +option ( USE_RPATH "Use relative linking." ON) +if ( USE_RPATH ) + string ( REGEX REPLACE "[^!/]+" ".." UP_DIR ${INSTALL_BIN} ) + set ( CMAKE_SKIP_BUILD_RPATH FALSE CACHE STRING "" FORCE ) + set ( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE CACHE STRING "" FORCE ) + set ( CMAKE_INSTALL_RPATH $ORIGIN/${UP_DIR}/${INSTALL_LIB} + CACHE STRING "" FORCE ) + set ( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE STRING "" FORCE ) + set ( CMAKE_INSTALL_NAME_DIR @executable_path/${UP_DIR}/${INSTALL_LIB} + CACHE STRING "" FORCE ) +endif () + +## MACROS +# Parser macro +macro ( parse_arguments prefix arg_names option_names) + set ( DEFAULT_ARGS ) + foreach ( arg_name ${arg_names} ) + set ( ${prefix}_${arg_name} ) + endforeach () + foreach ( option ${option_names} ) + set ( ${prefix}_${option} FALSE ) + endforeach () + + set ( current_arg_name DEFAULT_ARGS ) + set ( current_arg_list ) + foreach ( arg ${ARGN} ) + set ( larg_names ${arg_names} ) + list ( FIND larg_names "${arg}" is_arg_name ) + if ( is_arg_name GREATER -1 ) + set ( ${prefix}_${current_arg_name} ${current_arg_list} ) + set ( current_arg_name ${arg} ) + set ( current_arg_list ) + else () + set ( loption_names ${option_names} ) + list ( FIND loption_names "${arg}" is_option ) + if ( is_option GREATER -1 ) + set ( ${prefix}_${arg} TRUE ) + else () + set ( current_arg_list ${current_arg_list} ${arg} ) + endif () + endif () + endforeach () + set ( ${prefix}_${current_arg_name} ${current_arg_list} ) +endmacro () + + +# install_executable ( executable_targets ) +# Installs any executables generated using "add_executable". +# USE: install_executable ( lua ) +# NOTE: subdirectories are NOT supported +set ( CPACK_COMPONENT_RUNTIME_DISPLAY_NAME "${DIST_NAME} Runtime" ) +set ( CPACK_COMPONENT_RUNTIME_DESCRIPTION + "Executables and runtime libraries. Installed into ${INSTALL_BIN}." ) +macro ( install_executable ) + foreach ( _file ${ARGN} ) + if ( INSTALL_VERSION ) + set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION} + SOVERSION ${DIST_VERSION} ) + endif () + install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN} + COMPONENT Runtime ) + endforeach() +endmacro () + +# install_library ( library_targets ) +# Installs any libraries generated using "add_library" into apropriate places. +# USE: install_library ( libexpat ) +# NOTE: subdirectories are NOT supported +set ( CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "${DIST_NAME} Development Libraries" ) +set ( CPACK_COMPONENT_LIBRARY_DESCRIPTION + "Static and import libraries needed for development. Installed into ${INSTALL_LIB} or ${INSTALL_BIN}." ) +macro ( install_library ) + foreach ( _file ${ARGN} ) + if ( INSTALL_VERSION ) + set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION} + SOVERSION ${DIST_VERSION} ) + endif () + install ( TARGETS ${_file} + RUNTIME DESTINATION ${INSTALL_BIN} COMPONENT Runtime + LIBRARY DESTINATION ${INSTALL_LIB} COMPONENT Runtime + ARCHIVE DESTINATION ${INSTALL_LIB} COMPONENT Library ) + endforeach() +endmacro () + +# helper function for various install_* functions, for PATTERN/REGEX args. +macro ( _complete_install_args ) + if ( NOT("${_ARG_PATTERN}" STREQUAL "") ) + set ( _ARG_PATTERN PATTERN ${_ARG_PATTERN} ) + endif () + if ( NOT("${_ARG_REGEX}" STREQUAL "") ) + set ( _ARG_REGEX REGEX ${_ARG_REGEX} ) + endif () +endmacro () + +# install_header ( files/directories [INTO destination] ) +# Install a directories or files into header destination. +# USE: install_header ( lua.h luaconf.h ) or install_header ( GL ) +# USE: install_header ( mylib.h INTO mylib ) +# For directories, supports optional PATTERN/REGEX arguments like install(). +set ( CPACK_COMPONENT_HEADER_DISPLAY_NAME "${DIST_NAME} Development Headers" ) +set ( CPACK_COMPONENT_HEADER_DESCRIPTION + "Headers needed for development. Installed into ${INSTALL_INC}." ) +macro ( install_header ) + parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) + _complete_install_args() + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} + COMPONENT Header ${_ARG_PATTERN} ${_ARG_REGEX} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} + COMPONENT Header ) + endif () + endforeach() +endmacro () + +# install_data ( files/directories [INTO destination] ) +# This installs additional data files or directories. +# USE: install_data ( extra data.dat ) +# USE: install_data ( image1.png image2.png INTO images ) +# For directories, supports optional PATTERN/REGEX arguments like install(). +set ( CPACK_COMPONENT_DATA_DISPLAY_NAME "${DIST_NAME} Data" ) +set ( CPACK_COMPONENT_DATA_DESCRIPTION + "Application data. Installed into ${INSTALL_DATA}." ) +macro ( install_data ) + parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) + _complete_install_args() + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} + DESTINATION ${INSTALL_DATA}/${_ARG_INTO} + COMPONENT Data ${_ARG_PATTERN} ${_ARG_REGEX} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO} + COMPONENT Data ) + endif () + endforeach() +endmacro () + +# INSTALL_DOC ( files/directories [INTO destination] ) +# This installs documentation content +# USE: install_doc ( doc/ doc.pdf ) +# USE: install_doc ( index.html INTO html ) +# For directories, supports optional PATTERN/REGEX arguments like install(). +set ( CPACK_COMPONENT_DOCUMENTATION_DISPLAY_NAME "${DIST_NAME} Documentation" ) +set ( CPACK_COMPONENT_DOCUMENTATION_DESCRIPTION + "Application documentation. Installed into ${INSTALL_DOC}." ) +macro ( install_doc ) + parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) + _complete_install_args() + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} + COMPONENT Documentation ${_ARG_PATTERN} ${_ARG_REGEX} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} + COMPONENT Documentation ) + endif () + endforeach() +endmacro () + +# install_example ( files/directories [INTO destination] ) +# This installs additional examples +# USE: install_example ( examples/ exampleA ) +# USE: install_example ( super_example super_data INTO super) +# For directories, supports optional PATTERN/REGEX argument like install(). +set ( CPACK_COMPONENT_EXAMPLE_DISPLAY_NAME "${DIST_NAME} Examples" ) +set ( CPACK_COMPONENT_EXAMPLE_DESCRIPTION + "Examples and their associated data. Installed into ${INSTALL_EXAMPLE}." ) +macro ( install_example ) + parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) + _complete_install_args() + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} + COMPONENT Example ${_ARG_PATTERN} ${_ARG_REGEX} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} + COMPONENT Example ) + endif () + endforeach() +endmacro () + +# install_test ( files/directories [INTO destination] ) +# This installs tests and test files, DOES NOT EXECUTE TESTS +# USE: install_test ( my_test data.sql ) +# USE: install_test ( feature_x_test INTO x ) +# For directories, supports optional PATTERN/REGEX argument like install(). +set ( CPACK_COMPONENT_TEST_DISPLAY_NAME "${DIST_NAME} Tests" ) +set ( CPACK_COMPONENT_TEST_DESCRIPTION + "Tests and associated data. Installed into ${INSTALL_TEST}." ) +macro ( install_test ) + parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) + _complete_install_args() + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} + COMPONENT Test ${_ARG_PATTERN} ${_ARG_REGEX} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} + COMPONENT Test ) + endif () + endforeach() +endmacro () + +# install_foo ( files/directories [INTO destination] ) +# This installs optional or otherwise unneeded content +# USE: install_foo ( etc/ example.doc ) +# USE: install_foo ( icon.png logo.png INTO icons) +# For directories, supports optional PATTERN/REGEX argument like install(). +set ( CPACK_COMPONENT_OTHER_DISPLAY_NAME "${DIST_NAME} Unspecified Content" ) +set ( CPACK_COMPONENT_OTHER_DESCRIPTION + "Other unspecified content. Installed into ${INSTALL_FOO}." ) +macro ( install_foo ) + parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) + _complete_install_args() + foreach ( _file ${_ARG_DEFAULT_ARGS} ) + if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) + install ( DIRECTORY ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} + COMPONENT Other ${_ARG_PATTERN} ${_ARG_REGEX} ) + else () + install ( FILES ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} + COMPONENT Other ) + endif () + endforeach() +endmacro () + +## CTest defaults + +## CPack defaults +set ( CPACK_GENERATOR "ZIP" ) +set ( CPACK_STRIP_FILES TRUE ) +set ( CPACK_PACKAGE_NAME "${DIST_NAME}" ) +set ( CPACK_PACKAGE_VERSION "${DIST_VERSION}") +set ( CPACK_PACKAGE_VENDOR "LuaDist" ) +set ( CPACK_COMPONENTS_ALL Runtime Library Header Data Documentation Example Other ) +include ( CPack ) diff --git a/cmake/lua.cmake b/cmake/lua.cmake new file mode 100644 index 0000000..80bbc5f --- /dev/null +++ b/cmake/lua.cmake @@ -0,0 +1,293 @@ +# LuaDist CMake utility library for Lua. +# +# Copyright (C) 2007-2012 LuaDist. +# by David Manura, Peter Drahos +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Please note that the package source code is licensed under its own license. + +set ( INSTALL_LMOD ${INSTALL_LIB}/lua + CACHE PATH "Directory to install Lua modules." ) +set ( INSTALL_CMOD ${INSTALL_LIB}/lua + CACHE PATH "Directory to install Lua binary modules." ) + +option ( SKIP_LUA_WRAPPER + "Do not build and install Lua executable wrappers." OFF) + +# List of (Lua module name, file path) pairs. +# Used internally by add_lua_test. Built by add_lua_module. +set ( _lua_modules ) + +# utility function: appends path `path` to path `basepath`, properly +# handling cases when `path` may be relative or absolute. +macro ( _append_path basepath path result ) + if ( IS_ABSOLUTE "${path}" ) + set ( ${result} "${path}" ) + else () + set ( ${result} "${basepath}/${path}" ) + endif () +endmacro () + +# install_lua_executable ( target source ) +# Automatically generate a binary if srlua package is available +# The application or its source will be placed into /bin +# If the application source did not have .lua suffix then it will be added +# USE: lua_executable ( sputnik src/sputnik.lua ) +macro ( install_lua_executable _name _source ) + get_filename_component ( _source_name ${_source} NAME_WE ) + # Find srlua and glue + find_program( SRLUA_EXECUTABLE NAMES srlua ) + find_program( GLUE_EXECUTABLE NAMES glue ) + # Executable output + set ( _exe ${CMAKE_CURRENT_BINARY_DIR}/${_name}${CMAKE_EXECUTABLE_SUFFIX} ) + if ( NOT SKIP_LUA_WRAPPER AND SRLUA_EXECUTABLE AND GLUE_EXECUTABLE ) + # Generate binary gluing the lua code to srlua, this is a robuust approach for most systems + add_custom_command( + OUTPUT ${_exe} + COMMAND ${GLUE_EXECUTABLE} + ARGS ${SRLUA_EXECUTABLE} ${_source} ${_exe} + DEPENDS ${_source} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + VERBATIM + ) + # Make sure we have a target associated with the binary + add_custom_target(${_name} ALL + DEPENDS ${_exe} + ) + # Install with run permissions + install ( PROGRAMS ${_exe} DESTINATION ${INSTALL_BIN} COMPONENT Runtime) + # Also install source as optional resurce + install ( FILES ${_source} DESTINATION ${INSTALL_FOO} COMPONENT Other ) + else() + # Install into bin as is but without the lua suffix, we assume the executable uses UNIX shebang/hash-bang magic + install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN} + RENAME ${_source_name} + COMPONENT Runtime + ) + endif() +endmacro () + +macro ( _lua_module_helper is_install _name ) + parse_arguments ( _MODULE "LINK;ALL_IN_ONE" "" ${ARGN} ) + # _target is CMake-compatible target name for module (e.g. socket_core). + # _module is relative path of target (e.g. socket/core), + # without extension (e.g. .lua/.so/.dll). + # _MODULE_SRC is list of module source files (e.g. .lua and .c files). + # _MODULE_NAMES is list of module names (e.g. socket.core). + if ( _MODULE_ALL_IN_ONE ) + string ( REGEX REPLACE "\\..*" "" _target "${_name}" ) + string ( REGEX REPLACE "\\..*" "" _module "${_name}" ) + set ( _target "${_target}_all_in_one") + set ( _MODULE_SRC ${_MODULE_ALL_IN_ONE} ) + set ( _MODULE_NAMES ${_name} ${_MODULE_DEFAULT_ARGS} ) + else () + string ( REPLACE "." "_" _target "${_name}" ) + string ( REPLACE "." "/" _module "${_name}" ) + set ( _MODULE_SRC ${_MODULE_DEFAULT_ARGS} ) + set ( _MODULE_NAMES ${_name} ) + endif () + if ( NOT _MODULE_SRC ) + message ( FATAL_ERROR "no module sources specified" ) + endif () + list ( GET _MODULE_SRC 0 _first_source ) + + get_filename_component ( _ext ${_first_source} EXT ) + if ( _ext STREQUAL ".lua" ) # Lua source module + list ( LENGTH _MODULE_SRC _len ) + if ( _len GREATER 1 ) + message ( FATAL_ERROR "more than one source file specified" ) + endif () + + set ( _module "${_module}.lua" ) + + get_filename_component ( _module_dir ${_module} PATH ) + get_filename_component ( _module_filename ${_module} NAME ) + _append_path ( "${CMAKE_CURRENT_SOURCE_DIR}" "${_first_source}" _module_path ) + list ( APPEND _lua_modules "${_name}" "${_module_path}" ) + + if ( ${is_install} ) + install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD}/${_module_dir} + RENAME ${_module_filename} + COMPONENT Runtime + ) + endif () + else () # Lua C binary module + enable_language ( C ) + find_package ( Lua REQUIRED ) + include_directories ( ${LUA_INCLUDE_DIR} ) + + set ( _module "${_module}${CMAKE_SHARED_MODULE_SUFFIX}" ) + + get_filename_component ( _module_dir ${_module} PATH ) + get_filename_component ( _module_filenamebase ${_module} NAME_WE ) + foreach ( _thisname ${_MODULE_NAMES} ) + list ( APPEND _lua_modules "${_thisname}" + "${CMAKE_CURRENT_BINARY_DIR}/\${CMAKE_CFG_INTDIR}/${_module}" ) + endforeach () + + add_library( ${_target} MODULE ${_MODULE_SRC}) + target_link_libraries ( ${_target} ${LUA_LIBRARY} ${_MODULE_LINK} ) + set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY + "${_module_dir}" PREFIX "" OUTPUT_NAME "${_module_filenamebase}" ) + if ( ${is_install} ) + install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir} COMPONENT Runtime) + endif () + endif () +endmacro () + +# add_lua_module +# Builds a Lua source module into a destination locatable by Lua +# require syntax. +# Binary modules are also supported where this function takes sources and +# libraries to compile separated by LINK keyword. +# USE: add_lua_module ( socket.http src/http.lua ) +# USE2: add_lua_module ( mime.core src/mime.c ) +# USE3: add_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} ) +# USE4: add_lua_module ( ssl.context ssl.core ALL_IN_ONE src/context.c src/ssl.c ) +# This form builds an "all-in-one" module (e.g. ssl.so or ssl.dll containing +# both modules ssl.context and ssl.core). The CMake target name will be +# ssl_all_in_one. +# Also sets variable _module_path (relative path where module typically +# would be installed). +macro ( add_lua_module ) + _lua_module_helper ( 0 ${ARGN} ) +endmacro () + + +# install_lua_module +# This is the same as `add_lua_module` but also installs the module. +# USE: install_lua_module ( socket.http src/http.lua ) +# USE2: install_lua_module ( mime.core src/mime.c ) +# USE3: install_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} ) +macro ( install_lua_module ) + _lua_module_helper ( 1 ${ARGN} ) +endmacro () + +# Builds string representing Lua table mapping Lua modules names to file +# paths. Used internally. +macro ( _make_module_table _outvar ) + set ( ${_outvar} ) + list ( LENGTH _lua_modules _n ) + if ( ${_n} GREATER 0 ) # avoids cmake complaint + foreach ( _i RANGE 1 ${_n} 2 ) + list ( GET _lua_modules ${_i} _path ) + math ( EXPR _ii ${_i}-1 ) + list ( GET _lua_modules ${_ii} _name ) + set ( ${_outvar} "${_table} ['${_name}'] = '${_path}'\;\n") + endforeach () + endif () + set ( ${_outvar} +"local modules = { +${_table}}" ) +endmacro () + +# add_lua_test ( _testfile [ WORKING_DIRECTORY _working_dir ] ) +# Runs Lua script `_testfile` under CTest tester. +# Optional named argument `WORKING_DIRECTORY` is current working directory to +# run test under (defaults to ${CMAKE_CURRENT_BINARY_DIR}). +# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}. +# Any modules previously defined with install_lua_module are automatically +# preloaded (via package.preload) prior to running the test script. +# Under LuaDist, set test=true in config.lua to enable testing. +# USE: add_lua_test ( test/test1.lua [args...] [WORKING_DIRECTORY dir]) +macro ( add_lua_test _testfile ) + if ( NOT SKIP_TESTING ) + parse_arguments ( _ARG "WORKING_DIRECTORY" "" ${ARGN} ) + include ( CTest ) + find_program ( LUA NAMES lua lua.bat ) + get_filename_component ( TESTFILEABS ${_testfile} ABSOLUTE ) + get_filename_component ( TESTFILENAME ${_testfile} NAME ) + get_filename_component ( TESTFILEBASE ${_testfile} NAME_WE ) + + # Write wrapper script. + # Note: One simple way to allow the script to find modules is + # to just put them in package.preload. + set ( TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME} ) + _make_module_table ( _table ) + set ( TESTWRAPPERSOURCE +"local CMAKE_CFG_INTDIR = ... or '.' +${_table} +local function preload_modules(modules) + for name, path in pairs(modules) do + if path:match'%.lua' then + package.preload[name] = assert(loadfile(path)) + else + local name = name:gsub('.*%-', '') -- remove any hyphen prefix + local symbol = 'luaopen_' .. name:gsub('%.', '_') + --improve: generalize to support all-in-one loader? + local path = path:gsub('%$%{CMAKE_CFG_INTDIR%}', CMAKE_CFG_INTDIR) + package.preload[name] = assert(package.loadlib(path, symbol)) + end + end +end +preload_modules(modules) +arg[0] = '${TESTFILEABS}' +table.remove(arg, 1) +return assert(loadfile '${TESTFILEABS}')(unpack(arg)) +" ) + if ( _ARG_WORKING_DIRECTORY ) + get_filename_component ( + TESTCURRENTDIRABS ${_ARG_WORKING_DIRECTORY} ABSOLUTE ) + # note: CMake 2.6 (unlike 2.8) lacks WORKING_DIRECTORY parameter. + set ( _pre ${CMAKE_COMMAND} -E chdir "${TESTCURRENTDIRABS}" ) + endif () + file ( WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) + add_test ( NAME ${TESTFILEBASE} COMMAND ${_pre} ${LUA} + ${TESTWRAPPER} "${CMAKE_CFG_INTDIR}" + ${_ARG_DEFAULT_ARGS} ) + endif () + # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake + # Note: ${CMAKE_CFG_INTDIR} is a command-line argument to allow proper + # expansion by the native build tool. +endmacro () + + +# Converts Lua source file `_source` to binary string embedded in C source +# file `_target`. Optionally compiles Lua source to byte code (not available +# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua +# versions of bin2c [1] and luac [2] may be passed respectively as additional +# arguments. +# +# [1] http://lua-users.org/wiki/BinToCee +# [2] http://lua-users.org/wiki/LuaCompilerInLua +function ( add_lua_bin2c _target _source ) + find_program ( LUA NAMES lua lua.bat ) + execute_process ( COMMAND ${LUA} -e "string.dump(function()end)" + RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET ) + if ( NOT ${_LUA_DUMP_RESULT} ) + SET ( HAVE_LUA_DUMP true ) + endif () + message ( "-- string.dump=${HAVE_LUA_DUMP}" ) + + if ( ARGV2 ) + get_filename_component ( BIN2C ${ARGV2} ABSOLUTE ) + set ( BIN2C ${LUA} ${BIN2C} ) + else () + find_program ( BIN2C NAMES bin2c bin2c.bat ) + endif () + if ( HAVE_LUA_DUMP ) + if ( ARGV3 ) + get_filename_component ( LUAC ${ARGV3} ABSOLUTE ) + set ( LUAC ${LUA} ${LUAC} ) + else () + find_program ( LUAC NAMES luac luac.bat ) + endif () + endif ( HAVE_LUA_DUMP ) + message ( "-- bin2c=${BIN2C}" ) + message ( "-- luac=${LUAC}" ) + + get_filename_component ( SOURCEABS ${_source} ABSOLUTE ) + if ( HAVE_LUA_DUMP ) + get_filename_component ( SOURCEBASE ${_source} NAME_WE ) + add_custom_command ( + OUTPUT ${_target} DEPENDS ${_source} + COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo + ${SOURCEABS} + COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo + ">${_target}" ) + else () + add_custom_command ( + OUTPUT ${_target} DEPENDS ${SOURCEABS} + COMMAND ${BIN2C} ${_source} ">${_target}" ) + endif () +endfunction() From 60116e87e2433ef373a86835fc0fbe7480b9fbdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Drahos=CC=8C?= Date: Sat, 25 Aug 2012 04:02:32 +0200 Subject: [PATCH 19/21] Updated version in dist.info --- dist.info | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist.info b/dist.info index 8e5bdb0..6842a33 100644 --- a/dist.info +++ b/dist.info @@ -1,7 +1,7 @@ --- This file is part of LuaDist project name = "luacrypto" -version = "0.3.0.20120420" +version = "0.3.0" desc = "LuaCrypto provides a Lua frontend to the OpenSSL cryptographic library." author = "Keith Howe" From 70ecd0039058c6960fc609eaecaaab12f4e3e9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Drahos=CC=8C?= Date: Wed, 19 Dec 2012 14:32:54 +0100 Subject: [PATCH 20/21] Updated travis hook --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 049d304..43f8453 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,11 +4,12 @@ # We assume C build environments language: C - + # Try using multiple Lua Implementations env: - TOOL="" # Use native compiler (GCC usually) - COMPILER="clang" # Use clang +# - COMPILER="fortran" # Use fortran, make sure to modify the matrix section - TOOL="i686-w64-mingw32" # 32bit MinGW - TOOL="x86_64-w64-mingw32" # 64bit MinGW - TOOL="arm-linux-gnueabihf" # ARM hard-float (hf), linux @@ -48,4 +49,4 @@ notifications: - luadist-dev@googlegroups.com email: on_success: change - on_failure: always \ No newline at end of file + on_failure: always From 1a310ed2f1595abaf5fb1c523ab5ffb02423ded5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Drahos=CC=8C?= Date: Mon, 18 Feb 2013 16:36:42 +0100 Subject: [PATCH 21/21] Updated travis hook --- .travis.yml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 43f8453..57493eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,8 @@ language: C # Try using multiple Lua Implementations env: - - TOOL="" # Use native compiler (GCC usually) - - COMPILER="clang" # Use clang -# - COMPILER="fortran" # Use fortran, make sure to modify the matrix section + - TOOL="gcc" # Use native compiler (GCC usually) + - TOOL="clang" # Use clang - TOOL="i686-w64-mingw32" # 32bit MinGW - TOOL="x86_64-w64-mingw32" # 64bit MinGW - TOOL="arm-linux-gnueabihf" # ARM hard-float (hf), linux @@ -20,23 +19,23 @@ matrix: - env: TOOL="i686-w64-mingw32" - env: TOOL="x86_64-w64-mingw32" - env: TOOL="arm-linux-gnueabihf" - + # Install dependencies install: - - git clone git://github.com/LuaDist/_util.git ~/_util - - ~/_util/travis install + - git clone git://github.com/LuaDist/Tools.git ~/_tools + - ~/_tools/travis/travis install # Bootstap before_script: - - ~/_util/travis bootstrap + - ~/_tools/travis/travis bootstrap # Build the module script: - - ~/_util/travis build + - ~/_tools/travis/travis build # Execute additional tests or commands -#after_script: -# - ~/_util/travis test +after_script: + - ~/_tools/travis/travis test # Only watch the master branch branches: