19962016 Ericsson AB. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. erl_marshal Torbjörn Törnkvist Torbjörn Törnkvist Bjarne Däcker Torbjörn Törnkvist 980703 A erl_marshal.sgml
erl_marshal Encoding and Decoding of Erlang terms

This module contains functions for encoding Erlang terms into a sequence of bytes, and for decoding Erlang terms from a sequence of bytes.

interl_compare_ext(bufp1, bufp2) Compares encoded byte sequences unsigned char *bufp1,*bufp2;

This function compares two encoded terms.

is a buffer containing an encoded Erlang term term1.

is a buffer containing an encoded Erlang term term2.

The function returns 0 if the terms are equal, -1 if term1 is less than term2, or 1 if term2 is less than term1.

ETERM *erl_decode(bufp) ETERM *erl_decode_buf(bufpp) Converts a term from Erlang external format unsigned char *bufp; unsigned char **bufpp;

and decode the contents of a buffer and return the corresponding Erlang term. provides a simple mechanism for dealing with several encoded terms stored consecutively in the buffer.

is a pointer to a buffer containing one or more encoded Erlang terms.

is the address of a buffer pointer. The buffer contains one or more consecutively encoded Erlang terms. Following a successful call to , will be updated so that it points to the next encoded term.

returns an Erlang term corresponding to the contents of on success, or NULL on failure. returns an Erlang term corresponding to the first of the consecutive terms in and moves forward to point to the next term in the buffer. On failure, each of the functions returns NULL.

interl_encode(term, bufp) interl_encode_buf(term, bufpp) Converts a term into Erlang external format ETERM *term; unsigned char *bufp; unsigned char **bufpp;

and encode Erlang terms into external format for storage or transmission. provides a simple mechanism for encoding several terms consecutively in the same buffer.

term is an Erlang term to be encoded.

bufp is a pointer to a buffer containing one or more encoded Erlang terms.

bufpp is a pointer to a pointer to a buffer containing one or more consecutively encoded Erlang terms. Following a successful call to , bufpp will be updated so that it points to the position for the next encoded term.

These functions returns the number of bytes written to buffer if successful, otherwise returns 0.

Note that no bounds checking is done on the buffer. It is the caller's responsibility to make sure that the buffer is large enough to hold the encoded terms. You can either use a static buffer that is large enough to hold the terms you expect to need in your program, or use to determine the exact requirements for a given term.

The following can help you estimate the buffer requirements for a term. Note that this information is implementation specific, and may change in future versions. If you are unsure, use .

Erlang terms are encoded with a 1 byte tag that identifies the type of object, a 2- or 4-byte length field, and then the data itself. Specifically:

need 5 bytes, plus the space for each element. need 5 bytes, plus the space for each element, and 1 additional byte for the empty list at the end. need 3 bytes, plus 1 byte for each character (the terminating 0 is not encoded). Really long strings (more than 64k characters) are encoded as lists. Atoms cannot contain more than 256 characters. need 5 bytes. (integers < 256) need 2 bytes. need 32 bytes. need 10 bytes, plus the space for the node name, which is an atom. need 6 bytes, plus the space for the node name, which is an atom.

The total space required will be the result calculated from the information above, plus 1 additional byte for a version identifier.

interl_ext_size(bufp) Counts elements in encoded term unsigned char *bufp;

This function returns the number of elements in an encoded term.

unsigned charerl_ext_type(bufp) Determines type of an encoded byte sequence unsigned char *bufp;

This function identifies and returns the type of Erlang term encoded in a buffer. It will skip a trailing magic identifier. Returns if the type can't be determined or one of

ERL_INTEGER

ERL_ATOM

ERL_PID

ERL_PORT

ERL_REF

ERL_EMPTY_LIST

ERL_LIST

ERL_TUPLE

ERL_FLOAT

ERL_BINARY

ERL_FUNCTION

unsigned char *erl_peek_ext(bufp, pos) Steps over encoded term unsigned char *bufp; int pos;

This function is used for stepping over one or more encoded terms in a buffer, in order to directly access a later term.

is a pointer to a buffer containing one or more encoded Erlang terms.

indicates how many terms to step over in the buffer.

The function returns a pointer to a sub-term that can be used in a subsequent call to in order to retrieve the term at that position. If there is no term, or would exceed the size of the terms in the buffer, NULL is returned.

interl_term_len(t) Determines encoded size of term ETERM *t;

This function determines the buffer space that would be needed by if it were encoded into Erlang external format by .

The size in bytes is returned.