Skip to content

Commit

Permalink
Move external codec MD5 reading routines to separate source file.
Browse files Browse the repository at this point in the history
  • Loading branch information
enzo1982 committed Mar 22, 2024
1 parent e086894 commit 23bf68a
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 312 deletions.
30 changes: 30 additions & 0 deletions include/boca/application/external/utilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* BoCA - BonkEnc Component Architecture
* Copyright (C) 2007-2024 Robert Kausch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of
* the License, or (at your option) any later version.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */

#ifndef H_BOCA_EXTERNALUTILITIES
#define H_BOCA_EXTERNALUTILITIES

#include "../componentspecs.h"

namespace BoCA
{
namespace AS
{
class ExternalUtilities
{
public:
static String GetMD5(const ComponentSpecs *, const String &);
};
};
};

#endif
22 changes: 22 additions & 0 deletions msvc/BoCA/BoCA.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@
<ClCompile Include="..\..\runtime\application\dspcomponent.cpp" />
<ClCompile Include="..\..\runtime\application\encodercomponent.cpp" />
<ClCompile Include="..\..\runtime\application\extensioncomponent.cpp" />
<ClCompile Include="..\..\runtime\application\external\posix\utilities.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\runtime\application\external\win32\utilities.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(Filename)2.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(Filename)2.xdc</XMLDocumentationFileName>
</ClCompile>
<ClCompile Include="..\..\runtime\application\outputcomponent.cpp" />
<ClCompile Include="..\..\runtime\application\playlistcomponent.cpp" />
<ClCompile Include="..\..\runtime\application\registry.cpp" />
Expand Down Expand Up @@ -557,6 +578,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\boca.h" />
<ClInclude Include="..\..\include\boca\application\external\utilities.h" />
<ClInclude Include="..\..\include\boca\application\playlistcomponent.h" />
<ClInclude Include="..\..\include\boca\application\streamcomponent.h" />
<ClInclude Include="..\..\include\boca\application\verifiercomponent.h" />
Expand Down
9 changes: 9 additions & 0 deletions msvc/BoCA/BoCA.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@
<ClCompile Include="..\..\runtime\application\streamcomponent.cpp">
<Filter>Quelldateien\application</Filter>
</ClCompile>
<ClCompile Include="..\..\runtime\application\external\win32\utilities.cpp">
<Filter>Quelldateien\application\external\win32</Filter>
</ClCompile>
<ClCompile Include="..\..\runtime\application\external\posix\utilities.cpp">
<Filter>Quelldateien\application\external\posix</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\boca.h">
Expand Down Expand Up @@ -386,5 +392,8 @@
<ClInclude Include="..\..\include\boca\component\streamcomponent.h">
<Filter>Headerdateien\boca\component</Filter>
</ClInclude>
<ClInclude Include="..\..\include\boca\application\external\utilities.h">
<Filter>Headerdateien\boca\application\external</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ OBJECTS += component/component.o component/decodercomponent.o component/devicei
OBJECTS += core/core.o core/main.o

ifeq ($(BUILD_WIN32),True)
OBJECTS += application/external/win32/decodercomponentfile.o application/external/win32/decodercomponentstdio.o application/external/win32/encodercomponentfile.o application/external/win32/encodercomponentstdio.o
OBJECTS += application/external/win32/decodercomponentfile.o application/external/win32/decodercomponentstdio.o application/external/win32/encodercomponentfile.o application/external/win32/encodercomponentstdio.o application/external/win32/utilities.o
else ifeq ($(BUILD_UNIX),True)
OBJECTS += application/external/posix/decodercomponentfile.o application/external/posix/decodercomponentstdio.o application/external/posix/encodercomponentfile.o application/external/posix/encodercomponentstdio.o
OBJECTS += application/external/posix/decodercomponentfile.o application/external/posix/decodercomponentstdio.o application/external/posix/encodercomponentfile.o application/external/posix/encodercomponentstdio.o application/external/posix/utilities.o
endif

# Enter additional library dependencies here
Expand Down
53 changes: 2 additions & 51 deletions runtime/application/external/posix/decodercomponentfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */

#include <boca/application/external/decodercomponentfile.h>
#include <boca/application/external/utilities.h>
#include <boca/application/taggercomponent.h>
#include <boca/application/registry.h>
#include <boca/common/utilities.h>
Expand All @@ -37,57 +38,7 @@ BoCA::AS::DecoderComponentExternalFile::~DecoderComponentExternalFile()

