<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[NHollmann's RSS Feed]]></title><description><![CDATA[Personal site and blog by Nicolas Hollmann]]></description><link>https://nicolashollmann.de</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 19 Sep 2022 21:53:51 GMT</lastBuildDate><item><title><![CDATA[Run dynamic code in C on Unix]]></title><description><![CDATA[Run dynamic code in C on Unix This is just a short extension of my previous post regarding dynamic code execution
on Windows. While you can find examples of this technique for unix on other pages, I liked to try it out myself
and post it here for…]]></description><link>https://nicolashollmann.de/blog/run-dynamic-code-unix/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/run-dynamic-code-unix/</guid><pubDate>Sat, 03 Apr 2021 10:00:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;run-dynamic-code-in-c-on-unix&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#run-dynamic-code-in-c-on-unix&quot; aria-label=&quot;run dynamic code in c on unix permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Run dynamic code in C on Unix&lt;/h2&gt;
&lt;p&gt;This is just a short extension of my previous post regarding &lt;a href=&quot;https://nicolashollmann.de/blog/run-dynamic-code-windows/&quot;&gt;dynamic code execution&lt;/a&gt;
on Windows. While you can find examples of this technique for unix on other pages, I liked to try it out myself
and post it here for documentation purposes. Please read my previous post for more information, espacially regarding
the limitations.&lt;/p&gt;
&lt;h3 id=&quot;how-it-works-on-unix&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#how-it-works-on-unix&quot; aria-label=&quot;how it works on unix permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How it works (on Unix)&lt;/h3&gt;
&lt;p&gt;The technique is really the same as on windows, only that we need to call other system functions to allocate and
manage our memory. As always, the full sourcecode is on &lt;a href=&quot;https://gist.github.com/NHollmann/462d491220c7e648c9ce360f1925a7ee&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdbool.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;string.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;sys/types.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;sys/mman.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;/**
* Helper function to execute dynamic code.
* 
* @param code a pointer to the code to execute
* @param length the length of the code
* @return true if the code run successfully, otherwise false
*/&lt;/span&gt;
bool &lt;span class=&quot;token function&quot;&gt;run_code&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; code&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;size_t&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// Allocate new memory for code execution.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// We don&apos;t set the execute flag because we won&apos;t want to&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// open doors to malicious software.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; dynMemory &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mmap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PROT_READ &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; PROT_WRITE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MAP_PRIVATE &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; MAP_ANONYMOUS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; MAP_FAILED&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alloc Error\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; false&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Copy code to new memory.&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;memcpy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; code&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Change protection.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// We remove the write flag and set the execute flag on that page.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;mprotect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PROT_EXEC&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Protect Error\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; false&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Call the new function in memory.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Result: %d\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// At the end we free the memory.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;munmap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Free Error\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; false&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; true&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The explanation for this code is the same as in the Windows version, only with some different functions:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;left&quot;&gt;Windows&lt;/th&gt;
&lt;th align=&quot;left&quot;&gt;Unix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;VirtualAlloc&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;mmap&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;VirtualProtect&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;mprotect&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;VirtualFree&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;munmap&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Make sure you don’t run code from an untrusty source.&lt;/strong&gt;
&lt;strong&gt;It will run with the same permissions as the calling process/thread.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;links&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#links&quot; aria-label=&quot;links permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Links&lt;/h3&gt;
&lt;p&gt;Full sourcecode on GitHub: &lt;a href=&quot;https://gist.github.com/NHollmann/462d491220c7e648c9ce360f1925a7ee&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;GitHub Gist&lt;/a&gt;&lt;/p&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/run-dynamic-code-unix/&quot;&gt;https://nicolashollmann.de/blog/run-dynamic-code-unix/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Run dynamic code in C on Windows]]></title><description><![CDATA[Off-Topic: With this blogpost I may waste my chance for a good aprils fool but neither the less,
I just publish it today. On the other hand this technique is, while real, so niche, that it maybe
could be seen as a joke to somebody… Run dynamic code…]]></description><link>https://nicolashollmann.de/blog/run-dynamic-code-windows/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/run-dynamic-code-windows/</guid><pubDate>Thu, 01 Apr 2021 22:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Off-Topic: With this blogpost I may waste my chance for a good aprils fool but neither the less,
I just publish it today. On the other hand this technique is, while real, so niche, that it maybe
could be seen as a joke to somebody…&lt;/p&gt;
&lt;h2 id=&quot;run-dynamic-code-in-c-on-windows&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#run-dynamic-code-in-c-on-windows&quot; aria-label=&quot;run dynamic code in c on windows permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Run dynamic code in C on Windows&lt;/h2&gt;
&lt;p&gt;Lately I’m experimenting with running machinecode from within an C program. I found some
examples of this technique for Linux systems using &lt;code class=&quot;language-text&quot;&gt;mmap&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;mprotect&lt;/code&gt;, but nothing for
Windows. So in this short post I wan’t to write about how this works on windows. Maybe there
are already some nice guides about this topic, but I didn’t found them. Writing about this
myself helps me to document my work and maybe it helps somebody else.&lt;/p&gt;
&lt;p&gt;I really have a problem with finding a name for this technique. If you know a common name for
it, please write me on Twitter. Until then I stay with “running dynamic code” :)&lt;/p&gt;
&lt;h3 id=&quot;what-i-want-to-achieve&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#what-i-want-to-achieve&quot; aria-label=&quot;what i want to achieve permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What I want to achieve&lt;/h3&gt;
&lt;p&gt;I want to be able to supply some raw opcodes to a function and let them be executed at runtime.
This can be useful for example in a JIT compiler, where the sourcode is compiled just before the
program (or even function) is run (just-in-time). In such a compiler the required opcodes are
normally generated on the fly and not saved in a (executable) file but stored in memory.&lt;/p&gt;
&lt;p&gt;Another usage example would be a compressed or encrypted executable. There are some niche usecases
for this, like some maleware that wan’t to disguise their presence, or systems with really low
persistent memory but enough RAM to decompress the executable at runtime. A compressed or encrypted
executable would need an uncompressed loader that first uncompresses/decrypts the main application
and then launches it.&lt;/p&gt;
&lt;p&gt;At the moment I’m just interested in this technique by itself without any real usecase. But maybe
in the future I might look into JIT compiling and executable compression.&lt;/p&gt;
&lt;h3 id=&quot;limitations&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#limitations&quot; aria-label=&quot;limitations permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Limitations&lt;/h3&gt;
&lt;p&gt;First a disclaimer: This technique is more of an expert/niche thing and should not be used without
a second thought. You may even introduce a (many) security flaws or hard to debug code. In most cases
there are better ways to solve an problem than this. For exmple, if you wan’t an application wich allows
for user-modification like plugins or mods, you could use a dynmic library (.dll/.so/.dylib) which is
loaded at runtime. Or even an external but production ready scripting language like Lua.&lt;/p&gt;
&lt;p&gt;Another downside of this technique is that it is heavily unportable. At least, you need to generate
the right opcodes for the processor architecture you’re using. For example, the opcodes for an
x86-64 processor are completly different then for an ARM processor. On some architiectures you need
to apply extra steps like clearing a special cache for this to work. Some architectures even don’t
support this technique directly, like AVR. AVR uses different memory for code and data, and you would
need to reflash your code memory on the microcontroller at runtime. This is indeed possible, but it
would havily reduce the lifetime of such a microcontroller, as their flash memory isn’t really optimized
for this usecase and has normaly a guranteed lifetime of about 10,000 write-erase cycles &lt;a href=&quot;https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet-Summary.pdf&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;1&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But even if you know on what processor it runs, you may need to account for differences in the operating
system, espacially if you want to directly call standard library functions.&lt;/p&gt;
&lt;p&gt;If you want to use this technique in production I would recommend to look for a library which helps
with running code in a more cross-platform manner.&lt;/p&gt;
&lt;h3 id=&quot;how-it-works-on-windows&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#how-it-works-on-windows&quot; aria-label=&quot;how it works on windows permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How it works (on Windows)&lt;/h3&gt;
&lt;p&gt;Lets get our hands dirty! In this post I will only look at the function which runs the opcodes but you can find the full sourcecode on &lt;a href=&quot;https://gist.github.com/NHollmann/c4f17b3372ec6e137418edae57bdec63&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdbool.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;Windows.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;/**
* Helper function to execute dynamic code.
* 
* @param code a pointer to the code to execute
* @param length the length of the code
* @return true if the code run successfully, otherwise false
*/&lt;/span&gt;
bool &lt;span class=&quot;token function&quot;&gt;run_code&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; code&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;size_t&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// Allocate new memory for code execution.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// We don&apos;t set the execute flag because we won&apos;t want to&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// open doors to malicious software.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// dynMemory must be declared as volatile, as some Release&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// optimizations cause problems on VirtualFree. Maybe some&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// sort of reordering.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;volatile&lt;/span&gt; LPVOID dynMemory &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;VirtualAlloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MEM_COMMIT &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; MEM_RESERVE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAGE_READWRITE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alloc Error: %ld\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;GetLastError&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; false&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Copy code to new memory.&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;memcpy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; code&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Change protection.&lt;/span&gt;
	&lt;span class=&quot;token comment&quot;&gt;// We remove the write flag and set the execute flag on that page.&lt;/span&gt;
	DWORD oldProtection&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;VirtualProtect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAGE_EXECUTE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;oldProtection&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Protect Error: %ld\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;GetLastError&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; false&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// Call the new function in memory.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Result: %d\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token comment&quot;&gt;// At the end we free the memory.&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;VirtualFree&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MEM_RELEASE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Free Error: %ld\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;GetLastError&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; false&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; true&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function takes a pointer to some raw bytes and the length of it. It then uses the WinAPI function
&lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;VirtualAlloc&lt;/a&gt;
to allocate some memory pages for our code to live in. VirtualAlloc allocates enough whole pages so our
&lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt;-long code will fit. These new pages are declared as readable and writable but not executable.
This is because a memory page should never be executable AND writable at the same time. This is called W^X
(write XOR execute) and you can read more about it &lt;a href=&quot;https://en.wikipedia.org/wiki/W%5EX&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;here&lt;/a&gt;.
The result of VirtualAlloc is a pointer to our newly allocated memory and it is saved in an &lt;code class=&quot;language-text&quot;&gt;volatile LPVOID&lt;/code&gt;
pointer. The &lt;a href=&quot;https://en.cppreference.com/w/c/language/volatile&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;volatile keyword&lt;/a&gt; is needed because it disables
some optimizations that caused the program to crash when compiled with msvc in release mode. In debug mode
everything workes as expected even without &lt;code class=&quot;language-text&quot;&gt;volatile&lt;/code&gt;. To be honest, at the moment I don’t know why it breaks
in release, maybe some sort of instruction reordering? But it is just another example why you should be careful
with this technique. If somebody knows why this breaks in release, please write me, I really would like to know.&lt;/p&gt;
&lt;p&gt;In the next step we copy our code from the supplied pointer to the newly created, page-aligned memory. After
we copied our code, we don’t need to modifiy the memory anymore. Because of this we use
&lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;VirtualProtect&lt;/a&gt;
to modifiy the memory permissions to executable but not writable. The old permissions will be
saved in &lt;code class=&quot;language-text&quot;&gt;oldProtection&lt;/code&gt;. We aren’t interested in these, but without suppling a valid pointer as the fourth
parameter to VirtualProtect, the function will fail according to the documentation. &lt;/p&gt;
&lt;p&gt;After this we can jump into our code. We do this by casting the pointer to the code to a function pointer and
then calling it directly. The syntax for this is at least cryptic so let me go through this in more detail:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;dynMemory&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we cast our pointer to a function pointer which doesn’t expect any parameters and returns an integer.
The &lt;code class=&quot;language-text&quot;&gt;(void)&lt;/code&gt; denotes that this function doesn’t take parameters. The &lt;code class=&quot;language-text&quot;&gt;(*)&lt;/code&gt; with the paranthesis is required
for a function pointer cast. The &lt;code class=&quot;language-text&quot;&gt;int&lt;/code&gt; at the beginning denotes as expected the return type. And lastly the
paranthesis at the end is there because we want to call this function directly.&lt;/p&gt;
&lt;p&gt;On 64bit Windows the integer return value of a function is expected to be in the EAX register, so you
opcode should put a meaningful value in this register. This is the easiest way to communicate with the
calling code from within the dynamic code. If you’re not interested in a return value, you can change the
type of the function in the cast to void. On the other side, if you want to supply the dynmic code with
values, you can define parameters instead of void. The example code on GitHub performs an addition and
subtraction and then returns the value in eax. The result is printed directly after the call in the &lt;code class=&quot;language-text&quot;&gt;printf&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Make sure you don’t run code from an untrusty source.&lt;/strong&gt;
&lt;strong&gt;It will run with the same permissions as the calling process/thread.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Last but not least we need to free our code memory. This is done with a call to
&lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;VirtualFree&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;links&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#links&quot; aria-label=&quot;links permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Links&lt;/h3&gt;
&lt;p&gt;Full sourcecode on GitHub: &lt;a href=&quot;https://gist.github.com/NHollmann/c4f17b3372ec6e137418edae57bdec63&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;GitHub Gist&lt;/a&gt;&lt;/p&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/run-dynamic-code-windows/&quot;&gt;https://nicolashollmann.de/blog/run-dynamic-code-windows/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Writing Images: Windows Bitmap]]></title><description><![CDATA[Writing Images: Windows Bitmap Last time we looked at the very simple Portable Anymap format to
easily write images. But there is a catch with portable anymaps: they are a little but uncommon.
While macOS and most Linux desktops support them even…]]></description><link>https://nicolashollmann.de/blog/writing-bmp-files/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/writing-bmp-files/</guid><pubDate>Sun, 14 Mar 2021 08:00:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;writing-images-windows-bitmap&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#writing-images-windows-bitmap&quot; aria-label=&quot;writing images windows bitmap permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Writing Images: Windows Bitmap&lt;/h2&gt;
&lt;p&gt;Last time we looked at the very simple &lt;a href=&quot;https://nicolashollmann.de/blog/writing-portable-anymap/&quot;&gt;Portable Anymap&lt;/a&gt; format to
easily write images. But there is a catch with portable anymaps: they are a little but uncommon.
While macOS and most Linux desktops support them even with thumbnails, there is no default support on Windows.
Also, most webbrowsers don’t support them either. They are also a little bit unknown in non-development environments.
This time we’ll look at a much more common file format: Windows Bitmap.&lt;/p&gt;
&lt;p&gt;Windows bitmaps with their iconic file extension .bmp are undestood by almost every software that processes images.
As you will see they are just a little bit more complicated than Portable Anymaps and thus easy to write and read.
On the other side, BMPs have a lot of restrictions which limit their use. One limit is, that BMP cannot save any transparency
information. Another problem is the size of BMPs because they normaly save uncompressed image data.&lt;/p&gt;
&lt;h3 id=&quot;the-bitmap-file-format&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#the-bitmap-file-format&quot; aria-label=&quot;the bitmap file format permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The Bitmap File Format&lt;/h3&gt;
&lt;p&gt;We won’t look at every detail of the bitmap file format but focus on the important features and properties so that we
can write our own uncompressed bitmaps easily and fast. If you want are more indepth look at this file format I
recommend the &lt;a href=&quot;https://en.wikipedia.org/wiki/BMP_file_format&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Wikipedia&lt;/a&gt; article.&lt;/p&gt;
&lt;p&gt;The bitmap file format is a completely binary file format. This isn’t bad at all, in fact, while it is a little more
inconvienient for humans, it makes the data size smaller and its format is exactly descibed. You will see that
there is no black magic required to write such binary bitmap files. There is just one thing you need to be aware:
the byte order. I’ve already written an &lt;a href=&quot;https://nicolashollmann.de/blog/byte-order/&quot;&gt;article&lt;/a&gt; about that topic. Windows bitmaps always save
their multibyte data as little endian.&lt;/p&gt;
&lt;h4 id=&quot;header&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#header&quot; aria-label=&quot;header permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Header&lt;/h4&gt;
&lt;p&gt;A Bitmap starts with two headers, one that describes the file itself (called the BITMAPFILEHEADER in the documentation)
and one that describes the saved image (the BITMAPINFOHEADER). You always need to write both, but technicaly it doesn’t make
a difference if there were one big header or two smaller ones.&lt;/p&gt;
&lt;p&gt;In the next two subsections I present you the members of these headers. It may look a bit overwhelming at first, but
stay with me. You will see that many of them are always constant or are easily filled. If you don’t understand
everything at first you can also jump directly to the code, it will help you understand everything.&lt;/p&gt;
&lt;p&gt;One last word about the data type informations in these tables. These types are from the Microsoft documentation
and are commonly used in Windows code. They are in fact just different names for common datatypes. To write bitmaps,
you only need to understand what they represent. Here is a short table:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;left&quot;&gt;Type&lt;/th&gt;
&lt;th align=&quot;left&quot;&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;WORD&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;2 byte / 16 bit unsigned integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;4 byte / 32 bit unsigned integer, DWORD means DOUBLE WORD&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;LONG&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;4 byte / 32 bit signed integer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h5 id=&quot;bitmapfileheader&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#bitmapfileheader&quot; aria-label=&quot;bitmapfileheader permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;BITMAPFILEHEADER&lt;/h5&gt;
&lt;p&gt;This header is the first thing found in a bitmap file.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;left&quot;&gt;Name&lt;/th&gt;
&lt;th align=&quot;right&quot;&gt;Data type&lt;/th&gt;
&lt;th align=&quot;right&quot;&gt;Size&lt;/th&gt;
&lt;th align=&quot;left&quot;&gt;Short description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;bfType&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;WORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;2 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;This is always &lt;code class=&quot;language-text&quot;&gt;&amp;quot;BM&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;bfSize&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;The filesize in bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;bfReserved1&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;WORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;2 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Reserved and unused, must be 0, but some software uses it regardless&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;bfReserved2&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;WORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;2 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Reserved and unused, must be 0, but some software uses it regardless&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;bfOffBits&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;Offset to the image data from the beginning of the file&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;These names are directly from the Microsoft bitmap documentation.
It is linked at the &lt;a href=&quot;#format-specifications-and-links&quot;&gt;Format specifications and Links&lt;/a&gt; section.&lt;/p&gt;
&lt;h5 id=&quot;bitmapinfoheader&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#bitmapinfoheader&quot; aria-label=&quot;bitmapinfoheader permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;BITMAPINFOHEADER&lt;/h5&gt;
&lt;p&gt;Directly after the BITMAPFILEHEADER follows this header.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;left&quot;&gt;Name&lt;/th&gt;
&lt;th align=&quot;right&quot;&gt;Data type&lt;/th&gt;
&lt;th align=&quot;right&quot;&gt;Size&lt;/th&gt;
&lt;th align=&quot;left&quot;&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biSize&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;size of this header, here we can use the constant value 40&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biWidth&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;LONG&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;width of the image&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biHeight&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;LONG&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;height of the image, but this value can be negative, I come to this later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biPlanes&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;WORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;2 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;not used anymore, must be set to 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biBitCount&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;WORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;2 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;bits per pixel, can be 1, 4, 8, 16, 24 or 32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biCompression&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;which compression is used, can be 0, 1, 2 or 3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biSizeImage&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;the size of the image data in byte, can be 0 if no compression is used&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biXPelsPerMeter&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;LONG&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;horizontal resolution in pixels per meter, often just set to 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biYPelsPerMeter&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;LONG&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;vertical resolution in pixels per meter, often just set to 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biClrUsed&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;if no color table is used, this can be set to 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;biClrImportant&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;DWORD&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;4 bytes&lt;/td&gt;
&lt;td align=&quot;left&quot;&gt;if no color table is used, this can be set to 0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In this header are some things that need to be addressed in greater detail. Let’s start with the most wierd thing:
&lt;strong&gt;The height can be negative.&lt;/strong&gt; If the height is positive, the image data is expected to be written from bottom to top.
I don’t know why this was decided, but it seems that this is the most common form bitmaps are saved. Some of the compression
options are also only available, if this direction is choosen.
If the height is negative the image is expected to be written in the IMHO more natural top to bottom direction.&lt;/p&gt;
&lt;p&gt;Now we look at the &lt;code class=&quot;language-text&quot;&gt;biBitCount&lt;/code&gt; value. It describes how many bits are expected for a single pixel. The values 1, 4 and 8 are
for indexed colors. This means we have a color table where we define all colors that can appear in an image. We then only save
an index of an color in this table as the actual image data. We won’t look into the benefits and problems with this approch
today, as we only look at 24 bits per pixel. This is the most simple way to write images because every channel (red, green and blue) get a full byte per pixel (3 * 8 bit).&lt;/p&gt;
&lt;h4 id=&quot;data&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#data&quot; aria-label=&quot;data permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Data&lt;/h4&gt;
&lt;p&gt;And now the best thing: as we save bitmaps uncompressed we can easily write out our pixel data. A pixel with only
0 bytes represents black and only 255 bytes represents white. But there are two catches:
bitmaps save BGR (blue-green-red) data, not RGB (red-green-blue). Today you probably save your images in memory
in RGB so you need to inverse the byte order before putting out a pixel.&lt;/p&gt;
&lt;p&gt;The second catch is the already described order of rows, from bottom to top. If your image data is already
ordered this way, you can easily dump them out to a file. But if you save them from top to bottom, you
need to reverse them while writing or flip them before. Flipping beforehand may have some performance benefits
by utilising the processor cache lines better, at least if implemented correctly. But in this article we just
flip the order while writing it, even if it may not be the best performing solution. My focus in this post
is more on simplicity than performance.&lt;/p&gt;
&lt;h3 id=&quot;implementation&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#implementation&quot; aria-label=&quot;implementation permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Implementation&lt;/h3&gt;
&lt;p&gt;As I did with the portable anymap I show you now a simple implementation of a
windows bitmap writer in C.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdint.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;WIDTH&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token number&quot;&gt;300&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;HEIGHT&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// This changes the padding behaviour of the&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// compiler. I come to this later.&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;pragma&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token function&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;push&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; bfType&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; bfSize&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; bfReserved1&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; bfReserved2&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; bfOffBits&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; BitmapFileHeader&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; biSize&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;int32_t&lt;/span&gt; biWidth&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;int32_t&lt;/span&gt; biHeight&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; biPlanes&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; biBitCount&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; biCompression&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; biSizeImage&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;int32_t&lt;/span&gt; biXPelsPerMeter&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;int32_t&lt;/span&gt; biYPelsPerMeter&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; biClrUsed&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; biClrImportant&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; BitmapInfoHeader&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// This restores the padding behaviour.&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;pragma&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token function&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pop&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    FILE&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; outfile &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;out.bmp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Both headers have a combined size of 54 bytes.&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// We need WIDTH * HEIGHT pixels and each of them needs&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// three bytes (one for each channel).&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint32_t&lt;/span&gt; filesize &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; WIDTH &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; HEIGHT &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// First we fill the file header.&lt;/span&gt;
    BitmapFileHeader fileHeader&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    fileHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bfType &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;B&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;M&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// reversed because of Little Endian &lt;/span&gt;
    fileHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bfSize &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; filesize&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    fileHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bfReserved1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    fileHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bfReserved2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    fileHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bfOffBits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// both headers have a size of 54 byte&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Next we fill the info header.&lt;/span&gt;
    BitmapInfoHeader infoHeader&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biSize &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// this header is 40 bytes long&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biWidth &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biHeight &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biPlanes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// must be 1&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biBitCount &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 24 bit per pixel = 1 byte per channel&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biCompression &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// no compression&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biSizeImage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 0 because we don&apos;t use compression&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biXPelsPerMeter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biYPelsPerMeter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biClrUsed &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    infoHeader&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;biClrImportant &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Then we write them both to the file.&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// This only works because of the padding changes&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// and ignores endianess. Normaly you should NOT&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// do this, but this simplifices this example.&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;fileHeader&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;BitmapFileHeader&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;infoHeader&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;BitmapInfoHeader&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// We write the image from bottom to top.&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// In this example the image data is created &lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// procedurally, so we really don&apos;t need to do it&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// this way. But if you would read the image data&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// from an array, you could use the following loops:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; HEIGHT &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;// Image data creation:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;WIDTH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;WIDTH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;// We write a single pixel in BGR order&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; colors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;colors&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;fclose&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This example outputs the following image:
