<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.juanweixianshe.com/index.php?action=history&amp;feed=atom&amp;title=%E6%A8%A1%E5%9D%97%3ACrc32lua</id>
	<title>模块:Crc32lua - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.juanweixianshe.com/index.php?action=history&amp;feed=atom&amp;title=%E6%A8%A1%E5%9D%97%3ACrc32lua"/>
	<link rel="alternate" type="text/html" href="https://wiki.juanweixianshe.com/index.php?title=%E6%A8%A1%E5%9D%97:Crc32lua&amp;action=history"/>
	<updated>2026-04-11T16:38:57Z</updated>
	<subtitle>本wiki的该页面的版本历史</subtitle>
	<generator>MediaWiki 1.36.1</generator>
	<entry>
		<id>https://wiki.juanweixianshe.com/index.php?title=%E6%A8%A1%E5%9D%97:Crc32lua&amp;diff=76&amp;oldid=prev</id>
		<title>黯狐：导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://wiki.juanweixianshe.com/index.php?title=%E6%A8%A1%E5%9D%97:Crc32lua&amp;diff=76&amp;oldid=prev"/>
		<updated>2021-08-04T08:54:08Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--[[&lt;br /&gt;
&lt;br /&gt;
LUA MODULE&lt;br /&gt;
&lt;br /&gt;
  digest.crc32 - CRC-32 checksum implemented entirely in Lua.&lt;br /&gt;
&lt;br /&gt;
SYNOPSIS&lt;br /&gt;
&lt;br /&gt;
  local CRC = require 'digest.crc32lua'&lt;br /&gt;
  print(CRC.crc32 'test') --&amp;gt; 0xD87F7E0C or -662733300&lt;br /&gt;
  &lt;br /&gt;
  assert(CRC.crc32('st', CRC.crc32('te')) == CRC.crc32 'test')&lt;br /&gt;
  &lt;br /&gt;
DESCRIPTION&lt;br /&gt;
&lt;br /&gt;
  This can be used to compute CRC-32 checksums on strings.&lt;br /&gt;
  This is similar to [1-2].&lt;br /&gt;
&lt;br /&gt;
API&lt;br /&gt;
&lt;br /&gt;
  Note: in the functions below, checksums are 32-bit integers stored in&lt;br /&gt;
  numbers.  The number format currently depends on the bit&lt;br /&gt;
  implementation--see DESIGN NOTES below.&lt;br /&gt;
&lt;br /&gt;
  CRC.crc32_byte(byte [, crc]) --&amp;gt; rcrc&lt;br /&gt;
  &lt;br /&gt;
    Returns CRC-32 checksum `rcrc` of byte `byte` (number 0..255) appended to&lt;br /&gt;
    a string with CRC-32 checksum `crc`.  `crc` defaults to 0 (empty string)&lt;br /&gt;
    if omitted.&lt;br /&gt;
&lt;br /&gt;
  CRC.crc32_string(s, crc) --&amp;gt; bcrc&lt;br /&gt;
&lt;br /&gt;
    Returns CRC-32 checksum `rcrc` of string `s` appended to&lt;br /&gt;
    a string with CRC-32 checksum `crc`.  `crc` defaults to 0 (empty string)&lt;br /&gt;
    if omitted.&lt;br /&gt;
  &lt;br /&gt;
  CRC.crc32(o, crc) --&amp;gt; bcrc&lt;br /&gt;
&lt;br /&gt;
    This invokes `crc32_byte` if `o` is a byte or `crc32_string` if `o`&lt;br /&gt;
    is a string.&lt;br /&gt;
  &lt;br /&gt;
  CRC.bit&lt;br /&gt;
&lt;br /&gt;
    This contains the underlying bit library used by the module.  It&lt;br /&gt;
    should be considered a read-only copy.&lt;br /&gt;
&lt;br /&gt;
DESIGN NOTES&lt;br /&gt;
&lt;br /&gt;
  Currently, this module exposes the underlying bit array implementation in CRC&lt;br /&gt;
  checksums returned.  In BitOp, bit arrays are 32-bit signed integer numbers&lt;br /&gt;
  (may be negative).  In Lua 5.2 'bit32' and 'bit.numberlua', bit arrays are&lt;br /&gt;
  32-bit unsigned integer numbers (non-negative).  This is subject to change&lt;br /&gt;
  in the future but is currently done due to (unconfirmed) performance&lt;br /&gt;
  implications.&lt;br /&gt;
  &lt;br /&gt;
  On platforms with 64-bit numbers, one way to normalize CRC&lt;br /&gt;
  checksums to be unsigned is to do `crcvalue % 2^32`,&lt;br /&gt;
  &lt;br /&gt;
  The name of this module is inspired by Perl `Digest::CRC*`.&lt;br /&gt;
&lt;br /&gt;
DEPENDENCIES&lt;br /&gt;
 &lt;br /&gt;
  Requires one of the following bit libraries:&lt;br /&gt;
&lt;br /&gt;
    BitOp &amp;quot;bit&amp;quot; -- bitop.luajit.org -- This is included in LuaJIT and also available&lt;br /&gt;
      for Lua 5.1/5.2.  This provides the fastest performance in LuaJIT.&lt;br /&gt;
    Lua 5.2 &amp;quot;bit32&amp;quot; -- www.lua.org/manual/5.2 -- This is provided in Lua 5.2&lt;br /&gt;
      and is preferred in 5.2 (unless &amp;quot;bit&amp;quot; also happens to be installed).&lt;br /&gt;
    &amp;quot;bit.numberlua&amp;quot; (&amp;gt;=000.003) -- https://github.com/davidm/lua-bit-numberlua&lt;br /&gt;
      This is slowest and used as a last resort.&lt;br /&gt;
      It is only a few times slower than &amp;quot;bit32&amp;quot; though.&lt;br /&gt;
&lt;br /&gt;
DOWNLOAD/INSTALLATION&lt;br /&gt;
&lt;br /&gt;
  If using LuaRocks:&lt;br /&gt;
    luarocks install lua-digest-crc32lua&lt;br /&gt;
&lt;br /&gt;
  Otherwise, download &amp;lt;https://github.com/davidm/lua-digest-crc32lua/zipball/master&amp;gt;.&lt;br /&gt;
  Alternately, if using git:&lt;br /&gt;
    git clone git://github.com/davidm/lua-digest-crc32lua.git&lt;br /&gt;
    cd lua-digest-crc32lua&lt;br /&gt;
  Optionally unpack:&lt;br /&gt;
    ./util.mk&lt;br /&gt;
  or unpack and install in LuaRocks:&lt;br /&gt;
    ./util.mk install &lt;br /&gt;
  &lt;br /&gt;
REFERENCES&lt;br /&gt;
&lt;br /&gt;
  [1] http://www.axlradius.com/freestuff/CRC32.java&lt;br /&gt;
  [2] http://www.gamedev.net/reference/articles/article1941.asp&lt;br /&gt;
  [3] http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/CRC32.html&lt;br /&gt;
  [4] http://www.dsource.org/projects/tango/docs/current/tango.io.digest.Crc32.html&lt;br /&gt;
  [5] http://pydoc.org/1.5.2/zlib.html#-crc32&lt;br /&gt;
  [6] http://www.python.org/doc/2.5.2/lib/module-binascii.html&lt;br /&gt;
 &lt;br /&gt;
LICENSE&lt;br /&gt;
&lt;br /&gt;
  (c) 2008-2011 David Manura.  Licensed under the same terms as Lua (MIT).&lt;br /&gt;
&lt;br /&gt;
  Permission is hereby granted, free of charge, to any person obtaining a copy&lt;br /&gt;
  of this software and associated documentation files (the &amp;quot;Software&amp;quot;), to deal&lt;br /&gt;
  in the Software without restriction, including without limitation the rights&lt;br /&gt;
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&lt;br /&gt;
  copies of the Software, and to permit persons to whom the Software is&lt;br /&gt;
  furnished to do so, subject to the following conditions:&lt;br /&gt;
&lt;br /&gt;
  The above copyright notice and this permission notice shall be included in&lt;br /&gt;
  all copies or substantial portions of the Software.&lt;br /&gt;
&lt;br /&gt;
  THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&lt;br /&gt;
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&lt;br /&gt;
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE&lt;br /&gt;
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&lt;br /&gt;
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&lt;br /&gt;
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN&lt;br /&gt;
  THE SOFTWARE.&lt;br /&gt;
  (end license)&lt;br /&gt;
 &lt;br /&gt;
--]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local M = {_TYPE='module', _NAME='digest.crc32', _VERSION='0.3.20111128'}&lt;br /&gt;
&lt;br /&gt;
local type = type&lt;br /&gt;
local require = require&lt;br /&gt;
local setmetatable = setmetatable&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
 Requires the first module listed that exists, else raises like `require`.&lt;br /&gt;
 If a non-string is encountered, it is returned.&lt;br /&gt;
 Second return value is module name loaded (or '').&lt;br /&gt;
 --]]&lt;br /&gt;