String BoCA::AS::DecoderComponentExternalFile::GetMD5(const String &encFileName)
{
if (specs->external_md5_arguments == NIL) return NIL;

/* Start 3rd party command line decoder.
*/
String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter());
String arguments = String(specs->external_md5_arguments).Replace("%INFILE", String(encFileName).Replace("\\", "\\\\").Replace(" ", "\\ ")
.Replace("\"", "\\\"").Replace("\'", "\\\'").Replace("`", "\\`")
.Replace("(", "\\(").Replace(")", "\\)").Replace("<", "\\<").Replace(">", "\\>")
.Replace("&", "\\&").Replace(";", "\\;").Replace("$", "\\$").Replace("|", "\\|"));

FILE *rPipe = popen(String(command).Append(" ").Append(arguments).Append(specs->external_md5_stderr ? " 2>&1" : (specs->debug ? NIL : " 2> /dev/null")), "r");

/* Read output into buffer.
*/
Buffer<char> buffer(4096);
Int bytesReadTotal = 0;
Int bytesRead = 0;

do
{
bytesRead = fread(buffer + bytesReadTotal, 1, 4096 - bytesReadTotal, rPipe);

if (bytesRead != 4096 - bytesReadTotal && (ferror(rPipe) || bytesRead == 0)) break;

bytesReadTotal += bytesRead;
}
while (bytesReadTotal < 4096);

String output = (bytesReadTotal > 0 ? (char *) buffer : NIL);

/* Wait until the decoder exits.
*/
unsigned long exitStatus = pclose(rPipe);
unsigned long exitCode = WIFEXITED(exitStatus) ? WEXITSTATUS(exitStatus) : -1;
unsigned long exitSignal = WIFSIGNALED(exitStatus) ? WTERMSIG(exitStatus) : -1;

/* Check if anything went wrong.
*/
if (!specs->external_ignoreExitCode && exitCode != 0 && exitCode != 0x80 + SIGPIPE && exitSignal != SIGPIPE) return NIL;

/* Extract MD5 from output.
*/
String md5;

if (output.Contains(specs->external_md5_require) &&
output.Contains(specs->external_md5_prefix)) md5 = output.SubString(output.Find(specs->external_md5_prefix) + specs->external_md5_prefix.Length(),
output.Length() - output.Find(specs->external_md5_prefix) - specs->external_md5_prefix.Length()).Trim().Head(32).ToLower();

if (md5.Length() != 32 || md5.Contains("\n") || md5.Contains(" ")) md5 = NIL;

return md5;
return ExternalUtilities::GetMD5(specs, encFileName);
}

Error BoCA::AS::DecoderComponentExternalFile::GetStreamInfo(const String &streamURI, Track &track)
Expand Down
53 changes: 2 additions & 51 deletions runtime/application/external/posix/decodercomponentstdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */

#include <boca/application/external/decodercomponentstdio.h>
#include <boca/application/external/utilities.h>
#include <boca/application/taggercomponent.h>
#include <boca/application/registry.h>
#include <boca/common/utilities.h>
Expand Down Expand Up @@ -39,57 +40,7 @@ BoCA::AS::DecoderComponentExternalStdIO::~DecoderComponentExternalStdIO()

String BoCA::AS::DecoderComponentExternalStdIO::GetMD5(const String &encFileName)
{
if (specs->external_md5_arguments == NIL) return NIL;

/* Start 3rd party command line decoder.
*/
String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter());
String arguments = String(specs->external_md5_arguments).Replace("%INFILE", String(encFileName).Replace("\\", "\\\\").Replace(" ", "\\ ")
.Replace("\"", "\\\"").Replace("\'", "\\\'").Replace("`", "\\`")
.Replace("(", "\\(").Replace(")", "\\)").Replace("<", "\\<").Replace(">", "\\>")
.Replace("&", "\\&").Replace(";", "\\;").Replace("$", "\\$").Replace("|", "\\|"));

FILE *rPipe = popen(String(command).Append(" ").Append(arguments).Append(specs->external_md5_stderr ? " 2>&1" : (specs->debug ? NIL : " 2> /dev/null")), "r");

/* Read output into buffer.
*/
Buffer<char> buffer(4096);
Int bytesReadTotal = 0;
Int bytesRead = 0;

do
{
bytesRead = fread(buffer + bytesReadTotal, 1, 4096 - bytesReadTotal, rPipe);

if (bytesRead != 4096 - bytesReadTotal && (ferror(rPipe) || bytesRead == 0)) break;

bytesReadTotal += bytesRead;
}
while (bytesReadTotal < 4096);

