Something went wrong. Try again.
A FLAC encoder and decoder for Zig, written against RFC 9639, for native FLAC streams and FLAC in Ogg
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The two cyclic redundancy checks a FLAC stream carries.//!//! Both are catalogued CRCs that the standard library already generates, so//! this file is mostly a place to record *which* ones they are and to hold the//! tests that pin them to values printed in the specification.
const std = @import("std");
/// The frame header check: 8 bits, polynomial x^8 + x^2 + x^1 + x^0, initial/// value 0, no reflection and no final xor. It covers the whole frame header/// including the sync code, and is the last thing in the header (RFC 9639,/// Section 9.1.8). The catalogued name for these parameters is CRC-8/SMBUS.pub const Crc8 = std.hash.crc.@"CRC-8/SMBUS";
/// The frame footer check: 16 bits, polynomial x^16 + x^15 + x^2 + x^0,/// initial value 0, no reflection and no final xor. It covers the whole frame/// including the sync code but excluding the check itself, which follows the/// padding that byte-aligns the last subframe (RFC 9639, Section 9.3). The/// catalogued name for these parameters is CRC-16/UMTS.pub const Crc16 = std.hash.crc.@"CRC-16/UMTS";
test "frame header CRC of RFC 9639 example 1" { // The frame at 0x2a of the first example file, whose ten header bytes the // specification breaks down field by field, ends with a check of 0xbf // (RFC 9639, Appendix D.1.4, Table 29). var crc: Crc8 = .init(); crc.update(&.{ 0xff, 0xf8, 0x69, 0x18, 0x00, 0x00 }); try std.testing.expectEqual(@as(u8, 0xbf), crc.final());}
test "frame header CRC of RFC 9639 example 3" { // The mono 8-bit example's header, which differs from the one above in // every field that is not the sync code, checks to 0xe9 (RFC 9639, // Appendix D.3.4). var crc: Crc8 = .init(); crc.update(&.{ 0xff, 0xf8, 0x68, 0x02, 0x00, 0x17 }); try std.testing.expectEqual(@as(u8, 0xe9), crc.final());}
test "frame footer CRC of RFC 9639 example 1" { // The whole frame, from the sync code at 0x2a up to but not including the // two check bytes at 0x38, checks to 0xaa9a. var crc: Crc16 = .init(); crc.update(&.{ 0xff, 0xf8, 0x69, 0x18, 0x00, 0x00, 0xbf, 0x03, 0x58, 0xfd, 0x03, 0x12, 0x8b, }); try std.testing.expectEqual(@as(u16, 0xaa9a), crc.final());}