&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/eb21ca71f5bfcc32af3715a2431aa87a/5a46d/out.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.89189189189189%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABZklEQVQ4yw3DIQvQSgAA4JXlBxdMwupDrgorFmFVZPWFBYuwrCwLFywvXBUvWq68uP7YD7hiXJf9gWGZ3wdf1//14gkORkcnZxdXN5PZYnX3sHl6edv1L189wcHo6OTs4upmMlus7h42Ty9vu/7vN09wMDo6Obu4upnMFqu7h83Ty9uuf/3+CQ5GRydnF1c3k9lidfeweXp52/VvPzzBwejo5Ozi6mYyW6zuHjZPL2+7/t3nJzgYHZ2cXVzdTGaL1d3D5unlbdf/8/UJDkZHJ2cXVzeT2WJ197B5ennb9R+/P8HB6Ojk7OLqZjJbrO4eNk8vb7v+039PcDA6Ojm7uLqZzBaru4fN08vbrv/y/xMcjI5Ozi6ubiazxeruYfP08rbr//35BAejo5Ozi6ubyWyxunvYPL287fpvv57gYHR0cnZxdTOZLVZ3D5unl7dd/+P3ExyMjk7OLq5uJrPF6u5h8/Ty9g+x2muJ86ekugAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Bitmap sample&quot;
        title=&quot;Bitmap sample&quot;
        src=&quot;https://nicolashollmann.de/static/eb21ca71f5bfcc32af3715a2431aa87a/5a46d/out.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/eb21ca71f5bfcc32af3715a2431aa87a/12f09/out.png 148w,https://nicolashollmann.de/static/eb21ca71f5bfcc32af3715a2431aa87a/e4a3f/out.png 295w,https://nicolashollmann.de/static/eb21ca71f5bfcc32af3715a2431aa87a/5a46d/out.png 300w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;I actually converted the original BMP to PNG for a smaller size and because my site is not configured
serve BMP files.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You might have noticed I used types like uint16_t from &lt;code class=&quot;language-text&quot;&gt;stdint.h&lt;/code&gt; instead of WORD, DWORD, LONG.
This makes this code more portable as the other types are Windows specific. If you already include
&lt;code class=&quot;language-text&quot;&gt;windows.h&lt;/code&gt;, you can easily use those types instead. Actually, you might use BITMAPFILEHEADER and
BITMAPINFOHEADER directly.&lt;/p&gt;
&lt;p&gt;Because this is only an example how to write a bitmap file, I used code to create an image procedurally.
And in this case the orientation of the image doen’t really matter. Normaly it would, so for demonstration
purposes my Y-loop writes the data from bottom to top, as you would with an actual image, maybe read from an
array oder rendered via raytracing on the fly. I hope this clarifies the writing procedure.&lt;/p&gt;
&lt;p&gt;Last but not least I need to talk about padding, packed structs and why this example is not very portable.
Maybe you’ve already noticed the &lt;code class=&quot;language-text&quot;&gt;#pragma pack(...)&lt;/code&gt; directives in this example. The reason they exists is,
that C structs may have something called a padding. Simply put, a padding is introduced in a struct to align
the members of a struct to adresses that are dividable by some factor. For example a 2 byte uint16&lt;em&gt;t is aligned
to an even address while a 4 byte uint32&lt;/em&gt;t is aligned to an address divisible by 4. If a 2 byte member in a struct
is followed by a 4 byte member, a 2 byte padding is introduced, so that the second member is correctly aligned.
Excactly this happens with the BITMAPFILEHEADER in the example code between &lt;code class=&quot;language-text&quot;&gt;bfType&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;bfSize&lt;/code&gt;. This causes
the struct to have a size of 16 instead of 14 bytes. This will corrupt our header so we can’t read our image.
Padding is normaly useful, because it allows optimised memory access. But if we wan’t to write a struct directly
to a file, it causes havok.&lt;/p&gt;
&lt;p&gt;The solution to this is using a packed struct. A packed struct does not contain any padding, so we can write
it out without problems. Here we come to the first problem: There is no standarised way to declare a packed
struct. I choosed the &lt;code class=&quot;language-text&quot;&gt;#pragma pack(...)&lt;/code&gt; directive, because it seems to have been adopted by many compilers.
The &lt;code class=&quot;language-text&quot;&gt;#pragma pack(push, 1)&lt;/code&gt; directive saves the current packing status on a stack and also sets it to 1, which
means that we disable padding completly. The &lt;code class=&quot;language-text&quot;&gt;#pragma pack(pop)&lt;/code&gt; restores the old status.&lt;/p&gt;
&lt;p&gt;There are a lot of problems with this solution:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I’ve already written that it is not standarised. If your compiler doesn’t support it, your code wouldn’t work.&lt;/li&gt;
&lt;li&gt;Accessing struct membery that are not aligned may be slow. This isn’t that important if you just use this struct
to save one image file, but if you use the same struct for some calculations it may hurt your performance.&lt;/li&gt;
&lt;li&gt;It is also heavily coupled to the x86 processor architecture.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The x86 architecture is really common in personal computers, but it is by far not the only architecture out there.
Padding and alignment are specifics of the processor architecture. Some architectures even require some sort of
alignment, or you can’t access the membery. Also we ignored the endianness completly, because x86 uses little endian,
as the BMP file format do. But on big endian systems all multibyte members need to be swapped.&lt;/p&gt;
&lt;p&gt;Using the solution presented in the example really helped to keep the code short and understandable. It is also
okay for small experiments or demos. But for anything just a little bit more important you shouldn’t do it that way.
Most of the time it is better to output each member of the struct seperatly (preferably with some buffering). This way
you have a lot of repetitve code but on the other hand you don’t need to deal with padding or alignment and also you can
deal with endianess on each struct member as needed.&lt;/p&gt;
&lt;h3 id=&quot;format-specifications-and-links&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#format-specifications-and-links&quot; aria-label=&quot;format specifications and links permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Format specifications and Links&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/BMP_file_format&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Microsoft Docs: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapfileheader&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;BITMAPFILEHEADER&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Microsoft Docs: &lt;a href=&quot;https://docs.microsoft.com/en-us/previous-versions//dd183376(v=vs.85)&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;BITMAPINFOHEADER&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Sourcecode on GitHub: &lt;a href=&quot;https://gist.github.com/NHollmann/b99b5ab90fff83beb549375f2efc6c4d&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;GitHub Gist&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/writing-bmp-files/&quot;&gt;https://nicolashollmann.de/blog/writing-bmp-files/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Writing Images: Portable Anymap]]></title><description><![CDATA[Writing Images: Portable Anymap  Most of the time, image formats are somewhat complex and require some sort of 
compression. But there are some exceptions and Portable Anymaps are such an 
exception. Portable anymaps are not a single file format but…]]></description><link>https://nicolashollmann.de/blog/writing-portable-anymap/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/writing-portable-anymap/</guid><pubDate>Thu, 29 Aug 2019 14:00:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;writing-images-portable-anymap&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#writing-images-portable-anymap&quot; aria-label=&quot;writing images portable anymap permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Writing Images: Portable Anymap&lt;/h2&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/21b4d/output.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABi0lEQVQozxXMoUFDMRQAwJqqOBaoimOAuigWQMUxAC6KBVBxDICLYgFUXAeoimMBVBzucf8GuNP5nOLhIcXlkuLxMcX1muLpKcXzc4qXlxSvryne3lK8v6f4+Ejx+Zni6yvF93eK2y3F/Z7i5yfF72+Kv78Up/M5C7MwC7MwC7MwC7MwC7MwC7MwC7MwC7MwC7MwC/MRFmERFmERFmERFmERFmERFmERFmERFmERFmERFmE5wiqswiqswiqswiqswiqswiqswiqswiqswiqswiqsR9iETdiETdiETdiETdiETdiETdiETdiETdiETdiE7Qi7sAu7sAu7sAu7sAu7sAu7sAu7sAu7sAu7sAu7sB/hEA7hEA7hEA7hEA7hEA7hEA7hEA7hEA7hEA7hEI4jnMIpnMIpnMIpnMIpnMIpnMIpnMIpnMIpnMIpnMJ5hEu4hEu4hEu4hEu4hEu4hEu4hEu4hEu4hEu4hEu4jnALt3ALt3ALt3ALt3ALt3ALt3ALt3ALt3ALt3ALd/wDNJ3vnKSixK8AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Image Output&quot;
        title=&quot;Image Output&quot;
        src=&quot;https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/fcda8/output.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/12f09/output.png 148w,https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/e4a3f/output.png 295w,https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/fcda8/output.png 590w,https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/efc66/output.png 885w,https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/c83ae/output.png 1180w,https://nicolashollmann.de/static/d8187f1cdd22420a51dcf2781161043a/21b4d/output.png 1280w&quot;
        sizes=&quot;(max-width: 590px) 100vw, 590px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Most of the time, image formats are somewhat complex and require some sort of
