From 0e126ee090ecb7ade14006f6dcb1aa423e3c47c1 Mon Sep 17 00:00:00 2001 From: prozacx Date: Sun, 25 Sep 2005 07:54:29 +0000 Subject: [PATCH] Added *Chomp_n() functions and Base64Encode() git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@533 726aef4b-f618-498e-8847-2d620e286838 --- String.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ String.h | 3 ++ 2 files changed, 100 insertions(+) diff --git a/String.cpp b/String.cpp index b98b8c5f..f6c7d7f0 100644 --- a/String.cpp +++ b/String.cpp @@ -473,6 +473,91 @@ CString CString::Format(const CString& sFormatStr, ...) { return ""; } +bool CString::Base64Encode(CString& sRet, unsigned int uWrap) const { + static char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + sRet.clear(); + size_t len = size(); + const char* input = c_str(); + char *output, *p; + size_t i = 0, mod = len % 3, toalloc; + toalloc = (len / 3) * 4 + (3 - mod) % 3 + 1 + 8; + + if (uWrap) { + toalloc += len / 57; + if (len % 57) { + toalloc++; + } + } + + if (toalloc < len) { + return 0; + } + + //p = output = (unsigned char *)malloc(toalloc); + p = output = new char [toalloc]; + + if (!p) { + return false; + } + + while (i < len - mod) { + *p++ = b64table[input[i++] >> 2]; + *p++ = b64table[((input[i - 1] << 4) | (input[i] >> 4)) & 0x3f]; + *p++ = b64table[((input[i] << 2) | (input[i + 1] >> 6)) & 0x3f]; + *p++ = b64table[input[i + 1] & 0x3f]; + i += 2; + + if (uWrap && !(i % 57)) { + *p++ = '\n'; + } + } + + if (!mod) { + if (uWrap && i % 57) { + *p++ = '\n'; + } + + *p = 0; + + sRet = output; + delete[] output; + return true; + } else { + *p++ = b64table[input[i++] >> 2]; + *p++ = b64table[((input[i - 1] << 4) | (input[i] >> 4)) & 0x3f]; + if (mod == 1) { + *p++ = '='; + *p++ = '='; + + if (uWrap) { + *p++ = '\n'; + } + + *p = 0; + + sRet = output; + delete[] output; + return true; + } else { + *p++ = b64table[(input[i] << 2) & 0x3f]; + *p++ = '='; + + if (uWrap) { + *p++ = '\n'; + } + + *p = 0; + sRet = output; + delete[] output; + return true; + } + } + + sRet = output; + free(output); + return true; +} + unsigned long CString::Base64Decode(CString& sRet) const { const char* in = c_str(); char c, c1, *p; @@ -595,6 +680,18 @@ CString CString::TrimRight_n(const CString& s) const { return sRet; } +CString CString::LeftChomp_n(unsigned int uLen) { + CString sRet = *this; + sRet.LeftChomp(uLen); + return sRet; +} + +CString CString::RightChomp_n(unsigned int uLen) { + CString sRet = *this; + sRet.RightChomp(uLen); + return sRet; +} + bool CString::LeftChomp(unsigned int uLen) { bool bRet = false; diff --git a/String.h b/String.h index d68c0327..b4e4301f 100644 --- a/String.h +++ b/String.h @@ -77,6 +77,7 @@ public: static CString Format(const CString& sFormatStr, ...); unsigned long Base64Decode(CString& sRet) const; + bool Base64Encode(CString& sRet, unsigned int uWrap = 0) const; CString MD5() const; static CString ToString(char c); @@ -113,6 +114,8 @@ public: bool LeftChomp(unsigned int uLen = 1); bool RightChomp(unsigned int uLen = 1); + CString LeftChomp_n(unsigned int uLen = 1); + CString RightChomp_n(unsigned int uLen = 1); private: protected: