Skip to content

Commit

Permalink
ReaScript: add NF_Base64_Decode(), NF_Base64_Encode()
Browse files Browse the repository at this point in the history
closes reaper-oss#778

# delete unused NF_GetProjectStartupAction_CmdID()
  • Loading branch information
nofishonfriday committed Jan 10, 2023
1 parent fc099cf commit 3183e36
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ReaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ APIdef g_apidefs[] =
{ APIFUNC(NF_ClearProjectTrackSelectionAction), "bool", "", "", "Returns true if project track selection action was cleared successfully.", },

{ APIFUNC(NF_DeleteTakeFromItem), "bool", "MediaItem*,int", "item,takeIdx", "Deletes a take from an item. takeIdx is zero-based. Returns true on success.", },

// Base64
{ APIFUNC(NF_Base64_Decode), "bool","const char*,char*,int", "base64Str,decodedStrOutNeedBig,decodedStrOutNeedBig_sz", "Returns true on success.", },
{ APIFUNC(NF_Base64_Encode), "void","const char*,int,bool,char*,int", "str,str_sz,usePadding,encodedStrOutNeedBig,encodedStrOutNeedBig_sz", "Input string may contain null bytes in REAPER 6.44 or newer. Note: Doesn't allow padding in the middle (e.g. concatenated encoded strings), doesn't allow newlines.", },
// /*** nofish stuff ***

{ APIFUNC(SN_FocusMIDIEditor), "void", "", "", "Focuses the active/open MIDI editor.", },
Expand Down
10 changes: 5 additions & 5 deletions Utility/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ char* Base64::Encode(const char* pInput, int iInputLen, const bool pad)
int iEncodedLen;

//calculate encoded buffer size
if (pad)
iEncodedLen = 4 * ceil(iLen / 3.f);
else
iEncodedLen = ceil(4 * iLen / 3.f);
if (pad)
iEncodedLen = static_cast<int>(4 * ceil(iLen / 3.f));
else
iEncodedLen = static_cast<int>(ceil(4 * iLen / 3.f));

// allocate:
if (m_pEncodedBuf != NULL)
Expand Down Expand Up @@ -120,7 +120,7 @@ char* Base64::Decode(const char* pEncodedBuf, int *iOutLen)

// allocate buffer to hold the decoded string:
const int iEncodedLen = strlen(pEncodedBuf);
iDecodedLen = 3 * (iEncodedLen / 4.f);
iDecodedLen = static_cast<int>(3 * (iEncodedLen / 4.f));

// remove padding from decoded length
for(int i = iEncodedLen - 1; i >= 0 && pEncodedBuf[i] == '='; --i, --iDecodedLen);
Expand Down
1 change: 1 addition & 0 deletions Utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ PRIVATE
envelope.cpp
hidpi.cpp
RazorEditArea.cpp
ReaScript_Utility.cpp
)
46 changes: 46 additions & 0 deletions Utility/ReaScript_Utility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/******************************************************************************
/ ReaScript_Utilty.cpp
/
/ Copyright (c) 2022 ReaTeam
/
/ Permission is hereby granted, free of charge, to any person obtaining a copy
/ of this software and associated documentation files (the "Software"), to deal
/ in the Software without restriction, including without limitation the rights to
/ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
/ of the Software, and to permit persons to whom the Software is furnished to
/ do so, subject to the following conditions:
/
/ The above copyright notice and this permission notice shall be included in all
/ copies or substantial portions of the Software.
/
/ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
/ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
/ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
/ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
/ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
/ OTHER DEALINGS IN THE SOFTWARE.
/
******************************************************************************/

#include "stdafx.h"
#include <cstring>

#include "ReaScript_Utility.hpp"

