| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2025 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <boca/application/external/utilities.h> |
| 14 | |
| 15 | #include <boca/common/utilities.h> |
| 16 | |
| 17 | #include <windows.h> |
| 18 | |
| 19 | using namespace smooth::IO; |
| 20 | |
| 21 | namespace BoCA |
| 22 | { |
| 23 | namespace AS |
| 24 | { |
| 25 | static String GetComponentInstructions(ComponentSpecs *specs, const String &arguments, Bool useStderr) |
| 26 | { |
| 27 | /* Set up security attributes. |
| 28 | */ |
| 29 | SECURITY_ATTRIBUTES secAttr; |
| 30 | |
| 31 | ZeroMemory(&secAttr, sizeof(secAttr)); |
| 32 | |
| 33 | secAttr.nLength = sizeof(secAttr); |
| 34 | secAttr.bInheritHandle = True; |
| 35 | |
| 36 | HANDLE rPipe = NIL; |
| 37 | HANDLE wPipe = NIL; |
| 38 | |
| 39 | CreatePipe(&rPipe, &wPipe, &secAttr, 131072); |
| 40 | SetHandleInformation(rPipe, HANDLE_FLAG_INHERIT, 0); |
| 41 | |
| 42 | /* Start 3rd party command line executable. |
| 43 | */ |
| 44 | String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter()); |
| 45 | |
| 46 | STARTUPINFOA startupInfo; |
| 47 | |
| 48 | ZeroMemory(&startupInfo, sizeof(startupInfo)); |
| 49 | |
| 50 | startupInfo.cb = sizeof(startupInfo); |
| 51 | startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; |
| 52 | startupInfo.wShowWindow = SW_HIDE; |
| 53 | startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 54 | startupInfo.hStdOutput = useStderr ? NIL : wPipe; |
| 55 | startupInfo.hStdError = useStderr ? wPipe : NIL; |
| 56 | |
| 57 | PROCESS_INFORMATION processInfo; |
| 58 | |
| 59 | ZeroMemory(&processInfo, sizeof(processInfo)); |
| 60 | |
| 61 | CreateProcessA(NIL, String(command).Append(" ").Append(arguments), NIL, NIL, True, 0, NIL, NIL, &startupInfo, &processInfo); |
| 62 | |
| 63 | /* Close stdio pipe write handle. |
| 64 | */ |
| 65 | CloseHandle(wPipe); |
| 66 | |
| 67 | /* Check process handle. |
| 68 | */ |
| 69 | if (processInfo.hProcess == NIL) |
| 70 | { |
| 71 | CloseHandle(rPipe); |
| 72 | |
| 73 | return NIL; |
| 74 | } |
| 75 | |
| 76 | /* Read output into buffer. |
| 77 | */ |
| 78 | String output; |
| 79 | Buffer<char> buffer(4096); |
| 80 | |
| 81 | while (True) |
| 82 | { |
| 83 | DWORD bytesRead = 0; |
| 84 | |
| 85 | if (!ReadFile(rPipe, buffer, buffer.Size(), &bytesRead, NIL) || bytesRead == 0) break; |
| 86 | |
| 87 | output.Append((char *) buffer); |
| 88 | } |
| 89 | |
| 90 | CloseHandle(rPipe); |
| 91 | |
| 92 | TerminateProcess(processInfo.hProcess, 0); |
| 93 | |
| 94 | /* Wait until the program exits. |
| 95 | */ |
| 96 | while (WaitForSingleObject(processInfo.hProcess, 0) == WAIT_TIMEOUT) S::System::System::Sleep(10); |
| 97 | |
| 98 | /* Process and return output. |
| 99 | */ |
| 100 | output.Replace("\t", " "); |
| 101 | output.Replace("\r", ""); |
| 102 | output.Replace("\n", "\n "); |
| 103 | |
| 104 | return output; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Void BoCA::AS::ExternalUtilities::CheckParameterRequirements(ComponentSpecs *specs) |
| 110 | { |
| 111 | String instructions; |
| 112 | |
| 113 | for (Int i = specs->parameters.Length() - 1; i >= 0; i--) |
| 114 | { |
| 115 | Parameter *parameter = specs->parameters.GetNth(i); |
| 116 | const Array<ParameterRequirement> &requirements = parameter->GetRequirements(); |
| 117 | |
| 118 | foreach (const ParameterRequirement &requirement, requirements) |
| 119 | { |
| 120 | if (instructions == NIL) instructions = GetComponentInstructions(specs, requirement.arguments, requirement.useStderr); |
| 121 | |
| 122 | if (!instructions.Contains(String(" ").Append(requirement.option).Append(" "))) |
| 123 | { |
| 124 | specs->parameters.RemoveNth(i); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | String BoCA::AS::ExternalUtilities::GetMD5(const ComponentSpecs *specs, const String &encFileName) |
| 132 | { |
| 133 | if (specs->external_md5_arguments == NIL) return NIL; |
| 134 | |
| 135 | /* Set up security attributes. |
| 136 | */ |
| 137 | SECURITY_ATTRIBUTES secAttr; |
| 138 | |
| 139 | ZeroMemory(&secAttr, sizeof(secAttr)); |
| 140 | |
| 141 | secAttr.nLength = sizeof(secAttr); |
| 142 | secAttr.bInheritHandle = True; |
| 143 | |
| 144 | HANDLE rPipe = NIL; |
| 145 | HANDLE wPipe = NIL; |
| 146 | |
| 147 | CreatePipe(&rPipe, &wPipe, &secAttr, 131072); |
| 148 | SetHandleInformation(rPipe, HANDLE_FLAG_INHERIT, 0); |
| 149 | |
| 150 | /* Start 3rd party command line decoder. |
| 151 | */ |
| 152 | String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter()); |
| 153 | String arguments = String(specs->external_md5_arguments).Replace("%INFILE", String("\"").Append(encFileName).Append("\"")); |
| 154 | |
| 155 | if (specs->debug) AllocConsole(); |
| 156 | |
| 157 | STARTUPINFOA startupInfo; |
| 158 | |
| 159 | ZeroMemory(&startupInfo, sizeof(startupInfo)); |
| 160 | |
| 161 | startupInfo.cb = sizeof(startupInfo); |
| 162 | startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; |
| 163 | startupInfo.wShowWindow = specs->debug ? SW_SHOW : SW_HIDE; |
| 164 | startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 165 | startupInfo.hStdOutput = specs->external_md5_stderr ? (specs->debug ? GetStdHandle(STD_OUTPUT_HANDLE) : NIL) : wPipe; |
| 166 | startupInfo.hStdError = specs->external_md5_stderr ? wPipe : (specs->debug ? GetStdHandle(STD_ERROR_HANDLE) : NIL); |
| 167 | |
| 168 | PROCESS_INFORMATION processInfo; |
| 169 | |
| 170 | ZeroMemory(&processInfo, sizeof(processInfo)); |
| 171 | |
| 172 | CreateProcessA(NIL, String(command).Append(" ").Append(arguments), NIL, NIL, True, 0, NIL, NIL, &startupInfo, &processInfo); |
| 173 | |
| 174 | /* Close stdio pipe write handle. |
| 175 | */ |
| 176 | CloseHandle(wPipe); |
| 177 | |
| 178 | /* Check process handle. |
| 179 | */ |
| 180 | if (processInfo.hProcess == NIL) |
| 181 | { |
| 182 | CloseHandle(rPipe); |
| 183 | |
| 184 | return NIL; |
| 185 | } |
| 186 | |
| 187 | /* Read output into buffer. |
| 188 | */ |
| 189 | Buffer<char> buffer(4096); |
| 190 | Int bytesReadTotal = 0; |
| 191 | DWORD bytesRead = 0; |
| 192 | |
| 193 | do |
| 194 | { |
| 195 | if (!ReadFile(rPipe, buffer + bytesReadTotal, 4096 - bytesReadTotal, &bytesRead, NIL) || bytesRead == 0) break; |
| 196 | |
| 197 | bytesReadTotal += bytesRead; |
| 198 | } |
| 199 | while (bytesReadTotal < 4096); |
| 200 | |
| 201 | String output = (bytesReadTotal > 0 ? (char *) buffer : NIL); |
| 202 | |
| 203 | CloseHandle(rPipe); |
| 204 | |
| 205 | TerminateProcess(processInfo.hProcess, 0); |
| 206 | |
| 207 | /* Wait until the decoder exits. |
| 208 | */ |
| 209 | while (WaitForSingleObject(processInfo.hProcess, 0) == WAIT_TIMEOUT) S::System::System::Sleep(10); |
| 210 | |
| 211 | if (specs->debug) |
| 212 | { |
| 213 | BoCA::Utilities::InfoMessage("Click OK to close console window."); |
| 214 | |
| 215 | FreeConsole(); |
| 216 | } |
| 217 | |
| 218 | /* Check if anything went wrong. |
| 219 | */ |
| 220 | unsigned long exitCode = 0; |
| 221 | |
| 222 | GetExitCodeProcess(processInfo.hProcess, &exitCode); |
| 223 | |
| 224 | if (!specs->external_ignoreExitCode && exitCode != 0) return NIL; |
| 225 | |
| 226 | /* Extract MD5 from output. |
| 227 | */ |
| 228 | String md5; |
| 229 | |
| 230 | if (output.Contains(specs->external_md5_require) && |
| 231 | output.Contains(specs->external_md5_prefix)) md5 = output.SubString(output.Find(specs->external_md5_prefix) + specs->external_md5_prefix.Length(), |
| 232 | output.Length() - output.Find(specs->external_md5_prefix) - specs->external_md5_prefix.Length()).Trim().Head(32).ToLower(); |
| 233 | |
| 234 | if (md5.Length() != 32 || md5.Contains("\n") || md5.Contains(" ")) md5 = NIL; |
| 235 | |
| 236 | return md5; |
| 237 | } |