An entry with the mimetype application/x-microsoft.net.object.binary.base64 holds the output of BinaryFormatter in base64. The stream is the .NET Remoting Binary Format, which Microsoft specifies as [MS-NRBF]; the section numbers on this page are those of its revision 13.0 (19 November 2024). It is a sequence of records; each starts with a one-byte record type. Integers are little-endian (2.1.1). A length-prefixed string (2.1.1.6) is a length from 0 to 2,147,483,647 in one to five bytes of seven bits each (the low bits first, the high bit set when another byte follows; the top five bits of a fifth byte are 0) and then that many bytes of UTF-8.
The stream
- A
SerializedStreamHeaderrecord, always first (2.6.1):RootId(in an object graph, the id of a class, array or string of the stream),HeaderId(ignored on read),MajorVersion(1),MinorVersion(0), each a 32-bit integer. - Records for the objects: class records, strings and arrays, with the
BinaryLibraryrecords that give each assembly a number before the first record that uses it. The record grammar (2.7) allows one method call or method return among them. - A
MessageEndrecord (type 11).
Classes, arrays and strings have a 32-bit id that is unique in the stream. The id of an array or a string is positive (2.4.2.1, 2.4.3.1, 2.5.7); that of a class must be positive when anything refers to it and may otherwise be negative (2.3.1.1). A value that is an object written elsewhere is a MemberReference with a positive id; the record with that id must exist and may come later (2.5.3), so the stream is a graph and may contain cycles. A LibraryId is positive, unique, and given by a BinaryLibrary written before it is used (2.6.2, 2.1.1.8). The MetadataId of a ClassWithId is the ObjectId of an earlier class record of type 2 to 5 (2.3.2.5). A run of nulls stands for that many consecutive null members or elements: ObjectNullMultiple256 counts 0 to 255 (2.5.6), ObjectNullMultiple a positive Int32 (2.5.5).
Record types
The codes are the RecordTypeEnumeration (2.1.2.1). The class records are described in 2.3, the arrays in 2.4, references, nulls, strings and primitives in 2.5, the header, library and end records in 2.6, the call and return records in 2.2.3.
| Code | Record | Layout |
|---|---|---|
| 0 | SerializedStreamHeader | RootId, HeaderId, MajorVersion, MinorVersion (4 × Int32) |
| 1 | ClassWithId | ObjectId, MetadataId (the id of an earlier class record whose layout is reused); then the member values |
| 2 | SystemClassWithMembers | ClassInfo (ObjectId, Name, MemberCount, MemberNames); then the member values (a system class, no member types) |
| 3 | ClassWithMembers | ClassInfo, LibraryId; then the member values (no member types) |
| 4 | SystemClassWithMembersAndTypes | ClassInfo, MemberTypeInfo; then the member values |
| 5 | ClassWithMembersAndTypes | ClassInfo, MemberTypeInfo, LibraryId; then the member values |
| 6 | BinaryObjectString | ObjectId, Value (a length-prefixed string) |
| 7 | BinaryArray | ObjectId, ArrayType (0 Single, 1 Jagged, 2 Rectangular, 3 SingleOffset, 4 JaggedOffset, 5 RectangularOffset), Rank, Lengths (Rank × Int32), LowerBounds (Rank × Int32, only for the offset types), the element’s type (a member type code and its additional information); then the elements |
| 8 | MemberPrimitiveTyped | PrimitiveType, Value |
| 9 | MemberReference | IdRef |
| 10 | ObjectNull | — |
| 11 | MessageEnd | — |
| 12 | BinaryLibrary | LibraryId, LibraryName (a length-prefixed string, the full assembly name) |
| 13 | ObjectNullMultiple256 | NullCount (one byte): a run of nulls |
| 14 | ObjectNullMultiple | NullCount (Int32): a run of nulls |
| 15 | ArraySinglePrimitive | ObjectId, Length, PrimitiveType; then the raw values |
| 16 | ArraySingleObject | ObjectId, Length; then the elements as records |
| 17 | ArraySingleString | ObjectId, Length; then the elements as records (strings, references, nulls) |
| 21 | MethodCall (BinaryMethodCall) | MessageFlags (Int32), MethodName and TypeName (each a StringValueWithCode), then CallContext (a StringValueWithCode, only with the ContextInline flag) and Args (an ArrayOfValueWithCode, only with the ArgsInline flag); a call array may follow |
| 22 | MethodReturn (BinaryMethodReturn) | MessageFlags (Int32), then ReturnValue (a ValueWithCode, only with the ReturnValueInline flag), CallContext (only with ContextInline) and Args (only with ArgsInline); a call array may follow |
Records 2 and 3 carry no member types: the specification expects them to be known from elsewhere or inferred (2.3.2.2, 2.3.2.4). A BinaryArray has a rank of 0 or more and lengths of 0 or more, and holds as many elements as the product of its lengths (2.4, 2.4.3.1). The specification gives no upper bound for the rank and does not say how many elements a rank of 0 has. A primitive element type may not be Null or String.
Member type codes
| Code | Type | Additional information |
|---|---|---|
| 0 | Primitive | the primitive type code (a byte) |
| 1 | String | — |
| 2 | Object | — |
| 3 | SystemClass | the class name (a length-prefixed string) |
| 4 | Class | the class name and a LibraryId (Int32) |
| 5 | ObjectArray | — |
| 6 | StringArray | — |
| 7 | PrimitiveArray | the primitive type code of the elements |
These are the BinaryTypeEnumeration (2.1.2.2). In MemberTypeInfo (2.3.1.2) the member type codes of all the members come first (one byte each), then the additional information of those that have some, in the same order. A primitive member is written as its raw value, with no record around it; every other member is written as a record (a string, a reference, null, a nested class, an array).
Primitive type codes
| Code | Type | Bytes | Code | Type | Bytes |
|---|---|---|---|---|---|
| 1 | Boolean | 1 | 10 | SByte | 1 |
| 2 | Byte | 1 | 11 | Single | 4 |
| 3 | Char | 1 to 4 (UTF-8) | 12 | TimeSpan | 8 (ticks) |
| 5 | Decimal | a length-prefixed string | 13 | DateTime | 8 (ticks in 62 bits, kind in 2) |
| 6 | Double | 8 | 14 | UInt16 | 2 |
| 7 | Int16 | 2 | 15 | UInt32 | 4 |
| 8 | Int32 | 4 | 16 | UInt64 | 8 |
| 9 | Int64 | 8 | 17 | Null | 0 |
| 4 | not used | — | 18 | String | a length-prefixed string |
These are the PrimitiveTypeEnumeration (2.1.2.3). A Char is the UTF-8 bytes of the character (2.1.1.1); Double and Single are IEEE 754 (2.1.1.2, 2.1.1.3).
A Decimal (2.1.1.7) is a length-prefixed string: an optional minus sign, digits, and an optional point and digits, from −79,228,162,514,264,337,593,543,950,335 to the same number positive. When it has more than 29 digits, at most 29 of them before the point, a reader of the value rounds it to 29. A DateTime (2.1.1.5) is a 64-bit value: the low 62 bits count 100-nanosecond ticks from 1 January 0001, up to the end of 31 December 9999, and the top two bits are the kind (0 not specified, 1 UTC, 2 local). The specification notes that .NET 1.0 and 1.1 write the kind as 0 and ignore it. A TimeSpan (2.1.1.4) is a signed 64-bit count of the same ticks. Null (17) and String (18) cannot be the type of a MemberPrimitiveTyped, an ArraySinglePrimitive, or a primitive member or array element.
Remoting calls
A stream that holds a method call or its return starts with the same header, then a MethodCall (21) or MethodReturn (22) record in place of the root object. For a call or return with no call array, RootId and HeaderId are 0. For one with a call array, RootId is the id of the array and HeaderId is -1 (2.6.1).
MessageFlags (2.2.1.1) is an Int32 of flag bits. At most one flag of each group may be set. GenericMethod came with .NET 2.0.
| Group | Flag | Value | Meaning |
|---|---|---|---|
| Arguments | NoArgs | 0x1 | no arguments |
| Arguments | ArgsInline | 0x2 | the arguments are in the Args field of the record |
| Arguments | ArgsIsArray | 0x4 | each argument is an item of the call array |
| Arguments | ArgsInArray | 0x8 | the argument array is one item of the call array |
| Context | NoContext | 0x10 | no call context |
| Context | ContextInline | 0x20 | the call context is only a logical call id, in the CallContext field |
| Context | ContextInArray | 0x40 | the call context is an item of the call array |
| Signature | MethodSignatureInArray | 0x80 | the method signature is an item of the call array (calls only) |
| Properties | PropertiesInArray | 0x100 | the message properties are an item of the call array |
| Return | NoReturnValue | 0x200 | the return value is null |
| Return | ReturnValueVoid | 0x400 | the method returns nothing |
| Return | ReturnValueInline | 0x800 | the return value is in the ReturnValue field of the record |
| Return | ReturnValueInArray | 0x1000 | the return value is an item of the call array |
| Exception | ExceptionInArray | 0x2000 | an exception is an item of the call array (returns only) |
| Generic | GenericMethod | 0x8000 | the method is generic and its type arguments are items of the call array (calls only) |
A call must not carry Return or Exception flags. A return must not carry MethodSignatureInArray or GenericMethod. These pairs of groups exclude each other: Arguments and Exception, Return and Exception, Return and Signature, Exception and Signature.
A ValueWithCode is a primitive type code and then the value; for Null (17) there is no value. A StringValueWithCode is the code 18 and then a length-prefixed string. An ArrayOfValueWithCode is an Int32 count, 0 or more, and then that many ValueWithCode values (2.2.2).
The call array is an ArraySingleObject (16) whose items, in this order and only when the flag says so, are: the arguments, the generic type arguments, the method signature, the call context, the message properties (a call); or the return value, the output arguments, the exception, the call context, the message properties (a return). The items are ordinary records, so the array is read like any other (2.2.3.2, 2.2.3.4).
What a resource file usually holds
00 SerializedStreamHeader
01000000 FFFFFFFF 01000000 00000000 root 1, header -1, version 1.0
0C 02000000 "System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
BinaryLibrary: library 2
05 ClassWithMembersAndTypes
01000000 "System.Drawing.Bitmap" 01000000 "Data" object 1, one member, named Data
07 02 member type: PrimitiveArray of Byte (2)
02000000 LibraryId 2
0F 03000000 (length) 02 ArraySinglePrimitive of Byte: object 3, the PNG bytes
89504E47 …
0B MessageEnd
System.Drawing.Bitmap: a library, a class with one member (Data, a byte array) and the bytes of the picture file. An Icon has the member IconData; a System.Windows.Forms.ImageListStreamer has Data with the image list in the form the Windows common controls write (it starts with MSFt).The same layout carries other serialised objects: a string (BinaryObjectString), a list or dictionary (a class with members such as _items and _size), a System.Windows.Forms.AxHost.State (the persisted state of an ActiveX control), and application types. The viewer lists the class and its members for any of them, and shows byte arrays with their size and kind.
Divergence from spec
- HeaderId: for a stream without a call or return, section 2.6.1 asks for the id of a class, array or string in the stream. The .NET Framework stream of an
ImageListStreameramong the Windows Forms tests has -1 there, as in the figure above. The field is ignored on read, so the viewer reports nothing. - DateTime ticks: section 2.1.1.5 calls the 62-bit tick count signed, but the range it gives reaches 31 December 9999, which is 3,155,378,975,999,999,999 ticks and more than a signed 62-bit number holds. The viewer reads the 62 bits as unsigned.
- Array rank: the format sets no upper bound. .NET documents 32 dimensions as the most an array type can have (Type.MakeArrayType), so a
BinaryArrayof a higher rank fits the format but not .NET. The viewer reads it and says so.
Safety
Deserialising a BinaryFormatter stream can create any type the stream names, which is why BinaryFormatter is no longer recommended and is disabled in current .NET. The size fields of the format go up to 2,147,483,647, so a reader must not reserve memory from them before the data is there (section 4). The viewer never deserialises and does not follow a class name to code. It reads every record, including remoting calls and returns, however many there are and however deeply they are nested. The one bound on an array is 9,007,199,254,740,991 elements (253 − 1), the largest count the viewer’s script holds exactly.
A broken rule of the specification is listed as a problem, and the stream is read on where its bytes can still be told apart: the form of a string length, text that is not UTF-8, a Decimal that is badly written or out of range, flag combinations that 2.2.1.1 forbids, ids that are repeated, not positive or name nothing, a library used before it is given, a RootId that does not fit, and records where the grammar of 2.7 has no place for them. A DateTime out of range or of kind 3 is marked in its value. A Decimal is shown as written, not rounded. Of an array with rank 0 no elements are read.
Not described
Known, not yet covered
- Reference: the layout of the data inside the
ImageListStreamerandAxHost.Statemembers is not described. - Reference: the .NET Remoting types that travel in a call array (
LogicalCallContext,UnitySerializationHolder,DictionaryEntry) are described in MS-NRTP, not here. Viewer: it shows them as ordinary classes with their members. - Viewer: it has no member types from outside the stream. A class written without them (records 2 and 3) is read when all its member values are records; a primitive member, which has no record type byte, cannot be recognised, and the read then goes wrong or stops with a problem.