From 7f8e558ea89d9619143d3bf23076c4526fd7d1f8 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 15 Dec 2015 09:25:36 +0000 Subject: [PATCH] spec: Clarify the marshaling format in a few minor ways MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an attempt to make that section a little clearer. I don’t think any factual inaccuracies have been fixed (because I couldn’t find any). Including some wording and an example by Simon McVittie . https://bugs.freedesktop.org/show_bug.cgi?id=93382 --- doc/dbus-specification.xml | 56 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/doc/dbus-specification.xml b/doc/dbus-specification.xml index 0500220..ba1e739 100644 --- a/doc/dbus-specification.xml +++ b/doc/dbus-specification.xml @@ -968,8 +968,10 @@ Each value in a block of bytes is aligned "naturally," for example 4-byte values are aligned to a 4-byte boundary, and 8-byte values to an - 8-byte boundary. To properly align a value, alignment - padding may be necessary. The alignment padding must always + 8-byte boundary. Boundaries are calculated globally, with respect to + the first byte in the message. To properly align a value, + alignment padding may be necessary before the + value. The alignment padding must always be the minimum required padding to properly align the following value; and it must always be made up of nul bytes. The alignment padding must not be left uninitialized (it can't contain garbage), and more padding @@ -996,21 +998,50 @@ - The string-like types are all marshalled as a + The string-like types (STRING, OBJECT_PATH and SIGNATURE) are all + marshalled as a fixed-length unsigned integer n giving the length of the variable part, followed by n nonzero bytes of UTF-8 text, followed by a single zero (nul) byte which is not considered to be part of the text. The alignment of the string-like type is the same as the alignment of - n. + n: any padding required for n + appears immediately before n itself. There is never + any alignment padding between n and the string text, + or between the string text and the trailing nul. The alignment padding + for the next value in the message (if there is one) starts after the + trailing nul. For the STRING and OBJECT_PATH types, n is - encoded in 4 bytes, leading to 4-byte alignment. - For the SIGNATURE type, n is encoded as a single - byte. As a result, alignment padding is never required before a - SIGNATURE. + encoded in 4 bytes (a UINT32), leading to 4-byte + alignment. For the SIGNATURE type, n is encoded as a + single byte (a UINT8). As a result, alignment + padding is never required before a SIGNATURE. + + + + For example, if the current position is a multiple of 8 bytes from the + beginning of a little-endian message, strings ‘foo’, ‘+’ and ‘bar’ + would be serialized in sequence as follows: + + + no padding required, we are already at a multiple of 4 +0x03 0x00 0x00 0x00 length of ‘foo’ = 3 + 0x66 0x6f 0x6f ‘foo’ + 0x00 trailing nul + + no padding required, we are already at a multiple of 4 +0x01 0x00 0x00 0x00 length of ‘+’ = 1 + 0x2b ‘+’ + 0x00 trailing nul + + 0x00 0x00 2 bytes of padding to reach next multiple of 4 +0x03 0x00 0x00 0x00 length of ‘bar’ = 1 + 0x62 0x61 0x72 ‘bar’ + 0x00 trailing nul + @@ -1024,7 +1055,8 @@ element type, followed by the n bytes of the array elements marshalled in sequence. n does not include the padding after the length, or any padding after the - last element. + last element. i.e. n should be divisible by the + number of elements in the array. @@ -1033,7 +1065,7 @@ the 64-bit integer 5 would be marshalled as: -00 00 00 08 8 bytes of data +00 00 00 08 n = 8 bytes of data 00 00 00 00 padding to 8-byte boundary 00 00 00 00 00 00 00 05 first element = 5 @@ -1057,8 +1089,10 @@ marshalled value with the type given by that signature. The variant has the same 1-byte alignment as the signature, which means that alignment padding before a variant is never needed. - Use of variants may not cause a total message depth to be larger + Use of variants must not cause a total message depth to be larger than 64, including other container types such as structures. + (See Valid + Signatures.) -- 2.5.0