[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / doc / marshal.rdoc
blobabf9467262a3e900583aa81697b627f372b70ce1
1 = Marshal Format
3 The Marshal format is used to serialize ruby objects.  The format can store
4 arbitrary objects through three user-defined extension mechanisms.
6 For documentation on using Marshal to serialize and deserialize objects, see
7 the Marshal module.
9 This document calls a serialized set of objects a stream.  The Ruby
10 implementation can load a set of objects from a String, an IO or an object
11 that implements a +getc+ method.
13 == Stream Format
15 The first two bytes of the stream contain the major and minor version, each as
16 a single byte encoding a digit.  The version implemented in Ruby is 4.8
17 (stored as "\x04\x08") and is supported by ruby 1.8.0 and newer.
19 Different major versions of the Marshal format are not compatible and cannot
20 be understood by other major versions.  Lesser minor versions of the format
21 can be understood by newer minor versions.  Format 4.7 can be loaded by a 4.8
22 implementation but format 4.8 cannot be loaded by a 4.7 implementation.
24 Following the version bytes is a stream describing the serialized object.  The
25 stream contains nested objects (the same as a Ruby object) but objects in the
26 stream do not necessarily have a direct mapping to the Ruby object model.
28 Each object in the stream is described by a byte indicating its type followed
29 by one or more bytes describing the object.  When "object" is mentioned below
30 it means any of the types below that defines a Ruby object.
32 === true, false, nil
34 These objects are each one byte long.  "T" is represents +true+, "F"
35 represents +false+ and "0" represents +nil+.
37 === Fixnum and long
39 "i" represents a signed 32 bit value using a packed format.  One through five
40 bytes follows the type.  The value loaded will always be a Fixnum.  On
41 32 bit platforms (where the precision of a Fixnum is less than 32 bits)
42 loading large values will cause overflow on CRuby.
44 The fixnum type is used to represent both ruby Fixnum objects and the sizes of
45 marshaled arrays, hashes, instance variables and other types.  In the
46 following sections "long" will mean the format described below, which supports
47 full 32 bit precision.
49 The first byte has the following special values:
51 "\x00"::
52   The value of the integer is 0.  No bytes follow.
54 "\x01"::
55   The total size of the integer is two bytes.  The following byte is a
56   positive integer in the range of 0 through 255.  Only values between 123
57   and 255 should be represented this way to save bytes.
59 "\xff"::
60   The total size of the integer is two bytes.  The following byte is a
61   negative integer in the range of -1 through -256.
63 "\x02"::
64   The total size of the integer is three bytes.  The following two bytes are a
65   positive little-endian integer.
67 "\xfe"::
68   The total size of the integer is three bytes.  The following two bytes are a
69   negative little-endian integer.
71 "\x03"::
72   The total size of the integer is four bytes.  The following three bytes are
73   a positive little-endian integer.
75 "\xfd"::
76   The total size of the integer is four bytes.  The following three bytes are a
77   negative little-endian integer.
79 "\x04"::
80   The total size of the integer is five bytes.  The following four bytes are a
81   positive little-endian integer.  For compatibility with 32 bit ruby,
82   only Fixnums less than 1073741824 should be represented this way.  For sizes
83   of stream objects full precision may be used.
85 "\xfc"::
86   The total size of the integer is five bytes.  The following four bytes are a
87   negative little-endian integer.  For compatibility with 32 bit ruby,
88   only Fixnums greater than -10737341824 should be represented this way.  For
89   sizes of stream objects full precision may be used.
91 Otherwise the first byte is a sign-extended eight-bit value with an offset.
92 If the value is positive the value is determined by subtracting 5 from the
93 value.  If the value is negative the value is determined by adding 5 to the
94 value.
96 There are multiple representations for many values.  CRuby always outputs the
97 shortest representation possible.
99 === Symbols and Byte Sequence
101 ":" represents a real symbol.  A real symbol contains the data needed to
102 define the symbol for the rest of the stream as future occurrences in the
103 stream will instead be references (a symbol link) to this one.  The reference
104 is a zero-indexed 32 bit value (so the first occurrence of <code>:hello</code>
105 is 0).
107 Following the type byte is byte sequence which consists of a long indicating
108 the number of bytes in the sequence followed by that many bytes of data.  Byte
109 sequences have no encoding.
111 For example, the following stream contains the Symbol <code>:hello</code>:
113   "\x04\x08:\x0ahello"
115 ";" represents a Symbol link which references a previously defined Symbol.
116 Following the type byte is a long containing the index in the lookup table for
117 the linked (referenced) Symbol.
119 For example, the following stream contains <code>[:hello, :hello]</code>:
121   "\x04\b[\a:\nhello;\x00"
123 When a "symbol" is referenced below it may be either a real symbol or a
124 symbol link.
126 === Object References
128 Separate from but similar to symbol references, the stream contains only one
129 copy of each object (as determined by #object_id) for all objects except
130 true, false, nil, Fixnums and Symbols (which are stored separately as
131 described above) a one-indexed 32 bit value will be stored and reused when the
132 object is encountered again.  (The first object has an index of 1).
134 "@" represents an object link.  Following the type byte is a long giving the
135 index of the object.
137 For example, the following stream contains an Array of the same
138 <code>"hello"</code> object twice:
140   "\004\b[\a\"\nhello@\006"
142 === Instance Variables
144 "I" indicates that instance variables follow the next object.  An object
145 follows the type byte.  Following the object is a length indicating the number
146 of instance variables for the object.  Following the length is a set of
147 name-value pairs.  The names are symbols while the values are objects.  The
148 symbols must be instance variable names (<code>:@name</code>).
150 An Object ("o" type, described below) uses the same format for its instance
151 variables as described here.
153 For a String and Regexp (described below) a special instance variable
154 <code>:E</code> is used to indicate the Encoding.
156 === Extended
158 "e" indicates that the next object is extended by a module.  An object follows
159 the type byte.  Following the object is a symbol that contains the name of the
160 module the object is extended by.
162 === Array
164 "[" represents an Array.  Following the type byte is a long indicating the
165 number of objects in the array.  The given number of objects follow the
166 length.
168 === Bignum
170 "l" represents a Bignum which is composed of three parts:
172 sign::
173   A single byte containing "+" for a positive value or "-" for a negative
174   value.
175 length::
176   A long indicating the number of bytes of Bignum data follows, divided by
177   two.  Multiply the length by two to determine the number of bytes of data
178   that follow.
179 data::
180   Bytes of Bignum data representing the number.
182 The following ruby code will reconstruct the Bignum value from an array of
183 bytes:
185   result = 0
187   bytes.each_with_index do |byte, exp|
188    result += (byte * 2 ** (exp * 8))
189   end
191 === Class and Module
193 "c" represents a Class object, "m" represents a Module and "M" represents
194 either a class or module (this is an old-style for compatibility).  No class
195 or module content is included, this type is only a reference.  Following the
196 type byte is a byte sequence which is used to look up an existing class or
197 module, respectively.
199 Instance variables are not allowed on a class or module.
201 If no class or module exists an exception should be raised.
203 For "c" and "m" types, the loaded object must be a class or module,
204 respectively.
206 === Data
208 "d" represents a Data object.  (Data objects are wrapped pointers from ruby
209 extensions.)  Following the type byte is a symbol indicating the class for the
210 Data object and an object that contains the state of the Data object.
212 To dump a Data object Ruby calls _dump_data.  To load a Data object Ruby calls
213 _load_data with the state of the object on a newly allocated instance.
215 === Float
217 "f" represents a Float object.  Following the type byte is a byte sequence
218 containing the float value.  The following values are special:
220 "inf"::
221   Positive infinity
223 "-inf"::
224   Negative infinity
226 "nan"::
227   Not a Number
229 Otherwise the byte sequence contains a C double (loadable by strtod(3)).
230 Older minor versions of Marshal also stored extra mantissa bits to ensure
231 portability across platforms but 4.8 does not include these.  See
232 [ruby-talk:69518] for some explanation.
234 === Hash and Hash with Default Value
236 "{" represents a Hash object while "}" represents a Hash with a default value
237 set (<code>Hash.new 0</code>).  Following the type byte is a long indicating
238 the number of key-value pairs in the Hash, the size.  Double the given number
239 of objects follow the size.
241 For a Hash with a default value, the default value follows all the pairs.
243 === Module and Old Module
245 === Object
247 "o" represents an object that doesn't have any other special form (such as
248 a user-defined or built-in format).  Following the type byte is a symbol
249 containing the class name of the object.  Following the class name is a long
250 indicating the number of instance variable names and values for the object.
251 Double the given number of pairs of objects follow the size.
253 The keys in the pairs must be symbols containing instance variable names.
255 === Regular Expression
257 "/" represents a regular expression.  Following the type byte is a byte
258 sequence containing the regular expression source.  Following the type byte is
259 a byte containing the regular expression options (case-insensitive, etc.) as a
260 signed 8-bit value.
262 Regular expressions can have an encoding attached through instance variables
263 (see above).  If no encoding is attached escapes for the following regexp
264 specials not present in ruby 1.8 must be removed: g-m, o-q, u, y, E, F, H-L,
265 N-V, X, Y.
267 === String
269 '"' represents a String.  Following the type byte is a byte sequence
270 containing the string content.  When dumped from ruby 1.9 an encoding instance
271 variable (<code>:E</code> see above) should be included unless the encoding is
272 binary.
274 === Struct
276 "S" represents a Struct.  Following the type byte is a symbol containing the
277 name of the struct.  Following the name is a long indicating the number of
278 members in the struct.  Double the number of objects follow the member count.
279 Each member is a pair containing the member's symbol and an object for the
280 value of that member.
282 If the struct name does not match a Struct subclass in the running ruby an
283 exception should be raised.
285 If there is a mismatch between the struct in the currently running ruby and
286 the member count in the marshaled struct an exception should be raised.
288 === User Class
290 "C" represents a subclass of a String, Regexp, Array or Hash.  Following the
291 type byte is a symbol containing the name of the subclass.  Following the name
292 is the wrapped object.
294 === User Defined
296 "u" represents an object with a user-defined serialization format using the
297 +_dump+ instance method and +_load+ class method.  Following the type byte is
298 a symbol containing the class name.  Following the class name is a byte
299 sequence containing the user-defined representation of the object.
301 The class method +_load+ is called on the class with a string created from the
302 byte-sequence.
304 === User Marshal
306 "U" represents an object with a user-defined serialization format using the
307 +marshal_dump+ and +marshal_load+ instance methods.  Following the type byte
308 is a symbol containing the class name.  Following the class name is an object
309 containing the data.
311 Upon loading a new instance must be allocated and +marshal_load+ must be
312 called on the instance with the data.