| 1 | # Portable Storage Format |
| 2 | |
| 3 | ## Background |
| 4 | |
| 5 | Monero makes use of a set of helper classes from a small library named |
| 6 | [epee](https://github.com/monero-project/monero/tree/master/contrib/epee). Part |
| 7 | of this library implements a networking protocol called |
| 8 | [Levin](https://github.com/monero-project/monero/blob/master/contrib/epee/include/net/levin_base.h), |
| 9 | which internally uses a storage format called [Portable |
| 10 | Storage](https://github.com/monero-project/monero/tree/master/contrib/epee/include/storages). |
| 11 | This format (amongst the rest of the |
| 12 | [epee](https://github.com/monero-project/monero/tree/master/contrib/epee) |
| 13 | library), is undocumented - or rather relies on the code itself to serve as the |
| 14 | documentation. Unfortunately, whilst the rest of the library is fairly |
| 15 | straightforward to decipher, the Portable Storage is less-so. Hence this |
| 16 | document. |
| 17 | |
| 18 | ## String and Integer Encoding |
| 19 | |
| 20 | ### Integers |
| 21 | |
| 22 | With few exceptions, integers serialized in epee portable storage format are serialized |
| 23 | as little-endian. |
| 24 | |
| 25 | ### Varints |
| 26 | |
| 27 | Varints are used to pack integers in an portable and space optimized way. Varints are stored as little-endian integers, with the lowest 2 bits storing the amount of bytes required, which means the largest value integer that can be packed into 1 byte is 63 |
| 28 | (6 bits). |
| 29 | |
| 30 | #### Byte Sizes |
| 31 | |
| 32 | | Lowest 2 bits | Size value | Value range | |
| 33 | |---------------|---------------|-----------------------------------| |
| 34 | | b00 | 1 byte | 0 to 63 | |
| 35 | | b01 | 2 bytes | 64 to 16383 | |
| 36 | | b10 | 4 bytes | 16384 to 1073741823 | |
| 37 | | b11 | 8 bytes | 1073741824 to 4611686018427387903 | |
| 38 | |
| 39 | #### Represenations of Example Values |
| 40 | | Value | Byte Representation (hex) | |
| 41 | |----------------------|---------------------------| |
| 42 | | 0 | 00 | |
| 43 | | 7 | 1c | |
| 44 | | 101 | 95 01 | |
| 45 | | 17,000 | A2 09 01 00 | |
| 46 | | 7,942,319,744 | 03 BA 98 65 07 00 00 00 | |
| 47 | |
| 48 | ### Strings |
| 49 | |
| 50 | These are simply length (varint) prefixed char strings without a null |
| 51 | terminator (though one can always add one if desired). There is no |
| 52 | specific encoding enforced, and in fact, many times binary blobs are |
| 53 | stored as these strings. This type should not be confused with the keys |
| 54 | in sections, as those are restricted to a maximum length of 255 and |
| 55 | do not use varints to encode the length. |
| 56 | |
| 57 | "Howdy" => 14 48 6F 77 64 79 |
| 58 | |
| 59 | ### Section Keys |
| 60 | |
| 61 | These are similar to strings except that they are length limited to 255 |
| 62 | bytes, and use a single byte at the front of the string to describe the |
| 63 | length (as opposed to a varint). |
| 64 | |
| 65 | "Howdy" => 05 48 6F 77 64 79 |
| 66 | |
| 67 | ## Binary Format Specification |
| 68 | |
| 69 | ### Header |
| 70 | |
| 71 | The format must always start with the following header: |
| 72 | |
| 73 | | Field | Type | Value | |
| 74 | |------------------|----------|------------| |
| 75 | | Signature Part A | UInt32 | 0x01011101 | |
| 76 | | Signature Part B | UInt32 | 0x01020101 | |
| 77 | | Version | UInt8 | 0x01 | |
| 78 | |
| 79 | In total, the 9 byte header will look like this (in hex): `01 11 01 01 01 01 02 01 01` |
| 80 | |
| 81 | ### Section |
| 82 | |
| 83 | Next we have a root object (or section as the library calls it). This is a map |
| 84 | of name-value pairs called [entries](#Entry). It starts with a count: |
| 85 | |
| 86 | | Section | Type | |
| 87 | |---------------|-----------| |
| 88 | | Entry count | varint | |
| 89 | |
| 90 | |
| 91 | Which is followed by the section's name-value [entries](#Entry) sequentially: |
| 92 | |
| 93 | ### Entry |
| 94 | |
| 95 | | Entry | Type | |
| 96 | |-------------------|-----------------------| |
| 97 | | Name | section key | |
| 98 | | Type | byte | |
| 99 | | Count<sup>1</sup> | varint | |
| 100 | | Value(s) | (type dependant data) | |
| 101 | |
| 102 | <sup>1</sup> Note, this is only present if the entry type has the array flag |
| 103 | (see below). |
| 104 | |
| 105 | #### Entry types |
| 106 | |
| 107 | The types defined are: |
| 108 | |
| 109 | ```cpp |
| 110 | #define SERIALIZE_TYPE_INT64 1 |
| 111 | #define SERIALIZE_TYPE_INT32 2 |
| 112 | #define SERIALIZE_TYPE_INT16 3 |
| 113 | #define SERIALIZE_TYPE_INT8 4 |
| 114 | #define SERIALIZE_TYPE_UINT64 5 |
| 115 | #define SERIALIZE_TYPE_UINT32 6 |
| 116 | #define SERIALIZE_TYPE_UINT16 7 |
| 117 | #define SERIALIZE_TYPE_UINT8 8 |
| 118 | #define SERIALIZE_TYPE_DOUBLE 9 |
| 119 | #define SERIALIZE_TYPE_STRING 10 |
| 120 | #define SERIALIZE_TYPE_BOOL 11 |
| 121 | #define SERIALIZE_TYPE_OBJECT 12 |
| 122 | #define SERIALIZE_TYPE_ARRAY 13 |
| 123 | ``` |
| 124 | |
| 125 | The entry type can be bitwise OR'ed with a flag: |
| 126 | |
| 127 | ```cpp |
| 128 | #define SERIALIZE_FLAG_ARRAY 0x80 |
| 129 | ``` |
| 130 | |
| 131 | This signals there are multiple *values* for the entry. Since only one bit is |
| 132 | reserved for specifying an array, we can not directly represent nested arrays. |
| 133 | However, you can place each of the inner arrays inside of a section, and make |
| 134 | the outer array type `SERIALIZE_TYPE_OBJECT | SERIALIZE_FLAG_ARRAY`. Immediately following the type code byte is a varint specifying the length of the array. |
| 135 | Finally, the all the elements are serialized in sequence with no padding and |
| 136 | without any type information. For example: |
| 137 | |
| 138 | <p style="padding-left:1em; font:italic larger serif">type, count, |
| 139 | value<sub>1</sub>, value<sub>2</sub>,..., value<sub>n</sub></p> |
| 140 | |
| 141 | #### Entry values |
| 142 | |
| 143 | It's important to understand that entry *values* can be encoded any way in which |
| 144 | an implementation chooses. For example, the integers can be in either big or |
| 145 | little endian byte order. |
| 146 | |
| 147 | Entry values which are objects (i.e. `SERIALIZE_TYPE_OBJECT`), are stored as |
| 148 | [sections](#Section). |
| 149 | |
| 150 | Note, I have not yet seen the type `SERIALIZE_TYPE_ARRAY` in use. My assumption |
| 151 | is this would be used for *untyped* arrays and so subsequent entries could be of |
| 152 | any type. |
| 153 | |
| 154 | ### Overall example |
| 155 | |
| 156 | Let's put it all together and see what an entire object would look like serialized. To represent our data, let's create a JSON object (since it's a format |
| 157 | that most will be familiar with): |
| 158 | |
| 159 | ```json |
| 160 | { |
| 161 | "short_quote": "Give me liberty or give me death!", |
| 162 | "long_quote": "Monero is more than just a technology. It's also what the technology stands for.", |
| 163 | "signed_32bit_int": 20140418, |
| 164 | "array_of_bools": [true, false, true, true], |
| 165 | "nested_section": { |
| 166 | "double": -6.9, |
| 167 | "unsigned_64bit_int": 11111111111111111111 |
| 168 | } |
| 169 | } |
| 170 | ``` |
| 171 | |
| 172 | This would translate to: |
| 173 | |
| 174 |  |
| 175 | |
| 176 | ## Monero specifics |
| 177 | |
| 178 | ### Entry values |
| 179 | |
| 180 | #### Hashes, Keys, Blobs |
| 181 | |
| 182 | These are stored as strings, `SERIALIZE_TYPE_STRING`. |
| 183 | |
| 184 | #### STL containers (vector, list) |
| 185 | |
| 186 | These can be arrays of standard integer types, strings or |
| 187 | `SERIALIZE_TYPE_OBJECT`'s for structs. |
| 188 | |
| 189 | #### Links to some Monero struct definitions |
| 190 | |
| 191 | - [Core RPC |
| 192 | definitions](https://github.com/monero-project/monero/blob/master/src/rpc/core_rpc_server_commands_defs.h) |
| 193 | - [CryptoNote protocol |
| 194 | definitions](https://github.com/monero-project/monero/blob/master/src/cryptonote_protocol/cryptonote_protocol_defs.h) |
| 195 | |
| 196 | |
| 197 | |
| 198 | [//]: # ( vim: set tw=80: ) |