Debian Codegraph @

src/utils/checksum_extractor.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2018
4 * Author: Georgios Bitzes <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <utils/stringutils.hpp>
23 #include "libs/alibxx/crypto/base64.hpp"
24 #include "checksum_extractor.hpp"
25 #include <ctype.h>
26 #include <sstream>
27 #include <iomanip>
28
29 #define SSTR(message) static_cast<std::ostringstream&>(std::ostringstream().flush() << message).str()
30
31 namespace Davix {
32
33 static bool startsWithNoCase(const std::string &str, const std::string &prefix) {
34 if(prefix.size() > str.size()) return false;
35
36 for(size_t i = 0; i < prefix.size(); i++) {
37 if(tolower(str[i]) != tolower(prefix[i])) return false;
38 }
39
40 return true;
41 }
42
43 static bool equalsNoCase(const std::string &s1, const std::string &s2) {
44 if(s1.size() != s2.size()) return false;
45
46 for(size_t i = 0; i < s1.size(); i++) {
47 if(tolower(s1[i]) != tolower(s2[i])) return false;
48 }
49
50 return true;
51 }
52
53 static std::string hexEncode(const std::string &input, const std::string &separator="") {
54 std::ostringstream ss;
55 for(std::string::const_iterator it = input.begin(); it != input.end(); it++) {
56 ss << std::setw(2) << std::setfill('0') << std::hex << (unsigned int) ( (unsigned char) *it) << separator;
57 }
58 return ss.str();
59 }
60
61 static std::vector<std::string> split(std::string data, std::string token) {
62 std::vector<std::string> output;
63 size_t pos = std::string::npos;
64 do {
65 pos = data.find(token);
66 output.push_back(data.substr(0, pos));
67 if(std::string::npos != pos) data = data.substr(pos + token.size());
68 } while (std::string::npos != pos);
69 return output;
70 }
71
72 bool ChecksumExtractor::extractChecksum(const std::string &headerLine,
73 const std::string &desiredChecksum, std::string &checksum) {
74
75 std::string expectedPrefix = SSTR(desiredChecksum << "=");
76 std::vector<std::string> chunks = split(headerLine, ",");
77
78 for(size_t i = 0; i < chunks.size(); i++) {
79 if(startsWithNoCase(chunks[i], expectedPrefix)) {
80 // We have a match. Are we supposed to base64 decode this?
81 checksum = chunks[i].substr(expectedPrefix.size());
82
83 if(StrUtil::compare_ncase(desiredChecksum, "UNIXcksum") == 0 ||
84 StrUtil::compare_ncase(desiredChecksum, "CRC32c") == 0 ||
85 StrUtil::compare_ncase(desiredChecksum, "UNIXsum") == 0
86 ) {
87 // Nope, just extract the value.
88 return true;
89 }
90
91 if(StrUtil::compare_ncase(desiredChecksum, "ADLER32") == 0) {
92 checksum.insert(checksum.begin(), 8 - checksum.length(), '0'); // DMC-1245: Add leading 0 to Adler32
93 return true;
94 }
95
96 if(StrUtil::compare_ncase(desiredChecksum, "md5") == 0) {
97 // Maybe.. older versions of DPM don't base64 encode their output.
98 if(checksum.size() != 32) {
99 checksum = hexEncode(Base64::base64_decode(checksum));
100 }
101 return true;
102 }
103
104 // All other checksums should be base64 decoded.
105 checksum = hexEncode(Base64::base64_decode(checksum));
106 return true;
107 }
108 }
109
110 return false;
111 }
112
113 bool ChecksumExtractor::extractChecksum(const HeaderVec &headers,
114 const std::string &desiredChecksum, std::string &checksum) {
115
116 for(HeaderVec::const_iterator it = headers.begin(); it != headers.end(); it++) {
117 if(equalsNoCase(it->first, "Digest")) {
118 if(extractChecksum(it->second, desiredChecksum, checksum)) {
119 return true;
120 }
121 }
122 }
123
124 return false;
125 }
126
127 }