compression. But there are some exceptions and Portable Anymaps are such an
exception. Portable anymaps are not a single file format but a group of formats
for saving Bitmaps, Grayscale and RGB images.&lt;/p&gt;
&lt;p&gt;There are 7 different formats:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;left&quot;&gt;Format&lt;/th&gt;
&lt;th align=&quot;right&quot;&gt;Magic Number&lt;/th&gt;
&lt;th align=&quot;right&quot;&gt;File Ending&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;ASCII Bitmap&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;P1&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;.pbm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;ASCII Graymap&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;P2&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;.pgm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;ASCII Pixmap&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;P3&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;.ppm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;Binary Bitmap&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;P4&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;.pbm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;Binary Graymap&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;P5&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;.pgm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;left&quot;&gt;Binary Pixmap&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;P6&lt;/td&gt;
&lt;td align=&quot;right&quot;&gt;.ppm&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The seventh format is a generalization of the binary types and we just ignore it in this article.&lt;/p&gt;
&lt;h3 id=&quot;ascii&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#ascii&quot; aria-label=&quot;ascii permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;ASCII&lt;/h3&gt;
&lt;p&gt;The ASCII format is really simple. Ineed, it is so simple that you can export images in this format
from every language that can output text to a file or console. Yes, you could even use
&lt;a href=&quot;https://en.wikipedia.org/wiki/Brainfuck&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Brainfuck&lt;/a&gt; to export portable anymaps.&lt;/p&gt;
&lt;p&gt;All portable anymaps begin with a ASCII header in the following format:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;MAGIC_NUMBER
WIDTH HEIGHT
MAX_VALUE&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-text&quot;&gt;MAGIC_NUMBER&lt;/code&gt; is one of the 7 format identifiers we already looked at. &lt;code class=&quot;language-text&quot;&gt;WIDTH&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;HEIGHT&lt;/code&gt; are simply
the width and height of the image in pixels. We look at &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; a little bit later.&lt;/p&gt;
&lt;p&gt;The fields in the header are seperated by a whitespace so you could even write every field in its own line
or write the whole header in one line.&lt;/p&gt;
&lt;p&gt;The actual image data begins right after the header. They are encoded as human readable ASCII numbers
seperated by whitespace. Bitmaps can just use the &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt; character, graymaps and pixmaps can use
numbers from &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt;. Graymaps use one number per pixel while pixmaps use 3 numbers per pixel,
one for each channel. The channel order is red, green, blue.&lt;/p&gt;
&lt;p&gt;While not required, it is common to spereate lines in the image with a newline and also use a padding
for smaller numbers so they align in columns. This makes it easier to read by humans. Look at the
examples on this page to better undestand what I mean.&lt;/p&gt;
&lt;h4 id=&quot;comments&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#comments&quot; aria-label=&quot;comments permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Comments&lt;/h4&gt;
&lt;p&gt;Portable Anymaps can also include comments. Comments start with a &lt;code class=&quot;language-text&quot;&gt;#&lt;/code&gt; character and end with a carriage
return or newline.&lt;/p&gt;
&lt;h4 id=&quot;depth&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#depth&quot; aria-label=&quot;depth permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Depth&lt;/h4&gt;
&lt;p&gt;Now it is time to look at the &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; header field. It describes what the highest possible
value in an image may be. It must be greater than 0 and less than 65536. In a graymap, a pixel
with a value of &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; would be white. In a pixmap, if all three channels of a pixel are
&lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt;, it also would be white. The most common value for &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; is 255, because this
is the maximal value one byte can hold.&lt;/p&gt;
&lt;p&gt;Because portable bitmaps can only have &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt; as values, there is no &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; field for them.&lt;/p&gt;
&lt;h3 id=&quot;binary&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#binary&quot; aria-label=&quot;binary permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Binary&lt;/h3&gt;
&lt;p&gt;While ASCII is human readable and easy to output, it does take up a lot of
space. Even for a single value we need 2 bytes (1 whitespace + 1 data byte)
but it can easy be more.
Saving the same file in the binary mode we can save a lot space but we sacrifice
readability.&lt;/p&gt;
&lt;p&gt;To output a portable anymap in binary you just output the header in ASCII
but directly after the last whitespace of the header we output the image data.
Make sure that you don’t output any whitespaces after the last one in the header. They
get interpreted as image values.&lt;/p&gt;
&lt;p&gt;There is a little catch when using the binary mode: If &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; is less than 256 the
values are saved in one byte per pixel in graymaps and one byte per RGB channel in pixmaps.
If &lt;code class=&quot;language-text&quot;&gt;MAX_VALUE&lt;/code&gt; is equal or greater than 256 values are saved with two bytes using
&lt;a href=&quot;https://nicolashollmann.de/blog/byte-order/&quot;&gt;Big Endian&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;implementation-ascii&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#implementation-ascii&quot; aria-label=&quot;implementation ascii permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Implementation ASCII&lt;/h3&gt;
&lt;p&gt;There is nothing really special about the implementation of a portable
anymap writer. I’ve decided to show you a simple example using C and
writing a RGB image to a file. You can also find a C++ sample
in the full sourcecode.
The code should be easy to read even for a beginner.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;WIDTH&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token number&quot;&gt;300&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;HEIGHT&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    FILE &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;outfile &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;out.ppm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;P3&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;\n%d %d\n255\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;WIDTH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;WIDTH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;%3d %3d %3d &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// Output newlines so the result is a little bit more readable.&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;fclose&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;implementation-binary&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#implementation-binary&quot; aria-label=&quot;implementation binary permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Implementation Binary&lt;/h3&gt;
&lt;p&gt;This implementation isn’t very different from the ASCII version.
Notice that the header is still saved as ASCII characters.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;WIDTH&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token number&quot;&gt;300&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;HEIGHT&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    FILE &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;outfile &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;out.ppm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Now it is P6 instead of P3&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;P6&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;\n%d %d\n255\n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;WIDTH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;WIDTH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; colors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;colors&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;fclose&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outfile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;examples&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#examples&quot; aria-label=&quot;examples permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Examples&lt;/h3&gt;
&lt;p&gt;Here are some examples using ASCII anymaps. They are all written by hand.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bitmap&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;P1
3 3
1 1 1
0 1 0
0 1 0&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/6c41ed87edd0f57a1ab2363d5a500b11/5a46d/bitmap.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAgElEQVQ4y+2QSwrAIAxE1Z7LlSexG72U4gFEvJ6f0gYCJZDuSld1FkEmbxhViB9qY1JKcQxMTr5r3omstd57rTX4UkoE8GCMcc4BQHlxEo0xYKaU8J40XGuF1ZyT8uIgaq2BFULAv7hfCzPnDKveO+UfmmOMPFxKeWhe4RX+NHwBn4UUM1MWMPsAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Bitmap sample&quot;
        title=&quot;Bitmap sample&quot;
        src=&quot;https://nicolashollmann.de/static/6c41ed87edd0f57a1ab2363d5a500b11/5a46d/bitmap.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/6c41ed87edd0f57a1ab2363d5a500b11/12f09/bitmap.png 148w,https://nicolashollmann.de/static/6c41ed87edd0f57a1ab2363d5a500b11/e4a3f/bitmap.png 295w,https://nicolashollmann.de/static/6c41ed87edd0f57a1ab2363d5a500b11/5a46d/bitmap.png 300w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Graymap&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;P2
3 3
255
  0 100 200
 50 150 250
 75 175 255&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/609c9723fba9a5f9b9626d21974b6366/5a46d/graymap.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAABX0lEQVQ4y+3SsWrCQBjA8Yu7U30EiYOCtKEBB6dGHGpdDQSxmkbQTSLWwacwJrEKTpIqBGshuDhasB30ASwBEcw7WKzp1xThG3wBob/hpvvzHXdHCEJRFKw0TZdKJVEUHzySJOXz+cFgsFgs5vP5O0LONKYQn88HcSgUKpfLfxkoFouFQmE4HC6XSwg+kBOTg8EglLlc7t4DY7PZbL/fh92z2ewNIRdIIBDw+/2xWKzRaNRqtUdPvV6vVqvT6XS9Xq9Wq0+EXCMsy0ajUUEQer1eu91+8nQ6HU3TbNt2XXe/338j5AphGCYSifA83+12VVXVPLquK4oCMyHe7XZfyLnGlwj04XA4k8nAJbVaLdUDfbPZPB3fIBzHxeNx+F6maRqG8XwEj7zZbE7c9h2STqeTyaQsy5PJZDwevx6NRiPHcSA+HA4uQm6RVCqVSCQqlYplWRC8HMFBttvtf/zrB+sGkE0QoCE0AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Graymap sample&quot;
        title=&quot;Graymap sample&quot;
        src=&quot;https://nicolashollmann.de/static/609c9723fba9a5f9b9626d21974b6366/5a46d/graymap.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/609c9723fba9a5f9b9626d21974b6366/12f09/graymap.png 148w,https://nicolashollmann.de/static/609c9723fba9a5f9b9626d21974b6366/e4a3f/graymap.png 295w,https://nicolashollmann.de/static/609c9723fba9a5f9b9626d21974b6366/5a46d/graymap.png 300w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pixmap&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;P3
3 3
255
0   0   0   100 100 100  255 255 255
255 0   0   0   255   0    0   0 255
255 255 0   0   255 255  255   0 255&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/34e5835ccad118226eadbe315d019052/5a46d/pixmap.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAABZ0lEQVQ4y9WNTUrDQBiGv28mPYNrF0aPIGhbeoRIQQVpmibpD21BF669iAnFTbHG4kIP4FoDgmfwECbz40zUJMjsXHXy8JG88715AGoHEdW0bXsymfi+HxQnDEPXddM0lVJyzmXtwIaWKVRYRXnX3hlNx4Nw4A99RTAKel7v5VWXs4wzJkv+mIk2b++dn11Mj6ezk5lifjofd0fv6ZvBPIAKj2AA0G1uHS47rbjZWrQU7UX7YLF/FT/fRPI6YlEkS0C5S3JLzzjQH5QVkQDgRL84SS39hUPFZ1GO+ipHK0cUiByRUZSAR2v1D9JghMgSk9kzmtcG8//KWJE3yjJSVkQCgdPvMlbpDyazazTfmcxJRX5P5RLilw7IRypWOhIJcnV3e+l8PIFcUVFbV+UaubTUjOVQfVDJdCQkcn334OgVRuvr6uEVObfUjHm/KGc6EpxwfZc4Qu1mtL5uNnt1MynMa7N5I8tfLnxHsq5mkiMAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pixmap sample&quot;
        title=&quot;Pixmap sample&quot;
        src=&quot;https://nicolashollmann.de/static/34e5835ccad118226eadbe315d019052/5a46d/pixmap.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/34e5835ccad118226eadbe315d019052/12f09/pixmap.png 148w,https://nicolashollmann.de/static/34e5835ccad118226eadbe315d019052/e4a3f/pixmap.png 295w,https://nicolashollmann.de/static/34e5835ccad118226eadbe315d019052/5a46d/pixmap.png 300w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;full-sourcecode&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#full-sourcecode&quot; aria-label=&quot;full sourcecode permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Full sourcecode&lt;/h3&gt;
