1
{ Copyright (C) <2005> <Andrew Haines> chmwriter.pas
3
This library is free software; you can redistribute it and/or modify it
4
under the terms of the GNU Library General Public License as published by
5
the Free Software Foundation; either version 2 of the License, or (at your
6
option) any later version.
8
This program is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
13
You should have received a copy of the GNU Library General Public License
14
along with this library; if not, write to the Free Software Foundation,
15
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
See the file COPYING.FPC, included in this distribution,
19
for details about the copyright.
25
uses Classes, ChmBase, chmtypes, chmspecialfiles;
29
TGetDataFunc = function (const DataName: String; out PathInChm: String; out FileName: String; var Stream: TStream): Boolean of object;
30
// DataName : A FileName or whatever so that the getter can find and open the file to add
31
// PathInChm: This is the absolute path in the archive. i.e. /home/user/helpstuff/
32
// becomes '/' and /home/user/helpstuff/subfolder/ > /subfolder/
33
// FileName : /home/user/helpstuff/index.html > index.html
34
// Stream : the file opened with DataName should be written to this stream
39
TChmWriter = class(TObject)
40
FOnLastFile: TNotifyEvent;
47
FFullTextSearch: Boolean;
48
FInternalFiles: TFileEntryList; // Contains a complete list of files in the chm including
49
FFrameSize: LongWord; // uncompressed files and special internal files of the chm
50
FCurrentStream: TStream; // used to buffer the files that are to be compressed
51
FCurrentIndex: Integer;
52
FOnGetFileData: TGetDataFunc;
53
FStringsStream: TMemoryStream;
54
FContextStream: TMemoryStream; // the #IVB file
55
FSection0: TMemoryStream;
56
FSection1: TStream; // Compressed Stream
58
FSection1ResetTable: TMemoryStream; // has a list of frame positions NOT window positions
59
FDirectoryListings: TStream;
62
FDestroyStream: Boolean;
68
FWindowSize: LongWord;
69
FReadCompressedSize: QWord; // Current Size of Uncompressed data that went in Section1 (compressed)
70
// Linear order of file
71
ITSFHeader: TITSFHeader;
72
HeaderSection0Table: TITSFHeaderEntry; // points to HeaderSection0
73
HeaderSection1Table: TITSFHeaderEntry; // points to HeaderSection1
74
HeaderSuffix: TITSFHeaderSuffix; //contains the offset of CONTENTSection0 from zero
75
HeaderSection0: TITSPHeaderPrefix;
76
HeaderSection1: TITSPHeader; // DirectoryListings header
78
// CONTENT Section 0 (section 1 is contained in section 0)
80
// end linear header parts
81
procedure InitITSFHeader;
82
procedure InitHeaderSectionTable;
83
procedure SetTempRawStream(const AValue: TStream);
84
procedure WriteHeader(Stream: TStream);
85
procedure CreateDirectoryListings;
86
procedure WriteDirectoryListings(Stream: TStream);
87
procedure StartCompressingStream;
88
procedure WriteSYSTEM;
89
procedure WriteITBITS;
90
procedure WriteSTRINGS;
91
procedure WriteIVB; // context ids
92
procedure WriteREADMEFile;
93
procedure WriteSection0;
94
procedure WriteSection1;
95
procedure WriteDataSpaceFiles(const AStream: TStream);
96
function AddString(AString: String): LongWord;
97
// callbacks for lzxcomp
98
function AtEndOfData: Longbool;
99
function GetData(Count: LongInt; Buffer: PByte): LongInt;
100
function WriteCompressedData(Count: Longint; Buffer: Pointer): LongInt;
101
procedure MarkFrame(UnCompressedTotal, CompressedTotal: LongWord);
104
constructor Create(OutStream: TStream; FreeStreamOnDestroy: Boolean);
105
destructor Destroy; override;
107
procedure AppendTOC(AStream: TStream);
108
procedure AppendIndex(AStream: TStream);
109
procedure AppendSearchDB(AName: String; AStream: TStream);
110
procedure AddStreamToArchive(AFileName, APath: String; AStream: TStream; Compress: Boolean = True);
111
procedure PostAddStreamToArchive(AFileName, APath: String; AStream: TStream; Compress: Boolean = True);
112
procedure AddContext(AContext: DWord; ATopic: String);
113
property WindowSize: LongWord read FWindowSize write FWindowSize default 2; // in $8000 blocks
114
property FrameSize: LongWord read FFrameSize write FFrameSize default 1; // in $8000 blocks
115
property FilesToCompress: TStrings read FFileNames;
116
property OnGetFileData: TGetDataFunc read FOnGetFileData write FOnGetFileData;
117
property OnLastFile: TNotifyEvent read FOnLastFile write FOnLastFile;
118
property OutStream: TStream read FOutStream;
119
property Title: String read FTitle write FTitle;
120
property FullTextSearch: Boolean read FFullTextSearch write FFullTextSearch;
121
property DefaultFont: String read FDefaultFont write FDefaultFont;
122
property DefaultPage: String read FDefaultPage write FDefaultPage;
123
property TempRawStream: TStream read FTempStream write SetTempRawStream;
124
//property LocaleID: dword read ITSFHeader.LanguageID write ITSFHeader.LanguageID;
128
uses dateutils, sysutils, paslzxcomp;
132
LZX_WINDOW_SIZE = 16; // 16 = 2 frames = 1 shl 16
133
LZX_FRAME_SIZE = $8000;
137
procedure TChmWriter.InitITSFHeader;
139
with ITSFHeader do begin
140
ITSFsig := ITSFFileSig;
141
Version := NToLE(DWord(3));
142
// we fix endian order when this is written to the stream
143
HeaderLength := NToLE(DWord(SizeOf(TITSFHeader) + (SizeOf(TITSFHeaderEntry)*2) + SizeOf(TITSFHeaderSuffix)));
144
Unknown_1 := NToLE(DWord(1));
145
TimeStamp:= NToBE(MilliSecondOfTheDay(Now)); //bigendian
146
LanguageID := NToLE(DWord($0409)); // English / English_US
147
Guid1 := ITSFHeaderGUID;
148
Guid2 := ITSFHeaderGUID;
152
procedure TChmWriter.InitHeaderSectionTable;
155
HeaderSection0Table.PosFromZero := LEToN(ITSFHeader.HeaderLength);
156
HeaderSection0Table.Length := SizeOf(TITSPHeaderPrefix);
158
HeaderSection1Table.PosFromZero := HeaderSection0Table.PosFromZero + HeaderSection0Table.Length;
159
HeaderSection1Table.Length := SizeOf(TITSPHeader)+FDirectoryListings.Size;
161
//contains the offset of CONTENT Section0 from zero
162
HeaderSuffix.Offset := HeaderSection1Table.PosFromZero + HeaderSection1Table.Length;
164
// now fix endian stuff
165
HeaderSection0Table.PosFromZero := NToLE(HeaderSection0Table.PosFromZero);
166
HeaderSection0Table.Length := NToLE(HeaderSection0Table.Length);
167
HeaderSection1Table.PosFromZero := NToLE(HeaderSection1Table.PosFromZero);
168
HeaderSection1Table.Length := NToLE(HeaderSection1Table.Length);
170
with HeaderSection0 do begin // TITSPHeaderPrefix;
171
Unknown1 := NToLE(DWord($01FE));
173
// at this point we are putting together the headers. content sections 0 and 1 are complete
174
FileSize := NToLE(HeaderSuffix.Offset + FSection0.Size + FSection1Size);
178
with HeaderSection1 do begin // TITSPHeader; // DirectoryListings header
179
ITSPsig := ITSPHeaderSig;
180
Version := NToLE(DWord(1));
181
DirHeaderLength := NToLE(DWord(SizeOf(TITSPHeader))); // Length of the directory header
182
Unknown1 := NToLE(DWord($0A));
183
ChunkSize := NToLE(DWord($1000));
184
Density := NToLE(DWord(2));
185
// updated when directory listings were created
186
//IndexTreeDepth := 1 ; // 1 if there is no index 2 if there is one level of PMGI chunks. will update as
187
//IndexOfRootChunk := -1;// if no root chunk
188
//FirstPMGLChunkIndex,
189
//LastPMGLChunkIndex: LongWord;
191
Unknown2 := NToLE(Longint(-1));
192
//DirectoryChunkCount: LongWord;
193
LanguageID := NToLE(DWord($0409));
194
GUID := ITSPHeaderGUID;
195
LengthAgain := NToLE(DWord($54));
196
Unknown3 := NToLE(Longint(-1));
197
Unknown4 := NToLE(Longint(-1));
198
Unknown5 := NToLE(Longint(-1));
202
HeaderSuffix.Offset := NToLE(HeaderSuffix.Offset);
205
procedure TChmWriter.SetTempRawStream(const AValue: TStream);
207
if (FCurrentStream.Size > 0) or (FSection1.Size > 0) then
208
raise Exception.Create('Cannot set the TempRawStream once data has been written to it!');
210
raise Exception.Create('TempRawStream cannot be nil!');
211
if FCurrentStream = AValue then
214
FCurrentStream := AValue;
217
procedure TChmWriter.WriteHeader(Stream: TStream);
219
Stream.Write(ITSFHeader, SizeOf(TITSFHeader));
220
Stream.Write(HeaderSection0Table, SizeOf(TITSFHeaderEntry));
221
Stream.Write(HeaderSection1Table, SizeOf(TITSFHeaderEntry));
222
Stream.Write(HeaderSuffix, SizeOf(TITSFHeaderSuffix));
223
Stream.Write(HeaderSection0, SizeOf(TITSPHeaderPrefix));
227
procedure TChmWriter.CreateDirectoryListings;
229
TFirstListEntry = record
230
Entry: array[0..511] of byte;
234
Buffer: array [0..511] of Byte;
235
IndexBlock: TPMGIDirectoryChunk;
236
ListingBlock: TDirectoryChunk;
241
FileNameSize: Integer;
242
LastListIndex: Integer;
243
FirstListEntry: TFirstListEntry;
245
ListHeader: TPMGListChunk;
249
procedure UpdateLastListChunk;
253
if ChunkIndex < 1 then begin
256
Tmp := FDirectoryListings.Position;
257
FDirectoryListings.Position := (LastListIndex) * $1000;
258
FDirectoryListings.Read(ListHeader, SizeOf(TPMGListChunk));
259
FDirectoryListings.Position := (LastListIndex) * $1000;
260
ListHeader.NextChunkIndex := NToLE(ChunkIndex);
261
FDirectoryListings.Write(ListHeader, SizeOf(TPMGListChunk));
262
FDirectoryListings.Position := Tmp;
264
procedure WriteIndexChunk(ShouldFinish: Boolean = False);
266
IndexHeader: TPMGIIndexChunk;
268
TmpIndex: TPMGIDirectoryChunk;
270
with IndexHeader do begin
272
UnusedSpace := NToLE(IndexBlock.FreeSpace);
274
IndexBlock.WriteHeader(@IndexHeader);
275
IndexBlock.WriteChunkToStream(FDirectoryListings, ChunkIndex, ShouldFinish);
277
if HeaderSection1.IndexOfRootChunk < 0 then HeaderSection1.IndexOfRootChunk := ChunkIndex;
278
if ShouldFinish then begin;
279
HeaderSection1.IndexTreeDepth := 2;
280
ParentIndex := IndexBlock.ParentChunk;
281
if ParentIndex <> nil then repeat // the parent index is notified by our child index when to write
282
HeaderSection1.IndexOfRootChunk := ChunkIndex;
283
TmpIndex := ParentIndex;
284
ParentIndex := ParentIndex.ParentChunk;
286
Inc(HeaderSection1.IndexTreeDepth);
288
until ParentIndex = nil;
293
procedure WriteListChunk;
295
with ListHeader do begin
297
UnusedSpace := NToLE(ListingBlock.FreeSpace);
299
PreviousChunkIndex := NToLE(LastListIndex);
300
NextChunkIndex := NToLE(Longint(-1)); // we update this when we write the next chunk
302
if HeaderSection1.FirstPMGLChunkIndex <= 0 then
303
HeaderSection1.FirstPMGLChunkIndex := NToLE(ChunkIndex);
304
HeaderSection1.LastPMGLChunkIndex := NToLE(ChunkIndex);
305
ListingBlock.WriteHeader(@ListHeader);
306
ListingBlock.WriteChunkToStream(FDirectoryListings);
310
LastListIndex := ChunkIndex;
313
if not IndexBlock.CanHold(FirstListEntry.Size) then
315
IndexBlock.WriteEntry(FirstListEntry.Size, @FirstListEntry.Entry[0])
318
// first sort the listings
320
HeaderSection1.IndexTreeDepth := 1;
321
HeaderSection1.IndexOfRootChunk := -1;
325
IndexBlock := TPMGIDirectoryChunk.Create(SizeOf(TPMGIIndexChunk));
326
ListingBlock := TDirectoryChunk.Create(SizeOf(TPMGListChunk));
330
// add files to a pmgl block until it is full.
331
// after the block is full make a pmgi block and add the first entry of the pmgl block
332
// repeat until the index block is full and start another.
333
// the pmgi chunks take care of needed parent chunks in the tree
334
for I := 0 to FInternalFiles.Count-1 do begin
336
FileName := FInternalFiles.FileEntry[I].Path + FInternalFiles.FileEntry[I].Name;
337
FileNameSize := Length(FileName);
339
Inc(Size, WriteCompressedInteger(@Buffer[Size], FileNameSize));
341
Move(FileName[1], Buffer[Size], FileNameSize);
342
Inc(Size, FileNameSize);
344
// File is compressed...
345
Inc(Size, WriteCompressedInteger(@Buffer[Size], Ord(FInternalFiles.FileEntry[I].Compressed)));
346
// Offset from section start
347
Inc(Size, WriteCompressedInteger(@Buffer[Size], FInternalFiles.FileEntry[I].DecompressedOffset));
348
// Size when uncompressed
349
Inc(Size, WriteCompressedInteger(@Buffer[Size], FInternalFiles.FileEntry[I].DecompressedSize));
351
if not ListingBlock.CanHold(Size) then
354
ListingBlock.WriteEntry(Size, @Buffer[0]);
356
if ListingBlock.ItemCount = 1 then begin // add the first list item to the index
357
Move(Buffer[0], FirstListEntry.Entry[0], FESize);
358
FirstListEntry.Size := FESize + WriteCompressedInteger(@FirstListEntry.Entry[FESize], ChunkIndex);
361
if ListingBlock.ItemCount > 0 then WriteListChunk;
363
if ChunkIndex > 1 then begin
364
if (IndexBlock.ItemCount > 1)
365
or ( (IndexBlock.ItemCount > 0) and (HeaderSection1.IndexOfRootChunk > -1) )
366
then WriteIndexChunk(True);
369
HeaderSection1.DirectoryChunkCount := NToLE(DWord(FDirectoryListings.Size div $1000));
374
//now fix some endian stuff
375
HeaderSection1.IndexOfRootChunk := NToLE(HeaderSection1.IndexOfRootChunk);
376
HeaderSection1.IndexTreeDepth := NtoLE(HeaderSection1.IndexTreeDepth);
379
procedure TChmWriter.WriteDirectoryListings(Stream: TStream);
381
Stream.Write(HeaderSection1, SizeOf(HeaderSection1));
382
FDirectoryListings.Position := 0;
383
Stream.CopyFrom(FDirectoryListings, FDirectoryListings.Size);
384
FDirectoryListings.Position := 0;
385
//TMemoryStream(FDirectoryListings).SaveToFile('dirlistings.pmg');
388
procedure TChmWriter.WriteSystem;
390
Entry: TFileEntryRec;
394
VersionStr = 'HHA Version 4.74.8702'; // does this matter?
396
// this creates the /#SYSTEM file
397
Entry.Name := '#SYSTEM';
399
Entry.Compressed := False;
400
Entry.DecompressedOffset := FSection0.Position;
401
// EntryCodeOrder: 10 9 4 2 3 16 6 0 1 5
402
FSection0.WriteDWord(NToLE(Word(3))); // Version
406
TmpTitle := 'default';
408
// Code -> Length -> Data
410
FSection0.WriteWord(NToLE(Word(10)));
411
FSection0.WriteWord(NToLE(Word(SizeOf(DWord))));
412
FSection0.WriteDWord(NToLE(MilliSecondOfTheDay(Now)));
414
FSection0.WriteWord(NToLE(Word(9)));
415
FSection0.WriteWord(NToLE(Word(SizeOf(VersionStr)+1)));
416
FSection0.Write(VersionStr, SizeOf(VersionStr));
417
FSection0.WriteByte(0);
418
// 4 A struct that is only needed to set if full text search is on.
419
FSection0.WriteWord(NToLE(Word(4)));
420
FSection0.WriteWord(NToLE(Word(36))); // size
421
FSection0.WriteDWord(NToLE(DWord($0409)));
422
FSection0.WriteDWord(NToLE(DWord(Ord(FFullTextSearch))));
423
FSection0.WriteDWord(0);
424
FSection0.WriteDWord(0);
425
FSection0.WriteDWord(0);
427
FSection0.WriteDWord(0);
428
FSection0.WriteDWord(0);
430
FSection0.WriteDWord(0);
431
FSection0.WriteDWord(0);
436
////////////////////////<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
437
// 2 default page to load
438
if FDefaultPage <> '' then begin
439
FSection0.WriteWord(NToLE(Word(2)));
440
FSection0.WriteWord(NToLE(Word(Length(FDefaultPage)+1)));
441
FSection0.Write(FDefaultPage[1], Length(FDefaultPage));
442
FSection0.WriteByte(0);
445
if FTitle <> '' then begin
446
FSection0.WriteWord(NToLE(Word(3)));
447
FSection0.WriteWord(NToLE(Word(Length(FTitle)+1)));
448
FSection0.Write(FTitle[1], Length(FTitle));
449
FSection0.WriteByte(0);
453
if FDefaultFont <> '' then begin
454
FSection0.WriteWord(NToLE(Word(16)));
455
FSection0.WriteWord(NToLE(Word(Length(FDefaultFont)+1)));
456
FSection0.Write(FDefaultFont[1], Length(FDefaultFont));
457
FSection0.WriteByte(0);
461
// unneeded. if output file is : /somepath/OutFile.chm the value here is outfile(lowercase)
463
// 0 Table of contents filename
464
if FHasTOC then begin
465
TmpStr := 'default.hhc';
466
FSection0.WriteWord(0);
467
FSection0.WriteWord(NToLE(Word(Length(TmpStr)+1)));
468
FSection0.Write(TmpStr[1], Length(TmpStr));
469
FSection0.WriteByte(0);
473
if FHasIndex then begin
474
TmpStr := 'default.hhk';
475
FSection0.WriteWord(NToLE(Word(1)));
476
FSection0.WriteWord(NToLE(Word(Length(TmpStr)+1)));
477
FSection0.Write(TmpStr[1], Length(TmpStr));
478
FSection0.WriteByte(0);
483
Entry.DecompressedSize := FSection0.Position - Entry.DecompressedOffset;
484
FInternalFiles.AddEntry(Entry);
487
procedure TChmWriter.WriteITBITS;
489
Entry: TFileEntryRec;
491
// This is an empty and useless file
492
Entry.Name := '#ITBITS';
494
Entry.Compressed := False;
495
Entry.DecompressedOffset := FSection0.Position;
496
Entry.DecompressedSize := 0;
498
FInternalFiles.AddEntry(Entry);
501
procedure TChmWriter.WriteSTRINGS;
503
if FStringsStream.Size = 0 then;
504
FStringsStream.WriteByte(0);
505
FStringsStream.Position := 0;
506
AddStreamToArchive('#STRINGS', '/', FStringsStream);
509
procedure TChmWriter.WriteIVB;
511
if FContextStream = nil then exit;
513
FContextStream.Position := 0;
514
// the size of all the entries
515
FContextStream.WriteDWord(NToLE(DWord(FContextStream.Size-SizeOf(dword))));
517
FContextStream.Position := 0;
518
AddStreamToArchive('#IVB', '/', FContextStream);
521
procedure TChmWriter.WriteREADMEFile;
522
const DISCLAIMER_STR = 'This archive was not made by the MS HTML Help Workshop(r)(tm) program.';
524
Entry: TFileEntryRec;
526
// This procedure puts a file in the archive that says it wasn't compiled with the MS compiler
527
Entry.Compressed := False;
528
Entry.DecompressedOffset := FSection0.Position;
529
FSection0.Write(DISCLAIMER_STR, SizeOf(DISCLAIMER_STR));
530
Entry.DecompressedSize := FSection0.Position - Entry.DecompressedOffset;
532
Entry.Name := '_#_README_#_'; //try to use a name that won't conflict with normal names
533
FInternalFiles.AddEntry(Entry);
537
procedure TChmWriter.WriteSection0;
539
FSection0.Position := 0;
540
FOutStream.CopyFrom(FSection0, FSection0.Size);
543
procedure TChmWriter.WriteSection1;
545
WriteContentToStream(FOutStream, FSection1);
548
procedure TChmWriter.WriteDataSpaceFiles(const AStream: TStream);
550
Entry: TFileEntryRec;
552
// This procedure will write all files starting with ::
553
Entry.Compressed := False; // None of these files are compressed
555
// ::DataSpace/NameList
556
Entry.DecompressedOffset := FSection0.Position;
557
Entry.DecompressedSize := WriteNameListToStream(FSection0, [snUnCompressed,snMSCompressed]);
558
Entry.Path := '::DataSpace/';
559
Entry.Name := 'NameList';
560
FInternalFiles.AddEntry(Entry, False);
562
// ::DataSpace/Storage/MSCompressed/ControlData
563
Entry.DecompressedOffset := FSection0.Position;
564
Entry.DecompressedSize := WriteControlDataToStream(FSection0, 2, 2, 1);
565
Entry.Path := '::DataSpace/Storage/MSCompressed/';
566
Entry.Name := 'ControlData';
567
FInternalFiles.AddEntry(Entry, False);
569
// ::DataSpace/Storage/MSCompressed/SpanInfo
570
Entry.DecompressedOffset := FSection0.Position;
571
Entry.DecompressedSize := WriteSpanInfoToStream(FSection0, FReadCompressedSize);
572
Entry.Path := '::DataSpace/Storage/MSCompressed/';
573
Entry.Name := 'SpanInfo';
574
FInternalFiles.AddEntry(Entry, False);
576
// ::DataSpace/Storage/MSCompressed/Transform/List
577
Entry.DecompressedOffset := FSection0.Position;
578
Entry.DecompressedSize := WriteTransformListToStream(FSection0);
579
Entry.Path := '::DataSpace/Storage/MSCompressed/Transform/';
580
Entry.Name := 'List';
581
FInternalFiles.AddEntry(Entry, False);
583
// ::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/
584
// ::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable
585
Entry.DecompressedOffset := FSection0.Position;
586
Entry.DecompressedSize := WriteResetTableToStream(FSection0, FSection1ResetTable);
587
Entry.Path := '::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/';
588
Entry.Name := 'ResetTable';
589
FInternalFiles.AddEntry(Entry, True);
592
// ::DataSpace/Storage/MSCompressed/Content do this last
593
Entry.DecompressedOffset := FSection0.Position;
594
Entry.DecompressedSize := FSection1Size; // we will write it directly to FOutStream later
595
Entry.Path := '::DataSpace/Storage/MSCompressed/';
596
Entry.Name := 'Content';
597
FInternalFiles.AddEntry(Entry, False);
602
function TChmWriter.AddString(AString: String): LongWord;
604
// #STRINGS starts with a null char
605
if FStringsStream.Size = 0 then FStringsStream.WriteByte(0);
606
// each entry is a null terminated string
607
Result := FStringsStream.Position;
608
FStringsStream.WriteBuffer(AString[1], Length(AString));
609
FStringsStream.WriteByte(0);
612
function _AtEndOfData(arg: pointer): LongBool; cdecl;
614
Result := TChmWriter(arg).AtEndOfData;
617
function TChmWriter.AtEndOfData: LongBool;
619
Result := ForceExit or (FCurrentIndex >= FFileNames.Count-1);
621
Result := Integer(FCurrentStream.Position) >= Integer(FCurrentStream.Size)-1;
624
function _GetData(arg: pointer; Count: LongInt; Buffer: Pointer): LongInt; cdecl;
626
Result := TChmWriter(arg).GetData(Count, PByte(Buffer));
629
function TChmWriter.GetData(Count: LongInt; Buffer: PByte): LongInt;
631
FileEntry: TFileEntryRec;
634
while (Result < Count) and (not AtEndOfData) do begin
635
Inc(Result, FCurrentStream.Read(Buffer[Result], Count-Result));
636
if (Result < Count) and (not AtEndOfData)
638
// the current file has been read. move to the next file in the list
639
FCurrentStream.Position := 0;
641
ForceExit := OnGetFileData(FFileNames[FCurrentIndex], FileEntry.Path, FileEntry.Name, FCurrentStream);
642
FileEntry.DecompressedSize := FCurrentStream.Size;
643
FileEntry.DecompressedOffset := FReadCompressedSize; //269047723;//to test writing really large numbers
644
FileEntry.Compressed := True;
646
FInternalFiles.AddEntry(FileEntry);
647
// So the next file knows it's offset
648
Inc(FReadCompressedSize, FileEntry.DecompressedSize);
649
FCurrentStream.Position := 0;
652
// this is intended for programs to add perhaps a file
653
// after all the other files have been added.
655
and (FCurrentStream <> FPostStream) then
657
if Assigned(FOnLastFile) then
660
FCurrentStream := FPostStream;
661
FCurrentStream.Position := 0;
662
Inc(FReadCompressedSize, FCurrentStream.Size);
667
function _WriteCompressedData(arg: pointer; Count: LongInt; Buffer: Pointer): LongInt; cdecl;
669
Result := TChmWriter(arg).WriteCompressedData(Count, Buffer);
672
function TChmWriter.WriteCompressedData(Count: Longint; Buffer: Pointer): LongInt;
674
// we allocate a MB at a time to limit memory reallocation since this
675
// writes usually 2 bytes at a time
676
if (FSection1 is TMemoryStream) and (FSection1.Position >= FSection1.Size-1) then begin
677
FSection1.Size := FSection1.Size+$100000;
679
Result := FSection1.Write(Buffer^, Count);
680
Inc(FSection1Size, Result);
683
procedure _MarkFrame(arg: pointer; UncompressedTotal, CompressedTotal: LongWord); cdecl;
685
TChmWriter(arg).MarkFrame(UncompressedTotal, CompressedTotal);
688
procedure TChmWriter.MarkFrame(UnCompressedTotal, CompressedTotal: LongWord);
689
procedure WriteQWord(Value: QWord);
691
FSection1ResetTable.Write(NToLE(Value), 8);
693
procedure IncEntryCount;
698
OldPos := FSection1ResetTable.Position;
699
FSection1ResetTable.Position := $4;
700
Value := LeToN(FSection1ResetTable.ReadDWord)+1;
701
FSection1ResetTable.Position := $4;
702
FSection1ResetTable.WriteDWord(NToLE(Value));
703
FSection1ResetTable.Position := OldPos;
705
procedure UpdateTotalSizes;
709
OldPos := FSection1ResetTable.Position;
710
FSection1ResetTable.Position := $10;
711
WriteQWord(FReadCompressedSize); // size of read data that has been compressed
712
WriteQWord(CompressedTotal);
713
FSection1ResetTable.Position := OldPos;
716
if FSection1ResetTable.Size = 0 then begin
718
FSection1ResetTable.WriteDWord(NtoLE(DWord(2)));
719
FSection1ResetTable.WriteDWord(0); // number of entries. we will correct this with IncEntryCount
720
FSection1ResetTable.WriteDWord(NtoLE(DWord(8))); // Size of Entries (qword)
721
FSection1ResetTable.WriteDWord(NtoLE(DWord($28))); // Size of this header
722
WriteQWord(0); // Total Uncompressed Size
723
WriteQWord(0); // Total Compressed Size
724
WriteQWord(NtoLE($8000)); // Block Size
725
WriteQWord(0); // First Block start
729
WriteQWord(CompressedTotal); // Next Block Start
730
// We have to trim the last entry off when we are done because there is no next block in that case
733
constructor TChmWriter.Create(OutStream: TStream; FreeStreamOnDestroy: Boolean);
735
if OutStream = nil then Raise Exception.Create('TChmWriter.OutStream Cannot be nil!');
736
FCurrentStream := TMemoryStream.Create;
738
FOutStream := OutStream;
739
FInternalFiles := TFileEntryList.Create;
740
FStringsStream := TmemoryStream.Create;
741
FSection0 := TMemoryStream.Create;
742
FSection1 := TMemoryStream.Create;
743
FSection1ResetTable := TMemoryStream.Create;
744
FDirectoryListings := TMemoryStream.Create;
745
FPostStream := TMemoryStream.Create;;
746
FDestroyStream := FreeStreamOnDestroy;
747
FFileNames := TStringList.Create;
750
destructor TChmWriter.Destroy;
752
if FDestroyStream then FOutStream.Free;
753
if Assigned(FContextStream) then FContextStream.Free;
759
FSection1ResetTable.Free;
760
FDirectoryListings.Free;
765
procedure TChmWriter.Execute;
768
FOutStream.Position := 0;
771
// write any internal files to FCurrentStream that we want in the compressed section
775
// written to Section0 (uncompressed)
778
// move back to zero so that we can start reading from zero :)
779
FReadCompressedSize := FCurrentStream.Size;
780
FCurrentStream.Position := 0; // when compressing happens, first the FCurrentStream is read
781
// before loading user files. So we can fill FCurrentStream with
782
// internal files first.
784
// this gathers ALL files that should be in section1 (the compressed section)
785
StartCompressingStream;
786
FSection1.Size := FSection1Size;
788
// This creates and writes the #ITBITS (empty) file to section0
790
// This creates and writes the #SYSTEM file to section0
793
//this creates all special files in the archive that start with ::DataSpace
794
WriteDataSpaceFiles(FSection0);
796
// creates all directory listings including header
797
CreateDirectoryListings;
799
// do this after we have compressed everything so that we know the values that must be written
800
InitHeaderSectionTable;
802
// Now we can write everything to FOutStream
803
WriteHeader(FOutStream);
804
WriteDirectoryListings(FOutStream);
805
WriteSection0; //does NOT include section 1 even though section0.content IS section1
806
WriteSection1; // writes section 1 to FOutStream
809
procedure TChmWriter.AppendTOC(AStream: TStream);
812
PostAddStreamToArchive('default.hhc', '/', AStream, True);
815
procedure TChmWriter.AppendIndex(AStream: TStream);
818
PostAddStreamToArchive('default.hhk', '/', AStream, True);
821
procedure TChmWriter.AppendSearchDB(AName: String; AStream: TStream);
823
PostAddStreamToArchive(AName, '/', AStream);
827
// this procedure is used to manually add files to compress to an internal stream that is
828
// processed before FileToCompress is called. Files added this way should not be
829
// duplicated in the FilesToCompress property.
830
procedure TChmWriter.AddStreamToArchive(AFileName, APath: String; AStream: TStream; Compress: Boolean = True);
832
TargetStream: TStream;
833
Entry: TFileEntryRec;
835
if AStream = nil then Exit;
837
TargetStream := FCurrentStream
839
TargetStream := FSection0;
841
Entry.Name := AFileName;
843
Entry.Compressed := Compress;
844
Entry.DecompressedOffset := TargetStream.Position;
845
Entry.DecompressedSize := AStream.Size;
846
FInternalFiles.AddEntry(Entry);
847
AStream.Position := 0;
848
TargetStream.CopyFrom(AStream, AStream.Size);
851
procedure TChmWriter.PostAddStreamToArchive(AFileName, APath: String;
852
AStream: TStream; Compress: Boolean);
854
TargetStream: TStream;
855
Entry: TFileEntryRec;
857
if AStream = nil then Exit;
859
TargetStream := FPostStream
861
TargetStream := FSection0;
863
Entry.Name := AFileName;
865
Entry.Compressed := Compress;
867
Entry.DecompressedOffset := TargetStream.Position
869
Entry.DecompressedOffset := FReadCompressedSize + TargetStream.Position;
870
Entry.DecompressedSize := AStream.Size;
871
FInternalFiles.AddEntry(Entry);
872
AStream.Position := 0;
873
TargetStream.CopyFrom(AStream, AStream.Size);
876
procedure TChmWriter.AddContext(AContext: DWord; ATopic: String);
880
if FContextStream = nil then begin
881
// #IVB starts with a dword which is the size of the stream - sizeof(dword)
882
FContextStream.WriteDWord(0);
883
// we will update this when we write the file to the final stream
885
// an entry is a context id and then the offset of the name of the topic in the strings file
886
FContextStream.WriteDWord(NToLE(AContext));
887
Offset := NToLE(AddString(ATopic));
888
FContextStream.WriteDWord(Offset);
891
procedure TChmWriter.StartCompressingStream;
896
lzx_init(@LZXdata, LZX_WINDOW_SIZE, @_GetData, Self, @_AtEndOfData,
897
@_WriteCompressedData, Self, @_MarkFrame, Self);
899
WSize := 1 shl LZX_WINDOW_SIZE;
900
while not AtEndOfData do begin
902
lzx_compress_block(LZXdata, WSize, True);
905
//we have to mark the last frame manually
906
MarkFrame(LZXdata^.len_uncompressed_input, LZXdata^.len_compressed_output);
908
lzx_finish(LZXdata, nil);