String output = (bytesReadTotal > 0 ? (char *) buffer : NIL);

/* Wait until the decoder exits.
*/
unsigned long exitStatus = pclose(rPipe);
unsigned long exitCode = WIFEXITED(exitStatus) ? WEXITSTATUS(exitStatus) : -1;
unsigned long exitSignal = WIFSIGNALED(exitStatus) ? WTERMSIG(exitStatus) : -1;

/* Check if anything went wrong.
*/
if (!specs->external_ignoreExitCode && exitCode != 0 && exitCode != 0x80 + SIGPIPE && exitSignal != SIGPIPE) return NIL;

/* Extract MD5 from output.
*/
String md5;

if (output.Contains(specs->external_md5_require) &&
output.Contains(specs->external_md5_prefix)) md5 = output.SubString(output.Find(specs->external_md5_prefix) + specs->external_md5_prefix.Length(),
output.Length() - output.Find(specs->external_md5_prefix) - specs->external_md5_prefix.Length()).Trim().Head(32).ToLower();

if (md5.Length() != 32 || md5.Contains("\n") || md5.Contains(" ")) md5 = NIL;

return md5;
return ExternalUtilities::GetMD5(specs, encFileName);
}

Float BoCA::AS::DecoderComponentExternalStdIO::GetApproximateDuration(const String &encFileName)
Expand Down
75 changes: 75 additions & 0 deletions runtime/application/external/posix/utilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* BoCA - BonkEnc Component Architecture
* Copyright (C) 2007-2024 Robert Kausch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of
* the License, or (at your option) any later version.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */

#include <boca/application/external/utilities.h>

#include <boca/common/utilities.h>

#include <signal.h>
#include <sys/wait.h>

using namespace smooth::IO;

String BoCA::AS::ExternalUtilities::GetMD5(const ComponentSpecs *specs, const String &encFileName)
{
if (specs->external_md5_arguments == NIL) return NIL;

/* Start 3rd party command line decoder.
*/
String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter());
String arguments = String(specs->external_md5_arguments).Replace("%INFILE", String(encFileName).Replace("\\", "\\\\").Replace(" ", "\\ ")
.Replace("\"", "\\\"").Replace("\'", "\\\'").Replace("`", "\\`")
.Replace("(", "\\(").Replace(")", "\\)").Replace("<", "\\<").Replace(">", "\\>")
.Replace("&", "\\&").Replace(";", "\\;").Replace("$", "\\$").Replace("|", "\\|"));

FILE *rPipe = popen(String(command).Append(" ").Append(arguments).Append(specs->external_md5_stderr ? " 2>&1" : (specs->debug ? NIL : " 2> /dev/null")), "r");

/* Read output into buffer.
*/
Buffer<char> buffer(4096);
Int bytesReadTotal = 0;
Int bytesRead = 0;

do
{
bytesRead = fread(buffer + bytesReadTotal, 1, 4096 - bytesReadTotal, rPipe);

if (bytesRead != 4096 - bytesReadTotal && (ferror(rPipe) || bytesRead == 0)) break;

bytesReadTotal += bytesRead;
}
while (bytesReadTotal < 4096);

String output = (bytesReadTotal > 0 ? (char *) buffer : NIL);

/* Wait until the decoder exits.
*/
unsigned long exitStatus = pclose(rPipe);
unsigned long exitCode = WIFEXITED(exitStatus) ? WEXITSTATUS(exitStatus) : -1;
unsigned long exitSignal = WIFSIGNALED(exitStatus) ? WTERMSIG(exitStatus) : -1;

/* Check if anything went wrong.
*/
if (!specs->external_ignoreExitCode && exitCode != 0 && exitCode != 0x80 + SIGPIPE && exitSignal != SIGPIPE) return NIL;

/* Extract MD5 from output.
*/
String md5;

if (output.Contains(specs->external_md5_require) &&
output.Contains(specs->external_md5_prefix)) md5 = output.SubString(output.Find(specs->external_md5_prefix) + specs->external_md5_prefix.Length(),
output.Length() - output.Find(specs->external_md5_prefix) - specs->external_md5_prefix.Length()).Trim().Head(32).ToLower();

if (md5.Length() != 32 || md5.Contains("\n") || md5.Contains(" ")) md5 = NIL;

return md5;
}
Loading

0 comments on commit 23bf68a

Please sign in to comment.