commit b6964c7943ce45abb8056476e951234911c343aa Author: N0\A Date: Sun Jul 12 00:54:58 2026 +0200 first commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..01072ca --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.v] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9a98968 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +* text=auto eol=lf +*.bat eol=crlf + +*.v linguist-language=V +*.vv linguist-language=V +*.vsh linguist-language=V +v.mod linguist-language=V +.vdocignore linguist-language=ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80f807c --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# Binaries for programs and plugins +main +foferk +*.exe +*.exe~ +*.so +*.dylib +*.dll + +# Ignore binary output folders +bin/ + +# Ignore common editor/system specific metadata +.DS_Store +.idea/ +.vscode/ +*.iml + +# ENV +.env + +# Web assets and local databases +*.db +*.js + +# Ignore installed modules through `v install --local`: +modules/ + +foferk.gz diff --git a/main.v b/main.v new file mode 100644 index 0000000..1d5ce3b --- /dev/null +++ b/main.v @@ -0,0 +1,171 @@ +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) + write_byte(mut b, 0x8b) + write_byte(mut b, 0x08) + write_byte(mut b, 0x00) + write_u32(mut b, 0) + write_byte(mut b, 0x00) + write_byte(mut b, 0x03) +} + +// DEFLATE + +fn write_deflate_header(mut b Bits) { + write_lsb(mut b, 0x01, 1) + write_lsb(mut b, 0x01, 2) +} + +fn write_foferk(mut b Bits) { + write_msb(mut b, 0x96, 8) + write_msb(mut b, 0x9f, 8) + write_msb(mut b, 0x96, 8) + write_msb(mut b, 0x95, 8) + write_msb(mut b, 0xA2, 8) + write_msb(mut b, 0x9b, 8) + write_msb(mut b, 0x50, 8) +} + +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)! +} diff --git a/v.mod b/v.mod new file mode 100644 index 0000000..87f9d05 --- /dev/null +++ b/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'foferk' + description: '' + version: '0.0.0' + license: 'MIT' + dependencies: [] +}