From 0a7e8ead1a85f7003b729742d795730f2b782116 Mon Sep 17 00:00:00 2001 From: lloyd Date: Tue, 8 Sep 2009 15:22:31 +0000 Subject: Add *s before comment lines in file headers --- src/libstate/scan_name.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libstate/scan_name.h') diff --git a/src/libstate/scan_name.h b/src/libstate/scan_name.h index 9e7af40d6..b3f2004e2 100644 --- a/src/libstate/scan_name.h +++ b/src/libstate/scan_name.h @@ -1,6 +1,6 @@ /** -SCAN Name Abstraction -(C) 2008 Jack Lloyd +* SCAN Name Abstraction +* (C) 2008 Jack Lloyd * * Distributed under the terms of the Botan license */ -- cgit v1.2.3 From c86253042f0dab70fcf15030b25fcbc1a00dde76 Mon Sep 17 00:00:00 2001 From: lloyd Date: Tue, 6 Oct 2009 16:03:33 +0000 Subject: Extensions to SCAN_Name for dealing with cipher mode names --- src/libstate/scan_name.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++--- src/libstate/scan_name.h | 19 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) (limited to 'src/libstate/scan_name.h') diff --git a/src/libstate/scan_name.cpp b/src/libstate/scan_name.cpp index 88992d66e..13792720c 100644 --- a/src/libstate/scan_name.cpp +++ b/src/libstate/scan_name.cpp @@ -44,10 +44,50 @@ SCAN_Name::SCAN_Name(const std::string& algo_spec) { orig_algo_spec = algo_spec; - name = parse_and_deref_aliases(algo_spec); + try + { + name = parse_and_deref_aliases(algo_spec); + if(name.size() == 0) + throw Decoding_Error("Bad SCAN name " + algo_spec); + } + catch(Invalid_Algorithm_Name) + { + name.clear(); + } + + if(name.size() == 0 && algo_spec.find('/') != std::string::npos) + { + std::vector algo_parts = split_on(algo_spec, '/'); + + name = parse_and_deref_aliases(algo_parts[0]); + if(name.size() == 0) + throw Decoding_Error("Bad SCAN name " + algo_spec); + + for(size_t i = 1; i != algo_parts.size(); ++i) + mode_str.push_back(algo_parts[i]); + } + } - if(name.size() == 0) - throw Decoding_Error("Bad SCAN name " + algo_spec); +std::string SCAN_Name::algo_name_and_args() const + { + std::string out; + + out = name[0]; + + if(arg_count()) + { + out += '('; + for(u32bit i = 0; i != arg_count(); ++i) + { + out += arg(i); + if(i != arg_count() - 1) + out += ','; + } + out += ')'; + + } + + return out; } std::string SCAN_Name::arg(u32bit i) const diff --git a/src/libstate/scan_name.h b/src/libstate/scan_name.h index b3f2004e2..6dbd67c4f 100644 --- a/src/libstate/scan_name.h +++ b/src/libstate/scan_name.h @@ -37,6 +37,11 @@ class SCAN_Name */ std::string algo_name() const { return name[0]; } + /** + @return the algorithm name plus any arguments + */ + std::string algo_name_and_args() const; + /** @return the number of arguments */ @@ -67,9 +72,23 @@ class SCAN_Name @return the ith argument as a u32bit, or the default value */ u32bit arg_as_u32bit(u32bit i, u32bit def_value) const; + + /** + @return the cipher mode (if any) + */ + std::string cipher_mode() const + { return (mode_str.size() >= 1) ? mode_str[0] : ""; } + + /** + @return the cipher mode padding (if any) + */ + std::string cipher_mode_pad() const + { return (mode_str.size() >= 2) ? mode_str[1] : ""; } + private: std::string orig_algo_spec; std::vector name; + std::vector mode_str; }; } -- cgit v1.2.3 From 2d2cb73d20d5f08e5091147a69aa7b2b633880de Mon Sep 17 00:00:00 2001 From: lloyd Date: Wed, 7 Oct 2009 17:37:55 +0000 Subject: New parsing system for SCAN names --- src/libstate/scan_name.cpp | 118 ++++++++++++++++++++++++++++++++------------- src/libstate/scan_name.h | 13 ++--- 2 files changed, 92 insertions(+), 39 deletions(-) (limited to 'src/libstate/scan_name.h') diff --git a/src/libstate/scan_name.cpp b/src/libstate/scan_name.cpp index bdafde225..224d2e509 100644 --- a/src/libstate/scan_name.cpp +++ b/src/libstate/scan_name.cpp @@ -14,28 +14,50 @@ namespace Botan { namespace { -std::vector -parse_and_deref_aliases(const std::string& algo_spec) +std::string make_arg( + const std::vector >& name, u32bit start) { - std::vector parts = parse_algorithm_name(algo_spec); - std::vector out; + std::string output = name[start].second; + u32bit level = name[start].first; - for(size_t i = 0; i != parts.size(); ++i) + u32bit paren_depth = 0; + + for(u32bit i = start + 1; i != name.size(); ++i) { - std::string part_i = global_state().deref_alias(parts[i]); + if(name[i].first <= name[start].first) + break; - if(i == 0 && part_i.find_first_of(",()") != std::string::npos) + if(name[i].first > level) { - std::vector parts_i = parse_and_deref_aliases(part_i); - - for(size_t j = 0; j != parts_i.size(); ++j) - out.push_back(parts_i[j]); + output += '(' + name[i].second; + ++paren_depth; + } + else if(name[i].first < level) + { + output += ")," + name[i].second; + --paren_depth; } else - out.push_back(part_i); + { + if(output[output.size() - 1] != '(') + output += ","; + output += name[i].second; + } + + level = name[i].first; } - return out; + for(u32bit i = 0; i != paren_depth; ++i) + output += ')'; + + return output; + } + +std::pair +deref_aliases(const std::pair& in) + { + return std::make_pair(in.first, + global_state().deref_alias(in.second)); } } @@ -44,27 +66,57 @@ SCAN_Name::SCAN_Name(const std::string& algo_spec) { orig_algo_spec = algo_spec; - try - { - name = parse_and_deref_aliases(algo_spec); - if(name.size() == 0) - throw Decoding_Error("Bad SCAN name " + algo_spec); - } - catch(Invalid_Algorithm_Name) + std::vector > name; + u32bit level = 0; + std::pair accum = std::make_pair(level, ""); + + for(u32bit i = 0; i != algo_spec.size(); ++i) { - name.clear(); + char c = algo_spec[i]; + + if(c == '/' || c == ',' || c == '(' || c == ')') + { + if(c == '(') + ++level; + else if(c == ')') + { + if(level == 0) + throw Decoding_Error("Bad SCAN name " + algo_spec); + --level; + } + + if(c == '/' && level > 0) + accum.second.push_back(c); + else + { + if(accum.second != "") + name.push_back(deref_aliases(accum)); + accum = std::make_pair(level, ""); + } + } + else + accum.second.push_back(c); } - if(name.size() <= 1 && algo_spec.find('/') != std::string::npos) - { - std::vector algo_parts = split_on(algo_spec, '/'); + if(accum.second != "") + name.push_back(deref_aliases(accum)); + + if(level != 0 || name.size() == 0) + throw Decoding_Error("Bad SCAN name " + algo_spec); + + alg_name = name[0].second; - name = parse_and_deref_aliases(algo_parts[0]); - if(name.size() == 0) - throw Decoding_Error("Bad SCAN name " + algo_spec); + bool in_modes = false; - for(size_t i = 1; i != algo_parts.size(); ++i) - mode_str.push_back(algo_parts[i]); + for(u32bit i = 1; i != name.size(); ++i) + { + if(name[i].first == 0) + { + mode_info.push_back(make_arg(name, i)); + in_modes = true; + } + else if(name[i].first == 1 && !in_modes) + args.push_back(make_arg(name, i)); } } @@ -72,7 +124,7 @@ std::string SCAN_Name::algo_name_and_args() const { std::string out; - out = name[0]; + out = algo_name(); if(arg_count()) { @@ -94,21 +146,21 @@ std::string SCAN_Name::arg(u32bit i) const { if(i >= arg_count()) throw std::range_error("SCAN_Name::argument"); - return name[i+1]; + return args[i]; } std::string SCAN_Name::arg(u32bit i, const std::string& def_value) const { if(i >= arg_count()) return def_value; - return name[i+1]; + return args[i]; } u32bit SCAN_Name::arg_as_u32bit(u32bit i, u32bit def_value) const { if(i >= arg_count()) return def_value; - return to_u32bit(name[i+1]); + return to_u32bit(args[i]); } } diff --git a/src/libstate/scan_name.h b/src/libstate/scan_name.h index 6dbd67c4f..ced5f3e15 100644 --- a/src/libstate/scan_name.h +++ b/src/libstate/scan_name.h @@ -35,7 +35,7 @@ class SCAN_Name /** @return the algorithm name */ - std::string algo_name() const { return name[0]; } + std::string algo_name() const { return alg_name; } /** @return the algorithm name plus any arguments @@ -45,7 +45,7 @@ class SCAN_Name /** @return the number of arguments */ - u32bit arg_count() const { return name.size() - 1; } + u32bit arg_count() const { return args.size(); } /** @return if the number of arguments is between lower and upper @@ -77,18 +77,19 @@ class SCAN_Name @return the cipher mode (if any) */ std::string cipher_mode() const - { return (mode_str.size() >= 1) ? mode_str[0] : ""; } + { return (mode_info.size() >= 1) ? mode_info[0] : ""; } /** @return the cipher mode padding (if any) */ std::string cipher_mode_pad() const - { return (mode_str.size() >= 2) ? mode_str[1] : ""; } + { return (mode_info.size() >= 2) ? mode_info[1] : ""; } private: std::string orig_algo_spec; - std::vector name; - std::vector mode_str; + std::string alg_name; + std::vector args; + std::vector mode_info; }; } -- cgit v1.2.3