hex.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <cassert>
  16. #include <iterator>
  17. #include <iomanip>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. #include <exceptions/exceptions.h>
  23. #include <boost/foreach.hpp>
  24. #include <ctype.h>
  25. #include <stdint.h>
  26. #include <dns/hex.h>
  27. using namespace std;
  28. namespace isc {
  29. namespace dns {
  30. namespace {
  31. const char hexdigits[] = "0123456789ABCDEF";
  32. }
  33. std::string
  34. encodeHex(const std::vector<uint8_t>& binary) {
  35. // calculate the resulting length. it should be twice the
  36. // original data length
  37. const size_t len = (binary.size() * 2);
  38. std::ostringstream hex;
  39. BOOST_FOREACH(uint8_t octet, binary) {
  40. hex << hexdigits[octet >> 4] << hexdigits[octet & 0xf];
  41. }
  42. assert(len >= hex.str().length());
  43. return (hex.str());
  44. }
  45. void
  46. decodeHex(const std::string& hex, std::vector<uint8_t>& result) {
  47. ostringstream comp;
  48. // compress input by removing whitespace
  49. const size_t len = hex.length();
  50. for (int i = 0; i < len; ++i) {
  51. char c = hex.at(i);
  52. if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
  53. continue;
  54. }
  55. comp << c;
  56. }
  57. istringstream iss(comp.str());
  58. result.clear();
  59. char c1, c2;
  60. iss.width(1);
  61. if ((comp.str().length() % 2) == 1) {
  62. c2 = '0';
  63. iss >> c2;
  64. const char* pos = strchr(hexdigits, toupper(c2));
  65. if (pos == NULL) {
  66. isc_throw(BadHexString, "Invalid hex digit");
  67. }
  68. if (!iss.eof() && !iss.bad() && !iss.fail()) {
  69. result.push_back(pos - hexdigits);
  70. }
  71. }
  72. while (!iss.eof()) {
  73. c1 = c2 = '0';
  74. iss >> c1 >> c2;;
  75. if (iss.eof() || iss.fail() || iss.bad()) {
  76. break;
  77. }
  78. const char* pos1 = strchr(hexdigits, toupper(c1));
  79. const char* pos2 = strchr(hexdigits, toupper(c2));
  80. if (!pos1 || !pos2) {
  81. isc_throw(BadHexString, "Invalid hex digit");
  82. }
  83. const uint8_t n = ((pos1 - hexdigits) << 4) | (pos2 - hexdigits);
  84. result.push_back(n);
  85. }
  86. }
  87. }
  88. }