Skip to content

Instantly share code, notes, and snippets.

@notcancername
notcancername / gist:5f17a6a6ec7a587b1df27edef506fef2
Created September 20, 2024 17:59
random bit generator in c
struct random_bit_generator {
uint32_t word;
uint8_t bits_remaining;
};
static bool next_bit(struct random_bit_generator *rbg) {
if (rbg->bits_remaining == 0) {
rbg->word = arc4random();
rbg->bits_remaining = 32;
}
@notcancername
notcancername / passgen.zig
Last active August 28, 2024 17:22
passphrase generator, with adjustable entropy
const std = @import("std");
const delimiter = '\n';
const default_bits = 64;
const Wordlist = struct {
bytes: std.ArrayListUnmanaged(u8),
word_indices: std.ArrayListUnmanaged(usize),
allocator: std.mem.Allocator,
@notcancername
notcancername / size_report.zig
Created August 5, 2024 20:11
zig function to report on the size of containers
const std = @import("std");
pub fn sizeReport(comptime T: type, comptime indent: []const u8) []const u8 {
comptime {
var result: []const u8 = "";
result = result ++ std.fmt.comptimePrint("{s}size of {s} is {d} bytes ({d} bits)\n", .{indent, @typeName(T), @sizeOf(T), @bitSizeOf(T)});
switch (@typeInfo(T)) {
.Struct => |s| {
result = result ++ std.fmt.comptimePrint("{s}field report ({s}):\n", .{indent, @tagName(s.layout)});
for (s.fields) |field| {
@notcancername
notcancername / rc4.zig
Created July 29, 2024 14:28
random RC4 implementation I wrote at some point
pub const Rc4 = struct {
s: [256]u8 = undefined,
i: u8 = 0,
j: u8 = 0,
pub fn init(key: []const u8) Rc4 {
var self = Rc4{};
for(&self.s, 0..) |*c, i| c.* = @intCast(i);
@notcancername
notcancername / subnet_to_range.zig
Created March 11, 2024 18:25
Convert a /N subnet to an IP range.
const std = @import("std");
pub fn main() !void {
var rd_state = std.io.bufferedReader(std.io.getStdIn().reader());
const rd = rd_state.reader();
var wr_state = std.io.bufferedWriter(std.io.getStdOut().writer());
const wr = wr_state.writer();
var buf: [64]u8 = undefined;
@notcancername
notcancername / sse.zig
Last active March 3, 2024 00:34
zig server-sent events parser
const std = @import("std");
pub const Event = struct {
event: ?[]const u8,
data: ?std.ArrayListUnmanaged(u8),
id: ?[]const u8,
retry: ?u64,
pub fn deinit(event: Event, ally: std.mem.Allocator) void {
inline for (.{ event.event, event.id }) |mx| if (mx) |x| ally.free(x);
@notcancername
notcancername / free_list_allocator.zig
Created January 29, 2024 00:48
A simple allocator wrapper with a free list, sort of, to reuse allocations.
// SPDX-License-Identifier: 0BSD
// Copyright (C) 2024 by cancername <notcancername@protonmail.com>
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without
// fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
// SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
@notcancername
notcancername / bench_add_inline.zig
Last active January 7, 2024 03:41
Zig add vs add inline vs @addWithOverflow
const std = @import("std");
extern fn not_inlined(ptr: [*]usize, len: usize) usize;
extern fn inlined(ptr: [*]usize, len: usize) usize;
extern fn builtin(ptr: [*]usize, len: usize) usize;
extern fn builtin_no_destructure(ptr: [*]usize, len: usize) usize;
extern fn sum_not_inlined(ptr: [*]usize, len: usize) usize;
extern fn sum_inlined(ptr: [*]usize, len: usize) usize;
extern fn sum_builtin(ptr: [*]usize, len: usize) usize;
extern fn sum_builtin_no_destructure(ptr: [*]usize, len: usize) usize;
const std = @import("std");
fn splice(src_rd: anytype, dst_wr: anytype, comptime buf_len: usize, lim: usize) !void {
var buf: [buf_len]u8 = undefined;
var left = lim;
while (true) {
const len = try src_rd.read(buf[0..@min(left, buf.len)]);
if (len == 0) break;
left = try std.math.sub(usize, left, len);
try dst_wr.writeAll(buf[0..len]);
const std = @import("std");
const io = std.io;
const math = std.math;
// TODO: this is a massive hack that relies on implementation details
// and **WILL** break when BufferedReader or SeekableStream change,
// however, I do not see any other way, so this is fine for now.
/// An amalgam of `std.io.BufferedReader` and `std.io.SeekableStream`, such that they work together.
///