&lt;p&gt;You can find the full sourcecode for C and C++ on &lt;a href=&quot;https://gist.github.com/NHollmann/9c774031f44a00a003ff710371fc9b17&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;format-specifications&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#format-specifications&quot; aria-label=&quot;format specifications permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Format specifications&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Portable Bitmap: &lt;a href=&quot;http://netpbm.sourceforge.net/doc/pbm.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;PBM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Portable Graymap: &lt;a href=&quot;http://netpbm.sourceforge.net/doc/pgm.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;PGM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Portable Pixmap: &lt;a href=&quot;http://netpbm.sourceforge.net/doc/ppm.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;PPM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/writing-portable-anymap/&quot;&gt;https://nicolashollmann.de/blog/writing-portable-anymap/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Byte order: Big Endian vs. Little Endian]]></title><description><![CDATA[This article will lay the foundation for further articels on the topic
of writing binary files.  So, what exactly do I mean with byte ordering? What is this Endianness? Byte ordering When writing down numbers, for example , we compose the number of…]]></description><link>https://nicolashollmann.de/blog/byte-order/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/byte-order/</guid><pubDate>Thu, 15 Aug 2019 20:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This article will lay the foundation for further articels on the topic
of writing binary files. &lt;/p&gt;
&lt;p&gt;So, what exactly do I mean with byte ordering? What is this Endianness?&lt;/p&gt;
&lt;h2 id=&quot;byte-ordering&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#byte-ordering&quot; aria-label=&quot;byte ordering permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Byte ordering&lt;/h2&gt;
&lt;p&gt;When writing down numbers, for example &lt;code class=&quot;language-text&quot;&gt;153&lt;/code&gt;, we compose the number of multiple digits.
In this case &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;5&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;3&lt;/code&gt;. But just knowing these digits isn’t enough. Also their
position in the number, their ordering, is important. While the &lt;code class=&quot;language-text&quot;&gt;3&lt;/code&gt; alone has a higher
value than the &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt;, in our example the &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt; means one hundret, because it appers on
the first of three digits. In other words, it is the &lt;em&gt;most significant&lt;/em&gt; digit, while
the &lt;code class=&quot;language-text&quot;&gt;3&lt;/code&gt; is the &lt;em&gt;least significant&lt;/em&gt; digit.&lt;/p&gt;
&lt;p&gt;The memory of a computer is composed of bytes, which can hold a number between 0 and 255.
If you want to save bigger numbers, you can just put more bytes together.
Two bytes (or 16 bit) can hold a value between 0 and 65535. But here is the interesting thing:
In which order do you save those 2 bytes? Should the most significant byte come first in memory,
like we write down our numbers?&lt;/p&gt;
&lt;p&gt;There is no right answer to this questions as both approches were taken on different
processors. The ones who save the most significant byte first are called &lt;strong&gt;big endian&lt;/strong&gt;
and the ones who save the least significant byte first are called &lt;strong&gt;little endian&lt;/strong&gt;. In
the past there were even some &lt;strong&gt;mixed endian&lt;/strong&gt; systems with a totally crazy byte layout
but luckily they didn’t take over so we ignore them.&lt;/p&gt;
&lt;p&gt;Today the most personal computers are using little endian because it’s the endianness of the
widespread Intel x86-architecture. You should keep this in mind if you ever analyze
raw byte data. &lt;strong&gt;The bytes might be in a different order than you first expect.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you want to read more on this topic, look at the &lt;a href=&quot;https://en.wikipedia.org/wiki/Endianness&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Wikipedia article&lt;/a&gt;, it features a lot
more details.&lt;/p&gt;
&lt;h2 id=&quot;why-is-this-important-for-binary-file-formats&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#why-is-this-important-for-binary-file-formats&quot; aria-label=&quot;why is this important for binary file formats permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Why is this important for binary file formats?&lt;/h2&gt;
&lt;p&gt;The ordering of the bytes in memory isn’t really important. You calculations will work
and you don’t need to think about it. Problems arise when you try to send data from
one computer to another with a different byte ordering. This happens a lot when dealing
with internet traffic or file formats. They both are designed for sharing between different
computers.&lt;/p&gt;
&lt;p&gt;So we need to deal with different byte orderings. How?
There are two solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Allow both orderings and detect while reading which one was used.&lt;br&gt;
This way you can always write with your own ordering but if you try&lt;br&gt;
to read a file you might need to correct the ordering.&lt;br&gt;
For detection many formats use some &lt;em&gt;mark&lt;/em&gt; like the &lt;a href=&quot;https://en.wikipedia.org/wiki/Byte_order_mark&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;BOM&lt;/a&gt; of Unicode files.&lt;/li&gt;
&lt;li&gt;Just specify for the format which byte ordering should be used.&lt;br&gt;
Sounds simple and it is! Indeed, most network protocols took this approach&lt;br&gt;
and decided that everything sent should be in big endian. Also many file formats&lt;br&gt;
we look at decided to use just little endian. In this scenario we may need to convert&lt;br&gt;
the data on writing &lt;em&gt;and&lt;/em&gt; reading, if we use the “wrong” ordering.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To recap, we only need to correct data if:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;we have multibyte data. If everything just uses a single byte we don’t need a correction.&lt;/li&gt;
&lt;li&gt;we detected that our systems order doesn’t match the needed order.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&quot;how-do-we-detect-the-ordering&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#how-do-we-detect-the-ordering&quot; aria-label=&quot;how do we detect the ordering permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How do we detect the ordering?&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;I will use C++ for this articles code.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So first, lets write functions which allow us to detect the byte order:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;cpp&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-cpp line-numbers&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;union&lt;/span&gt; _endian_test &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; word&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint8_t&lt;/span&gt; byte&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;isLittleEndian&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    _endian_test test&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    test&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;word &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xAA00&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; test&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;byte&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;isBigEndian&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    _endian_test test&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    test&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;word &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xAA00&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; test&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;byte&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Whooaa, what is happing in this snippet?
The most important bit is the union at the beginning.
A &lt;a href=&quot;https://en.cppreference.com/w/cpp/language/union&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;union&lt;/a&gt; is a little bit like a struct, with some important difference:
&lt;em&gt;Every member uses exactly the same memory.&lt;/em&gt; A union is a little bit special
and they aren’t as useful as structs or classes. But for this task they shine!&lt;/p&gt;
&lt;p&gt;Let’s concentrate on the &lt;code class=&quot;language-text&quot;&gt;isLittleEndian()&lt;/code&gt;: We first create a new &lt;em&gt;endian&lt;/em&gt;test and
set the 16 bit unsigned int &lt;code class=&quot;language-text&quot;&gt;word&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;0xAA00&lt;/code&gt;. This value was picked randomly, every
other value would have worked, as long as the first byte is different from the second one.
Because one byte can be representated by exactly two hexadecimal digits it is easy to
work with them when the ordering of bytes is more important than the content itself.&lt;/p&gt;
&lt;p&gt;And now the magic: if &lt;code class=&quot;language-text&quot;&gt;test.byte[0] == 0x00&lt;/code&gt; is true, we know we are on a little endian
system. Why? Because we use a union, &lt;code class=&quot;language-text&quot;&gt;test.byte[0]&lt;/code&gt; uses the exact same memory as
the first byte of &lt;code class=&quot;language-text&quot;&gt;test.word&lt;/code&gt;. &lt;code class=&quot;language-text&quot;&gt;test.byte[1]&lt;/code&gt; would have used the same memory as
the second byte of &lt;code class=&quot;language-text&quot;&gt;test.word&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If this first byte is &lt;code class=&quot;language-text&quot;&gt;0x00&lt;/code&gt; and we put &lt;code class=&quot;language-text&quot;&gt;0xAA00&lt;/code&gt; in &lt;code class=&quot;language-text&quot;&gt;word&lt;/code&gt;, this means that the first byte
corresponds to the &lt;em&gt;least significant byte&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In &lt;code class=&quot;language-text&quot;&gt;isBigEndian()&lt;/code&gt; we use the same logic but check if &lt;code class=&quot;language-text&quot;&gt;test.byte[0] == 0xAA&lt;/code&gt; which would mean
that the first byte corresponds to the &lt;em&gt;most significant byte&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;and-how-do-we-correct-the-order&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#and-how-do-we-correct-the-order&quot; aria-label=&quot;and how do we correct the order permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;And how do we correct the order?&lt;/h2&gt;
&lt;p&gt;To correct the order, we just need to swap the bytes.
The important thing here is that you don’t do anything like adding, subtracting or so
when the ordering of the data isn’t the same as the one of your system.&lt;/p&gt;
&lt;p&gt;Let’s look at a simple byte swap for 16 bit integers:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;cpp&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-cpp line-numbers&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is short. We just shift all the bits by 8 positions in the input one time to the
left and one time to the right. Then combine the two with a binary OR. Shift operations
on &lt;strong&gt;unsigned&lt;/strong&gt; integers always shift zeros in. This doesn’t work for signed ints, so
we always need to cast them to unsigned before we swap them.&lt;/p&gt;
&lt;p&gt;This is what’s happening step by step:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;0xFF03 &amp;lt;=&amp;gt; 1111 1111 0000 0011
1111 1111 0000 0011 &amp;gt;&amp;gt; 8 =&amp;gt; 0000 0000 1111 1111
1111 1111 0000 0011 &amp;lt;&amp;lt; 8 =&amp;gt; 0000 0011 0000 0000
0000 0000 1111 1111 OR 0000 0011 0000 0000 =&amp;gt; 0000 0011 1111 1111
0000 0011 1111 1111 &amp;lt;=&amp;gt; 0x03FF&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I’ve implemented this swapping also for 32 bit and 64 bit but I just include them
without explanation. The idea is really the same, it’s just more bytes. In case of
64 bit I’ve reused the swap32 bit to avoid using tons of zeros in the source. This
way I save some error prone repetition. Try to understand what is happening yourself.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;cpp&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-cpp line-numbers&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;uint32_t&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xFF000000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x00FF0000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; 
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x0000FF00&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x000000FF&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xFFFFFFFF&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint64_t&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xFFFFFFFFL&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So now we can detect the endianness of the system and we can swap bytes. Great! This is
really everything we need. The most of the time, at least when writing data, we
know what the endianness of the data should be but we don’t know our own endianness.
So we should write helper for that job that swap the bytes only if we have a different
endianness then we want.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;cpp&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-cpp line-numbers&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;toLittleEndian16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;isLittleEndian&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; in &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;toBigEndian16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;isBigEndian&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; in &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Nothing really special. I’ve excluded the four functions for 32 bit and 64 bit but
you can find them on GitHub because I’ve bundled the code of this article as a
&lt;a href=&quot;https://nicolashollmann.de/blog/single-header-libraries/&quot;&gt;SHL&lt;/a&gt; and uploaded it on GitHub: &lt;a href=&quot;https://github.com/NHollmann/NH-Headers/blob/master/headers/nh_byteorder.hpp&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;nh_byteorder.hpp&lt;/a&gt;.&lt;/p&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/byte-order/&quot;&gt;https://nicolashollmann.de/blog/byte-order/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Binary file formats introduction]]></title><description><![CDATA[This article is just an introduction to a complete series of articles
about binary file formats. In this series we will learn about the structure of many different binary 
file formats and how to read and write them. First we will focus on
writing…]]></description><link>https://nicolashollmann.de/blog/binary-file-formats/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/binary-file-formats/</guid><pubDate>Wed, 14 Aug 2019 23:34:00 GMT</pubDate><content:encoded>&lt;p&gt;This article is just an introduction to a complete series of articles
about binary file formats.&lt;/p&gt;
&lt;p&gt;In this series we will learn about the structure of many different binary
file formats and how to read and write them. First we will focus on
writing various binary image file formats, because we can use this knowledge
in many projects on this blog.&lt;/p&gt;
&lt;p&gt;But why should you be interested in such formats? Today the most data is either
in XML or JSON and if I really need to write a binary file there is definitely
a library to do so, right?&lt;/p&gt;
&lt;p&gt;Yes, exactly. And you should use them! They are well tested and have many more features
than anything we will build in this series. But using something doesn’t mean that you
really understand it. That is, if you want to understand it at all. I was fascinated about the
inner workings of &lt;strong&gt;ANYTHING&lt;/strong&gt; since I was a young boy. And there is no better way in
understanding something than building it yourself.
Your implementation doesn’t even need to be perfect or well optimized. As long as your
implementation works it’s good enough.&lt;/p&gt;
&lt;p&gt;If you’re not as that interest in the inner workings of some of the most common file formats
we use every day, then this series will just bore you and you shouldn’t read it.&lt;/p&gt;
&lt;p&gt;But if you’re at least a little bit like me I will take you to a journy of understanding
some of the genius techniques used in everyday file formats. We will first look at
the simpler formats like Portable Anymaps and Bitmaps and then gradually go over
to the more complicated formats. While we first stick to images, I plan to look
at other formats like .tar or .zip Archives later.&lt;/p&gt;
&lt;p&gt;The article about the &lt;a href=&quot;https://nicolashollmann.de/blog/byte-order/&quot;&gt;byte order&lt;/a&gt; which will come out soon
is the foundation of many other articles in this series.&lt;/p&gt;
&lt;p&gt;So stay tuned!&lt;/p&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/binary-file-formats/&quot;&gt;https://nicolashollmann.de/blog/binary-file-formats/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Single Header Libraries]]></title><description><![CDATA[This article is targeted towards beginners. It should clarify what a single
header library (SHL) is and how you could create our own. When I use or create
single header libraries in later articles I can just link to this article for 
clarification…]]></description><link>https://nicolashollmann.de/blog/single-header-libraries/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/single-header-libraries/</guid><pubDate>Wed, 14 Aug 2019 19:20:00 GMT</pubDate><content:encoded>&lt;p&gt;This article is targeted towards beginners. It should clarify what a single
header library (SHL) is and how you could create our own. When I use or create
single header libraries in later articles I can just link to this article for
clarification.&lt;/p&gt;
&lt;p&gt;This article is about a concept in C and C++, the most other languages don’t
even have header files so you can’t create a single &lt;strong&gt;header&lt;/strong&gt; library.&lt;/p&gt;
&lt;p&gt;To understand what a single header library (SHL) is, first we look at the single parts of
this name one by one. We start with the last one, because it has the most meaning.&lt;/p&gt;
&lt;h3 id=&quot;library&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#library&quot; aria-label=&quot;library permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Library&lt;/h3&gt;
&lt;p&gt;Almost every langauge have some sort of libraries. In general one could say,
a software library is a collection of functions and datatypes, that can’t normaly
run directly. Instead it provides it’s features to other software (programs and other
libraries). This allows you to use the same code in different projects easily.
A user of a library only needs to know the public interface, not the internal
workings. Examples for libraries are the C++ STL or &lt;a href=&quot;https://www.libsdl.org&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;libSDL&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;header&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#header&quot; aria-label=&quot;header permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Header&lt;/h3&gt;
&lt;p&gt;If you ever used C or C++, you should know what a header file is. But for the sake
of completeness:&lt;/p&gt;
&lt;p&gt;In C/C++ a function must be deklared befor its name can be used in any way. This not
only holds for functions in the same source file (conventionally .c or .cpp) but also
applies to functions from different source files or external libraries. To declare them,
it is enough to write down the function header with the parameters and return type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Declaration&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Usage&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Definition / Implementation&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you may have spotted, it is even enough to just write down the types of the parameters,
their names are optional in the declaration. Still you normaly write them down too, so the
function documents itself.&lt;/p&gt;
&lt;p&gt;Declaring the function header for all functions you want to use, especially if they come from
libraries, would be really cumbersome. For this reason C and C++ use so called header files
(conventionally .h or .hpp). They include &lt;strong&gt;normaly&lt;/strong&gt; no code to run (although that would be
very possible), instead they include the function headers of alle functions that are part of
the public interface of some software module. These header files can then be included in
own source files by using &lt;code class=&quot;language-text&quot;&gt;#include&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;single&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#single&quot; aria-label=&quot;single permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Single&lt;/h3&gt;
&lt;p&gt;The “single” just means in this context that our complete library is contained in
a single file so it can be easily embedded in different projects.&lt;/p&gt;
&lt;h3 id=&quot;and-now-everything-together&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#and-now-everything-together&quot; aria-label=&quot;and now everything together permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;And now everything together!&lt;/h3&gt;
&lt;p&gt;Going by these parts of the name a SHL is nothing more than a software library which
consist of a single header file. That’s it.&lt;/p&gt;
&lt;h2 id=&quot;why-are-single-header-libraries-useful&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#why-are-single-header-libraries-useful&quot; aria-label=&quot;why are single header libraries useful permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Why are single header libraries useful?&lt;/h2&gt;
&lt;p&gt;Libraries are part of the daily life of a developer. But often it can be
cumbersome to embed them in new projects (at least in C and C++). First,
you need to compile it for all your target platforms by yourself. The
build processes may differ from library to library and often the setup and
compilation takes a lot time and don’t even work on the first try. Some
libraries even need other dependencies to work properly. In general,
many C and C++ libraries have a somewhat difficult setup which may need
to be done more than once for each platform.&lt;/p&gt;
&lt;p&gt;SHLs have a solution for that: They consist of a single file that can be copied easily
in any project. There is no big setup, they just use the build process of your own
project to compile themselves. They can also be included in the version control system
so they are available for everyone trying to build your project.&lt;/p&gt;
&lt;p&gt;This a big plus for open source projects which will be used by many people around
the world.&lt;/p&gt;
&lt;h2 id=&quot;in-which-situation-are-they-not-useful&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#in-which-situation-are-they-not-useful&quot; aria-label=&quot;in which situation are they not useful permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;In which situation are they not useful?&lt;/h2&gt;
&lt;p&gt;But even SHLs have their limits. One big problem is that you need to recompile the whole
library for every computer in every project. This takes time. Classic static or
dynamic libraries can be compiled once for each target platform and then the build
products (.lib, .so, .dll, .dylib, etc.) can be copied between computers.&lt;/p&gt;
&lt;p&gt;In my opinion, the scope of a library is the strongest criteria to decide if a SHL or a classic
library should be the way to go. Where the limit is, however, must be determined
individually for each project.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-includeuse-a-shl&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#how-do-you-includeuse-a-shl&quot; aria-label=&quot;how do you includeuse a shl permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How do you include/use a SHL?&lt;/h2&gt;
&lt;p&gt;The exact usage naturally differs from SHL to SHL but there are
some similar patterns.&lt;/p&gt;
&lt;p&gt;You decide on a source file in your project which is going to include the implementation
of the whole SHL and there you define a preprocessor constant before including the SHL.
If other source files in your project want to use the library they just need to include
it without defining the constant.&lt;/p&gt;
&lt;p&gt;Let’s look at an example: The SHL ”&lt;a href=&quot;https://github.com/nothings/stb/blob/master/stb_image.h&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;stb_image&lt;/a&gt;”
allows to read many image formats in a C/C++ program.&lt;/p&gt;
&lt;p&gt;First we need to implement the library in a C/C++ source file:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;STB_IMAGE_IMPLEMENTATION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;stb_image.h&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this file the &lt;code class=&quot;language-text&quot;&gt;stb_img.h&lt;/code&gt; header will expand all the implementations of the reading
functions. Every other source file, which also want to read images can just include
this header again but without setting &lt;code class=&quot;language-text&quot;&gt;STB_IMAGE_IMPLEMENTATION&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;stb_image.h&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Often you will observe that the file which includes the implementation will take a lot
longer in the compilation step. This is logical because even if we don’t see it, the SHL
injects a lot of functionality in this file. For this reason you should use a build system
like &lt;code class=&quot;language-text&quot;&gt;make&lt;/code&gt; which onle recompiles source files if their contents changed since the last
compilation. Additionally you should choose a source file for the implementaiton that
doesn’t change that often, maybe even a special file just for this SHL.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-create-your-own-shl&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#how-do-you-create-your-own-shl&quot; aria-label=&quot;how do you create your own shl permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How do you create your own SHL?&lt;/h2&gt;
&lt;p&gt;First, we need some code which can be used usefully in different projects.
For this article, we expect that we already have something like that.&lt;/p&gt;
&lt;p&gt;After that you must decide if your library should work with C &lt;em&gt;and&lt;/em&gt; C++ or just
for one of these languages. If you use C++ only features like classes your library
definitely won’t work with C code. A library written in C can be used in C++,
but it often feels not nativ. The usage of such a library just don’t feel &lt;em&gt;“C++ly”&lt;/em&gt;.
With the help of the preprocessor it is possible to write a SHL wich works behves
for C and C++ like anyone would expect but this may increases the effort a lot.&lt;/p&gt;
&lt;p&gt;Beside that, writing a SHL is really easy thanks to the preprocessor. Let’s look at
a example with a &lt;del&gt;useless&lt;/del&gt; fictional math library:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;c&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-c line-numbers&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;ifndef&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;MINI_MATH_H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;MINI_MATH_H&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;/* Public Interface */&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;ifdef&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;MINI_MATH_IMPLEMENTATION_H&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;/* Implementation */&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;endif&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* MINI_MATH_IMPLEMENTATION_H */&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;endif&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* MINI_MATH_H */&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Thats all to create your very own SHL.
It’s important that only on .c/.cpp file defines &lt;code class=&quot;language-text&quot;&gt;MINI_MATH_IMPLEMENTATION_H&lt;/code&gt;, otherwise
you get linker conflicts when building your project.&lt;/p&gt;
&lt;p&gt;If you want to use utility functions in the implementation that should not be part
of the public interface place them in the &lt;code class=&quot;language-text&quot;&gt;MINI_MATH_IMPLEMENTATION_H&lt;/code&gt; part.
THis also holds for external headers which are not needed for the interface.&lt;/p&gt;
&lt;p&gt;There are some different approaches in the development of a SHL. For example you could use
a program that bundles many files into a single header file. This allows you to
split your library code in meaningful submodules without loosing the comfort
of a SHL after the bundling.&lt;/p&gt;
&lt;p&gt;This is the end of this first small article.&lt;/p&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/single-header-libraries/&quot;&gt;https://nicolashollmann.de/blog/single-header-libraries/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[First Blogpost]]></title><description><![CDATA[This is my first entry in this blog. Here I will answer some general
questions about this blog. Why do I want to write a blog? I really love to read articles on Medium, Dev.to or blogs by other developers.
Often I get the desire to write about some…]]></description><link>https://nicolashollmann.de/blog/first-blogpost/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/first-blogpost/</guid><pubDate>Tue, 13 Aug 2019 14:38:00 GMT</pubDate><content:encoded>&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is my first entry in this blog. Here I will answer some general
questions about this blog.&lt;/p&gt;
&lt;h2 id=&quot;why-do-i-want-to-write-a-blog&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#why-do-i-want-to-write-a-blog&quot; aria-label=&quot;why do i want to write a blog permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Why do I want to write a blog?&lt;/h2&gt;
&lt;p&gt;I really love to read articles on Medium, Dev.to or blogs by other developers.
Often I get the desire to write about some themes myself. Other than that, I like
to teach people new skills. Because of that I decided to start my own blog.&lt;/p&gt;
&lt;p&gt;I will experiment with publishing articles from this site on Medium and Dev.to too.
I don’t know if I keep it that way.&lt;/p&gt;
&lt;h2 id=&quot;what-is-this-blog-about&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#what-is-this-blog-about&quot; aria-label=&quot;what is this blog about permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What is this blog about?&lt;/h2&gt;
&lt;p&gt;Everything I like to write about. I don’t want to restrict myself on this term.
In gerneral, the content will be of a technical nature, most of the time about software
dvelopment. Some themes will be more adequate for beginners, others for intermediate to
advanced users. The most articles are expecting that you already can progam. Some of the
articles will be more like a tutorial, that you can work through. Otherwise they are
about things I want to learn myself so they only describe how I approch something.&lt;/p&gt;
&lt;h2 id=&quot;who-am-i-anyway&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#who-am-i-anyway&quot; aria-label=&quot;who am i anyway permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Who am I anyway?&lt;/h2&gt;
&lt;p&gt;To answer this questions I have the &lt;a href=&quot;https://nicolashollmann.de/aboutme/&quot;&gt;about me&lt;/a&gt; site. Currently it isn’t really
filled with content but I will change that. Under &lt;a href=&quot;https://nicolashollmann.de/tags/project/&quot;&gt;projects&lt;/a&gt; you can find a selection
of projects I’ve worked on in the past.&lt;/p&gt;
&lt;h2 id=&quot;how-can-you-reach-me&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#how-can-you-reach-me&quot; aria-label=&quot;how can you reach me permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How can you reach me?&lt;/h2&gt;
&lt;p&gt;Best via Twitter, because I look there regulary. DMs are open for everyone, but you just can
tag me on a twwet, I will read that too. Also, for private requests you can reach me with the
email in the legal notices. It may take some time until I answere there.&lt;/p&gt;
&lt;p&gt;I really would appreciate feedback on the articles on this blog. At the moment I don’t have
a comment feature because I don’t want to embed external providers like Disqus. Maybe I
write an own system for that, but for now there aren’t any comments possible.&lt;/p&gt;
&lt;h2 id=&quot;which-system-do-i-use-for-this-blog&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#which-system-do-i-use-for-this-blog&quot; aria-label=&quot;which system do i use for this blog permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Which system do I use for this blog?&lt;/h2&gt;
&lt;p&gt;This site was created with &lt;a href=&quot;https://www.gatsbyjs.org&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Gatsbyjs&lt;/a&gt;. It is a
static site generator which takes &lt;a href=&quot;https://reactjs.org&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;React&lt;/a&gt; applications written in JavaScript
and builds HTML, CSS and JS Bundles from that. This allws me to use a simple WebServer without
any serverside logic to operate this blog. I write the articles as Markdown files. Who knows,
maybe I write an article about Gatsby ;)&lt;/p&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/first-blogpost/&quot;&gt;https://nicolashollmann.de/blog/first-blogpost/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Project: Grenzhus Geo]]></title><description><![CDATA[Introduction Grenzhus Geo is an Android App I developed back in middle school together with my
classmate Adrian. It was a voluntary project to improve our history grades.
Although it’s really some time ago (the app was last updated on November 2…]]></description><link>https://nicolashollmann.de/blog/grenzhus-geo/</link><guid isPermaLink="false">https://nicolashollmann.de/blog/grenzhus-geo/</guid><pubDate>Mon, 12 Aug 2019 21:53:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;introduction&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#introduction&quot; aria-label=&quot;introduction permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Grenzhus Geo is an Android App I developed back in middle school together with my
classmate Adrian. It was a voluntary project to improve our history grades.
Although it’s really some time ago (the app was last updated on November 23, 2014) I
thought it could be interesting to write about it and recap our results.&lt;/p&gt;
&lt;p&gt;So, whats the point of the app?&lt;/p&gt;
&lt;h2 id=&quot;the-app&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#the-app&quot; aria-label=&quot;the app permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The App&lt;/h2&gt;
&lt;p&gt;Our school was located not far away from the former &lt;a href=&quot;https://en.wikipedia.org/wiki/Inner_German_border&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;inner german border&lt;/a&gt; which
split the DDR from the BRD. We contacted a border museum called ”&lt;a href=&quot;http://www.grenzhus.de&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Grenzhus&lt;/a&gt;” in
Schlagsdorf. With their help we developed a concept for a geo-caching like app where
the user needs to find barcodes which we placed on some historical points along the
former border. Each point has its own descriptive text which included some historical
facts about that point.&lt;/p&gt;
&lt;p&gt;This way a user can walk around the area and search for barcodes while also learning
something about history. &lt;/p&gt;
&lt;h2 id=&quot;technical-challanges&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#technical-challanges&quot; aria-label=&quot;technical challanges permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Technical challanges&lt;/h2&gt;
&lt;p&gt;I think one of the biggest challanges was reading out the GPS sensor and converting its
coordinates to the correct pixel spots on screen. We decided to embed a own, simplified
map and not Google Maps or Open Street Map. The app only makes sense if you are near
Schlagsdorf so there is no need for a fancy, scrollable map.&lt;/p&gt;
&lt;p&gt;To convert the coordinates we just used a linear interpolation between some points we
determined empirically. This is definitly the wrong way to do it but the total area
of we needed to support wasn’t that big it did work really good.&lt;/p&gt;
&lt;h2 id=&quot;screenshots&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#screenshots&quot; aria-label=&quot;screenshots permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Screenshots&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;center&quot;&gt;The map view&lt;/th&gt;
&lt;th align=&quot;center&quot;&gt;The detail view&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 349px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/7f2178067d48323edd1334e6e9f9b0a2/e9bf8/screenshot-map.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 177.7027027027027%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAkCAIAAAAGkY33AAAACXBIWXMAAAsTAAALEwEAmpwYAAAFOklEQVRIx41WWWwbVRSdtGlslyxtldhpmsUe27g0DulO3UUNzUdooMpHaN2qIJoP/ov46B+qBBJCgMQvolRClfrRApWgSiUCamKPl9jOjh07dRLHsRM78T7xPgt35nmJUxA9Orrz5vqed++791k2hr0CqqqqwIrF4q6uroGBgSNHjnR0dLS3t2NSmaqz60LLoXb4rJmHpIiWSrS2th4+fLi7u1sul8t4YOd779y6Hbt46cbld3r7+i739/f39PT09fX19vYqlUpFJUAAOXEcBz1YTKF6W3X009dVJ9TqN9RqqKurs7MTrFqt7uAhewlSqbSQWSw+IGmqlUk7Dh5saWxsbGpqgvrhIZE0y3AcoiRwEEQeUHxbWytaYwKBsL7+wJkzGthvz549ewEiUU119YH9+986ffrY0aNCgEAgEon4p0ClUkFRQh7YdR7Xrl3VarXXb9zQvj/40Wdffvz9o6Gvf7j1DfDe0Lc/Dn33083bd7RXB69XAmO3g2HAUPFEwm4vvQJyFJXJ5bLZLFsJrKgqANazBuLvX36GBZ3P01R+i4znc1mapkKhcCqVyufzuVyOj2TKmXMsS6aS7mfPyEgkmk7RFFXw53KxWCwei0UikVAoBOtMJsPno0HMwBO2cj15sjoxsUEQ9NZWOp3eCAZLdZJbyQhJhiMRyByNRmP8RplMGoMC1tbWApub82NjxpERunhU8EMESZLJdDq2vj734EEoHAZnhgdFUVxmSLKwsEAYDKaJCZPVupVIoGyBwPrc3NzszMycw/HXk18fffE5OOPx+ObmJk3TqFFYNptxuVw6vd5oMo3pdD6/H4n9fj+I5x0O98rK1O+/eYeH4+m03+dLkCT0jOI6wjfsxQt3Sby4uIhq3j6S4MZGKpOBFkDBKC10kaLynDgQDELZBqNx3GKZnp4uDQ/ikIUmMYVSy4DBc2Loinl8fHRsbMXrhaqQhim2DQCt8kPz8/mKC8Vyo+JqsNlsUPnk1FRxhkUlH7fw8OFXn9yeHB7mbk65BP7M8O50ufQGA+hnZmfB4/F4CILQ6/V/jIyMm81/Pn06evfu4uPH+crLjKHdYQDo2HqCcDqdyeQWlJBMJhOJBAwPRg39CScSVPHalTODHjppt9tRz0d1uqnpaVBuzxINBpMkye7IDAbNLRwOQ52QHPRQs06nm7PbY6FQ3GpNxuOu+/fpVGr7V60gLl43BhqGkqMtoAtmOPm9e855B8uUR1BxZpg/csKV1xPQNsJgNCEazWadxWq3O+IJkt45agaDoWYyudJ4olHSZpsmCLPBMF4gYTKarBbLpMezSiZS23Nj68xNYID9ADHM3lrNap3R91yxK2VGrzjC7y4lB5dTg6s5bZD9EAVjS6x4iZXwlicjXmabV9iDO+hlWzxsM3B5WzDmYRU7yfwHX4oEsdzD4ssMzlmaWyDC6/8S87JSoI/FV1jcz+I+nl721cSm1aHnrgG9u83sVT53KvVu5ahTaVtXeNl/yV+qCxGbCWtsa8cng7LZiAL0Fh9YxWxEvkjhL7Jydw7nmMcXsjh4uDMy8rJ4IiA1e3HrGqeZ2lBYfMqJgIJYVHJ0K4BWv8LkUTyfV8LuK6x0iZaVyz51TnjqvPDkOaDo2Jma4xoBrE+cFcJac7H29IW9J84KTp7lAo5ras5datD01HEeLl6Ifvg5u69B0tqirKkRoX8DQmFts1jWeki+a9cu5Kmv39/9pqapsa30XwHbvbvA6uqqurrXBIJqCAbCor6+tqGhDvzwKXhEIkHDvjoUgCT/APg0cKBS5PcIAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of the map view&quot;
        title=&quot;A screenshot of the map view&quot;
        src=&quot;https://nicolashollmann.de/static/7f2178067d48323edd1334e6e9f9b0a2/e9bf8/screenshot-map.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/7f2178067d48323edd1334e6e9f9b0a2/12f09/screenshot-map.png 148w,https://nicolashollmann.de/static/7f2178067d48323edd1334e6e9f9b0a2/e4a3f/screenshot-map.png 295w,https://nicolashollmann.de/static/7f2178067d48323edd1334e6e9f9b0a2/e9bf8/screenshot-map.png 349w&quot;
        sizes=&quot;(max-width: 349px) 100vw, 349px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/td&gt;
