hrev46484 adds 1 changeset to branch 'master' old head: 5287b8a778715de8320fe2bd932ceab5c185cc41 new head: ab3fd9c828489bc13d6d26612ca7b013d6c187cb overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=ab3fd9c+%5E5287b8a ---------------------------------------------------------------------------- ab3fd9c: Move {en,de}code_base64 to the support kit It's not possible to use these from the network kit otherwise, as it would create a circular dependency (mail > bnetapi > mail). Is there a better way to solve this problem? [ Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> ] ---------------------------------------------------------------------------- Revision: hrev46484 Commit: ab3fd9c828489bc13d6d26612ca7b013d6c187cb URL: http://cgit.haiku-os.org/haiku/commit/?id=ab3fd9c Author: Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> Date: Wed Dec 4 12:51:31 2013 UTC ---------------------------------------------------------------------------- 3 files changed, 132 insertions(+), 117 deletions(-) src/kits/mail/mail_encoding.cpp | 117 -------------------------------- src/kits/support/Base64.cpp | 131 ++++++++++++++++++++++++++++++++++++ src/kits/support/Jamfile | 1 + ---------------------------------------------------------------------------- diff --git a/src/kits/mail/mail_encoding.cpp b/src/kits/mail/mail_encoding.cpp index 6aff122..e0a9910 100644 --- a/src/kits/mail/mail_encoding.cpp +++ b/src/kits/mail/mail_encoding.cpp @@ -15,16 +15,6 @@ #define DEC(c) (((c) - ' ') & 077) -static const char kBase64Alphabet[64] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - '+', - '/' - }; - static const char kHexAlphabet[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8','9','A','B','C','D','E','F'}; @@ -123,113 +113,6 @@ encoding_for_cte(const char *cte) ssize_t -encode_base64(char *out, const char *in, off_t length, int headerMode) -{ - uint32 concat; - int i = 0; - int k = 0; - int lineLength = 4; - // Stop before it actually gets too long - - while (i < length) { - concat = ((in[i] & 0xff) << 16); - - if ((i+1) < length) - concat |= ((in[i+1] & 0xff) << 8); - if ((i+2) < length) - concat |= (in[i+2] & 0xff); - - i += 3; - - out[k++] = kBase64Alphabet[(concat >> 18) & 63]; - out[k++] = kBase64Alphabet[(concat >> 12) & 63]; - out[k++] = kBase64Alphabet[(concat >> 6) & 63]; - out[k++] = kBase64Alphabet[concat & 63]; - - if (i >= length) { - int v; - for (v = 0; v <= (i - length); v++) - out[k-v] = '='; - } - - lineLength += 4; - - // No line breaks in header mode, since the text is part of a Subject: - // line or some other single header line. The header code will do word - // wrapping separately from this encoding stuff. - if (!headerMode && lineLength > BASE64_LINELENGTH) { - out[k++] = '\r'; - out[k++] = '\n'; - - lineLength = 4; - } - } - - return k; -} - - -ssize_t -decode_base64(char *out, const char *in, off_t length) -{ - uint32 concat, value; - int lastOutLine = 0; - int i, j; - int outIndex = 0; - - for (i = 0; i < length; i += 4) { - concat = 0; - - for (j = 0; j < 4 && (i + j) < length; j++) { - value = in[i + j]; - - if (value == '\n' || value == '\r') { - // jump over line breaks - lastOutLine = outIndex; - i++; - j--; - continue; - } - - if ((value >= 'A') && (value <= 'Z')) - value -= 'A'; - else if ((value >= 'a') && (value <= 'z')) - value = value - 'a' + 26; - else if ((value >= '0') && (value <= '9')) - value = value - '0' + 52; - else if (value == '+') - value = 62; - else if (value == '/') - value = 63; - else if (value == '=') - break; - else { - // there is an invalid character in this line - we will - // ignore the whole line and go to the next - outIndex = lastOutLine; - while (i < length && in[i] != '\n' && in[i] != '\r') - i++; - concat = 0; - } - - value = value << ((3-j)*6); - - concat |= value; - } - - if (j > 1) - out[outIndex++] = (concat & 0x00ff0000) >> 16; - if (j > 2) - out[outIndex++] = (concat & 0x0000ff00) >> 8; - if (j > 3) - out[outIndex++] = (concat & 0x000000ff); - } - - return outIndex; -} - - -ssize_t decode_qp(char *out, const char *in, off_t length, int underscoreIsSpace) { // decode Quoted Printable diff --git a/src/kits/support/Base64.cpp b/src/kits/support/Base64.cpp new file mode 100644 index 0000000..476208c --- /dev/null +++ b/src/kits/support/Base64.cpp @@ -0,0 +1,131 @@ +/* + * Copyright 2011-2013, Haiku, Inc. All rights reserved. + * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved. + */ + + +#include <mail_encoding.h> + +#include <ctype.h> +#include <string.h> +#include <SupportDefs.h> + + +static const char kBase64Alphabet[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '+', + '/' + }; + + +ssize_t +encode_base64(char *out, const char *in, off_t length, int headerMode) +{ + uint32 concat; + int i = 0; + int k = 0; + int lineLength = 4; + // Stop before it actually gets too long + + while (i < length) { + concat = ((in[i] & 0xff) << 16); + + if ((i+1) < length) + concat |= ((in[i+1] & 0xff) << 8); + if ((i+2) < length) + concat |= (in[i+2] & 0xff); + + i += 3; + + out[k++] = kBase64Alphabet[(concat >> 18) & 63]; + out[k++] = kBase64Alphabet[(concat >> 12) & 63]; + out[k++] = kBase64Alphabet[(concat >> 6) & 63]; + out[k++] = kBase64Alphabet[concat & 63]; + + if (i >= length) { + int v; + for (v = 0; v <= (i - length); v++) + out[k-v] = '='; + } + + lineLength += 4; + + // No line breaks in header mode, since the text is part of a Subject: + // line or some other single header line. The header code will do word + // wrapping separately from this encoding stuff. + if (!headerMode && lineLength > BASE64_LINELENGTH) { + out[k++] = '\r'; + out[k++] = '\n'; + + lineLength = 4; + } + } + + return k; +} + + +ssize_t +decode_base64(char *out, const char *in, off_t length) +{ + uint32 concat, value; + int lastOutLine = 0; + int i, j; + int outIndex = 0; + + for (i = 0; i < length; i += 4) { + concat = 0; + + for (j = 0; j < 4 && (i + j) < length; j++) { + value = in[i + j]; + + if (value == '\n' || value == '\r') { + // jump over line breaks + lastOutLine = outIndex; + i++; + j--; + continue; + } + + if ((value >= 'A') && (value <= 'Z')) + value -= 'A'; + else if ((value >= 'a') && (value <= 'z')) + value = value - 'a' + 26; + else if ((value >= '0') && (value <= '9')) + value = value - '0' + 52; + else if (value == '+') + value = 62; + else if (value == '/') + value = 63; + else if (value == '=') + break; + else { + // there is an invalid character in this line - we will + // ignore the whole line and go to the next + outIndex = lastOutLine; + while (i < length && in[i] != '\n' && in[i] != '\r') + i++; + concat = 0; + } + + value = value << ((3-j)*6); + + concat |= value; + } + + if (j > 1) + out[outIndex++] = (concat & 0x00ff0000) >> 16; + if (j > 2) + out[outIndex++] = (concat & 0x0000ff00) >> 8; + if (j > 3) + out[outIndex++] = (concat & 0x000000ff); + } + + return outIndex; +} + + diff --git a/src/kits/support/Jamfile b/src/kits/support/Jamfile index e2ac639..aed31d5 100644 --- a/src/kits/support/Jamfile +++ b/src/kits/support/Jamfile @@ -13,6 +13,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { Architecture.cpp Archivable.cpp ArchivingManagers.cpp + Base64.cpp Beep.cpp BlockCache.cpp BufferedDataIO.cpp