forked from billymeltdown/cocoafob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.c
113 lines (100 loc) · 4.44 KB
/
decoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright (c) 2007 Samuel Tesla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "decoder.h"
static inline uint8_t
decode_bits (const uint8_t bits)
{
uint8_t table[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
return table[bits];
}
size_t
base32_decoder_buffer_size (const size_t encodedTextLength)
{
if (encodedTextLength == 0 || encodedTextLength % 8 != 0)
return 0;
return encodedTextLength * 8 / 5;
}
size_t
base32_decode (uint8_t *output, const size_t outputLength, const uint8_t *input, const size_t inputLength)
{
if (outputLength == 0 || inputLength == 0 || inputLength % 8 != 0)
return 0;
size_t bytes = 0;
uint8_t currentByte = 0;
unsigned offset;
for (offset = 0; offset < inputLength && bytes < outputLength; offset += 8)
{
output[bytes] = decode_bits (input[offset + 0]) << 3;
currentByte = decode_bits (input[offset + 1]);
output[bytes] += currentByte >> 2;
output[bytes + 1] = (currentByte & 0x03) << 6;
if (input[offset + 2] == '=')
return bytes + 1;
else
bytes++;
output[bytes] += decode_bits (input[offset + 2]) << 1;
currentByte = decode_bits (input[offset + 3]);
output[bytes] += currentByte >> 4;
output[bytes + 1] = currentByte << 4;
if (input[offset + 4] == '=')
return bytes + 1;
else
bytes++;
currentByte = decode_bits (input[offset + 4]);
output[bytes] += currentByte >> 1;
output[bytes + 1] = currentByte << 7;
if (input[offset + 5] == '=')
return bytes + 1;
else
bytes++;
output[bytes] += decode_bits (input[offset + 5]) << 2;
currentByte = decode_bits (input[offset + 6]);
output[bytes] += currentByte >> 3;
output[bytes + 1] = (currentByte & 0x07) << 5;
if (input[offset + 7] == '=')
return bytes + 1;
else
bytes++;
output[bytes] += decode_bits (input[offset + 7]) & 0x1F;
bytes++;
}
return bytes;
}