Module:CRC 32 ISO HDLC: Difference between revisions

From Resonite Wiki
m Added prettyprinted version to be #invoke-d
m yeet that
 
(4 intermediate revisions by 2 users not shown)
Line 14: Line 14:
    
    
   for i=1,#str do
   for i=1,#str do
     state = crc32_update(state, string.byte(str)[i])
     state = crc32_update(state, string.byte(str, i))
   end
   end
    
    
   return bit32.xor(state, 0xffffffff)
   return bit32.bxor(state, 0xffffffff)
end
end


function p.crc32_hex(frame)
function p.crc32_hex(frame)
   local sum = p.crc32(frame.args[0])
  mw.logObject(frame.args[1])
   local sum = p.crc32(frame.args[1])
   return string.format("%X", sum)
   return string.format("%X", sum)
end
end


return p
return p

Latest revision as of 16:15, 9 February 2024

The CRC32 ISO HDLC algorithm is used by FrooxEngine to derive the color of an unknown C# type in ProtoFlux. This is done by computing the CRC of the type's name (without generic parameters, if any).

Invokable Functions
Function Purpose Example
crc32_hex Computes the hexadecimal CRC32 checksum of the given string {{#invoke:CRC_32_ISO_HDLC|crc32_hex|IAsset}}

The following functions are exported, but not invokable by Media Wiki.

Other Functions
Function Purpose Example
crc32 Computes the CRC32 checksum of the given string crc32("IAsset")

Example: crc32("IAsset") = 9CD97842 (check: 9CD97842)


-- CRC_32_ISO_HDLC implementation using a lookup table

local p = {}

local lookup = mw.loadData("Module:CRC_32_ISO_HDLC_LOOKUP")
local bit32 = require("bit32")

function crc32_update(state, bval)
  return bit32.bxor(lookup[bit32.band(bit32.bxor(state, bval), 0xff)], bit32.rshift(state, 8))
end

function p.crc32(str)
  local state = 0xffffffff
  
  for i=1,#str do
    state = crc32_update(state, string.byte(str, i))
  end
  
  return bit32.bxor(state, 0xffffffff)
end

function p.crc32_hex(frame)
  mw.logObject(frame.args[1])
  local sum = p.crc32(frame.args[1])
  return string.format("%X", sum)
end

return p