| 1 |
{ |
| 2 |
Double Commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
SevenZip archiver plugin |
| 5 |
|
| 6 |
Copyright (C) 2017-2023 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
Based on Far Manager arclite plugin |
| 9 |
|
| 10 |
Copyright © 2000 Far Group |
| 11 |
|
| 12 |
All rights reserved. |
| 13 |
|
| 14 |
Redistribution and use in source and binary forms, with or without |
| 15 |
modification, are permitted provided that the following conditions |
| 16 |
are met: |
| 17 |
1. Redistributions of source code must retain the above copyright |
| 18 |
notice, this list of conditions and the following disclaimer. |
| 19 |
2. Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
3. The name of the authors may not be used to endorse or promote products |
| 23 |
derived from this software without specific prior written permission. |
| 24 |
|
| 25 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 |
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
} |
| 36 |
|
| 37 |
unit SevenZipCodecs; |
| 38 |
|
| 39 |
{$mode delphi} |
| 40 |
|
| 41 |
interface |
| 42 |
|
| 43 |
uses |
| 44 |
Classes, SysUtils, SevenZip, fgl, ActiveX, Windows, JclCompression; |
| 45 |
|
| 46 |
const |
| 47 |
cmMaximum = PtrInt(Ord(High(TJclCompressionMethod))); |
| 48 |
|
| 49 |
type |
| 50 |
|
| 51 |
{ TLibraryInfo } |
| 52 |
|
| 53 |
TLibraryInfo = class |
| 54 |
public |
| 55 |
Handle: TLibHandle; |
| 56 |
CreateObject: TCreateObjectFunc; |
| 57 |
GetHandlerProperty2: TGetHandlerProperty2; |
| 58 |
GetHandlerProperty: TGetHandlerProperty; |
| 59 |
GetMethodProperty: TGetMethodProperty; |
| 60 |
GetNumberOfFormats: TGetNumberOfFormatsFunc; |
| 61 |
GetNumberOfMethods: TGetNumberOfMethodsFunc; |
| 62 |
SetLargePageMode: TSetLargePageMode; |
| 63 |
SetCodecs: function(compressCodecsInfo: ICompressCodecsInfo): HRESULT; stdcall; |
| 64 |
CreateDecoder: function(Index: Cardinal; IID: PGUID; out Decoder): HRESULT; stdcall; |
| 65 |
CreateEncoder: function(Index: Cardinal; IID: PGUID; out Coder): HRESULT; stdcall; |
| 66 |
end; |
| 67 |
|
| 68 |
{ TCodecInfo } |
| 69 |
|
| 70 |
TCodecInfo = class |
| 71 |
LibraryIndex: Integer; |
| 72 |
CodecIndex: Integer; |
| 73 |
EncoderIsAssigned: LongBool; |
| 74 |
DecoderIsAssigned: LongBool; |
| 75 |
Encoder: CLSID; |
| 76 |
Decoder: CLSID; |
| 77 |
ID: Cardinal; |
| 78 |
Name: UnicodeString; |
| 79 |
end; |
| 80 |
|
| 81 |
{ TCompressCodecsInfo } |
| 82 |
|
| 83 |
TCompressCodecsInfo = class(TInterfacedObject, ICompressCodecsInfo, IUnknown) |
| 84 |
private |
| 85 |
FCodecs: TFPGObjectList<TCodecInfo>; |
| 86 |
FLibraries: TFPGObjectList<TLibraryInfo>; |
| 87 |
public |
| 88 |
constructor Create(ACodecs: TFPGObjectList<TCodecInfo>; ALibraries: TFPGObjectList<TLibraryInfo>); |
| 89 |
public |
| 90 |
function GetNumberOfMethods(NumMethods: PCardinal): HRESULT; stdcall; |
| 91 |
function GetProperty(Index: Cardinal; PropID: TPropID; out Value: TPropVariant): HRESULT; stdcall; |
| 92 |
function CreateDecoder(Index: Cardinal; IID: PGUID; out Decoder): HRESULT; stdcall; |
| 93 |
function CreateEncoder(Index: Cardinal; IID: PGUID; out Coder): HRESULT; stdcall; |
| 94 |
end; |
| 95 |
|
| 96 |
procedure LoadLibraries; |
| 97 |
function GetCodecName(AMethod: Cardinal): WideString; |
| 98 |
|
| 99 |
var |
| 100 |
ACodecs: TFPGObjectList<TCodecInfo> = nil; |
| 101 |
|
| 102 |
implementation |
| 103 |
|
| 104 |
uses |
| 105 |
LazUTF8, FileUtil, SevenZipHlp; |
| 106 |
|
| 107 |
{ TCompressCodecsInfo } |
| 108 |
|
| 109 |
constructor TCompressCodecsInfo.Create(ACodecs: TFPGObjectList<TCodecInfo>; |
| 110 |
ALibraries: TFPGObjectList<TLibraryInfo>); |
| 111 |
begin |
| 112 |
FCodecs:= ACodecs; |
| 113 |
FLibraries:= ALibraries; |
| 114 |
end; |
| 115 |
|
| 116 |
function TCompressCodecsInfo.GetNumberOfMethods(NumMethods: PCardinal): HRESULT; stdcall; |
| 117 |
begin |
| 118 |
NumMethods^:= FCodecs.Count; |
| 119 |
Result:= S_OK; |
| 120 |
end; |
| 121 |
|
| 122 |
function TCompressCodecsInfo.GetProperty(Index: Cardinal; PropID: TPropID; out |
| 123 |
Value: TPropVariant): HRESULT; stdcall; |
| 124 |
var |
| 125 |
ACodecInfo: TCodecInfo; |
| 126 |
begin |
| 127 |
ACodecInfo:= FCodecs[Index]; |
| 128 |
if (PropID = kDecoderIsAssigned) then |
| 129 |
begin |
| 130 |
Value.vt:= VT_BOOL; |
| 131 |
Value.bool:= ACodecInfo.DecoderIsAssigned; |
| 132 |
Exit(S_OK); |
| 133 |
end |
| 134 |
else if (PropID = kEncoderIsAssigned) then |
| 135 |
begin |
| 136 |
Value.vt:= VT_BOOL; |
| 137 |
Value.bool:= ACodecInfo.EncoderIsAssigned; |
| 138 |
Exit(S_OK); |
| 139 |
end; |
| 140 |
Result:= FLibraries[ACodecInfo.LibraryIndex].GetMethodProperty(ACodecInfo.CodecIndex, PropID, Value); |
| 141 |
end; |
| 142 |
|
| 143 |
function TCompressCodecsInfo.CreateDecoder(Index: Cardinal; IID: PGUID; out |
| 144 |
Decoder): HRESULT; stdcall; |
| 145 |
var |
| 146 |
ACodecInfo: TCodecInfo; |
| 147 |
ALibraryInfo: TLibraryInfo; |
| 148 |
begin |
| 149 |
Result:= S_OK; |
| 150 |
ACodecInfo:= FCodecs[Index]; |
| 151 |
if (ACodecInfo.DecoderIsAssigned) then |
| 152 |
begin |
| 153 |
ALibraryInfo:= FLibraries[ACodecInfo.LibraryIndex]; |
| 154 |
if Assigned(ALibraryInfo.CreateDecoder) then |
| 155 |
Result:= ALibraryInfo.CreateDecoder(ACodecInfo.CodecIndex, IID, Decoder) |
| 156 |
else |
| 157 |
Result:= ALibraryInfo.CreateObject(@ACodecInfo.Decoder, IID, Decoder); |
| 158 |
end; |
| 159 |
end; |
| 160 |
|
| 161 |
function TCompressCodecsInfo.CreateEncoder(Index: Cardinal; IID: PGUID; out |
| 162 |
Coder): HRESULT; stdcall; |
| 163 |
var |
| 164 |
ACodecInfo: TCodecInfo; |
| 165 |
ALibraryInfo: TLibraryInfo; |
| 166 |
begin |
| 167 |
Result:= S_OK; |
| 168 |
ACodecInfo:= FCodecs[Index]; |
| 169 |
if (ACodecInfo.EncoderIsAssigned) then |
| 170 |
begin |
| 171 |
ALibraryInfo:= FLibraries[ACodecInfo.LibraryIndex]; |
| 172 |
if Assigned(ALibraryInfo.CreateEncoder) then |
| 173 |
Result:= ALibraryInfo.CreateEncoder(ACodecInfo.CodecIndex, IID, Coder) |
| 174 |
else |
| 175 |
Result:= ALibraryInfo.CreateObject(@ACodecInfo.Encoder, IID, Coder); |
| 176 |
end; |
| 177 |
end; |
| 178 |
|
| 179 |
function GetCoderInfo(GetMethodProperty: TGetMethodProperty; Index: UInt32; var AInfo: TCodecInfo): Boolean; |
| 180 |
var |
| 181 |
Value: TPropVariant; |
| 182 |
begin |
| 183 |
Value.vt:= VT_EMPTY; |
| 184 |
if (GetMethodProperty(Index, kDecoder, Value) <> S_OK) then |
| 185 |
Exit(False); |
| 186 |
if (Value.vt <> VT_EMPTY) then |
| 187 |
begin |
| 188 |
if (Value.vt <> VT_BSTR) then Exit(False); |
| 189 |
try |
| 190 |
if (SysStringByteLen(Value.bstrVal) < SizeOf(CLSID)) then |
| 191 |
begin |
| 192 |
Exit(False); |
| 193 |
end; |
| 194 |
AInfo.Decoder:= PGUID(Value.bstrVal)^; |
| 195 |
AInfo.DecoderIsAssigned:= True; |
| 196 |
finally |
| 197 |
VarStringClear(Value); |
| 198 |
end; |
| 199 |
end; |
| 200 |
|
| 201 |
if (GetMethodProperty(Index, kEncoder, Value) <> S_OK) then |
| 202 |
Exit(False); |
| 203 |
if (Value.vt <> VT_EMPTY) then |
| 204 |
begin |
| 205 |
if (Value.vt <> VT_BSTR) then Exit(False); |
| 206 |
try |
| 207 |
if (SysStringByteLen(Value.bstrVal) < SizeOf(CLSID)) then |
| 208 |
begin |
| 209 |
Exit(False); |
| 210 |
end; |
| 211 |
AInfo.Encoder:= PGUID(Value.bstrVal)^; |
| 212 |
AInfo.EncoderIsAssigned:= True; |
| 213 |
finally |
| 214 |
VarStringClear(Value); |
| 215 |
end; |
| 216 |
end; |
| 217 |
|
| 218 |
if (GetMethodProperty(Index, kID, Value) <> S_OK) then |
| 219 |
Exit(False); |
| 220 |
if (Value.vt <> VT_UI8) then |
| 221 |
Exit(False); |
| 222 |
AInfo.ID:= Value.uhVal.QuadPart; |
| 223 |
|
| 224 |
Value.vt:= VT_EMPTY; |
| 225 |
if (GetMethodProperty(Index, kName, Value) <> S_OK) then |
| 226 |
Exit(False); |
| 227 |
if (Value.vt = VT_BSTR) then |
| 228 |
try |
| 229 |
AInfo.Name:= BinaryToUnicode(Value.bstrVal); |
| 230 |
finally |
| 231 |
VarStringClear(Value); |
| 232 |
end; |
| 233 |
|
| 234 |
Result:= AInfo.DecoderIsAssigned or AInfo.EncoderIsAssigned; |
| 235 |
end; |
| 236 |
|
| 237 |
var |
| 238 |
ALibraries: TFPGObjectList<TLibraryInfo> = nil; |
| 239 |
|
| 240 |
procedure LoadCodecs; |
| 241 |
var |
| 242 |
Handle: TLibHandle; |
| 243 |
Index, J: Integer; |
| 244 |
AFiles: TStringList; |
| 245 |
ACodecCount: Integer; |
| 246 |
NumMethods: UInt32 = 1; |
| 247 |
ACodecInfo: TCodecInfo; |
| 248 |
ALibraryInfo: TLibraryInfo; |
| 249 |
ACompressInfo: ICompressCodecsInfo; |
| 250 |
begin |
| 251 |
AFiles:= FindAllFiles(ExtractFilePath(SevenzipLibraryName) + 'Codecs', '*.' + SharedSuffix); |
| 252 |
for Index:= 0 to AFiles.Count - 1 do |
| 253 |
begin |
| 254 |
Handle:= System.LoadLibrary(AFiles[Index]); |
| 255 |
if Handle <> 0 then |
| 256 |
begin |
| 257 |
ALibraryInfo:= TLibraryInfo.Create; |
| 258 |
|
| 259 |
ALibraryInfo.Handle:= Handle; |
| 260 |
ALibraryInfo.CreateObject:= GetProcAddress(Handle, 'CreateObject'); |
| 261 |
ALibraryInfo.CreateDecoder:= GetProcAddress(Handle, 'CreateDecoder'); |
| 262 |
ALibraryInfo.CreateEncoder:= GetProcAddress(Handle, 'CreateEncoder'); |
| 263 |
ALibraryInfo.GetNumberOfMethods:= GetProcAddress(Handle, 'GetNumberOfMethods'); |
| 264 |
ALibraryInfo.GetMethodProperty:= GetProcAddress(Handle, 'GetMethodProperty'); |
| 265 |
|
| 266 |
if (Assigned(ALibraryInfo.CreateObject) or Assigned(ALibraryInfo.CreateDecoder) or |
| 267 |
Assigned(ALibraryInfo.CreateEncoder)) and Assigned(ALibraryInfo.GetMethodProperty) then |
| 268 |
begin |
| 269 |
ACodecCount:= ACodecs.Count; |
| 270 |
if Assigned(ALibraryInfo.GetNumberOfMethods) then |
| 271 |
begin |
| 272 |
if ALibraryInfo.GetNumberOfMethods(@NumMethods) = S_OK then |
| 273 |
begin |
| 274 |
for J := 0 to Int32(NumMethods) - 1 do |
| 275 |
begin |
| 276 |
ACodecInfo:= TCodecInfo.Create; |
| 277 |
ACodecInfo.LibraryIndex:= ALibraries.Count; |
| 278 |
ACodecInfo.CodecIndex:= J; |
| 279 |
if (GetCoderInfo(ALibraryInfo.GetMethodProperty, J, ACodecInfo)) then |
| 280 |
ACodecs.Add(ACodecInfo) |
| 281 |
else |
| 282 |
ACodecInfo.Free; |
| 283 |
end; |
| 284 |
end; |
| 285 |
end; // GetNumberOfMethods |
| 286 |
if (ACodecCount < ACodecs.Count) then |
| 287 |
ALibraries.Add(ALibraryInfo) |
| 288 |
else begin |
| 289 |
ALibraryInfo.Free; |
| 290 |
FreeLibrary(Handle); |
| 291 |
end; |
| 292 |
end; |
| 293 |
end; |
| 294 |
end; |
| 295 |
AFiles.Free; |
| 296 |
if (ACodecs.Count > 0) then |
| 297 |
begin |
| 298 |
ACompressInfo:= TCompressCodecsInfo.Create(ACodecs, ALibraries); |
| 299 |
for Index:= 0 to ALibraries.Count - 1 do |
| 300 |
begin |
| 301 |
if Assigned(ALibraries[Index].SetCodecs) then |
| 302 |
ALibraries[Index].SetCodecs(ACompressInfo); |
| 303 |
end; |
| 304 |
end; |
| 305 |
end; |
| 306 |
|
| 307 |
procedure LoadLibraries; |
| 308 |
var |
| 309 |
ALibraryInfo: TLibraryInfo; |
| 310 |
begin |
| 311 |
ACodecs:= TFPGObjectList<TCodecInfo>.Create; |
| 312 |
ALibraries:= TFPGObjectList<TLibraryInfo>.Create; |
| 313 |
|
| 314 |
// Add default library |
| 315 |
ALibraryInfo:= TLibraryInfo.Create; |
| 316 |
ALibraryInfo.Handle:= SevenzipLibraryHandle; |
| 317 |
ALibraryInfo.CreateObject:= SevenZip.CreateObject; |
| 318 |
ALibraryInfo.GetHandlerProperty2:= SevenZip.GetHandlerProperty2; |
| 319 |
ALibraryInfo.GetHandlerProperty:= SevenZip.GetHandlerProperty; |
| 320 |
ALibraryInfo.GetMethodProperty:= SevenZip.GetMethodProperty; |
| 321 |
ALibraryInfo.GetNumberOfFormats:= SevenZip.GetNumberOfFormats; |
| 322 |
ALibraryInfo.GetNumberOfMethods:= SevenZip.GetNumberOfMethods; |
| 323 |
ALibraryInfo.SetLargePageMode:= SevenZip.SetLargePageMode; |
| 324 |
ALibraryInfo.SetCodecs:= GetProcAddress(SevenzipLibraryHandle, 'SetCodecs'); |
| 325 |
ALibraryInfo.CreateDecoder:= GetProcAddress(SevenzipLibraryHandle, 'CreateDecoder'); |
| 326 |
ALibraryInfo.CreateEncoder:= GetProcAddress(SevenzipLibraryHandle, 'CreateEncoder'); |
| 327 |
ALibraries.Add(ALibraryInfo); |
| 328 |
|
| 329 |
// Load external codecs |
| 330 |
LoadCodecs; |
| 331 |
end; |
| 332 |
|
| 333 |
function GetCodecName(AMethod: Cardinal): WideString; |
| 334 |
var |
| 335 |
Index: Integer; |
| 336 |
begin |
| 337 |
if Assigned(ACodecs) then begin |
| 338 |
for Index:= 0 to ACodecs.Count - 1 do |
| 339 |
begin |
| 340 |
if (ACodecs[Index].ID = AMethod) then |
| 341 |
Exit(ACodecs[Index].Name); |
| 342 |
end; |
| 343 |
end; |
| 344 |
Result:= EmptyWideStr; |
| 345 |
end; |
| 346 |
|
| 347 |
procedure Finish; |
| 348 |
var |
| 349 |
Index: Integer; |
| 350 |
begin |
| 351 |
if Assigned(ALibraries) then begin |
| 352 |
for Index:= 0 to ALibraries.Count - 1 do |
| 353 |
begin |
| 354 |
if Assigned(ALibraries[Index].SetCodecs) then |
| 355 |
ALibraries[Index].SetCodecs(nil); |
| 356 |
FreeLibrary(ALibraries[Index].Handle); |
| 357 |
end; |
| 358 |
end; |
| 359 |
end; |
| 360 |
|
| 361 |
finalization |
| 362 |
Finish; |
| 363 |
|
| 364 |
end. |
| 365 |
|