<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE rfc [
  <!ENTITY nbsp    "&#160;">
  <!ENTITY zwsp   "&#8203;">
  <!ENTITY nbhy   "&#8209;">
  <!ENTITY wj     "&#8288;">
]>

<rfc number="9285" docName="draft-faltstrom-base45-12" xmlns:xi="http://www.w3.org/2001/XInclude" ipr="trust200902" submissionType="IETF" category="info" obsoletes="" updates="" consensus="true" xml:lang="en" tocInclude="true" symRefs="true" sortRefs="true" version="3">
  <!-- xml2rfc v2v3 conversion 3.12.10 -->
  <front>
    <title abbrev="Base45">
      The Base45 Data Encoding
    </title>
    <seriesInfo name="RFC" value="9285" /> 
    <author fullname="Patrik Fältström" initials="P." surname="Fältström">
      <organization>Netnod</organization>
      <address>
        <email>paf@netnod.se</email>
      </address>
    </author>
    <author fullname="Fredrik Ljunggren" initials="F." surname="Ljunggren">
      <organization>Kirei</organization>
      <address>
        <email>fredrik@kirei.se</email>
      </address>
    </author>
    <author fullname="Dirk-Willem van Gulik" initials="D." surname="van Gulik">
      <organization>Webweaving</organization>
      <address>
        <email>dirkx@webweaving.org</email>
      </address>
    </author>
    <date month="July" year="2022"/>
    <keyword>BASE45</keyword>
    <abstract>
      <t>
	This document describes the Base45 encoding scheme, which is
	built upon the Base64, Base32, and Base16 encoding schemes.
      </t>
    </abstract>
  </front>
  <middle>
    <section anchor="intro" numbered="true" toc="default">
      <name>Introduction</name>
      <t>
	A QR code is used to encode text as a graphical
	image. Depending on the characters used in the text, various
	encoding options for a QR code exist, e.g., Numeric,
	Alphanumeric, and Byte mode.

<!-- [rfced] FYI, we have rephrased this sentence as follows to avoid
awkward hyphenation. Please let us know if you prefer otherwise.

Original:
   Even in Byte
   mode a typical QR-code reader tries to interpret a byte sequence as a
   UTF-8 or ISO/IEC 8859-1 encoded text

Current:
   Even in Byte
   mode, a typical QR code reader tries to interpret a byte sequence as
   text encoded in UTF-8 or ISO/IEC 8859-1.
