This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
Program JPGStrip;
{$APPTYPE CONSOLE}
{
ToDo:
- skip duplicate FF blocks (for damaged files)
- add feature to show all FF blocks in JPEG file (don't strip anything)
- add feature to manual add FF block to keep (/KEEE5 - keep EE and E5 blocks)
- show error if no files given
Version: 0.27 -> 0.28 (2014.10.31)
-=CHE@TER=- * Add simply Unicode filenames workaround (won't work if 8.3 names disabled in system)
Version: 0.26 -> 0.27 (2011.02.11)
---------------------
-=CHE@TER=- * Fix datetime restore for read-only files (FILE_WRITE_ATTRIBUTES instead of GENERIC_WRITE)
-=CHE@TER=- * Move few routines from HELPERS.INC to .DPR file for allow using with never version of this file
-=CHE@TER=- * Use DCC32HACK compiler - smaller size (about 512 bytes) than previous one
-=CHE@TER=- * Drop 'BETA' from version number
Version: 0.25 -> 0.26 (2008.11.22)
---------------------
Grom PE * -=CHE@TER=-: Added CMYK and arithmetic coded images support.
Version: 0.24 -> 0.25 (2008.03.27)
---------------------
-=CHE@TER=- * Filename doesn't output if file is already optimized
-=CHE@TER=- * Don't write to disk anything if nothing is stripped - a bit faster work
-=CHE@TER=- * Better support for .BAK: they won't created if nothing is stripped
-=CHE@TER=- * "File Access/Modification/Create Time" always restore for all files
(only if BTNoDatetime not specified), because file always readed for
JPEG signature which dropped "File Access Time" property
Version: 0.23 -> 0.24 (2008.01.21)
---------------------
Grom PE * New fancy make.bat
Grom PE * Smaller jpgstrip.ico
Grom PE * Removed double import of WriteFile (Write2 function)
Grom PE * Changed year to 2008
Version: 0.22 -> 0.23 (2008.01.21)
---------------------
-=CHE@TER=- * Create filelist before handling
-=CHE@TER=- * Fixed problem with two '\' characters in path
Version: 0.21 -> 0.22 (2007.12.01)
---------------------
-=CHE@TER=- * jTommy: Fixed bug with incorrect section size
-=CHE@TER=- * jTommy: Fixed filename output (CharToOEM)
-=CHE@TER=- * Replaced copyright Grom PE / -=CHE@TER=- to CTPAX-X Team
Version: 0.2 -> 0.21 (2007.10.24)
--------------------
Grom PE * Fixed incorrect file size counting
Grom PE * Tabbed output, looks better
Grom PE * Changed 'B' to 'bytes', added thousands separation
Version: 0.11 -> 0.2 (2007.10.24)
--------------------
-=CHE@TER=- * Added /R commandline option
-=CHE@TER=- * Added /J commandline option
-=CHE@TER=- * Fix problem with FFD9
-=CHE@TER=- * CR/LF output fixed
Version: 0.1 -> 0.11 (2007.10.14)
--------------------
Grom PE * Fixed some typos
Grom PE * Replaced SysUtils' functions by helpers.inc
Grom PE * Changed Write to Write2, used Int2Str, Int2StrSpaces from helpers.inc
Grom PE * Changed Delphi's file handling to WinAPI
Grom PE * Changed some Integers to Cardinals
Grom PE * Changed Internal dynamic array to simple memory dynamic array
Grom PE * Replaced lstrcmpi by helpers.inc's LowerCase
Version 0.1 (2007.10.11)
-----------
-=CHE@TER=- * Initial release
}
{$IFNDEF DCC32HACK}
Uses SysUtils, Windows;
{$ELSE}
{$I HELPERS2.INC}
{$ENDIF}
{$R JPGStrip.res}
Const
JPGSTRIP_VERSION = 'JPGStrip v0.28';
Type
PByteArray = ^TByteArray;
TByteArray = array [0..0] of Byte;
TList = ^TFlInfo;
TFlInfo = Record
Name: PChar;
CT, AT, WT: TFileTime;
Next: TList;
End;
{* 0.25 *}
TJPGSect = Record
SOffs, SSize: Cardinal;
End;
Var
BTRecursive: Boolean = False;
BTBackupFiles: Boolean = False;
BTNoDatetime: Boolean = False;
BTSilentMode: Boolean = False;
BTNoReadOnly: Boolean = False;
BTAllowJFIF: Boolean = False;
JSTotalFiles: Cardinal = 0;
JSTotalSize: Cardinal = 0;
JSShrunkFiles: Cardinal = 0;
JSSavedSize: Cardinal = 0;
JSErrorsCount: Cardinal = 0;
FlHead: TList = Nil;
FlCur: TList = Nil;
function CutFileExt(const FileName:string):string;
var DotPosition:integer;
begin
DotPosition:=Length(FileName);
while ((DotPosition > 0) and (FileName[DotPosition] <> '.')) do DotPosition:=DotPosition - 1;
if DotPosition<2 then Result:=FileName
else Result:=Copy(FileName,1,DotPosition-1);
end;
// <!-- 0.27
function Int2StrSpaces(Value,Spaces:Integer):String;
begin
Str(Value, Result);
while Length(Result)<Spaces do Result:=' '+Result;
end;
function PadWithSpaces(Value:String; Spaces:Integer):String;
begin
Result:=Value;
while Length(Result)<Spaces do Result:=' '+Result;
end;
// only for positive values!
function Int2Str3(Value:cardinal):string;
var Buf:array[0..13] of char;
Dst:pchar;
D:dword;
begin
Dst:=@Buf[13];
Dst^:=#0;
D:=Value;
repeat
Dec(Dst);
Dst^:=Char((D mod 10)+Byte('0'));
D:=D div 10;
if D=0 then break;
Dec(Dst);
Dst^:=Char((D mod 10)+Byte('0'));
D:=D div 10;
if D=0 then break;
Dec(Dst);
Dst^:=Char((D mod 10)+Byte('0'));
D:=D div 10;
Dec(Dst);
Dst^:=',';
until D=0;
if Dst^=',' then Inc(Dst);
Result:=Dst;
end;
// 0.27 -->
// v0.28
Function GetANSIName(TF: WIN32_FIND_DATA): String;
Begin
If Pos('?', TF.cFileName) <> 0 Then
result:=TF.cAlternateFileName
Else
result:=TF.cFileName;
End;
Procedure AddFileToList(FlDir: String; FlData: WIN32_FIND_DATA);
Var FlNow: TList;
Begin
New(FlNow);
FlNow^.Next:=Nil;
FlNow^.CT:=FlData.ftCreationTime;
FlNow^.AT:=FlData.ftLastAccessTime;
FlNow^.WT:=FlData.ftLastWriteTime;
FlDir:=FlDir + GetANSIName(FlData) + #0; // v0.28
GetMem(FlNow^.Name, Length(FlDir));
Move(FlDir[1], FlNow^.Name^, Length(FlDir));
If FlHead = Nil Then
Begin
FlHead:=FlNow;
FlCur:=FlNow;
End
Else
Begin
FlCur^.Next:=FlNow;
FlCur:=FlNow;
End;
End;
Procedure DestroyList;
Begin
While FlHead<>Nil Do
Begin
FlCur:=FlHead;
FlHead:=FlCur^.Next;
FreeMem(FlCur^.Name, Length(FlCur^.Name)+1);
Dispose(FlCur);
End;
End;
Function HandleParams: Boolean;
Var I: Integer;
Begin
result:=True;
For I:=2 To ParamCount Do
Begin
result:=False;
If LowerCase(ParamStr(I))='/s' Then
Begin
BTRecursive:=True;
result:=True;
End;
If LowerCase(ParamStr(I))='/b' Then
Begin
BTBackupFiles:=True;
result:=True;
End;
If LowerCase(ParamStr(I))='/r' Then
Begin
BTNoReadOnly:=True;
result:=True;
End;
If LowerCase(ParamStr(I))='/j' Then
Begin
BTAllowJFIF:=True;
result:=True;
End;
If LowerCase(ParamStr(I))='/t' Then
Begin
BTNoDatetime:=True;
result:=True;
End;
If LowerCase(ParamStr(I))='/q' Then
Begin
BTSilentMode:=True;
result:=True;
End;
If result = False Then
Begin
WriteLn('Unknown switch: '+ParamStr(I));
Break;
End;
End;
End;
// swap bytes in a word
Function swap16(Const W: Word): Word; Assembler;
Asm
xchg ah, al
End;
Function JPEGStripFile(FName: String): Boolean;
Const
// JPEGAllowBlocks = #$DB#$C0#$C1#$C2#$C3#$C4#$DD{#$EE};
JPEGAllowBlocks = #$DB#$C0#$C1#$C2#$C3#$C4#$C9#$CA#$CB#$CC#$DD; {* 0.26 *}
{
255 237 ED - X
255 254 FE - X
255 238 EE - !
255 218 DA - X
}
Var
JPGOldSize, I, JP, FAttr, BC: Cardinal;
JS, SCnt: LongInt;
W, Sz: Word;
Fl: THandle;
P: PByteArray;
FO: String;
Sect: Array[0..99] Of TJPGSect; {* 0.25 *}
Begin
FO:=FName;
CharToOem(@FO[1], @FO[1]);
result:=False;
FAttr:=GetFileAttributes(PChar(FName));
If ((BTNoReadOnly=True) And ((FAttr And FILE_ATTRIBUTE_READONLY)<>0)) Then
Begin
If Not BTSilentMode Then
WriteLn(FO+':'#9'SKIPPED: read-only file.');
Exit;
End;
Fl:=CreateFile(PChar(FName), GENERIC_READ, 0, nil, OPEN_ALWAYS, 0, 0);
If Fl=INVALID_HANDLE_VALUE Then
Begin
If Not BTSilentMode Then
WriteLn(FO+':'#9'ERROR: can''t open file!');
JSErrorsCount:=JSErrorsCount+1;
Exit;
End;
ReadFile(Fl, JS, 4, BC, nil);
{ filter: only JPEG files FFD8FF?? }
If ((BC = 4) And ((JS And $00FFFFFF) = $00FFD8FF)) Then
Begin
JPGOldSize:=GetFileSize(Fl, nil);
SetFilePointer(Fl, 0, nil, FILE_BEGIN);
GetMem(P, JPGOldSize);
ReadFile(Fl, P[0], JPGOldSize, BC, nil);
{* 0.25 *}
Sect[0].SOffs:=0;
Sect[0].SSize:=2;
SCnt:=1;
I:=0;
Sz:=0;
Repeat
Inc(I, swap16(Sz) + 2);
Repeat
Move(P[I], W, 2);
Move(P[I+2], Sz, 2);
If Lo(W)<>$FF Then I:=I+1;
Until ((Lo(W) = $FF) Or (I>=JPGOldSize));
If ((Pos(Char(Hi(W)), JPEGAllowBlocks)<>0) Or ((BTAllowJFIF=True) And (Hi(W)=$E0))
Or ((Hi(W) = $EE) And (P[I+15] <> 1) And
(CompareMem(@P[I+4], PChar('Adobe'), 5) = True)
// (CompareString(LOCALE_SYSTEM_DEFAULT, SORT_STRINGSORT, PChar(@P[I+4]), 6, PChar('Adobe'), 6) = 2)
)) Then
Begin
{* 0.26 *}
{* 0.25 *}
Sect[SCnt].SOffs:=I;
If Hi(W) = $EE Then
Begin
P[I+3]:=14;
Sect[SCnt].SSize:=16;
End
Else
Sect[SCnt].SSize:=swap16(Sz) + 2;
SCnt:=SCnt + 1;
// WriteLn(Int2Str(Lo(W))+#32+Int2Str(Hi(W)){+#33+IntToStr(FilePos(Fl)-2)+'/'+IntToStr(FileSize(Fl))});
End;
Until ((I>=JPGOldSize) Or (Hi(W) = $DA));
If Hi(W) = $DA Then { Search for end of file - $F9 }
Begin
JP:=I;
I:=I + swap16(Sz) + 2; { +12 }
Repeat
I:=I+1;
Until ((I>=JPGOldSize-1) Or ((P[I] = $FF) And (P[I+1] = $D9)));
I:=I+2;
If I>JPGOldSize Then I:=JPGOldSize;
{* 0.25 *}
Sect[SCnt].SOffs:=JP;
Sect[SCnt].SSize:=I-JP;
SCnt:=SCnt + 1;
End
Else
Begin
Sect[SCnt].SOffs:=I;
Sect[SCnt].SSize:=JPGOldSize-I;
SCnt:=SCnt + 1;
End;
{* 0.25 *}
JP:=0;
For I:=0 To SCnt-1 Do
JP:=JP+Sect[I].SSize;
If JP<JPGOldSize Then
Begin
CloseHandle(Fl);
SetFileAttributes(PChar(FName), 0);
If BTBackupFiles Then
MoveFile(PChar(FName), PChar(CutFileExt(FName)+'.bak'));
Fl:=CreateFile(PChar(FName), GENERIC_WRITE, 0, nil, CREATE_ALWAYS, 0, 0);
For I:=0 To SCnt-1 Do
WriteFile(Fl, P[Sect[I].SOffs], Sect[I].SSize, BC, nil);
JP:=JPGOldSize-JP;
Inc(JSSavedSize, JP);
If Not BTSilentMode Then
WriteLn(FO+':'#9'saved '+PadWithSpaces(Int2Str3(JP), 8)+' bytes');
JSShrunkFiles:=JSShrunkFiles+1;
result:=True;
End;
FreeMem(P);
JSTotalFiles:=JSTotalFiles+1;
JSTotalSize:=JSTotalSize+JPGOldSize;
End;
CloseHandle(Fl);
SetFileAttributes(PChar(FName), FAttr);
End;
Procedure HandleFiles(S: String);
Var
FH, Fl: THandle;
TF: WIN32_FIND_DATA;
St: String;
Begin
FillChar(TF, SizeOf(WIN32_FIND_DATA), 0);
If S[1] = '\' Then Delete(S, 1, 1);
If BTRecursive Then
Begin
St:=ExtractFilePath(S);
If ((St = '') Or (St[Length(St)] <> '\')) Then St:=St + '\';
If St[1] = '\' Then Delete(St, 1, 1);
FH:=FindFirstFile(PChar(St + '*.*'), TF);
If FH <> INVALID_HANDLE_VALUE Then
Begin
Repeat
Move(TF.cFileName, Fl, 4);
// compare with '.' and '..', delphi bug workaround
If ((TF.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY<>0) And
((Fl And $0000FFFF)<>$002E) And (Fl And $00FFFFFF<>$002E2E)) Then
HandleFiles(St + GetANSIName(TF) + '\' + ExtractFileName(S)); // v0.28
Until FindNextFile(FH, TF) = False;
FindClose(FH);
End;
End;
FH:=FindFirstFile(PChar(S), TF);
St:=ExtractFilePath(S);
If FH <> INVALID_HANDLE_VALUE Then
Begin
Repeat
If (TF.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY = 0) Then
AddFileToList(St, TF);
Until FindNextFile(FH, TF) = False;
FindClose(FH);
End;
FlCur:=FlHead;
While FlCur<>Nil Do
Begin
{* 0.25 *}
JPEGStripFile(FlCur^.Name);
If{ (JPEGStripFile(FlCur^.Name) And} (Not BTNoDatetime){)} Then
Begin
Fl:=CreateFile(FlCur^.Name, 256{FILE_WRITE_ATTRIBUTES}, 0, Nil, OPEN_EXISTING, 0, 0); {* 0.27 *}
If Fl<>INVALID_HANDLE_VALUE Then
Begin
SetFileTime(Fl, @FlCur^.CT, @FlCur^.AT, @FlCur^.WT);
CloseHandle(Fl);
End;
End;
FlCur:=FlCur^.Next;
End;
DestroyList; // free memory
End;
var JSAverage: Cardinal;
Begin
If Not HandleParams Then Exit;
If ((Not BTSilentMode) Or (ParamCount = 0)) Then
Begin
WriteLn(JPGSTRIP_VERSION);
WriteLn('(c) CTPAX-X Team 2007,2008,2011,2014');
WriteLn('http://www.CTPAX-X.org/');
WriteLn;
End;
If ParamCount = 0 Then
Begin
WriteLn('Usage: jpgstrip filename.ext [/S] [/B] [/R] [/J] [/T] [/Q]');
WriteLn;
WriteLn('filename.ext - filemask, wildcards allowed');
WriteLn('/S - handle all files from current folder and all subfolders');
WriteLn('/B - create backup files (*.bak)');
WriteLn('/R - don''t strip read-only files');
WriteLn('/J - don''t strip JFIF block (for old/stupid JPEG decoders)');
WriteLn('/T - don''t keep original file creation datetime');
WriteLn('/Q - quiet mode, don''t output anything');
WriteLn;
Exit;
End;
HandleFiles(ParamStr(1));
If Not BTSilentMode Then
Begin
If JSShrunkFiles = 0 Then
JSAverage:=0
Else
JSAverage:=JSSavedSize Div JSShrunkFiles;
WriteLn;
WriteLn('Statistics:');
WriteLn;
WriteLn('scanned: '+Int2StrSpaces(JSTotalFiles,10)+' file(s) / '+PadWithSpaces(Int2Str3(JSTotalSize),10)+' bytes');
WriteLn(' shrunk: '+Int2StrSpaces(JSShrunkFiles,10)+' file(s)');
WriteLn(' saved: '+PadWithSpaces(Int2Str3(JSSavedSize),10)+' bytes');
WriteLn('average: '+PadWithSpaces(Int2Str3(JSAverage),10)+' bytes');
WriteLn(' errors: '+Int2StrSpaces(JSErrorsCount,10));
WriteLn;
End;
End.