diff options
author | William Ahern <william@25thandClement.com> | 2015-12-21 17:34:08 +0800 |
---|---|---|
committer | William Ahern <william@25thandClement.com> | 2015-12-21 17:34:08 +0800 |
commit | 472e9873ca9929d7811953e8d27e615b19135692 (patch) | |
tree | d14050b991e054617b256f3b80e3420ae213f425 | |
parent | e3e33aee0b6ae6454ef2fcb2826124d9d575041f (diff) | |
parent | 2fa327bd7c95f797100bfe45fba956b290c309aa (diff) | |
download | luaossl-472e9873ca9929d7811953e8d27e615b19135692.tar.gz luaossl-472e9873ca9929d7811953e8d27e615b19135692.tar.bz2 luaossl-472e9873ca9929d7811953e8d27e615b19135692.zip |
Merge branch 'daurnimator-bignum-methods'
-rw-r--r-- | src/openssl.c | 93 |
1 files changed, 82 insertions, 11 deletions
diff --git a/src/openssl.c b/src/openssl.c index afd1f61..f03c129 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
@@ -1814,16 +1814,16 @@ static int bn__mul(lua_State *L) { | |||
1814 | } /* bn__mul() */ | 1814 | } /* bn__mul() */ |
1815 | 1815 | ||
1816 | 1816 | ||
1817 | static int bn__div(lua_State *L) { | 1817 | static int bn__idiv(lua_State *L) { |
1818 | BIGNUM *r, *a, *b; | 1818 | BIGNUM *dv, *a, *b; |
1819 | 1819 | ||
1820 | bn_prepops(L, &r, &a, &b, 0); | 1820 | bn_prepops(L, &dv, &a, &b, 0); |
1821 | 1821 | ||
1822 | if (!BN_div(r, NULL, a, b, getctx(L))) | 1822 | if (!BN_div(dv, NULL, a, b, getctx(L))) |
1823 | return auxL_error(L, auxL_EOPENSSL, "bignum:__div"); | 1823 | return auxL_error(L, auxL_EOPENSSL, "bignum:__idiv"); |
1824 | 1824 | ||
1825 | return 1; | 1825 | return 1; |
1826 | } /* bn__div() */ | 1826 | } /* bn__idiv() */ |
1827 | 1827 | ||
1828 | 1828 | ||
1829 | static int bn__mod(lua_State *L) { | 1829 | static int bn__mod(lua_State *L) { |
@@ -1856,6 +1856,36 @@ static int bn__pow(lua_State *L) { | |||
1856 | } /* bn__pow() */ | 1856 | } /* bn__pow() */ |
1857 | 1857 | ||
1858 | 1858 | ||
1859 | static int bn__shl(lua_State *L) { | ||
1860 | BIGNUM *r, *a; | ||
1861 | int n; | ||
1862 | |||
1863 | a = checkbig(L, 1); | ||
1864 | n = luaL_checkinteger(L, 2); | ||
1865 | r = bn_push(L); | ||
1866 | |||
1867 | if (!BN_lshift(r, a, n)) | ||
1868 | return auxL_error(L, auxL_EOPENSSL, "bignum:__shl"); | ||
1869 | |||
1870 | return 1; | ||
1871 | } /* bn__shl() */ | ||
1872 | |||
1873 | |||
1874 | static int bn__shr(lua_State *L) { | ||
1875 | BIGNUM *r, *a; | ||
1876 | int n; | ||
1877 | |||
1878 | a = checkbig(L, 1); | ||
1879 | n = luaL_checkinteger(L, 2); | ||
1880 | r = bn_push(L); | ||
1881 | |||
1882 | if (!BN_rshift(r, a, n)) | ||
1883 | return auxL_error(L, auxL_EOPENSSL, "bignum:__shr"); | ||
1884 | |||
1885 | return 1; | ||
1886 | } /* bn__shr() */ | ||
1887 | |||
1888 | |||
1859 | static int bn__unm(lua_State *L) { | 1889 | static int bn__unm(lua_State *L) { |
1860 | BIGNUM *a = checksimple(L, 1, BIGNUM_CLASS); | 1890 | BIGNUM *a = checksimple(L, 1, BIGNUM_CLASS); |
1861 | 1891 | ||
@@ -1911,7 +1941,7 @@ static int bn__gc(lua_State *L) { | |||
1911 | 1941 | ||
1912 | static BIO *getbio(lua_State *); | 1942 | static BIO *getbio(lua_State *); |
1913 | 1943 | ||
1914 | static int bn__tostring(lua_State *L) { | 1944 | static int bn_todec(lua_State *L) { |
1915 | BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); | 1945 | BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); |
1916 | char *txt = NULL; | 1946 | char *txt = NULL; |
1917 | BIO *bio; | 1947 | BIO *bio; |
@@ -1936,12 +1966,51 @@ static int bn__tostring(lua_State *L) { | |||
1936 | sslerr: | 1966 | sslerr: |
1937 | OPENSSL_free(txt); | 1967 | OPENSSL_free(txt); |
1938 | 1968 | ||
1939 | return auxL_error(L, auxL_EOPENSSL, "bignum:__tostring"); | 1969 | return auxL_error(L, auxL_EOPENSSL, "bignum:todec"); |
1940 | } /* bn__tostring() */ | 1970 | } /* bn_todec() */ |
1971 | |||
1972 | |||
1973 | static int bn_tohex(lua_State *L) { | ||
1974 | BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); | ||
1975 | char *txt = NULL; | ||
1976 | BIO *bio; | ||
1977 | BUF_MEM *buf; | ||
1978 | |||
1979 | if (!(txt = BN_bn2hex(bn))) | ||
1980 | goto sslerr; | ||
1981 | |||
1982 | /* use GC-visible BIO as temporary buffer */ | ||
1983 | bio = getbio(L); | ||
1984 | |||
1985 | if (BIO_puts(bio, txt) < 0) | ||
1986 | goto sslerr; | ||
1987 | |||
1988 | OPENSSL_free(txt); | ||
1989 | txt = NULL; | ||
1990 | |||
1991 | BIO_get_mem_ptr(bio, &buf); | ||
1992 | lua_pushlstring(L, buf->data, buf->length); | ||
1993 | |||
1994 | return 1; | ||
1995 | sslerr: | ||
1996 | OPENSSL_free(txt); | ||
1997 | |||
1998 | return auxL_error(L, auxL_EOPENSSL, "bignum:tohex"); | ||
1999 | } /* bn_tohex() */ | ||
1941 | 2000 | ||
1942 | 2001 | ||
1943 | static const luaL_Reg bn_methods[] = { | 2002 | static const luaL_Reg bn_methods[] = { |
2003 | { "add", &bn__add }, | ||
2004 | { "sub", &bn__sub }, | ||
2005 | { "mul", &bn__mul }, | ||
2006 | { "idiv", &bn__idiv }, | ||
2007 | { "mod", &bn__mod }, | ||
2008 | { "pow", &bn__pow }, | ||
2009 | { "shl", &bn__shl }, | ||
2010 | { "shr", &bn__shr }, | ||
1944 | { "tobin", &bn_tobin }, | 2011 | { "tobin", &bn_tobin }, |
2012 | { "todec", &bn_todec }, | ||
2013 | { "tohex", &bn_tohex }, | ||
1945 | { NULL, NULL }, | 2014 | { NULL, NULL }, |
1946 | }; | 2015 | }; |
1947 | 2016 | ||
@@ -1949,15 +2018,17 @@ static const luaL_Reg bn_metatable[] = { | |||
1949 | { "__add", &bn__add }, | 2018 | { "__add", &bn__add }, |
1950 | { "__sub", &bn__sub }, | 2019 | { "__sub", &bn__sub }, |
1951 | { "__mul", &bn__mul }, | 2020 | { "__mul", &bn__mul }, |
1952 | { "__div", &bn__div }, | 2021 | { "__idiv", &bn__idiv }, |
1953 | { "__mod", &bn__mod }, | 2022 | { "__mod", &bn__mod }, |
1954 | { "__pow", &bn__pow }, | 2023 | { "__pow", &bn__pow }, |
1955 | { "__unm", &bn__unm }, | 2024 | { "__unm", &bn__unm }, |
2025 | { "__shl", &bn__shl }, | ||
2026 | { "__shr", &bn__shr }, | ||
1956 | { "__eq", &bn__eq }, | 2027 | { "__eq", &bn__eq }, |
1957 | { "__lt", &bn__lt }, | 2028 | { "__lt", &bn__lt }, |
1958 | { "__le", &bn__le }, | 2029 | { "__le", &bn__le }, |
1959 | { "__gc", &bn__gc }, | 2030 | { "__gc", &bn__gc }, |
1960 | { "__tostring", &bn__tostring }, | 2031 | { "__tostring", &bn_todec }, |
1961 | { NULL, NULL }, | 2032 | { NULL, NULL }, |
1962 | }; | 2033 | }; |
1963 | 2034 | ||