Files
foferk/main.v
T
2026-07-12 10:54:44 +02:00

172 lines
3.4 KiB
V

import os
// BITS
struct Bits {
mut:
bytes []u8
current u8
count u8
}
fn write(mut b Bits, value u8) {
b.current = b.current | ((value & 1) << b.count)
b.count++
if b.count == 8 {
b.bytes << b.current
b.current = 0
b.count = 0
}
}
fn write_lsb(mut b Bits, value u8, length u8) {
for i in 0..length {
write(mut b, (value >> i) & 1)
}
}
fn write_msb(mut b Bits, code u16, length u16) {
mut i := int(length) - 1
for i >= 0 {
write(mut b, u8(code >> i) & 1)
i--
}
}
fn flush(mut b Bits) {
if b.count > 0 {
b.bytes << b.current
b.current = 0
b.count = 0
}
}
fn write_byte(mut b Bits, value u8) {
flush(mut b)
b.bytes << value
}
fn write_u32(mut b Bits, value u32) {
flush(mut b)
write_byte(mut b, u8((value >> 0) & 0xff))
write_byte(mut b, u8((value >> 8) & 0xff))
write_byte(mut b, u8((value >> 16) & 0xff))
write_byte(mut b, u8((value >> 24) & 0xff))
}
// GZIP
fn write_gzip_header(mut b Bits) {
write_byte(mut b, 0x1f) // MAGIC
write_byte(mut b, 0x8b) // BYTES
write_byte(mut b, 0x08) // COMPRESSION (8, DEFLATE)
write_byte(mut b, 0x00) // FLAG (0, NO FIELDS)
write_u32(mut b, 0) // CREATION TIME (FOFERK WAS CREATED AT THE DAWN OF TIME)
write_byte(mut b, 0x00) // XFL (0, SOME SPECIAL COMPRESSION THING, HAS TO BE LIKE THAT)
write_byte(mut b, 0x03) // OS (3, UNIX)
}
// DEFLATE
fn write_deflate_header(mut b Bits) {
write_lsb(mut b, 0x01, 1) // BFINAL (1, LAST BLOCK)
write_lsb(mut b, 0x01, 2) // BTYPE (0, FIXED HUFFMAN TREE)
}
fn write_foferk(mut b Bits) {
write_msb(mut b, 0x96, 8) // 'f' + 30
write_msb(mut b, 0x9f, 8) // 'o' + 30
write_msb(mut b, 0x96, 8) // 'f' + 30
write_msb(mut b, 0x95, 8) // 'e' + 30
write_msb(mut b, 0xA2, 8) // 'r' + 30
write_msb(mut b, 0x9b, 8) // 'k' + 30
write_msb(mut b, 0x50, 8) // ' ' + 30
}
fn write_tokens(mut b Bits, repeat int) {
mut remaining := (repeat - 1) * 7
for remaining >= 252 {
write_msb(mut b, 0xC4, 8)
write_lsb(mut b, 25, 5)
write_msb(mut b, 0x05, 5)
write_lsb(mut b, 0x00, 1)
remaining -= 252
}
for remaining >= 7 {
write_msb(mut b, 0x05, 7)
write_msb(mut b, 0x05, 5)
write_lsb(mut b, 0x00, 1)
remaining -= 7
}
if remaining == 6 {
write_msb(mut b, 0x04, 7)
write_msb(mut b, 0x05, 5)
write_lsb(mut b, 0x00, 1)
} else if remaining == 5 {
write_msb(mut b, 0x03, 7)
write_msb(mut b, 0x05, 5)
write_lsb(mut b, 0x00, 1)
} else if remaining == 4 {
write_msb(mut b, 0x02, 7)
write_msb(mut b, 0x05, 5)
write_lsb(mut b, 0x00, 1)
} else if remaining == 3 {
write_msb(mut b, 0x01, 7)
write_msb(mut b, 0x05, 5)
write_lsb(mut b, 0x00, 1)
}
}
fn write_footer(mut b Bits, repeat int) {
write_msb(mut b, 0x00, 7)
flush(mut b)
write_u32(mut b, crc32(repeat))
write_u32(mut b, u32(repeat * 7))
}
// CRC32
fn crc32(repeat int) u32 {
mut table := []u32{len: 256}
for i in 0..256 {
mut c := u32(i)
for _ in 0..8 {
if (c & 1) != 0 {
c = 0xEDB88320 ^ (c >> 1)
} else {
c = c >> 1
}
}
table[i] = c
}
mut crc := u32(0xffffffff)
for _ in 0..repeat {
for byte in "foferk ".bytes() {
crc = table[(crc ^ u32(byte)) & 0xFF] ^ (crc >> 8)
}
}
return crc ^ 0xffffffff
}
fn main() {
repeat := 1_000_000
mut b := Bits {
bytes: []u8{}
current: 0
count: 0
}
write_gzip_header(mut b)
write_deflate_header(mut b)
write_foferk(mut b)
write_tokens(mut b, repeat)
write_footer(mut b, repeat)
os.write_bytes("foferk.gz", b.bytes)!
}