The unified diff between revisions [cad0498d..] and [13704fef..] is displayed below. It can also be downloaded as a raw diff.

This diff has been restricted to the following files: 'src/parsing.cpp'

#
#
# patch "src/parsing.cpp"
#  from [9638034d8757f8efaf681e7f662a2a3536e357e2]
#    to [07004ffd721f7f70ccb2f50f0a1f4a7e98c028cb]
#
============================================================
--- src/parsing.cpp	9638034d8757f8efaf681e7f662a2a3536e357e2
+++ src/parsing.cpp	07004ffd721f7f70ccb2f50f0a1f4a7e98c028cb
@@ -1,11 +1,12 @@
 /*************************************************
 * Parser Functions Source File                   *
-* (C) 1999-2006 The Botan Project                *
+* (C) 1999-2007 Jack Lloyd                       *
 *************************************************/

 #include <botan/parsing.h>
 #include <botan/exceptn.h>
 #include <botan/charset.h>
+#include <botan/loadstor.h>

 namespace Botan {

@@ -30,7 +31,6 @@ u32bit to_u32bit(const std::string& numb
    return n;
    }

-
 /*************************************************
 * Convert an integer into a string               *
 *************************************************/
@@ -55,6 +55,37 @@ std::string to_string(u64bit n, u32bit m
    }

 /*************************************************
+* Convert a string into a time duration          *
+*************************************************/
+u32bit timespec_to_u32bit(const std::string& timespec)
+   {
+   if(timespec == "")
+      return 0;
+
+   const char suffix = timespec[timespec.size()-1];
+   std::string value = timespec.substr(0, timespec.size()-1);
+
+   u32bit scale = 1;
+
+   if(Charset::is_digit(suffix))
+      value += suffix;
+   else if(suffix == 's')
+      scale = 1;
+   else if(suffix == 'm')
+      scale = 60;
+   else if(suffix == 'h')
+      scale = 60 * 60;
+   else if(suffix == 'd')
+      scale = 24 * 60 * 60;
+   else if(suffix == 'y')
+      scale = 365 * 24 * 60 * 60;
+   else
+      throw Decoding_Error("timespec_to_u32bit: Bad input " + timespec);
+
+   return scale * to_u32bit(value);
+   }
+
+/*************************************************
 * Parse a SCAN-style algorithm name              *
 *************************************************/
 std::vector<std::string> parse_algorithm_name(const std::string& namex)
@@ -98,7 +129,7 @@ std::vector<std::string> parse_algorithm
             elems.push_back(substring.substr(1));
          else
             elems.push_back(substring);
-         substring = "";
+         substring.clear();
          }
       else
          substring += c;
@@ -125,7 +156,7 @@ std::vector<std::string> split_on(const
          {
          if(substr != "")
             elems.push_back(substr);
-         substr = "";
+         substr.clear();
          }
       else
          substr += *j;
@@ -155,7 +186,7 @@ std::vector<u32bit> parse_asn1_oid(const
          if(substring == "")
             throw Invalid_OID(oid);
          oid_elems.push_back(to_u32bit(substring));
-         substring = "";
+         substring.clear();
          }
       else
          substring += c;
@@ -211,31 +242,45 @@ bool x500_name_cmp(const std::string& na
    }

 /*************************************************
-* Parse and compute an arithmetic expression     *
+* Convert a decimal-dotted string to binary IP   *
 *************************************************/
-u32bit parse_expr(const std::string& expr)
+u32bit string_to_ipv4(const std::string& str)
    {
-   const bool have_add = (expr.find('+') != std::string::npos);
-   const bool have_mul = (expr.find('*') != std::string::npos);
+   std::vector<std::string> parts = split_on(str, '.');

-   if(have_add)
+   if(parts.size() != 4)
+      throw Decoding_Error("Invalid IP string " + str);
+
+   u32bit ip = 0;
+
+   for(size_t j = 0; j != parts.size(); j++)
       {
-      std::vector<std::string> sub_expr = split_on(expr, '+');
-      u32bit result = 0;
-      for(u32bit j = 0; j != sub_expr.size(); ++j)
-         result += parse_expr(sub_expr[j]);
-      return result;
+      u32bit octet = to_u32bit(parts[j]);
+
+      if(octet > 255)
+         throw Decoding_Error("Invalid IP string " + str);
+
+      ip = (ip << 8) | (octet & 0xFF);
       }
-   else if(have_mul)
+
+   return ip;
+   }
+
+/*************************************************
+* Convert an IP address to decimal-dotted string *
+*************************************************/
+std::string ipv4_to_string(u32bit ip)
+   {
+   std::string str;
+
+   for(size_t j = 0; j != sizeof(ip); j++)
       {
-      std::vector<std::string> sub_expr = split_on(expr, '*');
-      u32bit result = 1;
-      for(u32bit j = 0; j != sub_expr.size(); ++j)
-         result *= parse_expr(sub_expr[j]);
-      return result;
+      if(j)
+         str += ".";
+      str += to_string(get_byte(j, ip));
       }
-   else
-      return to_u32bit(expr);
+
+   return str;
    }

 }