-->

	Even in Byte mode, a typical
	QR code reader tries to interpret a byte sequence as text encoded in UTF-8
	or ISO/IEC 8859-1. Thus, QR codes cannot be used
	to encode arbitrary binary data directly. Such data has to be
	converted into an appropriate text before that text could be
	encoded as a QR code.  Compared to already established Base64,
	Base32, and Base16 encoding schemes that are described in
	<xref target="RFC4648" format="default"></xref>, the Base45 scheme
	described in this document offers a more compact QR code
	encoding.
      </t>
      <t>
	One important difference from those others and Base45 is the
	key table and that the padding with '=' is not required.
      </t>
    </section>
    <section numbered="true" toc="default">
      <name>Conventions Used in This Document</name>
        <t>
    The key words "<bcp14>MUST</bcp14>", "<bcp14>MUST NOT</bcp14>", "<bcp14>REQUIRED</bcp14>", "<bcp14>SHALL</bcp14>", "<bcp14>SHALL
    NOT</bcp14>", "<bcp14>SHOULD</bcp14>", "<bcp14>SHOULD NOT</bcp14>", "<bcp14>RECOMMENDED</bcp14>", "<bcp14>NOT RECOMMENDED</bcp14>",
    "<bcp14>MAY</bcp14>", and "<bcp14>OPTIONAL</bcp14>" in this document are to be interpreted as
    described in BCP&nbsp;14 <xref target="RFC2119"/> <xref target="RFC8174"/> 
    when, and only when, they appear in all capitals, as shown here.
        </t>
    </section>
    <section numbered="true" toc="default">
      <name>Interpretation of Encoded Data</name>
      <t>
	Encoded data is to be interpreted as described in <xref target="RFC4648" format="default"></xref> with the exception that a
	different alphabet is selected.
      </t>
    </section>
    <section numbered="true" toc="default">
      <name>The Base45 Encoding</name>
      <t>
	QR codes have a limited ability to store binary data. In
	practice, binary data have to be encoded in characters
	according to one of the modes already defined in the standard
	for QR codes. The easiest mode to use in called Alphanumeric
	mode (see Section 7.3.4 and Table 2 of <xref target="ISO18004" format="default"></xref>.  Unfortunately
	Alphanumeric mode uses 45 different characters which implies
	neither Base32 nor Base64 are very effective encodings.
      </t>
      <t>
	A 45-character subset of US-ASCII is used; the 45 characters
	usable in a QR code in Alphanumeric mode (see Section 7.3.4
	and Table 2 of <xref target="ISO18004" format="default"></xref>). Base45 encodes 2 bytes in 3 characters,
	compared to Base64 that encodes 3 bytes in 4 characters.
      </t>
      <t>
	For encoding, two bytes [a, b] <bcp14>MUST</bcp14> be interpreted as a number
	n in Base256, i.e. as an unsigned integer over 16 bits so
	that the number n = (a*256) + b.
      </t>
      <t>
	This number n is converted to Base45 [c, d, e] so that n = c
	+ (d*45) + (e*45*45). Note the order of c, d and e which are
	chosen so that the left-most [c] is the least significant.
      </t>
      <t>
	The values c, d and e are then looked up in <xref target="table1"/> to produce
	a three character string. The process is reversed when
	decoding.
      </t>
      <t>
	For encoding a single byte [a], it <bcp14>MUST</bcp14> be interpreted as a
	Base256 number, i.e. as an unsigned integer over 8 bits. That
	integer <bcp14>MUST</bcp14> be converted to Base45 [c d] so that a = c +
	(45*d). The values c and d are then looked up in <xref target="table1"/> to
	produce a two-character string.
      </t>
      <t>
	A byte string [a b c d ... x y z] with arbitrary content and
	arbitrary length <bcp14>MUST</bcp14> be encoded as follows: From left to
	right pairs of bytes <bcp14>MUST</bcp14> be encoded as described above. If the
	number of bytes is even, then the encoded form is a string
	with a length that is evenly divisible by 3. If the number of
	bytes is odd, then the last (rightmost) byte <bcp14>MUST</bcp14> be encoded
	on two characters as described above.
      </t>
      <t>
	For decoding a Base45 encoded string the inverse operations
	are performed.
      </t>
      <section numbered="true" toc="default">
<!--[rfced] We suggest updating this section title for readability. 
Do you have a preference for A or B or otherwise?

Original:
4.1.  When to, and not to, use Base45

A) 4.1.  When to Use Base45
B) 4.1.  When to Use and Not Use Base45
-->
        <name>When to, and not to, use Base45</name>
        <t>
	  If binary data is to be stored in a QR code, the suggested
	  mechanism is to use the Alphanumeric mode that uses 11 bits
	  for 2 characters as defined in Section 7.3.4 of <xref target="ISO18004" format="default"></xref>. The ECI mode
	  indicator for this encoding is 0010.
        </t>
        <t>
	  On the other hand if the data is to be sent via some other
	  transport, a transport encoding suitable for that transport
	  should be used instead of Base45. For example, it is not
	  recommended to first encode data in Base45 and then encode
	  the resulting string in Base64 if the data is to be sent via
	  email. Instead, the Base45 encoding should be removed, and
	  the data itself should be encoded in Base64.
        </t>
      </section>
      <section numbered="true" toc="default">
        <name>The Alphabet Used in Base45</name>
        <t>
	  The Alphanumeric mode is defined to use 45 characters as specified
	  in this alphabet.
        </t>
       
<table anchor="table1"> 
  <name>The Base45 Alphabet</name>
  <thead>
    <tr>
      <th>Value Encoding</th>    
      <th>Value Encoding</th>
      <th>Value Encoding</th>
      <th>Value Encoding</th>
    </tr>
  </thead>
  <tbody>         
    <tr>
      <td>00 0</td>
      <td>12 C</td>
      <td>24 O</td>
      <td>36 Space</td>
    </tr>
    <tr>
      <td>01 1</td>
      <td>13 D</td>
      <td>25 P</td>
      <td>37 $</td>
    </tr>
    <tr>
      <td>02 2</td>
      <td>14 E</td>
      <td>26 Q</td>
      <td>38 %</td>
    </tr>
    <tr>
      <td>03 3</td>
      <td>15 F</td>
      <td>27 R</td>
      <td>39 *</td>
     </tr>
    <tr>
      <td>04 4</td>
      <td>16 G</td>
      <td>28 S</td>
      <td>40 +</td>
    </tr>
<tr>
      <td>05 5</td>
      <td>17 H</td>
      <td>29 T</td>
      <td>41 -</td>
    </tr>
<tr>
      <td>06 6</td>
      <td>18 I</td>
      <td>30 U</td>
      <td>42 .</td>
    </tr>
<tr>
      <td>07 7</td>
      <td>19 J</td>
      <td>31 V</td>
      <td>43 /</td>
    </tr>
