diff options
| author | David Lönnhager <david.l@mullvad.net> | 2020-01-09 14:34:49 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2020-01-09 15:50:40 +0100 |
| commit | 3be633c0aad43874705355c1fe6c8a4df92f5a8f (patch) | |
| tree | eb0d0283fb29e20c05c11edee70809d849cf39cb /windows/nsis-plugins/src/string/string.cpp | |
| parent | 882c4bcaa4163e91ed7a97ba86f30f1fdf1dc420 (diff) | |
| download | mullvadvpn-3be633c0aad43874705355c1fe6c8a4df92f5a8f.tar.xz mullvadvpn-3be633c0aad43874705355c1fe6c8a4df92f5a8f.zip | |
Add nsis plugin for additional string manipulation
Diffstat (limited to 'windows/nsis-plugins/src/string/string.cpp')
| -rw-r--r-- | windows/nsis-plugins/src/string/string.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/windows/nsis-plugins/src/string/string.cpp b/windows/nsis-plugins/src/string/string.cpp new file mode 100644 index 0000000000..6071e6303d --- /dev/null +++ b/windows/nsis-plugins/src/string/string.cpp @@ -0,0 +1,81 @@ +#include "stdafx.h" +#include <nsis/pluginapi.h> +#include <stdexcept> +#include <string> + +// Suppress warnings caused by broken legacy code +#pragma warning (push) +#pragma warning (disable: 4005) +#include <nsis/pluginapi.h> +#pragma warning (pop) + +namespace +{ + +std::wstring PopString() +{ + // + // NSIS functions popstring() and popstringn() require that you definitely size the buffer + // before popping the string. Let's do it ourselves instead. + // + + if (!g_stacktop || !*g_stacktop) + { + throw std::runtime_error("NSIS variable stack is corrupted"); + } + + stack_t *th = *g_stacktop; + + std::wstring copy(th->text); + + *g_stacktop = th->next; + GlobalFree((HGLOBAL)th); + + return copy; +} + +} // anonymous namespace + +// +// Find "string" "substring" begin_offset +// +// Return the position of "substring" in "string", starting from 'begin_offset'. +// If the substring is not found or if an error occurs, return -1. +// + +void __declspec(dllexport) NSISCALL Find +( + HWND hwndParent, + int string_size, + LPTSTR variables, + stack_t **stacktop, + extra_parameters *extra, + ... +) +{ + EXDLL_INIT(); + + try + { + const auto searchString = PopString(); + const auto substring = PopString(); + const auto offset = popint(); + const auto position = searchString.find(substring, offset); + + if (std::wstring::npos == position) + { + pushint(-1); + return; + } + + pushint((int)position); + } + catch (const std::exception &) + { + pushint(-1); + } + catch (...) + { + pushint(-1); + } +} |
