X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/d8b71540598af16b7d9891dc015d5972908c7754..b2b89723a2427bba8290bd6967a1ab39cbb630be:/projects/stargazer/plugins/other/smux/types.cpp diff --git a/projects/stargazer/plugins/other/smux/types.cpp b/projects/stargazer/plugins/other/smux/types.cpp index f9c5eb50..5dc19b6f 100644 --- a/projects/stargazer/plugins/other/smux/types.cpp +++ b/projects/stargazer/plugins/other/smux/types.cpp @@ -1,11 +1,18 @@ +#include "types.h" + #include #include #include #include -#include "types.h" +namespace +{ -bool ParseArcs(const char * str, ptrdiff_t length, unsigned * a, size_t * pos) +bool ParseArcs(const char * str, ptrdiff_t length, uint32_t * a, size_t * pos); +bool StringToArcs(const char * str, size_t length, std::vector & arcs); +bool AppendToArcs(const char * str, size_t length, std::vector & arcs); + +bool ParseArcs(const char * str, ptrdiff_t length, uint32_t * a, size_t * pos) { if (length == 0) return false; @@ -15,22 +22,22 @@ if (*left == '.') size_t arcPos = 0; while ((left - str) < length) { - char * pos = NULL; - unsigned arc = strtoul(left, &pos, 10); - if (pos == left) + char * p = NULL; + uint32_t arc = static_cast(strtoul(left, &p, 10)); + if (p == left) return false; a[arcPos++] = arc; if (arcPos >= 1024) return false; - left = pos + 1; + left = p + 1; } *pos = arcPos; return true; } -bool StringToArcs(const char * str, size_t length, std::vector & arcs) +bool StringToArcs(const char * str, size_t length, std::vector & arcs) { -unsigned a[1024]; +uint32_t a[1024]; size_t pos = 0; if (!ParseArcs(str, length, a, &pos)) @@ -40,9 +47,9 @@ arcs.assign(a, a + pos); return true; } -bool AppendToArcs(const char * str, size_t length, std::vector & arcs) +bool AppendToArcs(const char * str, size_t length, std::vector & arcs) { -unsigned a[1024]; +uint32_t a[1024]; size_t pos = 0; if (!ParseArcs(str, length, a, &pos)) @@ -52,38 +59,44 @@ std::copy(&a[0], &a[pos], std::back_inserter(arcs)); return true; } +} + OID::OID(const std::string & str) + : arcs() { if (!StringToArcs(str.c_str(), str.length(), arcs)) throw std::runtime_error("Invalid oid"); } OID::OID(const char * str, size_t length) + : arcs() { if (!StringToArcs(str, length, arcs)) throw std::runtime_error("Invalid oid"); } -OID::OID(const std::vector & a) +OID::OID(const std::vector & a) : arcs(a) { } -OID::OID(const unsigned * a, size_t length) +OID::OID(const uint32_t * a, size_t length) + : arcs() { -std::vector newArcs(a, a + length); +std::vector newArcs(a, a + length); arcs.swap(newArcs); } OID::OID(OBJECT_IDENTIFIER_t * oid) + : arcs() { -unsigned a[1024]; -int count = OBJECT_IDENTIFIER_get_arcs(oid, a, sizeof(a[0]), 1024); +uint32_t a[1024]; +int count = OBJECT_IDENTIFIER_get_arcs(oid, a, 1024); if (count > 1024) throw std::runtime_error("OID is too long"); -std::vector newArcs(a, a + count); +std::vector newArcs(a, a + count); arcs.swap(newArcs); } @@ -110,19 +123,19 @@ if (!AppendToArcs(suffix.c_str(), suffix.length(), arcs)) return true; } -bool OID::addSuffix(const unsigned * suffix, size_t length) +bool OID::addSuffix(const uint32_t * suffix, size_t length) { std::copy(suffix, suffix + length, std::back_inserter(arcs)); return true; } -bool OID::addSuffix(const std::vector & suffix) +bool OID::addSuffix(const std::vector & suffix) { std::copy(suffix.begin(), suffix.end(), std::back_inserter(arcs)); return true; } -bool OID::addSuffix(unsigned a, unsigned b) +bool OID::addSuffix(uint32_t a, uint32_t b) { arcs.push_back(a); arcs.push_back(b); @@ -145,7 +158,7 @@ if (!oid.addSuffix(suffix)) return oid; } -OID OID::copyWithSuffix(const unsigned * suffix, size_t length) const +OID OID::copyWithSuffix(const uint32_t * suffix, size_t length) const { OID oid(*this); if (!oid.addSuffix(suffix, length)) @@ -153,7 +166,7 @@ if (!oid.addSuffix(suffix, length)) return oid; } -OID OID::copyWithSuffix(const std::vector & suffix) const +OID OID::copyWithSuffix(const std::vector & suffix) const { OID oid(*this); if (!oid.addSuffix(suffix)) @@ -161,7 +174,7 @@ if (!oid.addSuffix(suffix)) return oid; } -OID OID::copyWithSuffix(unsigned a, unsigned b) const +OID OID::copyWithSuffix(uint32_t a, uint32_t b) const { OID oid(*this); oid.addSuffix(a, b); @@ -178,7 +191,7 @@ return stream.str(); void OID::ToOID(OBJECT_IDENTIFIER_t * oid) const { -OBJECT_IDENTIFIER_set_arcs(oid, &arcs.front(), sizeof(unsigned), arcs.size()); +OBJECT_IDENTIFIER_set_arcs(oid, &arcs.front(), static_cast(arcs.size())); } OID & OID::operator=(const OID & rvalue)