diff options
| author | Timo Weingärtner <timo@tiwe.de> | 2021-04-22 23:29:10 +0200 |
|---|---|---|
| committer | Timo Weingärtner <timo@tiwe.de> | 2026-03-07 22:25:16 +0100 |
| commit | b929b048a1839c3da1cadf7f116b0f767f40267e (patch) | |
| tree | 2ccd826673e1113b4eade64b2e04b20722471e91 /rfc4251_gmp.C | |
| parent | 9d25142d506d1559b134d465ff0434ffbde7dca8 (diff) | |
| download | ssh-agent-filter-b929b048a1839c3da1cadf7f116b0f767f40267e.tar.gz | |
implement separate rfc4251::(string|mpint|name_list) types
Diffstat (limited to 'rfc4251_gmp.C')
| -rw-r--r-- | rfc4251_gmp.C | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/rfc4251_gmp.C b/rfc4251_gmp.C index b4c369b..06b6cf6 100644 --- a/rfc4251_gmp.C +++ b/rfc4251_gmp.C @@ -1,9 +1,9 @@ /* - * rfc4251_gmp.C -- implements mpint/gmp conversions for rfc4251::string + * rfc4251_gmp.C -- implements mpint/mpz conversions for rfc4251::mpint * * these functions need linking against libgmp * - * Copyright (C) 2013,2015 Timo Weingärtner <timo@tiwe.de> + * Copyright (C) 2013,2015,2021 Timo Weingärtner <timo@tiwe.de> * * This file is part of ssh-agent-filter. * @@ -25,36 +25,37 @@ namespace rfc4251 { -string::string (mpz_srcptr x) { - if (mpz_sgn(x) == 0) - return; - - auto const import_positive = [] (mpz_srcptr x, std::vector<char> & value) { +mpint::mpint (mpz_srcptr x) { + auto const import_positive = [] (mpz_srcptr x, std::vector<char> & buf) { size_t bits{mpz_sizeinbase(x, 2)}; size_t bytes{(bits + 7) / 8}; size_t extrabyte{(bits % 8) == 0}; // need extra byte if MSB is 1 to keep it non-negative - if (bytes + extrabyte > std::numeric_limits<uint32_t>::max()) - throw std::length_error{"32-bit limit for rfc4251::string exceeded"}; - value.resize(bytes + extrabyte); - value[0] = 0; - mpz_export(value.data() + extrabyte, nullptr, 1, 1, 1, 0, x); + check_length_against_limit(bytes + extrabyte); + buf.resize(bytes + extrabyte); + buf[0] = 0; + mpz_export(buf.data() + extrabyte, nullptr, 1, 1, 1, 0, x); }; - if (mpz_sgn(x) == 1) - import_positive(x, value); - else { - // handle two's complement: add 1, invert all bits - mpz_class tmp{x}; - tmp += 1; - import_positive(tmp.get_mpz_t(), value); - for (auto & i : value) - i ^= 0xff; + + switch (mpz_sgn(x)) { + case 0: + return; + case 1: + import_positive(x, buf); + return; + default: + // handle two's complement: add 1, invert all bits + mpz_class tmp{x}; + tmp += 1; + import_positive(tmp.get_mpz_t(), buf); + for (auto & i : buf) + i ^= 0xff; } } -string::operator mpz_class () const { - mpz_class ret; - mpz_import(ret.get_mpz_t(), value.size(), 1, 1, 1, 0, value.data()); - if (mpz_sizeinbase(ret.get_mpz_t(), 2) == value.size() * 8) { // negative +mpint::operator mpz_class () const { + mpz_class ret{}; + mpz_import(ret.get_mpz_t(), buf.size(), 1, 1, 1, 0, buf.data()); + if (mpz_sizeinbase(ret.get_mpz_t(), 2) == buf.size() * 8) { // negative mpz_com(ret.get_mpz_t(), ret.get_mpz_t()); ret += 1; mpz_neg(ret.get_mpz_t(), ret.get_mpz_t()); |
