From 8c722b1945e05ff8f14dc6f54df743b1d5a6ae0f Mon Sep 17 00:00:00 2001 From: Don Ho Date: Thu, 21 Dec 2023 00:53:23 +0100 Subject: [PATCH] Add URL per line encoding ability Fix #26, close #27 --- src/b64.h | 5 +---- src/mimeTools.cpp | 55 ++++++++++++++++++++++++++++++++--------------- src/mimeTools.h | 9 ++++---- src/qp.h | 5 +---- src/saml.h | 4 +--- src/url.cpp | 8 +++---- src/url.h | 8 +++---- 7 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/b64.h b/src/b64.h index 2221e64..f6a45a9 100644 --- a/src/b64.h +++ b/src/b64.h @@ -15,12 +15,9 @@ // along with this program. If not, see . -#ifndef NPP_B64_H -#define NPP_B64_H +#pragma once #include int base64Encode(char *resultString, const char *asciiString, size_t asciiStringLength, size_t wrapLength, bool padFlag, bool byLineFlag); int base64Decode(char *resultString, const char *encodedString, size_t encodedStringLength, bool strictFlag, bool whitespaceReset); - -#endif //NPP_B64_H \ No newline at end of file diff --git a/src/mimeTools.cpp b/src/mimeTools.cpp index d05e7e1..1a33c84 100644 --- a/src/mimeTools.cpp +++ b/src/mimeTools.cpp @@ -26,7 +26,7 @@ const TCHAR PLUGIN_NAME[] = TEXT("MIME Tools"); -const int nbFunc = 19; +const int nbFunc = 22; HINSTANCE g_hInst = nullptr;; NppData nppData; @@ -54,15 +54,18 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD reasonForCall, LPVOID /*lpReserved*/ funcItem[10]._pFunc = NULL; funcItem[11]._pFunc = convertURLMinEncode; - funcItem[12]._pFunc = convertURLEncodeExtended; - funcItem[13]._pFunc = convertURLFullEncode; - funcItem[14]._pFunc = convertURLDecode; + funcItem[12]._pFunc = convertURLMinEncodeByLine; + funcItem[13]._pFunc = convertURLEncodeExtended; + funcItem[14]._pFunc = convertURLEncodeExtendedByLine; + funcItem[15]._pFunc = convertURLFullEncode; + funcItem[16]._pFunc = convertURLFullEncodeByLine; + funcItem[17]._pFunc = convertURLDecode; - funcItem[15]._pFunc = NULL; - funcItem[16]._pFunc = convertSamlDecode; + funcItem[18]._pFunc = NULL; + funcItem[19]._pFunc = convertSamlDecode; - funcItem[17]._pFunc = NULL; - funcItem[18]._pFunc = about; + funcItem[20]._pFunc = NULL; + funcItem[21]._pFunc = about; lstrcpy(funcItem[0]._itemName, TEXT("Base64 Encode")); lstrcpy(funcItem[1]._itemName, TEXT("Base64 Encode with padding")); @@ -80,17 +83,20 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD reasonForCall, LPVOID /*lpReserved*/ lstrcpy(funcItem[10]._itemName, TEXT("-SEPARATOR-")); lstrcpy(funcItem[11]._itemName, TEXT("URL Encode (RFC1738)")); - lstrcpy(funcItem[12]._itemName, TEXT("URL Encode (Extended)")); - lstrcpy(funcItem[13]._itemName, TEXT("Full URL Encode")); - lstrcpy(funcItem[14]._itemName, TEXT("URL Decode")); + lstrcpy(funcItem[12]._itemName, TEXT("URL Encode (RFC1738) by line")); + lstrcpy(funcItem[13]._itemName, TEXT("URL Encode (Extended)")); + lstrcpy(funcItem[14]._itemName, TEXT("URL Encode (Extended) by line")); + lstrcpy(funcItem[15]._itemName, TEXT("URL Encode (Full)")); + lstrcpy(funcItem[16]._itemName, TEXT("URL Encode (Full) by line")); + lstrcpy(funcItem[17]._itemName, TEXT("URL Decode")); - lstrcpy(funcItem[15]._itemName, TEXT("-SEPARATOR-")); + lstrcpy(funcItem[18]._itemName, TEXT("-SEPARATOR-")); - lstrcpy(funcItem[16]._itemName, TEXT("SAML Decode")); + lstrcpy(funcItem[19]._itemName, TEXT("SAML Decode")); - lstrcpy(funcItem[17]._itemName, TEXT("-SEPARATOR-")); + lstrcpy(funcItem[20]._itemName, TEXT("-SEPARATOR-")); - lstrcpy(funcItem[18]._itemName, TEXT("About")); + lstrcpy(funcItem[21]._itemName, TEXT("About")); funcItem[0]._init2Check = false; funcItem[1]._init2Check = false; @@ -313,7 +319,22 @@ void convertURLFullEncode() convertURLEncode (UrlEncodeMethod::full); } -void convertURLEncode (UrlEncodeMethod method) +void convertURLMinEncodeByLine() +{ + convertURLEncode (UrlEncodeMethod::RFC1738, true); +} + +void convertURLEncodeExtendedByLine() +{ + convertURLEncode (UrlEncodeMethod::extended, true); +} + +void convertURLFullEncodeByLine() +{ + convertURLEncode (UrlEncodeMethod::full, true); +} + +void convertURLEncode (UrlEncodeMethod method, bool isByLine) { HWND hCurrScintilla = getCurrentScintillaHandle(); size_t selBufLen = ::SendMessage(hCurrScintilla, SCI_GETSELTEXT, 0, 0); @@ -327,7 +348,7 @@ void convertURLEncode (UrlEncodeMethod method) size_t destBufLen = strlen(selectedText) * 3 + 1; char* pEncodedText = new char[destBufLen]; - int len = AsciiToUrl(pEncodedText, selectedText, int(destBufLen), method); + int len = AsciiToUrl(pEncodedText, selectedText, int(destBufLen), method, isByLine); size_t start = ::SendMessage(hCurrScintilla, SCI_GETSELECTIONSTART, 0, 0); size_t end = ::SendMessage(hCurrScintilla, SCI_GETSELECTIONEND, 0, 0); diff --git a/src/mimeTools.h b/src/mimeTools.h index a893460..37556c1 100644 --- a/src/mimeTools.h +++ b/src/mimeTools.h @@ -18,8 +18,7 @@ // Copyright 2019 by Paul Nankervis -#ifndef NPP_TOOLS_H -#define NPP_TOOLS_H +#pragma once #define VERSION_VALUE "2.9\0" #define VERSION_DIGITALVALUE 2, 9, 0, 0 @@ -44,10 +43,12 @@ void convertToAsciiFromQuotedPrintable(); void convertURLMinEncode(); void convertURLEncodeExtended(); void convertURLFullEncode(); -void convertURLEncode(UrlEncodeMethod method); +void convertURLMinEncodeByLine(); +void convertURLEncodeExtendedByLine(); +void convertURLFullEncodeByLine(); +void convertURLEncode(UrlEncodeMethod method, bool isByLine = false); void convertURLDecode(); void convertSamlDecode(); void convertURLDecode(); void about(); -#endif //NPP_TOOLS_H diff --git a/src/qp.h b/src/qp.h index 8221a57..ba3385c 100644 --- a/src/qp.h +++ b/src/qp.h @@ -15,8 +15,7 @@ // along with this program. If not, see . -#ifndef NPP_QP_H -#define NPP_QP_H +#pragma once #include #include @@ -89,5 +88,3 @@ class QuotedPrintable { }; }; - -#endif //NPP_QP_H diff --git a/src/saml.h b/src/saml.h index 8411356..2b76ec7 100644 --- a/src/saml.h +++ b/src/saml.h @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef NPP_SAML_H -#define NPP_SAML_H +#pragma once #include "b64.h" #include "url.h" @@ -29,4 +28,3 @@ constexpr int SAML_MESSAGE_MAX_SIZE = 200000; int samlDecode(char *dest, const char *samlStr, int bufLength); -#endif //NPP_SAML_H diff --git a/src/url.cpp b/src/url.cpp index 6eea893..4fec11a 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -81,7 +81,7 @@ static const char gExtendedChar[] = "!*'()+$,"; static const char gHexChar[] = "0123456789ABCDEF"; -int AsciiToUrl (char* dest, const char* src, int destSize, UrlEncodeMethod method) +int AsciiToUrl(char* dest, const char* src, int destSize, UrlEncodeMethod method, bool isByLine) { int i; memset (dest, 0, destSize); @@ -92,9 +92,9 @@ int AsciiToUrl (char* dest, const char* src, int destSize, UrlEncodeMethod metho for (i = 0; (i < (destSize - 2)) && *src; ++i, ++src) { - // Encode source if it is a reserved or non-printable character. - // - if (method == UrlEncodeMethod::full || (strchr (reservedAscii.c_str(), *src) != 0) || !isprint(*src)) + if ((isByLine ? (strchr("\r\n", *src) != nullptr ? false : true) : true) && // if "by line" is demanded and current char is EOL, the false is returned to stop remain tests and EOL is not treated. Otherwise (true) we keep testing... + (method == UrlEncodeMethod::full || // if encode method is full, true is return to stop remain tests, then we convert the char whatever it is. Otherwise (true) we keep testing... + ((strchr(reservedAscii.c_str(), *src) != nullptr) || !isprint(*src)))) // Here we convert only reserved characters or non-printable characters. { *dest++ = '%'; *dest++ = gHexChar [((*src >> 4) & 0x0f)]; diff --git a/src/url.h b/src/url.h index 3058a8d..b8004c6 100644 --- a/src/url.h +++ b/src/url.h @@ -15,12 +15,10 @@ // along with this program. If not, see . -#ifndef NPP_URL_H -#define NPP_URL_H +#pragma once enum UrlEncodeMethod { RFC1738, extended, full }; -int AsciiToUrl (char* dest, const char* src, int destSize, UrlEncodeMethod method); -int UrlToAscii (char* dest, const char* src, int destSize); +int AsciiToUrl(char* dest, const char* src, int destSize, UrlEncodeMethod method, bool isByLine = false); +int UrlToAscii(char* dest, const char* src, int destSize); -#endif //NPP_URL_H