&lt;td align=&quot;center&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 349px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;https://nicolashollmann.de/static/d11120ff113e83b70f2ef584c5549c9c/e9bf8/screenshot-text.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 177.7027027027027%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAkCAIAAAAGkY33AAAACXBIWXMAAAsTAAALEwEAmpwYAAAHY0lEQVRIx31WeVRTZxZ/oBWyvPfyghBCQkASFhFIMgSyQzBsYQkgMKiFWhFEUUHAKuggM1Vh5FTnoOACllGham1dOoWyKksIa9ghgQBTbdVxZmp1pnbmn+mZuS/Q/jnv3HPP7917f/e7y5fzgnh68Gg0KvJ/Hzs7O9AuLi6BgYFJSUn+/v48Hg9eET9/kVAcweHwXF1ZbDbb1fawWKyfgQtoN9vD5XL9/PyEIiGfz9/ktQn4SFjkyR37X8uU2yO3RsbG6LVanUYTrYvVq5Vbo6MSdLFJcnk4jyfw9PD24Am4XC+OmwfPne/B43t4CJBgmUa6tShULpYrfWQKP7mSFHVYAGClerMqPEAq9wkSuQvFvCARTyj2AAkIWhOkc8rhXs/6L4z0BwPoAwP6xSB634B+OUQD/Hkv+tBIf2jEWkbwBwOMzim0ZRT/yoQPfU0ZWKYaV6jI+AusfZzVOcXsmGB2zxC9ZuLRLGFYJPoWGJ2TzD4L0WcBQHw1xuwxMx7PMyZf0lf+7bj4A8X6IwXI9M4p4DDaxwkg9y/ifRZSesw4hAIY/hptHWP2LeAkeY4x9i1qeUMB8sI/Kcj0X2ld04xHswzQraNwOAPigANx3dMMeIV04AXdNUWAEbTpGX3uFZUkT72kQcSjOVI6pog15jyj14IDAVxwIOBeM76aF0DPPGP4CQaHkye3TxCDf8YGlrGOCWJgCQMMEV3TTCBb3zou/ehotQkJ3v4CbGVb/kGBGua/p86/pprfUM2vqXOvaKDBYsPUVe/sK1JI8B0JzG9s5PHn9JYRp14LtEcYl8iSYLYwrY5JJhTfMUlAO/1Wsm2QgSW8a4bRNs7sX8DJsoEMET3zuMEK1ZKDAQwtdM8Stt5QW6sMwJCla4aA+MW3jlDj2sDIGU4zgAxxnZMMyAqHGBYxyAKjAhdkh7nARMBuXMGg7bVVTfyFDlbYyupIu6bJVRusOAwWcsFiIB3kXV2nYRGHLBAJnZNlw42BZZDbmiUvEGSBIm1XgswFFoPt2kDNnVOQBYf5zfydtjawiRf0bhjGDOnrIGsjzwEyXLjH82TN0C2khusJZwI2PafDJYGxL8DJY9/AT8IJglpGmKDbTESbiey8dZRoA8KYU7sJFo72WbCBJdS4ghqXyRsx+ZIKO0bmvqcOPcHGX6DDTzGoauY7uuk5+LDRb3HTM6xvCR18Sl/8gfbsvw5Pf3L45idH0CBP/uO49C8K0mehX2x0bbiDXm1iXG0mLt3Aa69Tzzc41FxD624wersEra3eF5soteBqdrnURNTfYjbecb583al7HENuPGT6cIQqKT9c7hWyha0I4oq8nVRCdoLcJznC62x6aK5GHiEjhEK2RMILEXFUwbxYlbcygF9zxRn57PEmpVIXE+UmkzkqZE5alZtKgmfG8w5tFxRncRoqdpXkpAT4IUFBlNBQ9zC5pybEXacSREv9Lje6ILe7NoWrddFRLIGvva//enEgVbTlnSPFmQUFuwsOZV5rbqqpb6w4Xf6HC9VRsTFqZWiMaotO7hkh8qprcEbuP2IfeD/r/R3Bwi0bE7W+yXECiZB+ob6x6OS5+O17UrNyjv22qul+W2f/UFa6PkotTtL4R0s5is3uFy8zkbttmDyAnRHvm71NlJMm2qbx1IcLKs/XVNZeKy2rOF5R+VHdx413/3T9k+ZtWp8oCTdc7BYpdQnmM6tr6MjtFnog11keyNKEuKZqOfHyjZk6YXVdfVXtx6dPnDp15ET1R7UNn37Z8MfG1K2bNSKuKsA1NtRN5s08d4GG3GnFArmsMBFbJ+Xr1bwMLW9/mub8lca8otLC4xVHT53NLyk7XVNfd+Vysto7NtQrUcZP0whihZyGBgJp7XH5tUKUlxRalRddnC7LTxEWZ8dfunm3vLq26Ddnjp85V1x+5sTvL56uqk7X+GdGiXbHSUt2anaohTdvuCGGEd7nFXvvVb336ZmdV0v1J/doy/ZElh3MzM9Oz85KO1xScOjAvmNHisqK9u3Ty4oyIipyEivzUwqToptv8pDOfnb5zrgPcyNPZGkq9+qKd4TnJAQXpityE8SFu2La2+7lpqhK0pXH3tWWvBtZuD2yfHdC1f7kkpTo27e8kAddG5NlwqzYgGyduDhdkaMPydOHHEhVHkpV/O5wxtjEaGFW4v5k+QeZMUczYz7I1JW+F39qb+LhpMjmJk+kpc8lIyooNy0gWx94NEualxq8N1l8MFWyTy85W5o6auou3hWXp/9VRW7c2YOJH+bFVR5MqC6Iy9crb932QGb+Rhsw48NWwjCPjliJYSvTaMYHLfjQAjHxhGV+5TdsdRq0MEzLzuMrzsOLxOiyk2nZyQjfnecoEqywk4YjoWGIPMJeokJCwhB4BQlRg2VdiMoOAPkahoBXHeWg1L4jsVkkSrvVDz+pcZzNcfPdsGHtX4KjI53lwudyfOzt161aMIwpDFQ4b+T98l8BAdeqrFtvh6K0DQ7r7ewREAAYRsNxOtjBCxYKZQPOQB1sAauU/wFrQ2CcrcUoDgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of the detail view&quot;
        title=&quot;A screenshot of the detail view&quot;
        src=&quot;https://nicolashollmann.de/static/d11120ff113e83b70f2ef584c5549c9c/e9bf8/screenshot-text.png&quot;
        srcset=&quot;https://nicolashollmann.de/static/d11120ff113e83b70f2ef584c5549c9c/12f09/screenshot-text.png 148w,https://nicolashollmann.de/static/d11120ff113e83b70f2ef584c5549c9c/e4a3f/screenshot-text.png 295w,https://nicolashollmann.de/static/d11120ff113e83b70f2ef584c5549c9c/e9bf8/screenshot-text.png 349w&quot;
        sizes=&quot;(max-width: 349px) 100vw, 349px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;conclusion&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#conclusion&quot; aria-label=&quot;conclusion permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;It was a fun project and one of my first contacts with Android App development.