// from https://github.com/cfillion/reaimgui/blob/b31d7fa6cf317167d363dc7882a193cc03f90762/api/input.cpp#L27-L39
void CopyToBuffer(const char* value, char* buf, const size_t bufSize)
{
int newSize{};
const size_t valuestrlen = strlen(value);
if (valuestrlen >= bufSize && realloc_cmd_ptr(&buf, &newSize, valuestrlen)) {
// the buffer is no longer null-terminated after using realloc_cmd_ptr!
std::memcpy(buf, value, newSize);
}
else {
const size_t limit{ std::min(bufSize - 1, valuestrlen) };
std::memcpy(buf, value, limit);
buf[limit] = '\0';
}
}
28 changes: 28 additions & 0 deletions Utility/ReaScript_Utility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/******************************************************************************
/ ReaScript_Utility.hpp
/
/ Copyright (c) 2022 ReaTeam
/
/ Permission is hereby granted, free of charge, to any person obtaining a copy
/ of this software and associated documentation files (the "Software"), to deal
/ in the Software without restriction, including without limitation the rights to
/ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
/ of the Software, and to permit persons to whom the Software is furnished to
/ do so, subject to the following conditions:
/
/ The above copyright notice and this permission notice shall be included in all
/ copies or substantial portions of the Software.
/
/ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
/ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
/ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
/ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
/ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
/ OTHER DEALINGS IN THE SOFTWARE.
/
******************************************************************************/
#pragma once

void CopyToBuffer(const char* value, char* buf, const size_t bufSize);
27 changes: 27 additions & 0 deletions nofish/NF_ReaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "../SnM/SnM_Notes.h" // #755
#include "../SnM/SnM_Project.h" // #974, GetProjectLoadAction(), GetGlobalStartupAction()
#include "../SnM/SnM_Util.h" // #974, SNM_NamedCommandLookup(), CheckSwsMacroScriptNumCustomId()
#include "../Utility/Base64.h"
#include "../Utility/ReaScript_Utility.hpp"

#include <taglib/fileref.h>

Expand Down Expand Up @@ -479,3 +481,28 @@ bool NF_DeleteTakeFromItem(MediaItem* item, int takeIdx)
SNM_TakeParserPatcher takePatcher(item);
return takePatcher.RemoveTake(takeIdx);
}

// Base64
bool NF_Base64_Decode(const char* base64_str, char* decodedStrOut, int)
{
Base64 b64; int decodedSize;
if (char* decoded = b64.Decode(base64_str, &decodedSize))
{
realloc_cmd_ptr(&decodedStrOut, &decodedSize, decodedSize); // allow null bytes in output
memcpy(decodedStrOut, decoded, decodedSize);
return true;
}
return false;
}

void NF_Base64_Encode(const char* str, int str_sz, const bool usePadding, char* encodedStrOut, const int encodedStrOut_sz)
{
static bool isStrSizeAccurate { atof(GetAppVersion()) >= 6.44 };
if (isStrSizeAccurate && str_sz > 0)
--str_sz; // ignore the null terminator
else
str_sz = strlen(str);
Base64 b64;
const char* encoded = b64.Encode(str, str_sz, usePadding);
CopyToBuffer(encoded, encodedStrOut, encodedStrOut_sz);
}
5 changes: 4 additions & 1 deletion nofish/NF_ReaScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ bool NF_SetGlobalStartupAction(const char* buf);
bool NF_ClearGlobalStartupAction();

bool NF_GetProjectStartupAction(char* descOut, int descOut_sz, char* cmdIdOut, int cmdIdOut_sz);
void NF_GetProjectStartupAction_CmdID(char *buf, int bufSize);
bool NF_SetProjectStartupAction(const char* buf);
bool NF_ClearProjectStartupAction();

Expand All @@ -70,3 +69,7 @@ bool NF_SetProjectTrackSelectionAction(const char* buf);
bool NF_ClearProjectTrackSelectionAction();

bool NF_DeleteTakeFromItem(MediaItem* item, int takeIdx);

// Base64
bool NF_Base64_Decode(const char* base64_str, char* decodedStrOut, int decodedStrOut_sz);
void NF_Base64_Encode(const char* str, int str_sz, bool usePadding, char* encodedStrOut, int encodedStrOut_sz);

0 comments on commit 3183e36

Please sign in to comment.