dcpp/Encoder.cpp

filter: Statementstmt 68.6% 35 / 51 Branchbran 44.7% 17 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 58.4% 52 / 89 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary dcpp/MerkleTree.h »
1 /*
2 * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18  
19 #include "stdinc.h"
20 #include "Encoder.h"
21  
22 /*
23 #include "Exception.h"
24 */
25  
26 #include <cstring>
27  
28 #include "debug.h"
29  
30 namespace dcpp {
31  
32 const int8_t Encoder::base32Table[] = {
33 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
34 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
35 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
36 -1,-1,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1,
37 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
38 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
39 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
40 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
41 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
42 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
43 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
44 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
45 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
46 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
47 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
48 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
49 };
50  
51 const char Encoder::base32Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
52  
53 17 string& Encoder::toBase32(const uint8_t* src, size_t len, string& dst) {
54 // Code snagged from the bitzi bitcollider
55 size_t i, index;
56 uint8_t word;
57 17 dst.reserve(((len * 8) / 5) + 1);
58  
59 606 ▶ for(i = 0, index = 0; i < len;) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0, index = 0; i < len;) { 589 17 606
60 /* Is the current word going to span a byte boundary? */
61 589 ▶ if (index > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (index > 3) { 302 287 589
62 302 word = (uint8_t)(src[i] & (0xFF >> index));
63 302 index = (index + 5) % 8;
64 302 word <<= index;
65 302 ▶ if ((i + 1) < len)
Branch: 1 branch
BranchTrueFalseTotal
if ((i + 1) < len) 285 17 302
66 285 word |= src[i + 1] >> (8 - index);
67  
68 302 i++;
69 } else {
70 287 word = (uint8_t)(src[i] >> (8 - (index + 5))) & 0x1F;
71 287 index = (index + 5) % 8;
72 287 ▶ if (index == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index == 0) 60 227 287
73 60 i++;
74 }
75  
76 dcassert(word < 32);
77 589 dst += base32Alphabet[word];
78 }
79 17 return dst;
80 }
81  
82 2 void Encoder::fromBase32(const char* src, uint8_t* dst, size_t len) {
83 size_t i, index, offset;
84  
85 2 memset(dst, 0, len);
86 41 ▶ for(i = 0, index = 0, offset = 0; src[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0, index = 0, offset = 0; src[i]; i++) { 41 0 41
87 // Skip what we don't recognise
88 41 int8_t tmp = base32Table[(unsigned char)src[i]];
89  
90 41 ▶ if(tmp == -1)
Branch: 1 branch
BranchTrueFalseTotal
if(tmp == -1) 0 41 41
91 0 continue;
92  
93 41 ▶ if (index <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (index <= 3) { 20 21 41
94 20 index = (index + 5) % 8;
95 20 ▶ if (index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index == 0) { 4 16 20
96 4 dst[offset] |= tmp;
97 4 offset++;
98 4 ▶ if(offset == len)
Branch: 1 branch
BranchTrueFalseTotal
if(offset == len) 0 4 4
99 0 break;
100 } else {
101 16 dst[offset] |= tmp << (8 - index);
102 }
103 } else {
104 21 index = (index + 5) % 8;
105 21 dst[offset] |= (tmp >> index);
106 21 offset++;
107 21 ▶ if(offset == len)
Branch: 1 branch
BranchTrueFalseTotal
if(offset == len) 2 19 21
108 2 break;
109 19 dst[offset] |= tmp << (8 - index);
110 }
111 }
112 2 }
113  
114 0 uint8_t decode16(char c) {
115 0 ▶ if (c >= '0' && c <= '9')
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') 0 0 0
if (c >= '0' && c <= '9') 0 0 0
116 0 return c - '0';
117 0 ▶ if (c >= 'A' && c <= 'F')
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') 0 0 0
if (c >= 'A' && c <= 'F') 0 0 0
118 0 return c - 'A';
119 0 ▶ if (c >= 'a' && c <= 'f')
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') 0 0 0
if (c >= 'a' && c <= 'f') 0 0 0
120 0 return c - 'a';
121 //throw Exception("can't decode");
122 }
123  
124 0 void Encoder::fromBase16(const char* src, uint8_t* dst, size_t len) {
125 0 memset(dst, 0, len);
126 0 ▶ for(size_t i = 0; src[i] && src[i+1] && i < len * 2; i += 2) {
Branch: 3 branches
BranchTrueFalseTotal
for(size_t i = 0; src[i] && src[i+1] && i < len * 2; i += 2) { 0 0 0
for(size_t i = 0; src[i] && src[i+1] && i < len * 2; i += 2) { 0 0 0
for(size_t i = 0; src[i] && src[i+1] && i < len * 2; i += 2) { 0 0 0
127 // Skip what we don't recognise
128 0 auto tmp = decode16(src[i]);
129 0 auto tmp2 = decode16(src[i+1]);
130 0 dst[i/2] = (tmp << 4) + tmp2;
131 }
132 0 }
133  
134 } // namespace dcpp
↑ Summary dcpp/MerkleTree.h »