<tr>
      <td>08 8</td>
      <td>20 K</td>
      <td>32 W</td>
      <td>44 :</td>
    </tr>
    <tr>
      <td>09 9</td>
      <td>21 L</td>
      <td>33 X</td>
      <td></td>
    </tr>
      <tr>
      <td>10 A</td>
      <td>22 M</td>
      <td>34 Y</td>
      <td></td>
    </tr>
<tr>
      <td>11 B</td>
      <td>23 N</td>
      <td>35 Z</td>
      <td></td>
    </tr>
  </tbody>
</table>
      </section>
      <section numbered="true" toc="default">
        <name>Encoding Examples</name>
        <t>
	  It should be noted that although the examples are all text,
	  Base45 is an encoding for binary data where each octet can
	  have any value 0-255.
        </t>
<!-- alternative formatting -->

<t>Option (a): added line breaks</t>

<t>Encoding example 1:</t>
<ul empty="true" spacing="compact">
  <li>The string is "AB".
  </li>
  <li>As ASCII, the byte sequence is [65 66].
  </li>
  <li>The 16-bit value is 65 * 256 + 66 = 16706.
  </li>
  <li>16706 equals 11 + (11 * 45) + (8 * 45 * 45), so in Base45, it is [11&nbsp;11&nbsp;8].
  </li>
  <li>Referring to <xref target="table1"/>, the encoded string is "BB8".
  </li>
</ul>

<t>Encoding example 2:</t>
<ul empty="true" spacing="compact">
  <li>The string is "Hello!!".
  </li>
  <li>As ASCII, the byte sequence is [72 101 108 108 111 33 33].
  </li>
  <li>The 16-bit values are [18533 27756 28449 33].
  </li>
  <li>In Base45, it is [[38 6 9] [36 31 13] [9 2 14] [33 0]]. Note the 33 for the last byte.
  </li>
  <li>Referring to <xref target="table1"/>, the encoded string is "%69 VD92EX0". Note that it includes a space.
  </li>
</ul>

<t>Encoding example 3:</t>
<ul empty="true" spacing="compact">
  <li>The string is "base-45".
  </li>
  <li>As ASCII, the byte sequence is [98 97 115 101 45 52 53].
  </li>
  <li>The 16-bit values are [25185 29541 11572 53].
  </li>
  <li>In Base45, it is [[30 19 12] [21 26 14] [7 32 5] [8 1]].
  </li>
  <li>Referring to <xref target="table1"/>, the encoded string is "UJCLQE7W581".
  </li>
</ul>

<!--
<t>TEST: using the artwork element</t>

<t>Encoding example 2:</t>
<artwork>
Hello!!                            // ASCII string
[72 101 108 108 111 33 33]         // byte sequence (decimal values)
[18533 27756 28449 33]             // each 16-bit value
[[38 6 9] [36 31 13] [9 2 14] [33 0]] // Base45
%69 VD92EX0                        // encoded by referring to Table 1
                                   // Note: It includes a space.
</artwork>

<t>Encoding example 3:</t>
<artwork>
base-45                            // ASCII string
[98 97 115 101 45 52 53]           // byte sequence (decimal values)
[25185 29541 11572 53]             // each 16-bit value
[[30 19 12] [21 26 14] [7 32 5] [8 1]] // Base45
UJCLQE7W581                        // encoded by referring to Table 1
</artwork>
-->

      </section>
      <section numbered="true" toc="default">
        <name>Decoding Examples</name>
        <t>Decoding example 1:</t> 
<ul empty="true" spacing="compact">
  <li>The encoded string is "QED8WEX0".
  </li>
  <li>Referring to <xref target="table1"/>, this represents [26 14 13 8 32 14 33 0].
  </li>
  <li>Arranging the numbers in chunks of three, except for the last one which can be two numbers, we get [[26 14 13] [8 32 14] [33 0]].
  </li>
  <li>In Base45, we get [26981 29798 33].
  </li>
  <li>The byte sequence is [[105 101] [116 102] [33]].
  </li>
  <li>The ASCII is string "ietf!".
  </li>
        </ul>

<!--
<t>Decoding example 1 (based on the original):</t>
<artwork>
QED8WEX0                      // encoded string
[26 14 13 8 32 14 33 0]       // values in Table 1
[[26 14 13] [8 32 14] [33 0]] // arranged in groups of three, except 
                              //   for the last one
[26981 29798 33]              // Base45
[[105 101] [116 102] [33]]    // byte sequence (decimal values)
ietf!                         // ASCII string
</artwork>

<t>Perhaps should be:</t>
<artwork>
QED8WEX0                      // encoded string
[26 14 13 8 32 14 33 0]       // values in Table 1
[[26 14 13] [8 32 14] [33 0]] // Base45 (arranged in groups of three,
                              //   except the last, which is two)
