Delphi 2007 comes with the RTL unit zlib.pas which links to adler32.obj. adler32.obj is a precompiled object file implementing the Adler-32 checksum (most likely implemented in c).
Delphi apparently doesn’t actually use that function since it is simply declared as …
procedure adler32; external;
… which is plain wrong but gets the job done of forcing the compiler / linker to actually include that object file so it is available for the zlib implementation which itself is another .obj file.
Later Delphi versions declare this function as …
function adler32(adler: LongWord; buf: PByte; len: Cardinal): LongWord; cdecl;
… which matches the declaration in the official zlib documentation.
Trying to simply use that object file in Delphi 2007 like this …
Unit u_Adler32; function adler32(adler: LongWord; buf: PByte; len: Cardinal): LongWord; cdecl; implementation {$L adler32.obj} // you need to copy the obj file to the same directory as this unit function adler32; external;
… results in a compiler error
[DCC Error] u_Adler32.pas(72): E2065 Unsatisfied forward or external declaration: '_llmod'
Fortunately I am not the first one to stumble upon this problem. Arnaud Bouchez with this answer on StackOverflow helped me, to declare the missing functions:
procedure _llmod; asm jmp System.@_llmod end; procedure free(P: Pointer); cdecl; { always cdecl } begin FreeMem(P); end; function malloc(size: cardinal): Pointer; cdecl; { always cdecl } begin GetMem(Result, size); end;
So, now it compiles. But does it actually work?
Let’s try the Wikipedia example:
var s: AnsiString; adler: LongWord; begin s := 'Wikipedia'; adler := adler32(0, nil, 0); adler := adler32(adler, PByte(@s[1]), Length(s)); WriteLn(adler);
This prints 300286872 which is exactly what the example states, so it can’t be too wrong.
Now, we need some adler-32 checksums for known files to do some more tests or an implementation that is known to work.
I found Adler 32 Hash at conversion-tool.com and let it create checksums for a few texts and files and the result always matched, so now I am fairly certain the above works.
Finding a Windows tool for calculating Adler-32 turned out to be a challenge. I downloaded several, let them virus check on VirusTotal and they came up as containting viruses or trojans. Even though only one virus scanner out of 44 claimed that, I didn’t want to take any risk. I ended up with rehash by Dominik Reichl, which passed the virus scan. The Adler-32 checksums calculated by that tool also matched the above results.
But don’t just take my word for it, do your own tests!