Skip to content

Instantly share code, notes, and snippets.

View leiless's full-sized avatar
🎯
Focusing

Fishbone° leiless

🎯
Focusing
View GitHub Profile
@leiless
leiless / _setmode.rs
Last active April 10, 2024 06:48
Rust windows: Set stdin(fd=0) translation mode to binary mode.
#[cfg(target_os = "windows")]
extern {
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170
// https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa298581(v=vs.60)
fn _setmode(fd: std::os::raw::c_int, mode: std::os::raw::c_int) -> std::os::raw::c_int;
// https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/wwfcfxas(v=vs.100)
fn _get_errno(p_value: *mut std::os::raw::c_int) -> std::os::raw::c_int;
}
#[cfg(target_os = "windows")]
@leiless
leiless / aes256_gcm_cbc_pkcs7.rs
Created April 3, 2024 03:27
Rust: AES-256, CBC mode, PKCS#7 padding
use aes::cipher::block_padding::Pkcs7;
use aes::cipher::{KeyIvInit, BlockEncryptMut, BlockDecryptMut};
use rand::RngCore;
type Aes256CbcEnc = cbc::Encryptor<aes::Aes256>;
type Aes256CbcDec = cbc::Decryptor<aes::Aes256>;
const AES256_SECRET_SIZE: usize = 32;
const AES256_BLOCK_SIZE: usize = 16;
@leiless
leiless / aes_256_cbc_pkcs7.py
Created April 3, 2024 02:18
Python: AES-256, CBC mode, PKCS#7 padding
#!/usr/bin/env python3
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
# https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/
# https://cryptography.io/en/latest/hazmat/primitives/padding/#cryptography.hazmat.primitives.padding.PKCS7
class AES256:
KEY_SIZE = 32
BLOCK_SIZE = 16
@leiless
leiless / pushd-popd.py
Last active February 22, 2024 06:18
Bring bash's pushd/popd to Python3
#!/usr/bin/env python3
import os
import sys
DIR_STACK = []
def pushd(path: str):
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
@leiless
leiless / lmdb_MDB_DUPSORT_write.rs
Created October 30, 2023 02:09
Test LMDB MDB_DUPSORT write with same key-value.
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / lmdb_INTEGER_KEY_cursor_get_bug.rs
Created October 12, 2023 07:17
LMDB INTEGER_KEY big key `mdb_cursor_get(MDB_SET_RANGE)` bug
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / test_lmdb_dup_sort_update_in_place.rs
Last active October 10, 2023 06:50
LMDB `DUP_SORT` `cursor.put(CURRENT)` different value size bug
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / server.c
Last active September 18, 2023 08:12
Windows AF_UNIX UNIX domain socket example
// Taken from with modifications
// https://devblogs.microsoft.com/commandline/windowswsl-interop-with-af_unix/#windows-server-code
#undef UNICODE
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <afunix.h>
#include <stdlib.h>
@leiless
leiless / std_deviation.rs
Created August 31, 2023 12:33
Standard deviation in Rust
fn mean(data: &[u32]) -> f64 {
let sum = data.iter().sum::<u32>() as f64;
let count = data.len();
sum / count as f64
}
fn std_deviation(data: &[u32]) -> f64 {
if data.len() != 0 {
let data_mean = mean(data);
let variance = data.iter().map(|v| {