I’ve learned about our history and also about geological coordinates.&lt;/p&gt;
&lt;p&gt;A big problem was the missing iPhone support. While it wasn’t possible to spend
79€ at that time for a yearly apple developer membership, the missing support
excluded about half of our classmates.&lt;/p&gt;
&lt;p&gt;Also, in retrospective the design of the app was really ugly.&lt;/p&gt;
&lt;p&gt;A big shout out to our history teacher and Dr. Wagner who gave us this oppertunity
and supported us during the development.&lt;/p&gt;
&lt;h2 id=&quot;links&quot; style=&quot;position:relative;&quot;&gt;&lt;a href=&quot;#links&quot; aria-label=&quot;links permalink&quot; class=&quot;anchor before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Links&lt;/h2&gt;
&lt;p&gt;The App is not maintained anymore, neither digital nor were the barcodes replaced.
But you can find it on the &lt;a href=&quot;https://play.google.com/store/apps/details?id=grenzhus.de.geosearch&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;Google playStore&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here are some german news articles regarding our app:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.svz.de/lokales/gadebusch-rehnaer-zeitung/schueler-entwickeln-programm-zur-digitalen-schnitzeljagd-an-der-grenze-id6737766.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;SVZ June 04, 2014&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.svz.de/lokales/gadebusch-rehnaer-zeitung/geschichte-als-digitale-schnitzeljagd-id6770181.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;SVZ June 07, 2014&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.ln-online.de/Lokales/Nordwestmecklenburg/Mit-der-App-das-Grenzhus-erkunden&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;&gt;LN Online June 07, 2014&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
                                    &lt;div style=&quot;margin-top=55px; font-style: italic;&quot;&gt;
                                        This article was posted to my blog at nicolashollmann.de. 
                                        You can read it online at &lt;a href=&quot;https://nicolashollmann.de/blog/grenzhus-geo/&quot;&gt;https://nicolashollmann.de/blog/grenzhus-geo/&lt;/a&gt;.&lt;/div&gt;</content:encoded></item></channel></rss>