Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "hash-string-converter.h" |
| 23 | |
| 24 | #include <boost/assert.hpp> |
| 25 | #include <boost/throw_exception.hpp> |
| 26 | #include <boost/make_shared.hpp> |
| 27 | #include <openssl/evp.h> |
| 28 | |
| 29 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 30 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 31 | |
| 32 | #include <boost/archive/iterators/transform_width.hpp> |
| 33 | #include <boost/iterator/transform_iterator.hpp> |
| 34 | #include <boost/archive/iterators/dataflow_exception.hpp> |
| 35 | |
| 36 | using namespace boost; |
| 37 | using namespace boost::archive::iterators; |
| 38 | using namespace std; |
| 39 | |
| 40 | |
| 41 | template<class CharType> |
| 42 | struct hex_from_4_bit |
| 43 | { |
| 44 | typedef CharType result_type; |
| 45 | CharType operator () (CharType ch) const |
| 46 | { |
| 47 | const char *lookup_table = "0123456789abcdef"; |
| 48 | // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 49 | BOOST_ASSERT (ch < 16); |
| 50 | return lookup_table[static_cast<size_t>(ch)]; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>, |
| 55 | transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary; |
| 56 | |
| 57 | |
| 58 | template<class CharType> |
| 59 | struct hex_to_4_bit |
| 60 | { |
| 61 | typedef CharType result_type; |
| 62 | CharType operator () (CharType ch) const |
| 63 | { |
| 64 | const signed char lookup_table [] = { |
| 65 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 66 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 67 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 68 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
| 69 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 70 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 71 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 72 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
| 73 | }; |
| 74 | |
| 75 | // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 76 | signed char value = -1; |
| 77 | if ((unsigned)ch < 128) |
| 78 | value = lookup_table [(unsigned)ch]; |
| 79 | if (value == -1) |
| 80 | BOOST_THROW_EXCEPTION (Error::HashConversion () << errmsg_info_int ((int)ch)); |
| 81 | |
| 82 | return value; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary; |
| 87 | |
| 88 | |
| 89 | std::ostream & |
| 90 | operator << (std::ostream &os, const Hash &hash) |
| 91 | { |
| 92 | if (hash.m_length == 0) |
| 93 | return os; |
| 94 | |
| 95 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 96 | // need to encode to base64 |
| 97 | copy (string_from_binary (reinterpret_cast<const char*> (hash.m_buf)), |
| 98 | string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)), |
| 99 | out_it); |
| 100 | |
| 101 | return os; |
| 102 | } |
| 103 | |
| 104 | Hash::Hash (const std::string &hashInTextEncoding) |
| 105 | { |
| 106 | if (hashInTextEncoding.size () == 0) |
| 107 | { |
| 108 | m_buf = 0; |
| 109 | m_length = 0; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2) |
| 114 | { |
| 115 | cerr << "Input hash is too long. Returning an empty hash" << endl; |
| 116 | m_buf = 0; |
| 117 | m_length = 0; |
| 118 | return; |
| 119 | } |
| 120 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame^] | 121 | m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 122 | |
| 123 | unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()), |
| 124 | string_to_binary (hashInTextEncoding.end ()), |
| 125 | m_buf); |
| 126 | |
| 127 | m_length = end-m_buf; |
| 128 | } |
| 129 | |