1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE cref SYSTEM "cref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
26
<title>erl_marshal</title>
27
<prepared>Torbjörn Törnkvist</prepared>
28
<responsible>Torbjörn Törnkvist</responsible>
30
<approved>Bjarne Däcker</approved>
31
<checked>Torbjörn Törnkvist</checked>
34
<file>erl_marshal.sgml</file>
36
<lib>erl_marshal</lib>
37
<libsummary>Encoding and Decoding of Erlang terms</libsummary>
39
<p>This module contains functions for encoding Erlang terms into
40
a sequence of bytes, and for decoding Erlang terms from a
41
sequence of bytes.</p>
45
<name><ret>int</ret><nametext>erl_compare_ext(bufp1, bufp2)</nametext></name>
46
<fsummary>Compares encoded byte sequences</fsummary>
48
<v>unsigned char *bufp1,*bufp2;</v>
51
<p>This function compares two encoded terms.
53
<p><c><![CDATA[bufp1]]></c> is a buffer containing an encoded Erlang
56
<p><c><![CDATA[bufp2]]></c> is a buffer containing an encoded Erlang
59
<p>The function returns 0 if the terms are equal, -1 if term1
60
is less than term2, or 1 if term2 is less than term1.
65
<name><ret>ETERM *</ret><nametext>erl_decode(bufp)</nametext></name>
66
<name><ret>ETERM *</ret><nametext>erl_decode_buf(bufpp)</nametext></name>
67
<fsummary>Converts a term from Erlang external format</fsummary>
69
<v>unsigned char *bufp;</v>
70
<v>unsigned char **bufpp;</v>
73
<p><c><![CDATA[erl_decode()]]></c> and <c><![CDATA[erl_decode_buf()]]></c> decode
74
the contents of a buffer and return the corresponding
75
Erlang term. <c><![CDATA[erl_decode_buf()]]></c> provides a simple
76
mechanism for dealing with several encoded terms stored
77
consecutively in the buffer.</p>
78
<p><c><![CDATA[bufp]]></c> is a pointer to a buffer containing one or
79
more encoded Erlang terms.
81
<p><c><![CDATA[bufpp]]></c> is the address of a buffer pointer. The buffer
82
contains one or more consecutively encoded Erlang terms.
83
Following a successful call to <c><![CDATA[erl_decode_buf()]]></c>,
84
<c><![CDATA[bufpp]]></c> will be updated so that it points to the next
87
<p><c><![CDATA[erl_decode()]]></c> returns an Erlang term
88
corresponding to the contents of <c><![CDATA[bufp]]></c> on success, or
89
NULL on failure. <c><![CDATA[erl_decode_buf()]]></c> returns an Erlang
90
term corresponding to the first of the consecutive terms in
91
<c><![CDATA[bufpp]]></c> and moves <c><![CDATA[bufpp]]></c> forward to point to the
92
next term in the buffer. On failure, each of the functions
98
<name><ret>int</ret><nametext>erl_encode(term, bufp)</nametext></name>
99
<name><ret>int</ret><nametext>erl_encode_buf(term, bufpp)</nametext></name>
100
<fsummary>Converts a term into Erlang external format</fsummary>
103
<v>unsigned char *bufp;</v>
104
<v>unsigned char **bufpp;</v>
107
<p><c><![CDATA[erl_encode()]]></c> and <c><![CDATA[erl_encode_buf()]]></c> encode
108
Erlang terms into external format for storage or transmission.
109
<c><![CDATA[erl_encode_buf()]]></c> provides a simple mechanism for
110
encoding several terms consecutively in the same
113
<p><c>term</c> is an Erlang term to be encoded.
115
<p><c>bufp</c> is a pointer to a buffer containing one or
116
more encoded Erlang terms.
118
<p><c>bufpp</c> is a pointer to a pointer to a buffer
119
containing one or more consecutively encoded Erlang terms.
120
Following a successful call to <c><![CDATA[erl_encode_buf()]]></c>,
121
<c>bufpp</c> will be updated so that it points to the
122
position for the next encoded term.
125
These functions returns the number of bytes written to buffer
126
if successful, otherwise returns 0.
128
<p>Note that no bounds checking is done on the buffer. It is
129
the caller's responsibility to make sure that the buffer is
130
large enough to hold the encoded terms. You can either use a
131
static buffer that is large enough to hold the terms you
132
expect to need in your program, or use <c><![CDATA[erl_term_len()]]></c>
133
to determine the exact requirements for a given term.
135
<p>The following can help you estimate the buffer
136
requirements for a term. Note that this information is
137
implementation specific, and may change in future versions.
138
If you are unsure, use <c><![CDATA[erl_term_len()]]></c>.
140
<p>Erlang terms are encoded with a 1 byte tag that
141
identifies the type of object, a 2- or 4-byte length field,
142
and then the data itself. Specifically:
145
<tag><c><![CDATA[Tuples]]></c></tag>
146
<item>need 5 bytes, plus the space for each element.</item>
147
<tag><c><![CDATA[Lists]]></c></tag>
148
<item>need 5 bytes, plus the space for each element, and 1
149
additional byte for the empty list at the end.</item>
150
<tag><c><![CDATA[Strings and atoms]]></c></tag>
151
<item>need 3 bytes, plus 1 byte for each character (the
152
terminating 0 is not encoded). Really long strings (more
153
than 64k characters) are encoded as lists. Atoms cannot
154
contain more than 256 characters.</item>
155
<tag><c><![CDATA[Integers]]></c></tag>
156
<item>need 5 bytes.</item>
157
<tag><c><![CDATA[Characters]]></c></tag>
158
<item>(integers < 256) need 2 bytes.</item>
159
<tag><c><![CDATA[Floating point numbers]]></c></tag>
160
<item>need 32 bytes.</item>
161
<tag><c><![CDATA[Pids]]></c></tag>
162
<item>need 10 bytes, plus the space for the node name, which
164
<tag><c><![CDATA[Ports and Refs]]></c></tag>
165
<item>need 6 bytes, plus the space for the node name, which
168
<p>The total space required will be the result calculated
169
from the information above, plus 1 additional byte for a
175
<name><ret>int</ret><nametext>erl_ext_size(bufp)</nametext></name>
176
<fsummary>Counts elements in encoded term</fsummary>
178
<v>unsigned char *bufp;</v>
181
<p>This function returns the number of elements in an
186
<name><ret>unsigned char</ret><nametext>erl_ext_type(bufp)</nametext></name>
187
<fsummary>Determines type of an encoded byte sequence</fsummary>
189
<v>unsigned char *bufp;</v>
192
<p>This function identifies and returns the type of Erlang term encoded
193
in a buffer. It will skip a trailing <em>magic</em> identifier.
194
Returns <c><![CDATA[0]]></c> if the type can't be determined or one of</p>
195
<list type="bulleted">
203
<p>ERL_PID <c><![CDATA[/* Erlang process identifier */]]></c></p>
209
<p>ERL_REF <c><![CDATA[/* Erlang reference */]]></c></p>
212
<p>ERL_EMPTY_LIST</p>
233
<name><ret>unsigned char *</ret><nametext>erl_peek_ext(bufp, pos)</nametext></name>
234
<fsummary>Steps over encoded term</fsummary>
236
<v>unsigned char *bufp;</v>
240
<p>This function is used for stepping over one or more
241
encoded terms in a buffer, in order to directly access a
244
<p><c><![CDATA[bufp]]></c> is a pointer to a buffer containing one or
245
more encoded Erlang terms.
247
<p><c><![CDATA[pos]]></c> indicates how many terms to step over in the
250
<p>The function returns a pointer to a sub-term that can be
251
used in a subsequent call to <c><![CDATA[erl_decode()]]></c> in order to retrieve
252
the term at that position. If there is no term, or <c><![CDATA[pos]]></c>
253
would exceed the size of the terms in the buffer, NULL is returned.
258
<name><ret>int</ret><nametext>erl_term_len(t)</nametext></name>
259
<fsummary>Determines encoded size of term</fsummary>
264
<p>This function determines the buffer space that would be
265
needed by <c><![CDATA[t]]></c> if it were encoded into Erlang external
266
format by <c><![CDATA[erl_encode()]]></c>.
268
<p>The size in bytes is returned.