Skip to content

Instantly share code, notes, and snippets.

// A short demo program that uses libjpeg to write cmyk/ycck jpeg files.
// It's possible to write ycck jpegs using ImageMagick's convert and Photoshop's Save As.
// I haven't found a good way to write subsampled true cmyk jpegs using any of cjpeg, convert, Photoshop, or GIMP though.
// (GIMP 2.99.16 has in-progress cmyk saving support, and by picking 4:2:0 subsampling it writes a 2111 CMYK file.
// But 2111 CMYK files don't make any sense, and 4:2:0 is a YCC term and applying it to CMYK is fairly silly.
// Anyways, I wasn't able to create a 2112 CMYK file, which is what is sometimes seen in pratice -- not that
// having C have more resolution than MY makes a ton of sense either.)
//
// compile with:
// clang++ -std=c++11 write_cmyk_ycck_jpegs.cc -I ~/Downloads/libjpeg-turbo-3.0.1 -I ~/Downloads/libjpeg-turbo-3.0.1/build -ljpeg -L ~/Downloads/libjpeg-turbo-3.0.1/build
@nico
nico / kpc_demo.c
Created April 20, 2023 15:06 — forked from ibireme/kpc_demo.c
A demo shows how to read Intel or Apple M1 CPU performance counter in macOS.
// =============================================================================
// XNU kperf/kpc demo
// Available for 64-bit Intel/Apple Silicon, macOS/iOS, with root privileges
//
//
// Demo 1 (profile a function in current thread):
// 1. Open directory '/usr/share/kpep/', find your CPU PMC database.
// For M1 (Pro/Max), the database file is '/usr/share/kpep/a14.plist'.
// 2. Select a few events that you are interested in,
// add their names to the `profile_events` array below.
@nico
nico / .sh
Created September 30, 2022 15:41
minimal gengetopt example
% cat minimal.txt
package "example"
version "0.0.1"
% ~/Downloads/gengetopt-2.23/src/gengetopt < minimal.txt
% cat cmdline.h
/** @file cmdline.h
* @brief The header file for the command line option parser
* generated by GNU Gengetopt version 2.23
* http://www.gnu.org/software/gengetopt.
@nico
nico / quine.py
Created March 24, 2021 12:44
This produces itself as output when run (`python3 quine.py`). Original from https://twitter.com/relsqui/status/1136130759784259584 , but that profile is now hidden
File "quine.py", line 1
File "quine.py", line 1
^
IndentationError: unexpected indent
@nico
nico / c_inline.md
Last active July 25, 2023 18:43
C vs C++ inline function semantics, and ODR vs /arch:

The semantics of inline are one of the areas where C and C++ are pretty different. This post is about the C++ semantics, but the history is interesting, so here's a short summary of it.

The meaning of "inline" is intuitively easy to understand: It gives the compiler a hint that it'd be nice if a function could be inlined. It gets a bit complicated because of two issues:

  1. If the function ends up not being inlined, where should the definition of the function be emitted?
  2. If the inline function contains a static local variable, should that be inlined? Should there be several copies of the static local, or just one?
@nico
nico / blocks.txt
Last active February 4, 2024 10:53
unicode blocks for terminal UI drawing; lots new in unicode 13
General Punctuation
U+201x ‐ ‒ – — ― ‖ ‗ ‘ ’ ‚ ‛ “ ” „ ‟
U+202x † ‡ • ‣ ․ ‥ … ‧
U+203x ‰ ‱ ′ ″ ‴ ‵ ‶ ‷ ‸ ‹ › ※ ‼ ‽ ‾ ‿
U+204x ⁀ ⁁ ⁂ ⁃ ⁄ ⁅ ⁆ ⁇ ⁈ ⁉ ⁊ ⁋ ⁌ ⁍ ⁎ ⁏
U+205x ⁐ ⁑ ⁒ ⁓ ⁔ ⁕ ⁖ ⁗ ⁘ ⁙ ⁚ ⁛ ⁜ ⁝ ⁞
Arrows
U+219x ← ↑ → ↓ ↔ ↕ ↖ ↗ ↘ ↙ ↚ ↛ ↜ ↝ ↞ ↟
U+21Ax ↠ ↡ ↢ ↣ ↤ ↥ ↦ ↧ ↨ ↩ ↪ ↫ ↬ ↭ ↮ ↯
@ech off
REM Creates "myroot" which can be used with `clang-cl /winsysroot myroot`
setlocal
REM Adjust this one accordingly:
set VSInstallDir="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional"
set WinSdkDir="%ProgramFiles(x86)%\Windows Kits"
@nico
nico / gist:5bd81b9f887abd48e29f70cdb0978b0c
Last active December 4, 2020 15:02
inotifywait -m with event coalescing
#!/bin/bash
inotifywait -m -e create -r $1 | while read dir action file; do
# Coalese additional queued up events, if any
# If events keep arriving faster than we can read them off stdin here,
# we'll never run the script! If you're worried about that, set
# SECONDS to 0 before this loop and make the conditional also check that
# $SECONDS is smaller than some timeout to make sure that the script runs
# every now and then. The stdin pipe buffer size means there won't be an
# unbounded buildup on stdin -- instead inotifywait will block writing
commit 0c783ec6a639401153d96de1f4bc6ec8117c409c
Author: Nico Weber <thakis@chromium.org>
Date: Sun Jul 19 19:49:54 2020 -0400
fix one ppm fuzzer issue and two jpeg issues, all harmless DoS
diff --git a/Libraries/LibGfx/JPGLoader.cpp b/Libraries/LibGfx/JPGLoader.cpp
index 57772534d..03f7b2822 100644
--- a/Libraries/LibGfx/JPGLoader.cpp
+++ b/Libraries/LibGfx/JPGLoader.cpp
@nico
nico / bmp fuzz.diff
Last active September 4, 2020 17:00
commit c9d1d14e56d7ed3cb3d43053c06a86229dc9ca65
Author: Nico Weber <thakis@chromium.org>
Date: Sat Jul 18 22:35:01 2020 -0400
bmp loader does not validate data_offset, can use that for arbitrary read, with <canvas> can probably leak arbitrary user mem to js
diff --git a/Libraries/LibGfx/BMPLoader.cpp b/Libraries/LibGfx/BMPLoader.cpp
index a6c82ca15..91299749a 100644
--- a/Libraries/LibGfx/BMPLoader.cpp
+++ b/Libraries/LibGfx/BMPLoader.cpp