~ubuntu-branches/debian/lenny/fpc/lenny

« back to all changes in this revision

Viewing changes to fpcsrc/packages/base/paszlib/adler.pas

  • Committer: Bazaar Package Importer
  • Author(s): Mazen Neifer, Torsten Werner, Mazen Neifer
  • Date: 2008-05-17 17:12:11 UTC
  • mfrom: (3.1.9 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080517171211-9qi33xhd9evfa0kg
Tags: 2.2.0-dfsg1-9
[ Torsten Werner ]
* Add Mazen Neifer to Uploaders field.

[ Mazen Neifer ]
* Moved FPC sources into a version dependent directory from /usr/share/fpcsrc
  to /usr/share/fpcsrc/${FPCVERSION}. This allow installing more than on FPC
  release.
* Fixed far call issue in compiler preventing building huge binearies.
  (closes: #477743)
* Updated building dependencies, recomennded and suggested packages.
* Moved fppkg to fp-utils as it is just a helper tool and is not required by
  compiler.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
unit adler;
 
2
 
 
3
{
 
4
  adler32.c -- compute the Adler-32 checksum of a data stream
 
5
  Copyright (C) 1995-1998 Mark Adler
 
6
 
 
7
  Pascal tranlastion
 
8
  Copyright (C) 1998 by Jacques Nomssi Nzali
 
9
  For conditions of distribution and use, see copyright notice in readme.txt
 
10
}
 
11
 
 
12
interface
 
13
 
 
14
{$I zconf.inc}
 
15
 
 
16
function adler32(adler : cardinal; buf : Pbyte; len : cardinal) : cardinal;
 
17
 
 
18
{    Update a running Adler-32 checksum with the bytes buf[0..len-1] and
 
19
   return the updated checksum. If buf is NIL, this function returns
 
20
   the required initial value for the checksum.
 
21
   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
 
22
   much faster. Usage example:
 
23
 
 
24
   var
 
25
     adler : cardinal;
 
26
   begin
 
27
     adler := adler32(0, nil, 0);
 
28
 
 
29
     while (read_buffer(buffer, length) <> EOF) do
 
30
       adler := adler32(adler, buffer, length);
 
31
 
 
32
     if (adler <> original_adler) then
 
33
       error();
 
34
   end;
 
35
}
 
36
 
 
37
implementation
 
38
 
 
39
const
 
40
  BASE = cardinal(65521); { largest prime smaller than 65536 }
 
41
  {NMAX = 5552; original code with unsigned 32 bit integer }
 
42
  { NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 }
 
43
  NMAX = 3854;        { code with signed 32 bit integer }
 
44
  { NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^31-1 }
 
45
  { The penalty is the time loss in the extra MOD-calls. }
 
46
 
 
47
 
 
48
{ ========================================================================= }
 
49
 
 
50
function adler32(adler : cardinal; buf : Pbyte; len : cardinal) : cardinal;
 
51
var
 
52
  s1, s2 : cardinal;
 
53
  k : integer;
 
54
begin
 
55
  s1 := adler and $ffff;
 
56
  s2 := (adler shr 16) and $ffff;
 
57
 
 
58
  if not Assigned(buf) then
 
59
  begin
 
60
    adler32 := cardinal(1);
 
61
    exit;
 
62
  end;
 
63
 
 
64
  while (len > 0) do
 
65
  begin
 
66
    if len < NMAX then
 
67
      k := len
 
68
    else
 
69
      k := NMAX;
 
70
    Dec(len, k);
 
71
    {
 
72
    while (k >= 16) do
 
73
    begin
 
74
      DO16(buf);
 
75
      Inc(buf, 16);
 
76
      Dec(k, 16);
 
77
    end;
 
78
    if (k <> 0) then
 
79
    repeat
 
80
      Inc(s1, buf^);
 
81
      Inc(puf);
 
82
      Inc(s2, s1);
 
83
      Dec(k);
 
84
    until (k = 0);
 
85
    }
 
86
    while (k > 0) do
 
87
    begin
 
88
      Inc(s1, buf^);
 
89
      Inc(s2, s1);
 
90
      Inc(buf);
 
91
      Dec(k);
 
92
    end;
 
93
    s1 := s1 mod BASE;
 
94
    s2 := s2 mod BASE;
 
95
  end;
 
96
  adler32 := (s2 shl 16) or s1;
 
97
end;
 
98
 
 
99
{
 
100
#define DO1(buf,i)
 
101
  begin
 
102
    Inc(s1, buf[i]);
 
103
    Inc(s2, s1);
 
104
  end;
 
105
#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
 
106
#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
 
107
#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
 
108
#define DO16(buf)   DO8(buf,0); DO8(buf,8);
 
109
}
 
110
end.
 
111