Cksum.pm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package String::CRC::Cksum;
  2. #use 5.6.1;
  3. use strict;
  4. use warnings;
  5. use Carp;
  6. require Exporter;
  7. our @ISA = qw(Exporter);
  8. our @EXPORT_OK = qw(cksum);
  9. our @EXPORT = qw();
  10. our $VERSION = '0.03';
  11. use fields qw(cksum size);
  12. my @crctab = (
  13. 0x00000000,
  14. 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6,
  15. 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac,
  16. 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a,
  17. 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58,
  18. 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, 0xd4326d90, 0xd0f37027, 0xddb056fe,
  19. 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4,
  20. 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5,
  21. 0x2ac12072, 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, 0x7897ab07,
  22. 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1,
  23. 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b,
  24. 0xbb60adfc, 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d,
  25. 0x94ea7b2a, 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 0xc6bcf05f,
  26. 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80,
  27. 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a,
  28. 0x58c1663d, 0x558240e4, 0x51435d53, 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c,
  29. 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e,
  30. 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, 0xd727bbb6, 0xd3e6a601, 0xdea580d8,
  31. 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2,
  32. 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74,
  33. 0x857130c3, 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 0x7b827d21,
  34. 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, 0x18197087,
  35. 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d,
  36. 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb,
  37. 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 0x89b8fd09,
  38. 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf,
  39. 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  40. );
  41. sub new {
  42. my $class = shift;
  43. my String::CRC::Cksum $self = fields::new(ref $class || $class);
  44. return $self->reset;
  45. } # new
  46. sub reset {
  47. my String::CRC::Cksum $self = shift;
  48. $self->{cksum} = $self->{size} = 0;
  49. return $self;
  50. } # reset
  51. sub add {
  52. use integer;
  53. my String::CRC::Cksum $self = shift;
  54. my $cksum = $self->{cksum};
  55. my $size = $self->{size};
  56. while(@_) {
  57. my $n = length $_[0];
  58. for(my $i = 0; $i < $n; ++$i) {
  59. my $c = unpack 'C', substr $_[0], $i, 1;
  60. $cksum = ($cksum << 8) ^ $crctab[($cksum >> 24) ^ $c];
  61. ++$size;
  62. }
  63. }
  64. continue { shift }
  65. $self->{cksum} = $cksum;
  66. $self->{size} = $size;
  67. return $self;
  68. } # add
  69. sub addfile {
  70. my String::CRC::Cksum $self = shift;
  71. my $stat;
  72. local $_;
  73. while(my $ifd = shift) {
  74. $self->add($_) while $stat = read $ifd, $_, 4096;
  75. if(! defined $stat) {
  76. croak "error reading from filehandle: $!";
  77. }
  78. }
  79. return $self;
  80. } # addfile
  81. sub peek {
  82. use integer;
  83. my String::CRC::Cksum $self = shift;
  84. my $cksum = $self->{cksum};
  85. my $size = $self->{size};
  86. # Extend with the length of the data
  87. while($size != 0) {
  88. my $c = $size & 0377;
  89. $size >>= 8;
  90. $cksum = ($cksum << 8) ^ $crctab[($cksum >> 24) ^ $c];
  91. }
  92. $cksum = ~ $cksum;
  93. no integer;
  94. my $crc = $cksum;
  95. $crc += 4294967296 if $crc < 0;
  96. return wantarray ? ($crc, $self->{size}) : $crc;
  97. } # addfile
  98. sub result {
  99. my String::CRC::Cksum $self = shift;
  100. my ($cksum, $size) = $self->peek;
  101. $self->reset;
  102. return wantarray ? ($cksum, $size) : $cksum;
  103. } # result
  104. sub cksum(@) {
  105. my $sum = String::CRC::Cksum->new;
  106. while(@_) {
  107. if(ref $_[0])
  108. { $sum->addfile($_[0]) }
  109. else
  110. { $sum->add($_[0]) }
  111. }
  112. continue { shift }
  113. return $sum->result;
  114. } # cksum
  115. 1;
  116. __END__
  117. =head1 NAME
  118. String::CRC::Cksum - Perl extension for calculating checksums
  119. in a manner compatible with the POSIX cksum program.
  120. =head1 SYNOPSIS
  121. B<OO style>:
  122. use String::CRC::Cksum;
  123. $cksum = String::CRC::Cksum->new;
  124. $cksum1 = $cksum->new; # clone (clone is reset)
  125. $cksum->add("string1");
  126. $cksum->add("string2");
  127. $cksum->add("string3", "string4", "string5", ...);
  128. ...
  129. ($cksum, $size) = $cksum->peek;
  130. $cksum->add("string6", ...);
  131. ...
  132. ($cksum, $size) = $cksum->result;
  133. $cksum1->addfile(\*file1); # note: adding many files
  134. $cksum1->addfile(\*file2); # is probably a silly thing
  135. $cksum1->addfile(\*file3); # to do, but you *could*...
  136. ...
  137. B<Functional style>:
  138. use String::CRC::Cksum qw(cksum);
  139. $cksum = cksum("string1", "string2", ...);
  140. ($cksum, $size) = cksum("string1", "string2", ...);
  141. $cksum = cksum(\*FILE);
  142. ($cksum, $size) = cksum(\*FILE);
  143. =head1 DESCRIPTION
  144. The String::CRC::Cksum module calculates a 32 bit CRC,
  145. generating the same CRC value as the POSIX cksum program.
  146. If called in a list context, returns the length of the data
  147. object as well, which is useful for fully emulating
  148. the cksum program. The returned checksum will always be
  149. a non-negative integral number in the range 0..2^32-1.
  150. Despite its name, this module is able to compute the
  151. checksum of files as well as of strings.
  152. Just pass in a reference to a filehandle,
  153. or a reference to any object that can respond to
  154. a read() call and eventually return 0 at "end of file".
  155. Beware: consider proper use of binmode()
  156. if you are on a non-UNIX platform
  157. or processing files derived from other platforms.
  158. The object oriented interface can be used
  159. to progressively add data into the checksum
  160. before yielding the result.
  161. The functional interface is a convenient way
  162. to get a checksum of a single data item.
  163. None of the routines make local copies of passed-in strings
  164. so you can safely Cksum large strings safe in the knowledge
  165. that there won't be any memory issues.
  166. Passing in multiple files is acceptable,
  167. but perhaps of questionable value.
  168. However I don't want to hamper your creativity...
  169. =head1 FUNCTIONS
  170. The following functions are provided
  171. by the "String::CRC::Cksum" module.
  172. None of these functions are exported by default.
  173. =over 4
  174. =item B<new()>
  175. Creates a new String::CRC::Cksum object
  176. which is in a reset state, ready for action.
  177. If passed an existing String::CRC::Cksum object,
  178. it takes only the class -
  179. ie yields a fresh, reset object.
  180. =item B<reset()>
  181. Resets the Cksum object to the intialized state.
  182. An interesting phenomenom is,
  183. the CRC is not zero but 0xFFFFFFFF
  184. for a reset Cksum object.
  185. The returned size of a reset item will be zero.
  186. =item B<add("string", ...)>
  187. Progressively inject data into the Cksum object
  188. prior to requesting the final result.
  189. =item B<addfile(\*FILE, ...)>
  190. Progressively inject all (remaining) data from the file
  191. into the Cksum object prior to requesting the final result.
  192. The file handle passed in
  193. need only respond to the read() function to be usable,
  194. so feel free to pass in IO handles as needed.
  195. [hmmm - methinks I should have a test for that]
  196. =item B<peek($)>
  197. Yields the CRC checksum
  198. (and optionally the total size in list context)
  199. but does not reset the Cksum object.
  200. Repeated calls to peek() may be made
  201. and more data may be added.
  202. =item B<result($)>
  203. Yields the CRC checksum
  204. (and optionally the total size in list context)
  205. and then resets the Cksum object.
  206. =item B<cksum(@)>
  207. A convenient functional interface
  208. that may be passed a list of strings and filehandles.
  209. It will instantiate a Cksum object,
  210. apply the data and return the result
  211. in one swift, sweet operation.
  212. See how much I'm looking after you?
  213. NOTE: the filehandles must be passed as \*FD
  214. because I'm detecting a file handle using the ref() function.
  215. Therefore any blessed IO handle will also satisfy ref()
  216. and be interpreted as a file handle.
  217. =back
  218. =head2 EXPORT
  219. None by default.
  220. =head1 SEE ALSO
  221. manpages: cksum(1) or cksum(C) depending on your flavour of UNIX.
  222. http://www.opengroup.org/onlinepubs/007904975/utilities/cksum.html
  223. =head1 AUTHOR
  224. Andrew Hamm, E<lt>ahamm@cpan.orgE<gt>.
  225. =head1 COPYRIGHT AND LICENSE
  226. Copyright disclaimed 2003 by Andrew Hamm
  227. This library is free software; you can redistribute it and/or modify
  228. it under the same terms as Perl itself.
  229. Since I collected the algorithm
  230. from the Open Group web pages,
  231. they might have some issues but I doubt it.
  232. Let better legal minds than mine
  233. determine the issues if you need.
  234. [hopefully the CPAN and PAUSE administrators and/or testers
  235. will understand the issues better,
  236. and will replace this entire section
  237. with something reasonable - hint hint.]
  238. =cut