phasm/examples/crc32.html
2025-06-05 19:49:07 +02:00

70 lines
2.6 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Examples - CRC32</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<h1>Examples - CRC32</h1>
<div class="menu">
<a href="index.html">List</a> - <a href="crc32.py.html">Source</a> - <a href="crc32.wat.html">WebAssembly</a><br />
</div>
<div class="description">
<p>
This example shows a fold implementation of a <a href="https://en.wikipedia.org/wiki/Cyclic_redundancy_check">cyclic redundancy check</a>.
There are actually many variations of these CRCs - this one is specifically know as CRC-32/ISO-HDLC.
</p>
</div>
<h3>Try it out!</h3>
<div class="example">
<textarea id="example-data" style="width: 75%; height: 4em;">The quick brown fox jumps over the lazy dog</textarea><br />
<button type="click" id="example-click" disabled>Calculate</button>
<input type="text" id="example-crc32" />
</div>
<div class="example-list">
<ul>
<li><a href="#" data-n="123456789">crc32("123456789")</a> = cbf43926</li>
<li><a href="#" data-n="Hello, world!">crc32("Hello, world!")</a> = ebe6c6e6</li>
<li><a href="#" data-n="The quick brown fox jumps over the lazy dog">crc32("The quick brown fox jumps over the lazy dog")</a> = 414fa339</li>
<li><a href="#" data-n="CRC-32/ISO-HDLC
Also referred to as ISO 3309, ITU-T V.42, CRC-32-IEEE, and many other names.
The CRC of ASCII &quot;123456789&quot; is 0xcbf43926.
Examples of formats that use CRC-32/ISO-HDLC: ZIP, PNG, Gzip, ARJ.">crc32("CRC-32/ISO-HDLC...")</a> = 126afcf</li>
</ul>
</div>
<!-- We'll need to use some interface glue - WebAssembly doesn't let us pass strings directly. -->
<script type="text/javascript" src="./include.js"></script>
<script>
let importObject = {};
let exampleN = document.querySelector('#example-data');
let exampleClick = document.querySelector('#example-click');
let exampleCrc32 = document.querySelector('#example-crc32');
WebAssembly.instantiateStreaming(fetch('crc32.wasm'), importObject)
.then(app => {
exampleClick.addEventListener('click', event => {
let in_put = exampleN.value;
let in_put_addr = alloc_bytes(app, in_put);
let result = app.instance.exports.crc32(in_put_addr);
exampleCrc32.value = i32_to_u32(result).toString(16);
});
exampleClick.removeAttribute('disabled');
});
for(let exmpl of document.querySelectorAll('a[data-n]') ) {
exmpl.addEventListener('click', event => {
exampleN.value = exmpl.getAttribute('data-n');
exampleClick.click();
});
}
</script>
</body>
</html>