diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2016-09-06 15:54:21 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2016-09-06 15:54:21 +0800 |
commit | e6ccbbea70fb8d6a89f5593df47b160d01a29fd3 (patch) | |
tree | 6ad9ce27d5c2673f50fd370fa7b39626c6642b16 /src | |
parent | 240ec03f333c6ab09f409ab719a2a5b31c7dc60d (diff) | |
download | dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.tar dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.tar.gz dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.tar.bz2 dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.tar.lz dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.tar.xz dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.tar.zst dexon-bls-e6ccbbea70fb8d6a89f5593df47b160d01a29fd3.zip |
make class
Diffstat (limited to 'src')
-rw-r--r-- | src/bls_if.cpp | 116 |
1 files changed, 75 insertions, 41 deletions
diff --git a/src/bls_if.cpp b/src/bls_if.cpp index 8dc99f8..8cd4916 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -4,65 +4,78 @@ #include <sstream> #include <memory.h> -void blsInit(void) -{ - bls::init(); -} - -blsId *blsIdCreate(void) +template<class Inner, class Outer> +Outer *createT() try { - return (blsId*)new bls::Id(); + return (Outer*)new Inner(); } catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); + fprintf(stderr, "err createT %s\n", e.what()); return NULL; } -void blsIdDestroy(blsId *id) -{ - delete (bls::Id*)id; -} - -void blsIdPut(const blsId *id) -{ - std::cout << *(const bls::Id*)id << std::endl; -} - -int blsIdSetStr(blsId *id, const char *buf, size_t bufSize) +template<class Inner, class Outer> +int setStrT(Outer *p, const char *buf, size_t bufSize) try { std::istringstream iss(std::string(buf, bufSize)); - iss >> *(bls::Id*)id; + iss >> *(Inner*)p; return 0; } catch (std::exception& e) { + fprintf(stderr, "err setStrT %s\n", e.what()); return 1; } -size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize) +template<class Inner, class Outer> +size_t getStrT(const Outer *p, char *buf, size_t maxBufSize) try { std::ostringstream oss; - oss << *(const bls::Id*)id; + oss << *(const Inner*)p; std::string s = oss.str(); - if (s.size() > maxBufSize) return 0; + if (s.size() > maxBufSize) { + fprintf(stderr, "err getStrT size is small %d %d\n", (int)s.size(), (int)maxBufSize); + return 0; + } memcpy(buf, s.c_str(), s.size()); return s.size(); } catch (std::exception& e) { return 0; } +void blsInit() +{ + bls::init(); +} + +blsId *blsIdCreate() +{ + return createT<bls::Id, blsId>(); +} + +void blsIdDestroy(blsId *id) +{ + delete (bls::Id*)id; +} + +int blsIdSetStr(blsId *id, const char *buf, size_t bufSize) +{ + return setStrT<bls::Id, blsId>(id, buf, bufSize); +} + +size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize) +{ + return getStrT<bls::Id, blsId>(id, buf, maxBufSize); +} + void blsIdSet(blsId *id, const uint64_t *p) { ((bls::Id*)id)->set(p); } -blsSecretKey* blsSecretKeyCreate(void) - try +blsSecretKey* blsSecretKeyCreate() { - return (blsSecretKey*)new bls::SecretKey(); -} catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); - return NULL; + return createT<bls::SecretKey, blsSecretKey>(); } void blsSecretKeyDestroy(blsSecretKey *sec) @@ -75,6 +88,15 @@ void blsSecretKeyPut(const blsSecretKey *sec) std::cout << *(const bls::SecretKey*)sec << std::endl; } +int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize) +{ + return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize); +} +size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize) +{ + return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize); +} + void blsSecretKeyInit(blsSecretKey *sec) { ((bls::SecretKey*)sec)->init(); @@ -89,14 +111,11 @@ void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, siz ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sign, std::string(m, size)); } -blsPublicKey *blsPublicKeyCreate(void) - try +blsPublicKey *blsPublicKeyCreate() { - return (blsPublicKey*)new bls::PublicKey(); -} catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); - return NULL; + return createT<bls::PublicKey, blsPublicKey>(); } + void blsPublicKeyDestroy(blsPublicKey *pub) { delete (bls::PublicKey*)pub; @@ -106,14 +125,20 @@ void blsPublicKeyPut(const blsPublicKey *pub) std::cout << *(const bls::PublicKey*)pub << std::endl; } -blsSign *blsSignCreate(void) - try +int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize) { - return (blsSign*)new bls::Sign(); -} catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); - return NULL; + return setStrT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize); } +size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize) +{ + return getStrT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize); +} + +blsSign *blsSignCreate() +{ + return createT<bls::Sign, blsSign>(); +} + void blsSignDestroy(blsSign *sign) { delete (bls::Sign*)sign; @@ -123,6 +148,15 @@ void blsSignPut(const blsSign *sign) std::cout << *(const bls::Sign*)sign << std::endl; } +int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize) +{ + return setStrT<bls::Sign, blsSign>(sign, buf, bufSize); +} +size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize) +{ + return getStrT<bls::Sign, blsSign>(sign, buf, maxBufSize); +} + int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size) { return ((const bls::Sign*)sign)->verify(*(const bls::PublicKey*)pub, std::string(m, size)); |