1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
procedure TFrmTestFichier.BtnCompresserByteHashClick(Sender: TObject);
var
FichierOri: file;
FichierPack: file; // of TCompresseRec;
FileNameIn, FileNameOut: string;
BlockOri: array[Byte] of Char;
HashChanged, HashCompleted: Boolean;
BlockHash: array[Byte] of Boolean;
TableHash: array of Byte;
BlockPack: array of Byte;
// FlagInc, FlagLast: Boolean;
idxBlockHash, idxHash, LenHash, BitPerHash: Byte;
idxBlockOri: Integer;
AmtTransferred: Integer;
AmtCompressed: Integer;
SizeOri: Cardinal;
BlockOriLen, BlockPackLen: Word;
function IndexHash(Value: Byte): Byte;
begin
for Result := Low(TableHash) to High(TableHash) do
if TableHash[Result] = Value then
Exit;
raise Exception.Create('Oh Merde !');
end;
begin
BtnCompresserByteHash.Enabled := False;
try
// Bar de Progression à Zéro
ProgressBarReel.Position := 0;
ProgressBarCompresse.Position := 0;
FileNameIn := EdPathFileToPack.Text;
FileNameOut := ChangeFileExt(FileNameIn, '.spbhz');
// Prise en Main du Fichier d'Entrée
AssignFile(FichierOri, FileNameIn);
// Ouverture du Fichier d'Entrée
Reset(FichierOri, 1);
try
if Eof(FichierOri) then begin
Exit;
end;
// Initialisation
BlockOriLen := High(BlockOri) - Low(BlockOri);
// Bar de Progression de 0 à Fin de Fichier
ProgressBarReel.Max := FileSize(FichierOri);
ProgressBarReel.Position := 0;
// Création de la Hash Binaire
ZeroMemory(@BlockHash, SizeOf(BlockHash));
HashCompleted := False;
while not Eof(FichierOri) and not HashCompleted do begin
// Lecture du Fichier d'Entrée
BlockRead(FichierOri, BlockOri, BlockOriLen, AmtTransferred);
// Bar de Progession
ProgressBarReel.StepBy(AmtTransferred - 1);
HashChanged := False;
// Indice de 0 à Len-1 : "Min( BlockOriLen, AmtTransferred ) - 1"
for idxBlockOri := Low(BlockOri) to AmtTransferred - 1 do begin
if not BlockHash[Byte(BlockOri[idxBlockOri])] then begin
BlockHash[Byte(BlockOri[idxBlockOri])] := True;
HashChanged := True;
end;
end;
if HashChanged then
begin
HashCompleted := True;
for idxBlockHash := Low(BlockHash) to High(BlockHash) do begin
if not BlockHash[idxBlockHash] then
begin
HashCompleted := False;
Break;
end;
end;
end;
end;
// Remplissage de Table de Hashage
LenHash := 0;
for idxBlockHash := Low(BlockHash) to High(BlockHash) do
if BlockHash[idxBlockHash] then
Inc(LenHash);
SetLength(TableHash, LenHash);
idxHash := 0;
for idxBlockHash := Low(BlockHash) to High(BlockHash) do begin
if BlockHash[idxBlockHash] then
begin
TableHash[idxHash] := idxBlockHash;
Inc(idxHash);
end;
end;
BitPerHash := Ceil(Log2(LenHash));
if BitPerHash = 8 then // Inutile, la Table de Hash contient autant d'élement ...
Exit;
//BlockPackLen := BlockOriLen div Trunc(Log2(256)) * BitPerHash;
BlockOriLen := 8 * 8;
BlockPackLen := BitPerHash * 8;
SetLength(BlockPack, BlockPackLen);
// Retour au Début, et nouvel taille de block de lecture
Reset(FichierOri, BlockOriLen);
// Prise en Main du Fichier de Sortie
AssignFile(FichierPack, FileNameOut);
// Prêt pour l'écriture dans le Fichier de Sortie
Rewrite(FichierPack, 1);
try
if Eof(FichierOri) then begin
Exit;
end;
// Bar de Progression de 0 à Fin de Fichier
ProgressBarReel.Max := (FileSize(FichierOri) - 1) * 2;
ProgressBarCompresse.Max := ProgressBarReel.Max;
ProgressBarReel.Position := 0;
ProgressBarCompresse.Position := 0;
// Initialisation
SizeOri := FileSize(FichierOri);
// ATTENTION : Ecriture de la Taille Original (sur 4 Octects)
BlockWrite(FichierPack, SizeOri, SizeOf(SizeOri));
// ATTENTION
// Dans le Fichier c'est écrit 0 à 255
// ...mais cela se comprend de 1 à 256
// Lecture jusqu'au bout du Fichier d'Entrée
while not Eof(FichierOri) do begin
// Lecture du Fichier d'Entrée
BlockRead(FichierOri, BlockOri, 1, AmtTransferred);
// Bar de Progession
ProgressBarReel.StepBy(AmtTransferred - 1);
AmtCompressed := BlockPackLen;
// Compression Bit à Bit !
for idxBlockOri := Succ(Low(BlockOri)) to AmtTransferred - 1 do begin
//BlockOri[idxBlockOri]
for idxBlockHash := Low(BlockHash) to High(BlockHash) do begin
(*
idxHash := IndexHash(BlockHash[idxBlockHash]);
// - un compteur comptant le nombre d'entrés
case BitPerHash of
1: begin // Sub Pack Octet (8)
// - un compteur qui compte les Bit (décalage) de 0 à 7
// - 8 Octets en Entrée, 1 Octet en Sortie
// 0 : Bytes[0] := idxHash; // 1
// i : Bytes[0] := Bytes[0] or idxHash shl i; // Inc(i, 1)
end;
2: begin // Sub Pack Octet (8)
// - un compteur qui compte les Bit (décalage par 2) de 0, 2, 4, 6
// - 4 Octets en Entrée, 1 Octet en Sortie
// 0 : Bytes[0] := idxHash; // 2
// i : Bytes[0] := Bytes[0] or idxHash shl i; // Inc(i, 2)
end;
3: begin // Sub Pack 3 Octets (24)
// - un compteur qui l'octet 0 à 2
// - un compteur qui compte les Bit par Octet
// - 8 Octets en Entrée, 3 Octet en Sortie
// 1 - 0 - 0 : Bytes[0] := idxHash; // 3
// 2 - 0 - 3 : Bytes[0] := Bytes[0] or idxHash shl 3; // 3
// 3 - 0 - 6 : Bytes[0] := Bytes[0] or idxHash shl 6; // 2
// Bytes[1] := idxHash shr 2; // 1 +
// 4 - 1 - 1 : Bytes[1] := Bytes[1] or idxHash shl 1; // 3
// 5 - 1 - 4 : Bytes[1] := Bytes[1] or idxHash shl 4; // 3
// 6 - 1 - 7 : Bytes[1] := Bytes[1] or idxHash shl 7; // 1
// : Bytes[2] := idxHash shr 1; // 2 +
// 7 - 2 - 2 : Bytes[2] := Bytes[2] or idxHash shl 2; // 3
// 8 - 2 - 5 : Bytes[2] := Bytes[2] or idxHash shl 5; // 3
// Terminé // 24
end;
4: begin // Sub Pack Octet (8)
// - un compteur qui compte les Bit (décalage par 4) de 0, 4
// - 2 Octets en Entrée, 1 Octet en Sortie
// 0 : Bytes[0] := idxHash; // 2
// i : Bytes[0] := Bytes[0] or idxHash shl i; // Inc(i, 4)
end;
5: begin // Sub Pack 5 Octets (40)
// - un compteur qui l'octet 0 à 4
// - un compteur qui compte les Bit par Octet
// - 8 Octets en Entrée, 5 Octet en Sortie
// 1 - 0 - 0 : Bytes[0] := idxHash; // 5
// 2 - 0 - 5 : Bytes[0] := Bytes[0] or idxHash shl 5; // 3
// Bytes[1] := idxHash shr 3; // 2 +
// 3 - 1 - 2 : Bytes[1] := Bytes[1] or idxHash shl 2; // 5
// 4 - 1 - 7 : Bytes[1] := Bytes[1] or idxHash shl 7; // 1
// Bytes[2] := idxHash shr 1; // 4 +
// 5 - 2 - 4 : Bytes[2] := Bytes[2] or idxHash shl 4; // 4
// Bytes[3] := idxHash shr 4; // 1 +
// 6 - 3 - 1 : Bytes[3] := Bytes[3] or idxHash shl 1; // 5
// 7 - 3 - 6 : Bytes[3] := Bytes[3] or idxHash shl 6; // 2
// Bytes[4] := idxHash shr 2; // 3 +
// 8 - 4 - 3 : Bytes[4] := Bytes[4] or idxHash shl 3; // 5
end;
6: begin // Sub Pack 3 Octets (24)
// - un compteur qui l'octet 0 à 2
// - un compteur qui compte les Bit par Octet
// - 4 Octets en Entrée, 3 Octet en Sortie
// 1 - 0 - 0 : Bytes[0] := idxHash; // 6
// 2 - 0 - 6 : Bytes[0] := Bytes[1] or idxHash shl 6; // 2
// Bytes[1] := idxHash shr 2; // 4 +
// 3 - 1 - 4 : Bytes[1] := Bytes[1] or idxHash shl 4; // 4
// Bytes[2] := idxHash shr 4; // 2 +
// 4 - 2 - 2 : Bytes[2] := Bytes[2] or idxHash shl 2; // 6
end;
7: begin // Sub Pack 7 Octets (56)
// - un compteur qui l'octet 0 à 6
// - un compteur qui compte les Bit par Octet
// - 8 Octets en Entrée, 7 Octet en Sortie
// 1 - 0 - 0 : Bytes[0] := idxHash; // 7
// 2 - 0 - 7 : Bytes[0] := Bytes[0] or idxHash shl 7; // 1
// Bytes[1] := idxHash shr 1; // 6 +
// 3 - 1 - 6 : Bytes[1] := Bytes[1] or idxHash shl 6; // 2
// Bytes[2] := idxHash shr 2; // 5 +
// 4 - 2 - 5 : Bytes[2] := Bytes[2] or idxHash shl 5; // 3
// Bytes[3] := idxHash shr 3; // 4 +
// 5 - 3 - 4 : Bytes[3] := Bytes[3] or idxHash shl 4; // 4
// Bytes[4] := idxHash shr 4; // 3 +
// 6 - 4 - 3 : Bytes[4] := Bytes[4] or idxHash shl 3; // 5
// Bytes[5] := idxHash shr 5; // 2 +
// 7 - 5 - 2 : Bytes[5] := Bytes[5] or idxHash shl 2; // 6
// Bytes[6] := idxHash shr 6; // 1 +
// 8 - 6 - 1 : Bytes[6] := Bytes[5] or idxHash shl 1; // 7
end;
end;
*)
end;
end;
ProgressBarCompresse.StepBy(AmtCompressed);
BlockWrite(FichierPack, BlockPack, BlockPackLen);
end;
finally
CloseFile(FichierPack);
end;
finally
CloseFile(FichierOri);
end;
finally
BtnCompresserByteHash.Enabled := True;
end;
end;
procedure TFrmTestFichier.BtnDeCompresserByteHashClick(Sender: TObject);
begin
//
end; |
Partager