1
0
Fork 0
mirror of https://github.com/ganelson/inform.git synced 2024-07-16 22:14:23 +03:00
inform7/docs/inblorb/3-bs6.html

170 lines
16 KiB
HTML
Raw Normal View History

2019-03-17 14:40:57 +02:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
2020-04-14 19:56:54 +03:00
<title>Base64</title>
2020-03-19 02:11:25 +02:00
<meta name="viewport" content="width=device-width initial-scale=1">
2019-03-17 14:40:57 +02:00
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en-gb">
2020-03-19 02:11:25 +02:00
<link href="../inweb.css" rel="stylesheet" rev="stylesheet" type="text/css">
2020-04-14 19:56:54 +03:00
2019-03-17 14:40:57 +02:00
</head>
<body>
2020-03-19 02:11:25 +02:00
<nav role="navigation">
2020-04-14 19:56:54 +03:00
<h1><a href="../index.html">
<img src="../docs-src/Figures/Inform.png" height=72">
</a></h1>
<ul><li><a href="../compiler.html">compiler tools</a></li>
<li><a href="../other.html">other tools</a></li>
2020-03-19 02:11:25 +02:00
<li><a href="../extensions.html">extensions and kits</a></li>
<li><a href="../units.html">unit test tools</a></li>
2020-04-14 19:56:54 +03:00
</ul><h2>Other Tools</h2><ul>
<li><a href="index.html"><span class="selectedlink">inblorb</span></a></li>
2020-03-19 02:11:25 +02:00
<li><a href="../indoc/index.html">indoc</a></li>
<li><a href="../inpolicy/index.html">inpolicy</a></li>
<li><a href="../inrtps/index.html">inrtps</a></li>
2020-04-14 19:56:54 +03:00
</ul><h2>Foundation</h2><ul>
2020-03-19 02:11:25 +02:00
<li><a href="../../../inweb/docs/foundation-module/index.html">foundation</a></li>
2020-04-14 19:56:54 +03:00
</ul>
2020-03-19 02:11:25 +02:00
</nav>
<main role="main">
2020-04-14 19:56:54 +03:00
<!--Weave of 'Base64' generated by 7-->
<ul class="crumbs"><li><a href="../index.html">Home</a></li><li><a href="../other.html">Other Tools</a></li><li><a href="index.html">inblorb</a></li><li><a href="index.html#3">Chapter 3: Other Material</a></li><li><b>Base64</b></li></ul><p class="purpose">To produce base64-encoded story files ready for in-browser play by a Javascript-based interpreter such as Parchment.</p>
2019-03-17 14:40:57 +02:00
<p class="inwebparagraph"><a id="SP1"></a><b>&#167;1. </b>The Base64 encoding scheme is defined by the Internet standard RFC 1113.
Broadly, the idea is to take a binary stream of bytes, break it into
threes, and then convert this into a sequence of four emailable
characters. To encode 24 bits in four characters, we need six bits per
2020-04-14 19:56:54 +03:00
character, so we need \(2^6 = 64\) characters in all. Since \(64 = 26 +
26 + 10 + 2\(, we can nearly get there with alphanumeric characters alone,
2019-03-17 14:40:57 +02:00
adding just two others &mdash; conventionally, plus and forward-slash.
That's more or less the whole thing, except that we use an equals sign
to indicate incompleteness of the final triplet (which might have only
1 or 2 bytes in it).
</p>
<p class="inwebparagraph">RFC 1113 permits white space to be used freely, including in particular
line breaks, but we don't avail ourselves.
</p>
<pre class="display">
<span class="reserved">char</span><span class="plain"> *</span><span class="identifier">RFC1113_table</span><span class="plain"> = </span><span class="string">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="</span><span class="plain">;</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP2"></a><b>&#167;2. </b>The encoding routine is as follows.
</p>
<pre class="display">
2020-04-14 19:56:54 +03:00
<span class="reserved">void</span><span class="plain"> </span><span class="functiontext">Base64::encode<button class="popup" onclick="togglePopup('usagePopup118')">...<span class="popuptext" id="usagePopup118">Usage of <b>Base64::encode</b>:<br>Releaser - <a href="3-rls.html#SP6_5">&#167;6.5</a></span></button></span><span class="plain">(</span><span class="reserved">filename</span><span class="plain"> *</span><span class="identifier">in_filename</span><span class="plain">, </span><span class="reserved">filename</span><span class="plain"> *</span><span class="identifier">out_filename</span><span class="plain">, </span><span class="reserved">text_stream</span><span class="plain"> *</span><span class="identifier">top</span><span class="plain">, </span><span class="reserved">text_stream</span><span class="plain"> *</span><span class="identifier">tail</span><span class="plain">) {</span>
<span class="reserved">FILE</span><span class="plain"> *</span><span class="identifier">IN</span><span class="plain"> = </span><span class="functiontext"><a href="../../../inweb/docs/foundation-module/6-bf.html#SP8">BinaryFiles::open_for_reading</a></span><span class="plain">(</span><span class="identifier">in_filename</span><span class="plain">);</span>
2019-03-17 14:40:57 +02:00
<span class="reserved">text_stream</span><span class="plain"> </span><span class="identifier">TO_struct</span><span class="plain">;</span>
<span class="reserved">text_stream</span><span class="plain"> *</span><span class="identifier">TO</span><span class="plain"> = &amp;</span><span class="identifier">TO_struct</span><span class="plain">;</span>
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">STREAM_OPEN_TO_FILE</span><span class="plain">(</span><span class="identifier">TO</span><span class="plain">, </span><span class="identifier">out_filename</span><span class="plain">, </span><span class="constant">UTF8_ENC</span><span class="plain">) == </span><span class="constant">FALSE</span><span class="plain">)</span>
2020-04-14 19:56:54 +03:00
<span class="functiontext"><a href="1-be.html#SP2">BlorbErrors::fatal_fs</a></span><span class="plain">(</span><span class="string">"can't open base-64 encoded story file for output"</span><span class="plain">, </span><span class="identifier">out_filename</span><span class="plain">);</span>
<span class="functiontext"><a href="3-bs6.html#SP2">Base64::encode_inner</a></span><span class="plain">(</span><span class="identifier">TO</span><span class="plain">, </span><span class="identifier">IN</span><span class="plain">, </span><span class="identifier">top</span><span class="plain">, </span><span class="identifier">tail</span><span class="plain">);</span>
<span class="functiontext"><a href="../../../inweb/docs/foundation-module/6-bf.html#SP8">BinaryFiles::close</a></span><span class="plain">(</span><span class="identifier">IN</span><span class="plain">);</span>
2019-03-17 14:40:57 +02:00
<span class="identifier">STREAM_CLOSE</span><span class="plain">(</span><span class="identifier">TO</span><span class="plain">);</span>
<span class="plain">}</span>
2020-04-14 19:56:54 +03:00
<span class="reserved">void</span><span class="plain"> </span><span class="functiontext">Base64::encode_inner<button class="popup" onclick="togglePopup('usagePopup119')">...<span class="popuptext" id="usagePopup119">Usage of <b>Base64::encode_inner</b>:<br>none</span></button></span><span class="plain">(</span><span class="constant">OUTPUT_STREAM</span><span class="plain">, </span><span class="reserved">FILE</span><span class="plain"> *</span><span class="identifier">IN</span><span class="plain">, </span><span class="reserved">text_stream</span><span class="plain"> *</span><span class="identifier">top</span><span class="plain">, </span><span class="reserved">text_stream</span><span class="plain"> *</span><span class="identifier">tail</span><span class="plain">) {</span>
2019-03-17 14:40:57 +02:00
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">top</span><span class="plain">) </span><span class="identifier">WRITE</span><span class="plain">(</span><span class="string">"%S"</span><span class="plain">, </span><span class="identifier">top</span><span class="plain">);</span>
<span class="reserved">while</span><span class="plain"> (</span><span class="constant">TRUE</span><span class="plain">) {</span>
2020-04-07 03:06:09 +03:00
<span class="reserved">int</span><span class="plain"> </span><span class="identifier">triplet</span><span class="plain">[3], </span><span class="identifier">triplet_size</span><span class="plain"> = </span><span class="constant">0</span><span class="plain">;</span>
2019-03-17 14:40:57 +02:00
&lt;<span class="cwebmacro">Read the triplet of binary bytes, storing 0 to 3 in the size read</span> <span class="cwebmacronumber">2.1</span>&gt;<span class="plain">;</span>
2020-04-07 03:06:09 +03:00
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">triplet_size</span><span class="plain"> == </span><span class="constant">0</span><span class="plain">) </span><span class="reserved">break</span><span class="plain">;</span>
2019-03-17 14:40:57 +02:00
<span class="reserved">int</span><span class="plain"> </span><span class="identifier">quartet</span><span class="plain">[4];</span>
&lt;<span class="cwebmacro">Convert triplet to a quartet</span> <span class="cwebmacronumber">2.2</span>&gt;<span class="plain">;</span>
<span class="reserved">int</span><span class="plain"> </span><span class="identifier">i</span><span class="plain">; </span><span class="reserved">for</span><span class="plain"> (</span><span class="identifier">i</span><span class="plain">=0; </span><span class="identifier">i</span><span class="plain">&lt;4; </span><span class="identifier">i</span><span class="plain">++) </span><span class="identifier">PUT</span><span class="plain">(</span><span class="identifier">RFC1113_table</span><span class="plain">[</span><span class="identifier">quartet</span><span class="plain">[</span><span class="identifier">i</span><span class="plain">]]);</span>
2020-04-07 03:06:09 +03:00
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">triplet_size</span><span class="plain"> &lt; </span><span class="constant">3</span><span class="plain">) </span><span class="reserved">break</span><span class="plain">;</span>
2019-03-17 14:40:57 +02:00
<span class="plain">}</span>
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">tail</span><span class="plain">) </span><span class="identifier">WRITE</span><span class="plain">(</span><span class="string">"%S"</span><span class="plain">, </span><span class="identifier">tail</span><span class="plain">);</span>
<span class="plain">}</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP2_1"></a><b>&#167;2.1. </b>If the file ends in mid-triplet, we pad out with zeros.
</p>
<p class="macrodefinition"><code class="display">
&lt;<span class="cwebmacrodefn">Read the triplet of binary bytes, storing 0 to 3 in the size read</span> <span class="cwebmacronumber">2.1</span>&gt; =
</code></p>
<pre class="displaydefn">
<span class="identifier">triplet</span><span class="plain">[0] = </span><span class="identifier">fgetc</span><span class="plain">(</span><span class="identifier">IN</span><span class="plain">);</span>
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">triplet</span><span class="plain">[0] != </span><span class="identifier">EOF</span><span class="plain">) {</span>
<span class="identifier">triplet_size</span><span class="plain">++;</span>
<span class="identifier">triplet</span><span class="plain">[1] = </span><span class="identifier">fgetc</span><span class="plain">(</span><span class="identifier">IN</span><span class="plain">);</span>
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">triplet</span><span class="plain">[1] != </span><span class="identifier">EOF</span><span class="plain">) {</span>
<span class="identifier">triplet_size</span><span class="plain">++;</span>
<span class="identifier">triplet</span><span class="plain">[2] = </span><span class="identifier">fgetc</span><span class="plain">(</span><span class="identifier">IN</span><span class="plain">);</span>
<span class="reserved">if</span><span class="plain"> (</span><span class="identifier">triplet</span><span class="plain">[2] != </span><span class="identifier">EOF</span><span class="plain">)</span>
<span class="identifier">triplet_size</span><span class="plain">++;</span>
<span class="plain">}</span>
<span class="plain">}</span>
2020-04-07 03:06:09 +03:00
<span class="reserved">int</span><span class="plain"> </span><span class="identifier">i</span><span class="plain">; </span><span class="reserved">for</span><span class="plain"> (</span><span class="identifier">i</span><span class="plain">=</span><span class="identifier">triplet_size</span><span class="plain">; </span><span class="identifier">i</span><span class="plain">&lt;3; </span><span class="identifier">i</span><span class="plain">++) </span><span class="identifier">triplet</span><span class="plain">[</span><span class="identifier">i</span><span class="plain">] = </span><span class="constant">0</span><span class="plain">;</span>
2019-03-17 14:40:57 +02:00
</pre>
<p class="inwebparagraph"></p>
<p class="endnote">This code is used in <a href="3-bs6.html#SP2">&#167;2</a>.</p>
2019-03-17 14:40:57 +02:00
<p class="inwebparagraph"><a id="SP2_2"></a><b>&#167;2.2. </b><code class="display">
&lt;<span class="cwebmacrodefn">Convert triplet to a quartet</span> <span class="cwebmacronumber">2.2</span>&gt; =
</code></p>
<pre class="displaydefn">
2020-04-07 03:06:09 +03:00
<span class="reserved">int</span><span class="plain"> </span><span class="identifier">i</span><span class="plain">; </span><span class="reserved">for</span><span class="plain"> (</span><span class="identifier">i</span><span class="plain">=0; </span><span class="identifier">i</span><span class="plain">&lt;4; </span><span class="identifier">i</span><span class="plain">++) </span><span class="identifier">quartet</span><span class="plain">[</span><span class="identifier">i</span><span class="plain">] = </span><span class="constant">0</span><span class="plain">;</span>
<span class="identifier">quartet</span><span class="plain">[0] += (</span><span class="identifier">triplet</span><span class="plain">[0] &amp; </span><span class="constant">0xFC</span><span class="plain">) &gt;&gt; </span><span class="constant">2</span><span class="plain">;</span>
<span class="identifier">quartet</span><span class="plain">[1] += (</span><span class="identifier">triplet</span><span class="plain">[0] &amp; </span><span class="constant">0x03</span><span class="plain">) &lt;&lt; </span><span class="constant">4</span><span class="plain">;</span>
<span class="identifier">quartet</span><span class="plain">[1] += (</span><span class="identifier">triplet</span><span class="plain">[1] &amp; </span><span class="constant">0xF0</span><span class="plain">) &gt;&gt; </span><span class="constant">4</span><span class="plain">;</span>
<span class="identifier">quartet</span><span class="plain">[2] += (</span><span class="identifier">triplet</span><span class="plain">[1] &amp; </span><span class="constant">0x0F</span><span class="plain">) &lt;&lt; </span><span class="constant">2</span><span class="plain">;</span>
<span class="identifier">quartet</span><span class="plain">[2] += (</span><span class="identifier">triplet</span><span class="plain">[2] &amp; </span><span class="constant">0xC0</span><span class="plain">) &gt;&gt; </span><span class="constant">6</span><span class="plain">;</span>
<span class="identifier">quartet</span><span class="plain">[3] += (</span><span class="identifier">triplet</span><span class="plain">[2] &amp; </span><span class="constant">0x3F</span><span class="plain">) &lt;&lt; </span><span class="constant">0</span><span class="plain">;</span>
2019-03-17 14:40:57 +02:00
<span class="reserved">switch</span><span class="plain"> (</span><span class="identifier">triplet_size</span><span class="plain">) {</span>
2020-04-07 03:06:09 +03:00
<span class="reserved">case</span><span class="plain"> </span><span class="constant">1</span><span class="plain">: </span><span class="identifier">quartet</span><span class="plain">[2] = </span><span class="constant">64</span><span class="plain">; </span><span class="identifier">quartet</span><span class="plain">[3] = </span><span class="constant">64</span><span class="plain">; </span><span class="reserved">break</span><span class="plain">;</span>
<span class="reserved">case</span><span class="plain"> </span><span class="constant">2</span><span class="plain">: </span><span class="identifier">quartet</span><span class="plain">[3] = </span><span class="constant">64</span><span class="plain">; </span><span class="reserved">break</span><span class="plain">;</span>
2019-03-17 14:40:57 +02:00
<span class="plain">}</span>
</pre>
<p class="inwebparagraph"></p>
<p class="endnote">This code is used in <a href="3-bs6.html#SP2">&#167;2</a>.</p>
2019-03-17 14:40:57 +02:00
<hr class="tocbar">
<ul class="toc"><li><a href="3-wm.html">Back to 'Website Maker'</a></li><li><i>(This section ends Chapter 3: Other Material.)</i></li></ul><hr class="tocbar">
2019-04-22 17:42:10 +03:00
<!--End of weave-->
2020-04-14 19:56:54 +03:00
<script>
MathJax = {
tex: {
inlineMath: '$', '$'], ['\\(', '\\)'
},
svg: {
fontCache: 'global'
}
};
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
<script>
function togglePopup(material_id) {
var popup = document.getElementById(material_id);
popup.classList.toggle("show");
}
</script>
<link href="Popups.css" rel="stylesheet" rev="stylesheet" type="text/css">
2020-03-19 02:11:25 +02:00
</main>
2019-03-17 14:40:57 +02:00
</body>
</html>