Skip to content

Instantly share code, notes, and snippets.

View jamen's full-sized avatar
🕴️
veloping

Jamen Marz jamen

🕴️
veloping
View GitHub Profile
@jamen
jamen / hme_int.rs
Created December 21, 2022 15:57
Simple homomorphic encrypted integer idea
use rand_core::RngCore;
pub struct Key(i64);
pub struct Secret(i64);
impl Secret {
pub fn new<R: RngCore>(rng: &mut R, value: i64) -> (Key, Self) {
let key = rng.next_u64() as i64;
(Key(key), Self(key.wrapping_add(value)))
@jamen
jamen / config
Last active November 30, 2022 08:29
Sway workspace helper
workspace 1a output $left_display
workspace 2a output $left_display
workspace 3a output $left_display
workspace 4a output $left_display
workspace 5a output $left_display
workspace 6a output $left_display
workspace 7a output $left_display
workspace 8a output $left_display
workspace 9a output $left_display
use core::slice;
use std::io::{stdout, Write};
use std::ptr::null;
use x11_dl::xlib;
use libvips::{
ops::{pngsave_buffer, BandFormat},
VipsImage,
};
@jamen
jamen / lib.rs
Created January 12, 2022 02:22
Semi-cursed macro that serves embedded files as warp filters.
macro_rules! include_file_as_resp {
($file:expr) => {{
use headers::HeaderMapExt;
let body = include_bytes!($file).as_slice();
let mut resp = warp::reply::Response::new(body.into());
let mime = mime_guess::from_path($file).first_or_octet_stream();
resp.headers_mut().typed_insert(headers::ContentLength(body.len() as u64));
resp.headers_mut().typed_insert(headers::ContentType::from(mime));
resp.headers_mut().typed_insert(headers::AcceptRanges::bytes());
resp
@jamen
jamen / lib.rs
Last active May 5, 2020 23:27
Count the digits of an integer in any base efficiently.
// We can use log2(x) = size_of(x) - 1 - clz(x).
// And then do a base conversion log_b(x) = log2(x) / log2(b).
// Then log_b(x) + 1 is the number of digits in base b for x > 0.
fn count_digits(x: usize, base: u8) -> usize {
1 + (8 * mem::size_of::<usize>() - 1 - x.leading_zeros() as usize) / (7 - base.leading_zeros() as usize)
}
// Say we know we're counting in base 10 or base 16. We can calculate the divisor.
fn count_base10_digits(x: usize) -> usize {
1 + (8 * mem::size_of::<usize>() - 1 - x.leading_zeros() as usize) / 3
@jamen
jamen / main.rs
Created December 24, 2019 15:32
Binary HTML encoder
use std::default::Default;
use std::io;
use html5ever::tendril::*;
use html5ever::tokenizer::BufferQueue;
use html5ever::tokenizer::{CharacterTokens, EndTag, NullCharacterToken, StartTag, TagToken, DoctypeToken, CommentToken, EOFToken};
use html5ever::tokenizer::{ParseError, Token, TokenSink, TokenSinkResult, Tokenizer, TokenizerOpts};
use std::io::Write;
@jamen
jamen / readme.md
Last active May 30, 2019 10:45
Use MSYS2 inside VSCode's terminal

Use MSYS2 inside VSCode's terminal

{
    "terminal.integrated.shell.windows": "C:\\msys64\\msys2_shell.cmd",
    "terminal.integrated.shellArgs.windows": [
        "-defterm",
        "-no-start",
        "-use-full-path",
 "-mingw",
@jamen
jamen / archive.sh
Last active May 19, 2020 22:22
script to archive my repos
git archive --format=tar.gz -o ~/repos/attic/archives/$(date --date="@$(git log -1 --format=%ct)" +%Y)-$(basename $PWD).tar.gz --prefix=$(basename $PWD)/ master

Keybase proof

I hereby claim:

  • I am jamen on github.
  • I am jamen (https://keybase.io/jamen) on keybase.
  • I have a public key ASBB1XYtedNHggJWmFwSS9nXy1-7SQglp5k7UqdIiRjQ0Qo

To claim this, I am signing this object: