mirror of
https://github.com/glebd/cocoafob.git
synced 2026-05-31 15:07:35 +00:00
114 lines
4.4 KiB
C
114 lines
4.4 KiB
C
/* 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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
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FF, 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;
|
|
}
|