fpimgcmn.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. Auxiliary routines for image support.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}{$h+}
  12. unit FPImgCmn;
  13. interface
  14. function Swap(This : qword): qword;
  15. function Swap(This : int64): int64;
  16. function Swap(This : Longword): longword;
  17. function Swap(This : integer): integer;
  18. function Swap(This : Word): Word;
  19. function CalculateCRC (var data; alength:integer) : longword;
  20. function CalculateCRC (CRC:longword; var data; alength:integer) : longword;
  21. implementation
  22. uses sysutils;
  23. function Swap(This : Word): Word;
  24. var
  25. Tmp1, Tmp2 : Byte;
  26. AWord : Word;
  27. begin
  28. Tmp1 := This AND $00FF;
  29. Tmp2 := (This AND $FF00) SHR 8;
  30. AWord := Tmp1;
  31. result := (AWord SHL 8) + Tmp2;
  32. end;
  33. function Swap(This : integer): integer;
  34. var r,p : ^longword;
  35. res : integer;
  36. begin
  37. p := @This;
  38. r := @res;
  39. r^ := Swap (p^);
  40. result := res;
  41. end;
  42. function Swap(This : longword): longword;
  43. var
  44. TmpW1 : Word;
  45. TmpB1,
  46. TmpB2 : Byte;
  47. AnInt : longword;
  48. begin
  49. TmpW1 := This AND $0000FFFF;
  50. TmpB1 := TmpW1 AND $00FF;
  51. TmpB2 := (TmpW1 AND $FF00) SHR 8;
  52. AnInt := TmpB1;
  53. AnInt := (AnInt SHL 8) + TmpB2;
  54. TmpW1 := (This AND $FFFF0000) SHR 16;
  55. TmpB1 := TmpW1 AND $00FF;
  56. TmpB2 := (TmpW1 AND $FF00) SHR 8;
  57. TmpW1 := TmpB1;
  58. result := (AnInt SHL 16) + (TmpW1 SHL 8) + TmpB2;
  59. end;
  60. function Swap(This : qword): qword;
  61. var l1, l2 : longword;
  62. res : qword;
  63. begin
  64. l1:=This and $00000000FFFFFFFF;
  65. l2:=(This and $FFFFFFFF00000000) shr 32;
  66. l1:=swap(l1);
  67. l2:=swap(l2);
  68. res:=l1;
  69. Result:=(res shl 32) + l2;
  70. end;
  71. function Swap(This : int64): int64;
  72. var r,p : ^qword;
  73. res : int64;
  74. begin
  75. p := @This;
  76. r := @res;
  77. r^ := Swap (p^);
  78. result := res;
  79. end;
  80. var CRCtable : array[0..255] of longword;
  81. procedure MakeCRCtable;
  82. var c : longword;
  83. r, t : integer;
  84. begin
  85. for r := 0 to 255 do
  86. begin
  87. c := r;
  88. for t := 0 to 7 do
  89. begin
  90. if (c and 1) = 1 then
  91. c := $EDB88320 xor (c shr 1)
  92. else
  93. c := c shr 1
  94. end;
  95. CRCtable[r] := c;
  96. end;
  97. end;
  98. function CalculateCRC (CRC:longword; var data; alength:integer) : longword;
  99. var d : pbyte;
  100. r, t : integer;
  101. begin
  102. d := @data;
  103. result := CRC;
  104. for r := 0 to alength-1 do
  105. begin
  106. t := (byte(result) xor d^);
  107. result := CRCtable[t] xor (result shr 8);
  108. inc (d);
  109. end;
  110. end;
  111. function CalculateCRC (var data; alength:integer) : longword;
  112. var f : longword;
  113. begin
  114. f := CalculateCRC($FFFFFFFF, data, alength);
  115. result := f xor $FFFFFFFF;
  116. end;
  117. initialization
  118. MakeCRCtable;
  119. end.