[fpc-pascal] LZMA Algorithm fails on 64bit

Jonas Maebe jonas.maebe at elis.ugent.be
Sat Jan 23 14:19:23 CET 2010


> Does anyone know why the code fails to work?

Not really, but at least the following is will be evaluated differently on 32 and 64 bit platforms:

> 
> ###################################################
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> CodeSequence [-1-]:
> -----
> 
> var inStream:TBufferedFS;
>    outStream:TBufferedFS;
>    decoder: TLZMADecoder;
>    properties:array[0..4] of byte;
>    filesize,outSize:Int64;
>    i: Integer;
>    v: byte;
> const propertiessize=5;
> begin
> if not FileExists(ipkfile) then Exception.Create('IPK file does not
> exists!');
> 
> try
>  inStream:=TBufferedFS.Create(ipkfile, fmOpenRead or fmShareDenyNone);
> try
>  outStream:=TBufferedFS.Create(workdir+'ipktar.tar', fmCreate);
> 
>  decoder:=TLZMADecoder.Create;
>  inStream.position:=0;
>    with decoder do
>    begin
>      if inStream.read(properties, propertiesSize) <> propertiesSize then
>       raise Exception.Create('input .lzma file is too short');
>      if not SetDecoderProperties(properties) then
>       raise Exception.Create('Incorrect stream properties');
> 
>      outSize := 0;
>      for i := 0 to 7 do
>      begin
>       v := inStream.ReadByte;
>       if v < 0 then
>        raise Exception.Create('Can''t read stream size');
>       outSize := outSize or v shl (8 * i);

v is a byte, i is a longint (integer in Delphi mode). Outsize is an int64.

On a 32 bit platform, "v shl (8 * i)" will be evaluated as 32 bit. This means that the upper 32 bits of the result of that expression will be discarded before the "or" operation is performed. On a 64 bit platform, that entire expression will be evaluated using 64 bit arithmetic.


Jonas


More information about the fpc-pascal mailing list