Skip to content

Instantly share code, notes, and snippets.

View mmozeiko's full-sized avatar

Mārtiņš Možeiko mmozeiko

View GitHub Profile
@mmozeiko
mmozeiko / armv8_tsc.h
Last active April 2, 2024 08:34
armv8 timer & cycle counter
#pragma once
#define _GNU_SOURCE
#include <stdint.h>
#include <stdbool.h>
#include <sched.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/perf_event.h>
@mmozeiko
mmozeiko / unitypackage2zip.py
Created February 9, 2019 12:31
Python script that converts .unitypackage file to .zip archive
#!/usr/bin/env python3
import sys
import tarfile
import zipfile
from pathlib import Path
src = sys.argv[1]
dst = Path(src).name + ".zip"
@mmozeiko
mmozeiko / build.cmd
Last active March 27, 2024 04:20
download & build llvm+clang on Windows
@echo off
setlocal enabledelayedexpansion
rem !!! build requirements !!!
rem Visual Studio 2022 - https://visualstudio.microsoft.com/vs/
rem 7-Zip - https://www.7-zip.org/download.html
rem Python - https://www.python.org/downloads/
rem CMake - http://www.cmake.org/download/
rem ninja.exe - https://github.com/ninja-build/ninja/releases/latest
@mmozeiko
mmozeiko / tls_client.c
Last active March 26, 2024 02:27
simple example of TLS socket client using win32 schannel api
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#define SECURITY_WIN32
#include <security.h>
#include <schannel.h>
#include <shlwapi.h>
#include <assert.h>
#include <stdio.h>
@mmozeiko
mmozeiko / floor.c
Last active March 17, 2024 08:25
floor function for floats with SSE1 or SSE2
// floor function for floats with SSE1 or SSE2
#include <emmintrin.h>
#include <smmintrin.h>
#ifdef _MSC_VER
#include <intrin.h>
#else
#define __rdtsc() __builtin_ia32_rdtsc()
#endif
@mmozeiko
mmozeiko / overlapped_named_pipe.cpp
Last active March 15, 2024 13:06
Example how to use read-only named pipes with single client in non-blocking way on single thread
#include <windows.h>
#include <stdio.h>
#include <assert.h> // for potential error handling, change to real errors in your code
int main()
{
HANDLE pipe = CreateNamedPipeW(
L"\\\\.\\pipe\\lolcat",
PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
@mmozeiko
mmozeiko / hook.c
Last active March 14, 2024 05:33
reads process stdout + stderr and prints it out as it becomes available
// compile this to hook.dll
// for example, with MSVC: cl.exe /LD /MT /O2 hook.c /Fehook.dll
#include <windows.h>
// get this from https://github.com/kubo/plthook
#include "plthook_win32.c"
static plthook_t* hook;
@mmozeiko
mmozeiko / etw_createfile.c
Last active March 14, 2024 00:14
Monitor which files are accessed with ETW
// this code will work only when compiled as 64-bit code, and on Windows 10
// older Windows version might require different structure definitions
#define NOMINMAX
#define INITGUID
#include <windows.h>
#include <evntrace.h>
#include <evntcons.h>
#pragma comment (lib, "shell32.lib")
@mmozeiko
mmozeiko / FlushFileCache.c
Last active March 14, 2024 00:14
flush file cache on Windows
// clang.exe -Os -nostdlib -fuse-ld=lld -Wl,-fixed,-subsystem:console FlushFileCache.c -o FlushFileCache.exe
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment (lib, "kernel32.lib")
#pragma comment (lib, "advapi32.lib")
#define error(str) do { DWORD written; WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, sizeof(str)-1, &written, NULL); } while (0)