[26981 29798 33]              // each 16-bit value
[105 101 116 102 33]          // byte sequence (decimal values)
ietf!                         // ASCII string
</artwork>
-->

      </section>
    </section>
    <section numbered="true" toc="default">
      <name>IANA Considerations</name>
      <t>
	This document has no IANA actions.
      </t>
    </section>
    <section numbered="true" toc="default">
      <name>Security Considerations</name>

<!-- [rfced] To make the sentence parallel, we suggest adding 
a noun after "similar". 

Original:
When implementing encoding and decoding it is important to be
very careful so that buffer overflow or similar does not
occur.  

Suggested:
When implementing encoding and decoding it is important to be
very careful so that buffer overflow or similar issues do not
occur.  
-->

      <t>
	When implementing encoding and decoding it is important to be
	very careful so that buffer overflow or similar does not
	occur.  This of course includes the calculations in Base45
	and lookup in the table of characters (<xref target="table1"/>). A decoder
	must also be robust regarding input, including proper handling
	of any octet value 0-255, including the NUL character (ASCII
	0).
      </t>
      <t>
	It should be noted that Base64 and some other encodings pad
	the string so that the encoding starts with an aligned number
	of characters while Base45 specifically avoids padding. Because of
	this, special care has to be taken when odd numbers of octets
	are to be encoded. Similarly, care must be taken if the number
	of characters to decode are not evenly divisible by 3.
      </t>
      <t>
	Base encodings use a specific, reduced alphabet to encode
	binary data. Non-alphabet characters could exist within
	base-encoded data, caused by data corruption or by
	design. Non-alphabet characters may be exploited as a "covert
	channel", where non-protocol data can be sent for nefarious
	purposes. Non-alphabet characters might also be sent in order
	to exploit implementation errors leading to, for example, buffer
	overflow attacks.
      </t>
      <t>
	Implementations <bcp14>MUST</bcp14> reject any input that is not a valid
	encoding. For example, it <bcp14>MUST</bcp14> reject the input (encoded data)
	if it contains characters outside the base alphabet (in <xref target="table1"/>) when interpreting base-encoded data.
      </t>
      <t>
	Even though  a Base45-encoded string contains only characters
	from the alphabet in <xref target="table1"/>,  cases like the following have to
	be considered: The string "FGW" represents 65535 (FFFF in Base16),  which  is  a  valid  encoding of  16  bits.  A  slightly
	different  encoded string  of  the same  length, "GGW",  would
	represent 65536  (10000 in Base16), which is  represented by
	more  than  16  bits.  Implementations <bcp14>MUST</bcp14>  also  reject  the
	encoded data  if it  contains a  triplet of  characters that,
	when decoded, results in an  unsigned integer that is greater
	than 65535 (ffff in Base16).
      </t>
      <t>
	It should be noted that the resulting string after encoding to
	Base45 might include non-URL-safe characters so if the URL
	including the Base45 encoded data has to be URL-safe, one
	has to use percent-encoding.
      </t>
    </section>
  </middle>
  <back>
    <references>
      <name>Normative References</name>
      <xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4648.xml"/>
      <xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"/>
      <xi:include href="https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.8174.xml"/>

      <reference anchor="ISO18004" target="https://www.iso.org/standard/62021.html">
        <front>
          <title>
	    Information technology - Automatic identification and data capture
	    techniques - QR Code bar code symbology specification
          </title>
          <author>
            <organization>ISO/IEC</organization>
          </author>
          <date month="February" year="2015"/>
        </front>
        <seriesInfo name="ISO/IEC" value="18004:2015"/>
      </reference>
    </references>
    <section numbered="false" toc="default">
      <name>Acknowledgements</name>
      <t>
	The authors thank <contact fullname="Mark Adler"/>, <contact fullname="Anders Ahl"/>, <contact fullname="Alan Barrett"/>, <contact fullname="Sam Spens Clason"/>, <contact fullname="Alfred Fiedler"/>, <contact fullname="Tomas Harreveld"/>, <contact fullname="Erik Hellman"/>, <contact fullname="Joakim Jardenberg"/>, <contact fullname="Michael Joost"/>, <contact fullname="Erik Kline"/>, <contact fullname="Christian Landgren"/>, <contact fullname="Anders Lowinger"/>, <contact fullname="Mans Nilsson"/>, <contact fullname="Jakob Schlyter"/>, <contact fullname="Peter Teufl"/>, and <contact fullname="Gaby Whitehead"/> for the feedback. Also, everyone who has been working with Base64 over a long period of years and has proven the implementations are stable.
      </t>
    </section>
  </back>
</rfc>
