summaryrefslogtreecommitdiff
path: root/zen/base64.h
diff options
context:
space:
mode:
Diffstat (limited to 'zen/base64.h')
-rw-r--r--zen/base64.h55
1 files changed, 26 insertions, 29 deletions
diff --git a/zen/base64.h b/zen/base64.h
index fd4e3611..5f18ea36 100644
--- a/zen/base64.h
+++ b/zen/base64.h
@@ -1,31 +1,31 @@
// **************************************************************************
// * This file is part of the zenXML project. It is distributed under the *
-// * Boost Software License, Version 1.0. See accompanying file *
-// * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt. *
-// * Copyright (C) ZenJu (zenju AT gmx DOT de) - All Rights Reserved *
+// * Boost Software License: http://www.boost.org/LICENSE_1_0.txt *
+// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef BASE64_HEADER_08473021856321840873021487213453214
#define BASE64_HEADER_08473021856321840873021487213453214
+#ifndef NDEBUG //no release build dependencies!
#include <iterator>
#include <cassert>
-#include "assert_static.h"
+#endif
namespace zen
{
//http://en.wikipedia.org/wiki/Base64
/*
Usage:
- const std::string input = "Sample text";
+ const std::string input = "Sample text";
std::string output;
zen::encodeBase64(input.begin(), input.end(), std::back_inserter(output));
//output contains "U2FtcGxlIHRleHQ="
*/
template <class InputIterator, class OutputIterator>
-OutputIterator encodeBase64(InputIterator first, InputIterator last, OutputIterator result); //throw ()
+OutputIterator encodeBase64(InputIterator first, InputIterator last, OutputIterator result); //nothrow!
template <class InputIterator, class OutputIterator>
-OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputIterator result); //throw ()
+OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputIterator result); //nothrow!
@@ -38,14 +38,11 @@ OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputItera
-
-
-
-
-//---------------------------------------- implementation ----------------------------------------
+//------------------------- implementation -------------------------------
namespace implementation
{
-const char ENCODING_MIME[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; //64 chars for base64 encoding + padding char
+//64 chars for base64 encoding + padding char
+const char ENCODING_MIME[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const signed char DECODING_MIME[] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -57,7 +54,7 @@ const signed char DECODING_MIME[] =
-1, 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, -1, -1, -1, -1, -1
};
-const size_t INDEX_PAD = 64;
+const unsigned char INDEX_PAD = 64; //"="
}
@@ -66,12 +63,12 @@ template <class InputIterator, class OutputIterator> inline
OutputIterator encodeBase64(InputIterator first, InputIterator last, OutputIterator result)
{
using namespace implementation;
- assert_static(sizeof(std::iterator_traits<InputIterator>::value_type) == 1);
- assert_static(sizeof(ENCODING_MIME) == 65 + 1);
+ static_assert(sizeof(typename std::iterator_traits<InputIterator>::value_type) == 1, "");
+ static_assert(sizeof(ENCODING_MIME) == 65 + 1, "");
while (first != last)
{
- const unsigned char a = *first++;
+ const unsigned char a = static_cast<unsigned char>(*first++);
*result++ = ENCODING_MIME[a >> 2];
if (first == last)
@@ -81,7 +78,7 @@ OutputIterator encodeBase64(InputIterator first, InputIterator last, OutputItera
*result++ = ENCODING_MIME[INDEX_PAD];
break;
}
- const unsigned char b = *first++;
+ const unsigned char b = static_cast<unsigned char>(*first++);
*result++ = ENCODING_MIME[((a & 0x3) << 4) | (b >> 4)];
if (first == last)
@@ -90,7 +87,7 @@ OutputIterator encodeBase64(InputIterator first, InputIterator last, OutputItera
*result++ = ENCODING_MIME[INDEX_PAD];
break;
}
- const unsigned char c = *first++;
+ const unsigned char c = static_cast<unsigned char>(*first++);
*result++ = ENCODING_MIME[((b & 0xf) << 2) | (c >> 6)];
*result++ = ENCODING_MIME[c & 0x3f];
}
@@ -103,10 +100,10 @@ template <class InputIterator, class OutputIterator> inline
OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputIterator result)
{
using namespace implementation;
- assert_static(sizeof(std::iterator_traits<InputIterator>::value_type) == 1);
- assert_static(sizeof(DECODING_MIME) == 128);
+ static_assert(sizeof(typename std::iterator_traits<InputIterator>::value_type) == 1, "");
+ static_assert(sizeof(DECODING_MIME) == 128, "");
- const int INDEX_END = INDEX_PAD + 1;
+ const unsigned char INDEX_END = INDEX_PAD + 1;
auto readIndex = [&]() -> unsigned char //return index within [0, 64] or INDEX_END if end of input
{
@@ -115,17 +112,17 @@ OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputItera
if (first == last)
return INDEX_END;
- unsigned char ch = *first++;
- if (ch < 128) //skip all unknown characters (including carriage return, line-break, tab)
+ const unsigned char ch = static_cast<unsigned char>(*first++);
+ if (ch < 128) //we're in lower ASCII table half
{
const int index = implementation::DECODING_MIME[ch];
- if (0 <= index && index <= INDEX_PAD) //respect padding
+ if (0 <= index && index <= static_cast<int>(INDEX_PAD)) //skip all unknown characters (including carriage return, line-break, tab)
return index;
}
}
};
- while (true)
+ for (;;)
{
const unsigned char index1 = readIndex();
const unsigned char index2 = readIndex();
@@ -134,7 +131,7 @@ OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputItera
assert(index1 == INDEX_END && index2 == INDEX_END);
break;
}
- *result++ = (index1 << 2) | (index2 >> 4);
+ *result++ = static_cast<char>((index1 << 2) | (index2 >> 4));
const unsigned char index3 = readIndex();
if (index3 >= INDEX_PAD) //padding
@@ -142,7 +139,7 @@ OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputItera
assert(index3 == INDEX_PAD);
break;
}
- *result++ = ((index2 & 0xf) << 4) | (index3 >> 2);
+ *result++ = static_cast<char>(((index2 & 0xf) << 4) | (index3 >> 2));
const unsigned char index4 = readIndex();
if (index4 >= INDEX_PAD) //padding
@@ -150,7 +147,7 @@ OutputIterator decodeBase64(InputIterator first, InputIterator last, OutputItera
assert(index4 == INDEX_PAD);
break;
}
- *result++ = ((index3 & 0x3) << 6) | index4;
+ *result++ = static_cast<char>(((index3 & 0x3) << 6) | index4);
}
return result;
}
bgstack15