<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
    <!ENTITY rfc2119 PUBLIC '' 
      'http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml'>
    <!ENTITY rfc3339 PUBLIC ''
      'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3339.xml'>
    <!ENTITY rfc3629 PUBLIC ''
      'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3629.xml'>
    <!ENTITY rfc5905 PUBLIC ''
      'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5905.xml'>
      ]>      

<rfc category="exp" ipr="trust200902" docName="draft-ruoska-encoding-02">

<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>

<?rfc toc="yes" ?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes"?>
<?rfc iprnotified="no" ?>
<?rfc strict="yes" ?>

  <front>
    <title>Ruoska Encoding</title>
    <author initials="JP" surname="Makela" fullname="Jukka-Pekka Makela">
      <organization/>
      <address>
        <postal>
          <street></street>
          <city>Janakkala</city>
          <region></region>
          <code></code>
          <country>Finland</country>
        </postal>
      </address>
    </author>
    <date/>
    <abstract>
      <t>
        This document describes hierarchically structured binary encoding format called the Ruoska Encoding (RSK). The main design goals are minimal resource usage, well defined structured with good selection of widely known data types, and still extendable for future usage.
      </t>
      <t>
        The main benefit when compared to non binary hierarchically structured formats like XML is simplicity and minimal resource demands. Even basic XML parsing is time and memory consuming operation.
      </t>
      <t>
        When compared to other binary formats like encoding formats of ASN.1 the main benefit is simplicity. ASN.1 with different encondings is complex and even simple implementation needs a lot of effort.
      </t>
    </abstract>
  </front>

  <middle>
    <section title="Introduction">
      <t>
        The RSK is very suitable for fast prototyping and studying data exchange and storage solutions in environments with wide range of different kind of devices from small microcontrollers to mainframe computers.     
      </t>
      <t>
        Only small subset of all features and data types are strictly required for every single implementation. This enables implementations for minimal systems like microcontrollers with modest computing capacity and memory. Minimal requirements are defined in <xref target="minimal_requirements" />.
      </t>
      <t>
        This version defines also mechanisms for later extensions. Actual structures and data types are not known but common structure for extended features is defined so they can be skipped by implementation versions which are not able interpret them. See <xref target="forward_compatibility"/> for details.
      </t>
    </section>
    
    <section anchor="document_structure" title="Document Structure">
      <t>
        The principal entity of RSK document is frame. Two main classes of frames exist. Meta Frames to define structure and Data Frames to hold actual payload data. Additional frame class is Extended Frame which enables forward compatibility with future versions. See <xref target="forward_compatibility"/> for details.
      </t>
      <t>
        All frames start with Leading Byte which defines frame type and some type depended instructions. Some meta frames and all data frames can be tagged with an identifier. Identifier type is defined in Leading Byte. If identifier exists it is placed right after the Leading Byte. In Data Frames payload comes after identifier. Meta Frames may also have payload or special fields or both. Data type of the payload is defined by frame type and type depended instructions. All frame types are explained in <xref target="frame" />.
      </t>
      <t>
        All implementations may not be able to handle all frame types but minimal requirements are defined in <xref target="minimal_requirements" />.
      </t>
      <t>
        RSK document is structured as finite tree. The tree is rooted to Begin Frame. After the rooting Begin Frame follows data frames as leafs and Begin – End Frame pairs as branches. Branches may contain data frames as leafs and again Begin – End Frame pairs as sub branches. Nesting levels start from 0 and maximum level is artifially limited to 255 to keep implementations simple.
      </t>
      <t>
        RSK document always ends with End Frame. Use of End Frame is two fold. It is used to return from branch to parent level and terminate the document. So document must always start with Begin Frame and end with End Frame. Root nesting level 0 must not contain any other than rooting Begin and terminating End Frames. Between root Begin and terminating End Frame is nesting level 1. Nesting level 1 may contain data frames or branches or both.
      </t>
      <figure anchor="structure" title="Tree Structure">
        <artwork><![CDATA[
Example Tree Structure

0  1  2  Nesting levels
|  |  |
Begin[id:tractor]                              Begin Frame at level 0
|  String[id:manufacturer, value:Valmet]         Leaf at level 1
|  String[id:model, value:33D]                   Leaf at level 1
|  Begin[id:engine]                              Branch at level 1
|  |  String[id:fuel, value:Diesel]                Leaf at level 2
|  |  Int8[signed:true, id:horsepower, value:37]   Leaf at level 2
|  End                                           Ending branch
End                                            Terminating at level 0
]]>
        </artwork>
      </figure>
      <t>
      </t>
      <section anchor="endianness" title="Endianness">
        <t>
          Big-endian networking byte order is used. Endianness applies to all integer and floating point numbers. This includes payload of any data frames like Integer, Float, and Timestamp and 16-bits wide integer identifier values and also meta data fields like length of payload. Canonical network byte order is fully described in RFC791, Appendix B.
        </t>
      </section>
      <section anchor="string_encoding" title="String Encoding">
        <t>
          All strings are UTF-8 encoded. This applies to string identifiers and payload of String and DateTime Frames.
        </t>
        <t>
          Implementations using String Frame, String Identifier, or DateTime Frame must be able to validate UTF-8 encoding. See <xref target="RFC3629">RFC 3629</xref> for details.
        </t>
      </section>
    </section>
    <section anchor="frame" title="Frame">
      <t>        
        As mentioned earlier the principal entity is frame. This section explains all frame types and type dependent special instructions in detail.
      </t>
      <section title="Leading Byte">
        <t>        
          All frames start with Leading Byte. Leading Byte determines frame type and type dependent instructions. The most significant bit is reserved for Extended Frames which may be introduced in later versions. See <xref target="extended_frame" /> and <xref target="forward_compatibility" /> for details.
        </t>
        <t>
          Leading Byte is presented as bit array where left-most bit is the most significant bit. MSB 0 bit numbering scheme is used with two exceptions. Left-most bit is reserved for Extended Frame and thus marked as 'X' for all Leading Byte definitions. Also some bits are marked with 'R' meaning that they are reserved for later use and must not be set in this version.
        </t>
        <t>
          Leading Byte is followed by frame type dependent fields like Identifier or Payload or both. These fields are presented as labeled byte blocks with possible lengths in bytes or in kilobytes like 64k.
        </t>
        <figure title="Leading Byte">
        <artwork><![CDATA[
   Leading Byte    Example Field   Example Payload
+-+-------+-----+ +-------------+ +---------------+
|X|1|2|3|4|5|6|7| |   4 or 8    | |    0 - 64k    |
+-+-------+-----+ +-------------+ +---------------+
 | \     / \   /
 |  \   /   \ /
 |   \ /     | Frame type dependent instructions bits
 |    |
 |    | Frame type bits
 |
 | Extended frame bit
 ]]>
        </artwork>
      </figure>
          <t>
            <list style="hanging">
              <t hangText="Example Field:">Example of possible frame type dependent byte field. 4 or 8 means that field can be 4 or 8 bytes long. Actual length can be determined by frame type, instruction bits, or some other field.</t>
              <t hangText="Example Payload:">Example of possible frame type dependent payload field. 0 - 64k means that field can be from 0 to 65535 bytes long. Actual length can be determined by frame type, instruction bits, or some other field.</t>
              <t hangText="Frame type dependent instructions bits:">These bits determine type dependent instructions. See specific frame type sections for details.</t>
              <t hangText="Frame type bits:">This bit field determines frame type.</t>
              <t hangText="Extended frame bit:">Extended frame bit. See <xref target="extended_frame" /> for details.</t>
            </list>
          </t>  
      </section>
      <section title="Meta Frames">
        <t>
          Meta Frames define document structure. Some of the Meta Frames are optional for minimal implementations. See <xref target="minimal_requirements" /> for details.
        </t>      
        <section title="Begin Frame">
          <t>
            Document and branches starts with Begin Frame. Begin Frame may have an identifier. More details about tree structure see <xref target="document_structure" />.
          </t>
          <figure title="Begin Frame">
            <artwork><![CDATA[
  Leading Byte     Identifier
+-+-------+-+---+ +----------+
|X|1|2|3|4|R|6|7| | 0 - 256  |
+-+-------+-+---+ +----------+
   \     /   \ /
    \   /     | Id bits
     \ /
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Type bits:">Begin Frame type: 0000</t>
            </list>
          </t>          
        </section>

        <section title="End Frame">
          <t>
            End Frame is used to return from branch to parent level in tree structure and also used to terminate a document. More details about tree structure see <xref target="document_structure" />.
          </t>
          <figure title="End Frame">
            <artwork><![CDATA[
  Leading Byte  
+-+-------+-----+
|X|1|2|3|4|R|R|R|
+-+-------+-----+
   \     /
    \   /
     \ /
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Type bits:">End Frame type: 0001</t>
            </list>
          </t>
        </section>
        <section title="Modifier Frame">
          <t>
            Modifier Frame is used to apply a modifier for example compression or encryption to same portions of document. RSK does not define any modifiers. When used they are defined in specifications based on RSK.
          </t>
          <t>
            Modified payload must consists of valid frames. On reading phase payload and meta data is passed to demodifier routine and then handled like ordinary frames. Modifier Frame is wrapper for other frames but not a sub branch for document tree structure. Payload may contain sub branches but branches must return to same nesting level as at beginning of payload.
          </t>
          <t>
            Meta field is intended to be used to store modifier specific data. For example original data length and padding byte count for a block compression. Meaning of Meta data must be defined in specifications based on RSK when Modifier Frame is used.
          </t>
          <figure anchor="modifier_frame" title="Modifier Frame">
            <artwork><![CDATA[

   Leading Byte   Identifier  MetLen Meta   Length   Payload
+-+-------+-+---+ +---------+ +----+------+ +----+ +---------+
|X|1|2|3|4|R|6|7| | 0 - 256 | | 2  | 0-4k | | 2  | | 0 - 64k |
+-+-------+-+---+ +---------+ +----+------+ +----+ +---------+
   \     /   \ /
    \   /     | Id bits
     \ /    
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="MetLen:">16-bits wide unsigned integer telling length of meta data. MetLen must not exceed 4095 bytes!</t>
              <t hangText="Meta:">Custom meta data.</t>
              <t hangText="Length:">16-bits wide unsigned integer telling length of modified payload data.</t>
              <t hangText="Payload:">Modified payload data.</t>
              <t hangText="Type bits:">Modifier Frame type: 0010</t>
            </list>
          </t>
        </section>
        <section anchor="array_frame" title="Array Frame">
          <t>
            Fixed length Integer or Float frames with null or integer identifier can be enclosed into an array. This saves some resources when dealing with large number of items. This can be also used to make portions of encoded document random accessible.
          </t>
          <t>
            Array itself and each item may have identifiers. All items are same type. All items have identifier of same type. Frame and identifier type for all items are defined by CLB (Common Leading Byte).
          </t>
          <figure title="Array Frame">
            <artwork><![CDATA[                                              
  Leading Byte    Identifier   CLB    Length     II      IP
+-+-------+-+---+ +---------+ +---+ +-------+ +-------+-------+ 
|X|1|2|3|4|5|6|7| | 0 - 256 | | 1 | | 1 - 2 | | 0 - 4 | 1 - 8 | * * *
+-+-------+-+---+ +---------+ +---+ +-------+ +-------+-------+
   \     / | \ /                              |                     |
    \   /  |  | Id bits                       \---- Array items ----/
     \ /   |
      |    | Length bit
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">Array identifier. See <xref target="identifiers" /> for details.</t>
              <t hangText="CLB:"> Common Leading Byte for all Items. Determines type of Item and it's identifier. Frame type must be Integer or Float and identifier type must be Null, Uint8, or Uint32.</t>
              <t hangText="Length:">8 or 16-bits wide unsigned integer telling length of array items in bytes.</t>
              <t hangText="II:">Item Identifier. See <xref target="identifiers" /> for details.</t>
              <t hangText="IP:">Item Payload.</t>
              <t hangText="Length bit:">If bit is set then Length is 16-bits otherwise 8-bits wide unsigned integer.</t>
              <t hangText="Type bits:">Array Frame type: 0011</t>
            </list>
          </t>        
        </section>
      </section>
      <section anchor="data_frames" title="Data Frames">
        <t>
          Data Frames are collection of widely used data types. There are frames for boolean, integer and floating point numbers, UTF-8 encoded strings, dates, and timestamps. There is also frame for raw binary data. All data frames can be tagged with an identifier.
        </t>
        <section title="Boolean Frame">
          <t>
            Boolean frame can hold single True or False value.
          </t>
          <figure title="Boolean Frame">
            <artwork><![CDATA[
   Leading Byte    Identifier  
+-+-------+-+---+ +----------+ 
|X|1|2|3|4|5|6|7| | 0 - 256  |
+-+-------+-+---+ +----------+ 
   \     / | \ /
    \   /  |  | Id bits
     \ /   |
      |    | Boolean bit
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Boolean bit:">If Boolean bit is set when boolean value is True otherwise False.</t>
              <t hangText="Type bits:">0100</t>
            </list>
          </t>
        </section>
        <section title="Integer Frames">
          <t>
            Wide range of integer types are supported. Integer width is defined by choosing corresponding Integer Frame type. Integers can be defined as unsigned by setting Unsigned bit in Leading Byte otherwise they are signed. Negative values of signed integers are presented in two's complement notation.
          </t>
          <t>
            Integer payload is always stored in big-endian format. See <xref target="endianness" /> for details.
          </t>
          <figure title="Integer Frames">
            <artwork><![CDATA[
   Leading Byte    Identifier       Payload
+-+-------+-+---+ +----------+ +--------------+
|X|1|2|3|4|5|6|7| | 0 - 256  | |  1,2,4 or 8  |
+-+-------+-+---+ +----------+ +--------------+
   \     / | \ /
    \   /  |  | Id bits
     \ /   |
      |    | Unsigned bit
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Unsigned bit:">If Unsigned bit is set when payload is unsigned otherwise signed integer.</t>
              <t hangText="Payload:">Payload integer value.</t>
              <t hangText="Type bits:">Integer Frame types
                <list style="hanging">
                  <t hangText="0101">8-bits wide integer.</t>
                  <t hangText="0110">16-bits wide integer.</t>
                  <t hangText="0111">32-bits wide integer.</t>
                  <t hangText="1000">64-bits wide integer.</t>
                </list>
              </t>
            </list>
          </t>
        </section>
        <section title="Float Frames">
          <t>
             Floating point number precision is defined by choosing corresponding Float Frame type. Floats are presented in IEEE754 standard format and endianness big-endian. See <xref target="endianness" /> for details.
          </t>
          <figure title="Float Frames">
            <artwork><![CDATA[
   Leading Byte    Identifier      Payload
+-+-------+-+---+ +----------+ +------------+
|X|1|2|3|4|R|6|7| | 0 - 256  | |  2,4 or 8  |
+-+-------+-+---+ +----------+ +------------+
   \     /   \ /
    \   /     | Id bits
     \ /    
      |  
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Payload:">Payload float value.</t>
              <t hangText="Type bits:">Float Frame types
                <list style="hanging">
                  <t hangText="1001">16-bits wide half precision float.</t>
                  <t hangText="1010">32-bits wide single precision float.</t>
                  <t hangText="1011">64-bits wide double precision float.</t>
                </list>
              </t>
            </list>
          </t>
        </section>
        <section title="String Frame">
          <t>
            String Frame can hold UTF-8 encoded string. If implementation supports String Frame it must be able to validate UTF-8  encoding. See <xref target="string_encoding" /> for details.
          </t>
          <figure title="String Frame">
            <artwork><![CDATA[
   Leading Byte    Identifier   Length       Payload
+-+-------+-+---+ +----------+ +-------+ +-------------+
|X|1|2|3|4|5|6|7| | 0 - 256  | | 1 - 2 | |   0 - 64k   |
+-+-------+-+---+ +----------+ +-------+ +-------------+
   \     / | \ /
    \   /  |  | Id bits
     \ /   |
      |    | Length bit
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Length:">8 or 16-bits wide unsigned integer telling length of string in bytes.</t>
              <t hangText="Length bit:">If bit is set then Length is 16-bits otherwise 8-bits wide unsigned integer.</t>
              <t hangText="Payload:">UTF-8 encoded string.</t>
              <t hangText="Type bits:">String Frame type: 1100</t>
            </list>
          </t>
        </section>
        <section title="Binary Frame">
          <t>
            Single Binary Frame can hold up to 65535 bytes and obviously a document may contain as many binary frames as needed. So the only issue with big binary masses is data fragmentation to multiple frames.
          </t>
          <figure title="Binary Frame">
            <artwork><![CDATA[
   Leading Byte    Identifier   Length       Payload
+-+-------+-+---+ +----------+ +-------+ +-------------+
|X|1|2|3|4|5|6|7| | 0 - 256  | | 1 - 2 | |   0 - 64k   |
+-+-------+-+---+ +----------+ +-------+ +-------------+
   \     / | \ /
    \   /  |  | Id bits
     \ /   |
      |    | Length bit
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Length:">8 or 16-bits wide unsigned integer telling length of payload in bytes.</t>
              <t hangText="Length bit:">If bit is set then Length is 16-bits otherwise 8-bits wide unsigned integer.</t>
              <t hangText="Payload:">Binary payload.</t>
              <t hangText="Type bits:">Binary Frame type: 1101</t>
            </list>
          </t>
        </section>
        <section title="DateTime Frame">
          <t>
            DateTime Frame holds date or date and time in UTC timescale as UTF-8 encoded string. Date is presented as ("YYYY-MM-DD") and DateTime is ("YYYY-MM-DDTHH:MM:SS.SSSZ"). String formats are compatible with <xref target="RFC3339">RFC 3339</xref>.
          </t>
          <t>
            If implementation supports DateTime Frame it must be able to validate UTF-8  encoding. See <xref target="string_encoding" /> for details.
          </t>          
          <figure title="DateTime Frame">
            <artwork><![CDATA[
   Leading Byte    Identifier  Date/DateTime
+-+-------+-+---+ +----------+ +-----------+
|X|1|2|3|4|5|6|7| | 0 - 256  | | 10 or 24  |
+-+-------+-+---+ +----------+ +-----------+
   \     / | \ /
    \   /  |  | Id bits
     \ /   |
      |    | DateTime bit
      |
      | Type bits]]>
            </artwork>
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Date/DateTime:">Date or DateTime string.</t>
              <t hangText="DateTime bit:">If bit is set then payload is DateTime otherwise Date.</t>
              <t hangText="Type bits:">DateTime Frame type: 1110</t>
            </list>
          </t>
        </section>
        <section title="Timestamp Frame">
          <t>
            Timestamp is very similar, with some exceptions, as NTP Date Format defined in <xref target="RFC5905">RFC 5905</xref>.
          </t>
          <t>
            Differences to NTP Date Format
            <list style="hanging">
              <t hangText="Width of Era field:">NTP Date Format has 32-bits wide Era field. Here Era is only 8-bits wide. Intepretation is same but with narrowed range. Epoch is same so Era 0 starts at 1900-01-01 00:00:00 UTC like in NTP Date Format.</t>
              <t hangText="Fraction field is optional:">In NTP Date Format Fraction field always exists. Here it is optional. It exists only if Fraction bit is set in Leading Byte.</t>
              <t hangText="Width of Fraction Field:">NTP Date Format uses 64-bits wide Fraction field. Here fraction is only 32-bits wide and thus capable to 232 picosecond resolution.</t>
            </list>
          </t>
          <figure title="Timestamp Frame">
            <artwork><![CDATA[
   Leading Byte    Identifier   Era   Offset   Fraction
+-+-------+-+---+ +----------+ +---+ +------+ +--------+
|X|1|2|3|4|5|6|7| | 0 - 256  | | 1 | |  4   | | 0 or 4 |
+-+-------+-+---+ +----------+ +---+ +------+ +--------+
   \     / | \ /
    \   /  |  | Id bits
     \ /   |
      |    | Fraction bit
      |
      | Type bits]]>
            </artwork>          
          </figure>
          <t>
            <list style="hanging">
              <t hangText="Identifier & Id bits:">See <xref target="identifiers" /> for details.</t>
              <t hangText="Era:">8-bit signed integer telling the era of timestamp. See <xref target="RFC5905">RFC 5905</xref> for era definitions.</t>
              <t hangText="Offset:">32-bit unsigned integer holding number of seconds since beginning of the Era.</t>
              <t hangText="Fraction:">32-bit unsigned integer holding fractions of second.</t>
              <t hangText="Fraction bit:">If Fraction bit is set then optional Fraction field exists.</t>
              <t hangText="Type bits:">Timestamp Frame type: 1111</t>
            </list>
          </t>
        </section>        
      </section>
      <section anchor="extended_frame" title="Extended Frames">
        <t>
          Extended Frame is concept to introduce new structures and data types to future versions of RSK. The most significant bit in Leading Byte is reserved for Extended Frames. In this version Extended bit must not be set when writing a RSK document.
        </t>
        <t>
          Forward compatible implementations must be able to detect Extended Frames and interprete Length bit and Length field to get correct length of payload. Payload can be then skipped and processing continues with next frame.
        </t>
        <t>
          Extended Frame is not part of Minimal Requirements see <xref target="minimal_requirements" />.
        </t>
        <figure title="Extended Frames">
          <artwork><![CDATA[
   Leading Byte       Length       Payload
+-+-----------+-+   +--------+   +---------+
|X|1|2|3|4|5|6|7|   | 1 - 2  |   | 0 - 64k |
+-+-----------+-+   +--------+   +---------+
 | \         / |
 |  \---+---/  | Length bit
 |      |
 |      | Type and instructions bits
 |
 | Extended bit]]>
          </artwork>
        </figure>
          <t>
            <list style="hanging">
              <t hangText="Length:">8 or 16-bits wide unsigned integer telling length of payload in bytes.</t>
              <t hangText="Payload:">Payload can be skipped in this version.</t>
              <t hangText="Length bit:">If bit is set then Length is 16-bits otherwise 8-bits wide unsigned integer.</t>
              <t hangText="Type and instructions bits:">Extended Frame type and instruction bits will be defined in future Ruoska Encoding versions. These can be skipped in this version.</t>
              <t hangText="Extended bit:">If set frame must be handled as Extended Frame.</t>
            </list>
          </t>
      </section>
    </section>
    <section anchor="identifiers" title="Identifiers">
      <t>
        All data frames and some of the meta frames can be tagged with an identifier. Identifier can be defined as 8 or 16-bit wide unsigned integer or as length-prefixed UTF-8 encoded string. If identifier is not needed it can be set to Null.
      </t>
      <t>
        Frame's Leading Byte tells type of identifier. Identifier bytes are placed immediately after the Leading Byte. In case of integer identifier there is one or two bytes depending on selected integer width. String identifier can take up to 256 bytes. See following sections for details.
      </t>
      <t>
        All identifier types are not required for minimal implementations. See <xref target="minimal_requirements" /> for details.
      </t>
      <section title="Identifier Types in Leading Byte">
        <t>
          Two least significant bits of Leading Byte are reserved for Id bits in all frame types which can be tagged with an identifier.
        </t>
        <figure title="Identifier Types">
          <artwork><![CDATA[
  Leading Byte     Identifier
+-+---------+---+ +----------+
|X|1|2|3|4|5|6|7| | 0 - 256  |
+-+---------+---+ +----------+
             \ /
              | Id bits]]>
          </artwork>
        </figure>
        <t>
          Id bits values and identifier types:
          <list style="hanging">
            <t hangText="00">Null Identifier. See <xref target="null_identifier"/>.</t>
            <t hangText="01">8-bits wide Integer Identifier. See <xref target="integer_identifier"/>.</t>
            <t hangText="10">16-bits wide Integer Identifier. See <xref target="integer_identifier"/>.</t>
            <t hangText="11">String Identifier. See <xref target="string_identifier"/></t>
          </list>
        </t>
      </section>
      <section anchor="null_identifier" title="Null Identifier">
        <t>
          Some frames in a document may not need identifier so it can be left empty by setting it Null in Leading Byte.
        </t>
      </section>
      <section anchor="integer_identifier" title="Integer Identifiers">
        <t>
          Integer identifier types are 8 or 16-bits wide unsigned integers. Integer identifiers are presented in big-endian format as all numbers in RSK. See <xref target="endianness" /> for details.
        </t>
      </section>
      <section anchor="string_identifier" title="String Identifier">
        <t>
          String identifier is length-prefixed and UTF-8 encoded. Length is presented by one byte as 8-bits wide unsigned integer at the beginning of identifier field. String identifier itself can be 255 bytes long at maximum.
        </t>
        <t>
          If implementation supports string identifiers it must be able to validate UTF-8 encoding. See <xref target="string_encoding" /> for details.
        </t>        
        <figure title="String Identifier">
          <artwork><![CDATA[
  Leading Byte      Length     UTF-8 Encoded String
+---------------+ +--------+ +----------------------+
|X|1|2|3|4|5|6|7| | 1 byte | |    0 - 255 bytes     |
+---------------+ +--------+ +----------------------+
                  |                                 |
                  \---------- Identifier -----------/

]]>
          </artwork>
        </figure>        
      </section>
    </section>
    <section anchor="implementation_notes" title="Implementation Notes">
      <t>
        RSK is designed so that implementations could have very small memory and other resource demands. Pay attention to memory usage and try to perform IO operations efficiently.
      </t>
      <t>
        Implementations must make sure that well formed documents are written. On reading phase any deformation in document or frame structure must be detected and handled as error condition.
      </t>
      <t>
        Implementations can vary many ways depending on environment and usage. Minimal Requirements define absolute minimum requirements for all implementations. All other feature can be left out to save resources in implementations for small devices. See <xref target="minimal_requirements" /> for details.
      </t>
      <section anchor="minimal_requirements" title="Minimal Requirements">
        <t>
          Absolute minimal requirements for all implementations to be compatible with RSK are listed here.
        </t>
        <t>
          <list style="hanging">
            <t hangText="Binary Frame Payload length:">8 bytes.</t>
            <t hangText="Nesting levels:">0 - 255</t>
            <t hangText="Meta Frame types:">Begin and End Frames.</t>
            <t hangText="Identifier types:">Null and Integer identifiers.</t>
            <t hangText="Data Frame types:">Boolean, Binary, and 8 and 16-bits wide Integer Frames.</t>
          </list>
        </t>
        <t>
          All other features like frame types Array, Modifier, Float, String, DateTime, and Timestamp are optional. String identifier is optional. Longer than 8 bytes payloads are optional. Support for Extended Frames is also optional.
        </t>
      </section>
      <section anchor="forward_compatibility" title="Forward Compatibility">
        <t>
          An implementation may take advantage of Extended Frame concept to provide forward compatibility for later RSK versions. This is not required for all implementations and can be left out if not needed. See <xref target="minimal_requirements" /> for Minimal Requirements and <xref target="extended_frame" /> for Extended Frame.
        </t>
      </section>
    </section>

    <section title="Requirements notation">
    <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL",
    "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY",
    and "OPTIONAL" in this document are to be interpreted as
    described in <xref target="RFC2119"/>.</t>
    </section>

    <section title="Security Considerations">      
      <t>
        RSK is data encoding format and does not include any executable commands. Implementations must make sure that any parts of encoded documents are not leaked into execution memory. Even malformed documents must be handled so that memory leaks are avoided.
      </t>
      <t>
        RSK does not include any means to validate payload data integrity. Protocols based on RSK or underlying mechanisms which are utilized by those protocols must take care of this. If data integrity is not checked can data get corrupted by malfunctioning devices, software, or malicious attackers.
      </t>
      <t>
        Modifier Frame enables encryption for selected portions of a document. RSK does not define any encryption methods but almost any kind of encryption cipher could be used. If needed encryption methods should be defined in specifications based on RSK.
      </t>
    </section>

    <section title="IANA Considerations">
      <t>
        The MIME media type for RSK documents is application/ruoska.
        <list style="hanging">
          <t hangText="Type name:">application</t>
          <t hangText="Subtype name:">ruoska</t>
          <t hangText="Required parameters:">n/a</t>
          <t hangText="Optional parameters:">n/a</t>
        </list>
      </t>
    </section>
  </middle>

    <back>
      <references title='Normative References'>
        &rfc2119;
        &rfc3339;
        &rfc3629;
        &rfc5905;
      </references>
    </back>

</rfc>
