Skip to content

Commit

Permalink
Add URL per line encoding ability
Browse files Browse the repository at this point in the history
Fix #26, close #27
  • Loading branch information
donho committed Dec 20, 2023
1 parent 430417b commit 8c722b1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 41 deletions.
5 changes: 1 addition & 4 deletions src/b64.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.


#ifndef NPP_B64_H
#define NPP_B64_H
#pragma once

#include <windows.h>

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
55 changes: 38 additions & 17 deletions src/mimeTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/mimeTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
// Copyright 2019 by Paul Nankervis <[email protected]>


#ifndef NPP_TOOLS_H
#define NPP_TOOLS_H
#pragma once

#define VERSION_VALUE "2.9\0"
#define VERSION_DIGITALVALUE 2, 9, 0, 0
Expand All @@ -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
5 changes: 1 addition & 4 deletions src/qp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.


#ifndef NPP_QP_H
#define NPP_QP_H
#pragma once

#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -89,5 +88,3 @@ class QuotedPrintable {
};

};

#endif //NPP_QP_H
4 changes: 1 addition & 3 deletions src/saml.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#ifndef NPP_SAML_H
#define NPP_SAML_H
#pragma once

#include "b64.h"
#include "url.h"
Expand All @@ -29,4 +28,3 @@ constexpr int SAML_MESSAGE_MAX_SIZE = 200000;

int samlDecode(char *dest, const char *samlStr, int bufLength);

#endif //NPP_SAML_H
8 changes: 4 additions & 4 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)];
Expand Down
8 changes: 3 additions & 5 deletions src/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.


#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

0 comments on commit 8c722b1

Please sign in to comment.