From b85df3bdcea93108a0c1c12e9471ba3e700329f6 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 5 May 2017 08:07:47 +0900 Subject: change api of GetStr/SetStr --- src/bls.cpp | 32 ++++++++--------- src/bls_if.cpp | 109 +++++++++++++++------------------------------------------ 2 files changed, 45 insertions(+), 96 deletions(-) (limited to 'src') diff --git a/src/bls.cpp b/src/bls.cpp index 51c774b..dd8a1b2 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -239,13 +239,13 @@ std::istream& operator>>(std::istream& is, Id& id) { return is >> id.getInner().v; } -void Id::getData(std::string& str) const +void Id::getStr(std::string& str, int ioMode) const { - getInner().v.getStr(str, mcl::IoEcComp); + getInner().v.getStr(str, ioMode); } -void Id::setData(const std::string& str) +void Id::setStr(const std::string& str, int ioMode) { - getInner().v.setStr(str, mcl::IoEcComp); + getInner().v.setStr(str, ioMode); } bool Id::isZero() const @@ -272,13 +272,13 @@ std::istream& operator>>(std::istream& os, Sign& s) { return os >> s.getInner().sHm; } -void Sign::getData(std::string& str) const +void Sign::getStr(std::string& str, int ioMode) const { - getInner().sHm.getStr(str, mcl::IoEcComp); + getInner().sHm.getStr(str, ioMode); } -void Sign::setData(const std::string& str) +void Sign::setStr(const std::string& str, int ioMode) { - getInner().sHm.setStr(str, mcl::IoEcComp); + getInner().sHm.setStr(str, ioMode); } bool Sign::verify(const PublicKey& pub, const std::string& m) const @@ -347,13 +347,13 @@ std::istream& operator>>(std::istream& is, PublicKey& pub) return is >> pub.getInner().sQ; } -void PublicKey::getData(std::string& str) const +void PublicKey::getStr(std::string& str, int ioMode) const { - getInner().sQ.getStr(str, mcl::IoEcComp); + getInner().sQ.getStr(str, ioMode); } -void PublicKey::setData(const std::string& str) +void PublicKey::setStr(const std::string& str, int ioMode) { - getInner().sQ.setStr(str, mcl::IoEcComp); + getInner().sQ.setStr(str, ioMode); } void PublicKey::set(const PublicKey *mpk, size_t k, const Id& id) { @@ -392,13 +392,13 @@ std::istream& operator>>(std::istream& is, SecretKey& sec) { return is >> sec.getInner().s; } -void SecretKey::getData(std::string& str) const +void SecretKey::getStr(std::string& str, int ioMode) const { - getInner().s.getStr(str, mcl::IoEcComp); + getInner().s.getStr(str, ioMode); } -void SecretKey::setData(const std::string& str) +void SecretKey::setStr(const std::string& str, int ioMode) { - getInner().s.setStr(str, mcl::IoEcComp); + getInner().s.setStr(str, ioMode); } void SecretKey::init() diff --git a/src/bls_if.cpp b/src/bls_if.cpp index 7d2d78a..c495818 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -15,11 +15,10 @@ Outer *createT() } template -int setStrT(Outer *p, const char *buf, size_t bufSize) +int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode) try { - std::istringstream iss(std::string(buf, bufSize)); - iss >> *(Inner*)p; + ((Inner*)p)->setStr(std::string(buf, bufSize), ioMode); return 0; } catch (std::exception& e) { fprintf(stderr, "err setStrT %s\n", e.what()); @@ -28,48 +27,30 @@ int setStrT(Outer *p, const char *buf, size_t bufSize) size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s) { - if (s.size() >= maxBufSize) { + if (s.size() > maxBufSize + 1) { return 0; } memcpy(buf, s.c_str(), s.size()); buf[s.size()] = '\0'; return s.size(); } - -template -size_t getStrT(const Outer *p, char *buf, size_t maxBufSize) - try -{ - std::ostringstream oss; - oss << *(const Inner*)p; - std::string s = oss.str(); - return checkAndCopy(buf, maxBufSize, s); -} catch (std::exception&) { - return 0; -} - template -int setDataT(Outer *p, const char *buf, size_t bufSize) - try -{ - ((Inner*)p)->setData(std::string(buf, bufSize)); - return 0; -} catch (std::exception& e) { - fprintf(stderr, "err setDataT %s\n", e.what()); - return 1; -} - -template -size_t getDataT(const Outer *p, char *buf, size_t maxBufSize) +size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode) try { std::string s; - ((const Inner*)p)->getData(s); - if (s.size() > maxBufSize) { - fprintf(stderr, "err getDataT size is small %d %d\n", (int)s.size(), (int)maxBufSize); + ((const Inner*)p)->getStr(s, ioMode); + size_t terminate = 0; + if (ioMode == 0 || ioMode == BlsIoBin || ioMode == BlsIoDec || ioMode == BlsIoHex) { + terminate = 1; // for '\0' + } + if (s.size() > maxBufSize + terminate) { return 0; } memcpy(buf, s.c_str(), s.size()); + if (terminate) { + buf[s.size()] = '\0'; + } return s.size(); } catch (std::exception&) { return 0; @@ -113,14 +94,6 @@ void blsIdDestroy(blsId *id) { delete (bls::Id*)id; } -size_t blsIdGetData(const blsId *id, char *buf, size_t maxBufSize) -{ - return getDataT(id, buf, maxBufSize); -} -int blsIdSetData(blsId *id, const char *buf, size_t bufSize) -{ - return setDataT(id, buf, bufSize); -} int blsIdIsSame(const blsId *lhs, const blsId *rhs) { return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0; @@ -134,14 +107,14 @@ void blsIdCopy(blsId *dst, const blsId *src) *((bls::Id*)dst) = *((const bls::Id*)src); } -int blsIdSetStr(blsId *id, const char *buf, size_t bufSize) +int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode) { - return setStrT(id, buf, bufSize); + return setStrT(id, buf, bufSize, ioMode); } -size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize) +size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode) { - return getStrT(id, buf, maxBufSize); + return getStrT(id, buf, maxBufSize, ioMode); } void blsIdSet(blsId *id, const uint64_t *p) @@ -158,14 +131,6 @@ void blsSecretKeyDestroy(blsSecretKey *sec) { delete (bls::SecretKey*)sec; } -size_t blsSecretKeyGetData(const blsSecretKey *sec, char *buf, size_t maxBufSize) -{ - return getDataT(sec, buf, maxBufSize); -} -int blsSecretKeySetData(blsSecretKey *sec, const char *buf, size_t bufSize) -{ - return setDataT(sec, buf, bufSize); -} int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs) { return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0; @@ -184,13 +149,13 @@ void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p) ((bls::SecretKey*)sec)->set(p); } -int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize) +int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode) { - return setStrT(sec, buf, bufSize); + return setStrT(sec, buf, bufSize, ioMode); } -size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize) +size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode) { - return getStrT(sec, buf, maxBufSize); + return getStrT(sec, buf, maxBufSize, ioMode); } void blsSecretKeyInit(blsSecretKey *sec) @@ -234,14 +199,6 @@ void blsPublicKeyDestroy(blsPublicKey *pub) { delete (bls::PublicKey*)pub; } -size_t blsPublicKeyGetData(const blsPublicKey *pub, char *buf, size_t maxBufSize) -{ - return getDataT(pub, buf, maxBufSize); -} -int blsPublicKeySetData(blsPublicKey *pub, const char *buf, size_t bufSize) -{ - return setDataT(pub, buf, bufSize); -} int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs) { return *(const bls::PublicKey*)lhs == *(const bls::PublicKey*)rhs ? 1 : 0; @@ -255,13 +212,13 @@ void blsPublicKeyPut(const blsPublicKey *pub) std::cout << *(const bls::PublicKey*)pub << std::endl; } -int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize) +int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode) { - return setStrT(pub, buf, bufSize); + return setStrT(pub, buf, bufSize, ioMode); } -size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize) +size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode) { - return getStrT(pub, buf, maxBufSize); + return getStrT(pub, buf, maxBufSize, ioMode); } void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs) { @@ -285,14 +242,6 @@ void blsSignDestroy(blsSign *sign) { delete (bls::Sign*)sign; } -size_t blsSignGetData(const blsSign *sign, char *buf, size_t maxBufSize) -{ - return getDataT(sign, buf, maxBufSize); -} -int blsSignSetData(blsSign *sign, const char *buf, size_t bufSize) -{ - return setDataT(sign, buf, bufSize); -} int blsSignIsSame(const blsSign *lhs, const blsSign *rhs) { return *(const bls::Sign*)lhs == *(const bls::Sign*)rhs ? 1 : 0; @@ -306,13 +255,13 @@ void blsSignPut(const blsSign *sign) std::cout << *(const bls::Sign*)sign << std::endl; } -int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize) +int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode) { - return setStrT(sign, buf, bufSize); + return setStrT(sign, buf, bufSize, ioMode); } -size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize) +size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode) { - return getStrT(sign, buf, maxBufSize); + return getStrT(sign, buf, maxBufSize, ioMode); } void blsSignAdd(blsSign *sign, const blsSign *rhs) { -- cgit v1.2.3