local function requireany(...)&lt;br /&gt;
  local errs = {}&lt;br /&gt;
  for _,name in ipairs{...} do&lt;br /&gt;
    if type(name) ~= 'string' then return name, '' end&lt;br /&gt;
    local ok, mod = pcall(require, name)&lt;br /&gt;
    if ok then return mod, name end&lt;br /&gt;
    errs[#errs+1] = mod&lt;br /&gt;
  end&lt;br /&gt;
  error(table.concat(errs, '\n'), 2)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local bit, name_ = requireany('bit32', 'bit', 'bit.numberlua')&lt;br /&gt;
local bxor = bit.bxor&lt;br /&gt;
local bnot = bit.bnot&lt;br /&gt;
local band = bit.band&lt;br /&gt;
local rshift = bit.rshift&lt;br /&gt;
&lt;br /&gt;
-- CRC-32-IEEE 802.3 (V.42)&lt;br /&gt;
local POLY = 0xEDB88320&lt;br /&gt;
&lt;br /&gt;
-- Memoize function pattern (like http://lua-users.org/wiki/FuncTables ).&lt;br /&gt;
local function memoize(f)&lt;br /&gt;
  local mt = {}&lt;br /&gt;
  local t = setmetatable({}, mt)&lt;br /&gt;
  function mt:__index(k)&lt;br /&gt;
    local v = f(k); t[k] = v&lt;br /&gt;
    return v&lt;br /&gt;
  end&lt;br /&gt;
  return t&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- CRC table.&lt;br /&gt;
local crc_table = memoize(function(i)&lt;br /&gt;
  local crc = i&lt;br /&gt;
  for _=1,8 do&lt;br /&gt;
    local b = band(crc, 1)&lt;br /&gt;
    crc = rshift(crc, 1)&lt;br /&gt;
    if b == 1 then crc = bxor(crc, POLY) end&lt;br /&gt;
  end&lt;br /&gt;
  return crc&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function M.crc32_byte(byte, crc)&lt;br /&gt;
  crc = bnot(crc or 0)&lt;br /&gt;
  local v1 = rshift(crc, 8)&lt;br /&gt;
  local v2 = crc_table[bxor(crc % 256, byte)]&lt;br /&gt;
  return bnot(bxor(v1, v2))&lt;br /&gt;
end&lt;br /&gt;
local M_crc32_byte = M.crc32_byte&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function M.crc32_string(s, crc)&lt;br /&gt;
  crc = crc or 0&lt;br /&gt;
  for i=1,#s do&lt;br /&gt;
    crc = M_crc32_byte(s:byte(i), crc)&lt;br /&gt;
  end&lt;br /&gt;
  return crc&lt;br /&gt;
end&lt;br /&gt;
local M_crc32_string = M.crc32_string&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function M.crc32(s, crc)&lt;br /&gt;
  if type(s) == 'string' then&lt;br /&gt;
    return M_crc32_string(s, crc)&lt;br /&gt;
  else&lt;br /&gt;
    return M_crc32_byte(s, crc)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
M.bit = bit  -- bit library used&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return M&lt;/div&gt;</summary>
		<author><name>黯狐</name></author>
	</entry>
</feed>