mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-10 08:00:38 +03:00
Tests and documentation for base32.
This commit is contained in:
parent
28a4f4173d
commit
469981cce5
10
base64.cpp
10
base64.cpp
@ -33,16 +33,14 @@ namespace data
|
||||
}
|
||||
|
||||
/*
|
||||
* Reverse Substitution Table (built in run time)
|
||||
*/
|
||||
|
||||
* Reverse Substitution Table (built in run time)
|
||||
*/
|
||||
static char iT64[256];
|
||||
static int isFirstTime = 1;
|
||||
|
||||
/*
|
||||
* Padding
|
||||
*/
|
||||
|
||||
static char P64 = '=';
|
||||
|
||||
|
||||
@ -168,7 +166,6 @@ namespace data
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
static void iT64Build()
|
||||
{
|
||||
int i;
|
||||
@ -208,6 +205,9 @@ namespace data
|
||||
|
||||
size_t ByteStreamToBase32 (const uint8_t * inBuf, size_t len, char * outBuf, size_t outLen)
|
||||
{
|
||||
if(!len)
|
||||
return 0; // No data given
|
||||
|
||||
size_t ret = 0, pos = 1;
|
||||
int bits = 8, tmp = inBuf[0];
|
||||
while (ret < outLen && (bits > 0 || pos < len))
|
||||
|
28
base64.h
28
base64.h
@ -12,28 +12,46 @@ namespace data
|
||||
|
||||
/*
|
||||
* Base64 encodes an array of bytes.
|
||||
* @return the number of characters in the output buffer
|
||||
* @return the number of characters written to the output buffer
|
||||
* @param InBuffer array of input bytes to be encoded
|
||||
* @param InCount length of the input array
|
||||
* @param OutBuffer array of output characters
|
||||
* @param OutBuffer array to store output characters
|
||||
* @param len length of the output buffer
|
||||
*/
|
||||
size_t ByteStreamToBase64 (const uint8_t * InBuffer, size_t InCount, char * OutBuffer, size_t len);
|
||||
|
||||
/**
|
||||
* Decodes base 64 encoded data to an array of bytes.
|
||||
* @return the number of bytes in the output buffer
|
||||
* @return the number of bytes written to the output buffer
|
||||
* @param InBuffer array of input characters to be decoded
|
||||
* @param InCount length of the input array
|
||||
* @param OutBuffer array of output bytes
|
||||
* @param OutBuffer array to store output bytes
|
||||
* @param len length of the output buffer
|
||||
* @todo Do not return a negative value on failure, size_t could be unsigned.
|
||||
*/
|
||||
size_t Base64ToByteStream (const char * InBuffer, size_t InCount, uint8_t * OutBuffer, size_t len );
|
||||
|
||||
const char * GetBase64SubstitutionTable ();
|
||||
|
||||
/**
|
||||
* Decodes base 32 encoded data to an array of bytes.
|
||||
* @return the number of bytes written to the output buffer
|
||||
* @param inBuf array of input characters to be decoded
|
||||
* @param len length of the input buffer
|
||||
* @param outBuf array to store output bytes
|
||||
* @param outLen length of the output array
|
||||
*/
|
||||
size_t Base32ToByteStream (const char * inBuf, size_t len, uint8_t * outBuf, size_t outLen);
|
||||
size_t ByteStreamToBase32 (const uint8_t * InBuf, size_t len, char * outBuf, size_t outLen);
|
||||
|
||||
/**
|
||||
* Base 32 encodes an array of bytes.
|
||||
* @return the number of bytes written to the output buffer
|
||||
* @param inBuf array of input bytes to be encoded
|
||||
* @param len length of the input buffer
|
||||
* @param outBuf array to store output characters
|
||||
* @param outLen length of the output array
|
||||
*/
|
||||
size_t ByteStreamToBase32 (const uint8_t * inBuf, size_t len, char * outBuf, size_t outLen);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,12 +9,12 @@ using namespace i2p::data;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base64EncodeEmpty)
|
||||
{
|
||||
ByteStreamToBase64(nullptr, 0, nullptr, 0);
|
||||
BOOST_CHECK_EQUAL(ByteStreamToBase64(nullptr, 0, nullptr, 0), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base64DecodeEmpty)
|
||||
{
|
||||
Base64ToByteStream(nullptr, 0, nullptr, 0);
|
||||
BOOST_CHECK_EQUAL(Base64ToByteStream(nullptr, 0, nullptr, 0), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base64Encode)
|
||||
@ -43,7 +43,40 @@ BOOST_AUTO_TEST_CASE(Base64Decode)
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result, result + 25, output, output + 25);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base32EncodeEmpty)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(ByteStreamToBase32(nullptr, 0, nullptr, 0), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base32DecodeEmpty)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(Base32ToByteStream(nullptr, 0, nullptr, 0), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base32Encode)
|
||||
{
|
||||
const uint8_t input[] = {
|
||||
0x53, 0xd3, 0x60, 0xfa, 0xf9, 0x58, 0xd0, 0x5e, 0x41, 0xa9, 0x6c,
|
||||
0xf1, 0x9f, 0xc4, 0xe, 0x23, 0x9b, 0xca, 0xb1, 0x61, 0xa7, 0x33, 0xcf,
|
||||
0x1f, 0x30
|
||||
};
|
||||
const char* output = "kpjwb6xzldif4qnjntyz7raoeon4vmlbu4z46hzq";
|
||||
char result[40];
|
||||
ByteStreamToBase32(input, 25, result, 40);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result, result + 40, output, output + 40);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Base32Decode)
|
||||
{
|
||||
const char* input = "kpjwb6xzldif4qnjntyz7raoeon4vmlbu4z46hzq";
|
||||
const uint8_t output[] = {
|
||||
0x53, 0xd3, 0x60, 0xfa, 0xf9, 0x58, 0xd0, 0x5e, 0x41, 0xa9, 0x6c,
|
||||
0xf1, 0x9f, 0xc4, 0xe, 0x23, 0x9b, 0xca, 0xb1, 0x61, 0xa7, 0x33, 0xcf,
|
||||
0x1f, 0x30
|
||||
};
|
||||
uint8_t result[25];
|
||||
Base32ToByteStream(input, 40, result, 25);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result, result + 25, output, output + 25);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
Loading…
Reference in New Issue
Block a user