---
name: "ei"
section: "3erl"
platform: "linux"
package: "erlang-manpages"
version: "1:25.2.3+dfsg-1+deb12u4"
release: "Debian 12 (Bookworm)"
platforms: ["linux"]
url: "https://new.carta.tech/ei"
---

# ei(3erl)

Routines for handling the Erlang binary term format.

## NAME

ei - Routines for handling the Erlang binary term format.

## DESCRIPTION

The library *ei* contains macros and functions to encode and decode the Erlang binary term format.

*ei* allows you to convert atoms, lists, numbers, and binaries to and from the binary format. This is useful when writing port programs and drivers. *ei* uses a given buffer, no dynamic memory (except *ei\_decode\_fun()*) and is often quite fast.

*ei* also handles C-nodes, C-programs that talks Erlang distribution with Erlang nodes (or other C-nodes) using the Erlang distribution format.The *ei* library is thread safe, and using threads, one process can handle multiple C-nodes.

The decode and encode functions use a buffer and an index into the buffer, which points at the point where to encode and decode. The index is updated to point right after the term encoded/decoded. No checking is done whether the term fits in the buffer or not. If encoding goes outside the buffer, the program can crash.

All functions take two parameters:

- \*

  *buf* is a pointer to the buffer where the binary data is or will be.

- \*

  *index* is a pointer to an index into the buffer. This parameter is incremented with the size of the term decoded/encoded.

The data is thus at *buf\[\*index\]* when an *ei* function is called.

All encode functions assume that the *buf* and *index* parameters point to a buffer large enough for the data. To get the size of an encoded term, without encoding it, pass *NULL* instead of a buffer pointer. Parameter *index* is incremented, but nothing will be encoded. This is the way in *ei* to "preflight" term encoding.

There are also encode functions that use a dynamic buffer. It is often more convenient to use these to encode data. All encode functions comes in two versions; those starting with *ei\_x\_* use a dynamic buffer of type *ei\_x\_buff*.

All functions return *0* if successful, otherwise *-1* (for example, if a term is not of the expected type, or the data to decode is an invalid Erlang term).

Some of the decode functions need a pre-allocated buffer. This buffer must be allocated large enough, and for non-compound types the *ei\_get\_type()* function returns the size required (notice that for strings an extra byte is needed for the *NULL*-terminator).

## DATA TYPES

- ***ei\_term*:**

  ```
typedef struct {
    char ei_type;
    int arity;
    int size;
    union {
	long i_val;
	double d_val;
	char atom_name[MAXATOMLEN_UTF8];
	erlang_pid pid;
	erlang_port port;
	erlang_ref ref;
    } value;
} ei_term;
```

Structure written by *ei\_decode\_ei\_term()*. The *ei\_type* field is the type of the term which equals to what *ei\_get\_type()* sets *\*type* to.

- ***ei\_x\_buff*:**

  A dynamically resized buffer. It is a *struct* with two fields of interest for the user:

  - ***char \*buff*:**

    Pointer to the dynamically allocated buffer.

  - ***int index*:**

    Offset to the next byte to write which also equals the amount of bytes currently written.

  An *ei\_x\_buff* is initialized by calling either *ei\_x\_new()* or *ei\_x\_new\_with\_version()*. The memory used by an initialized *ei\_x\_buff* is released by calling *ei\_x\_free()*.

- ***erlang\_char\_encoding*:**

  ```
typedef enum {
    ERLANG_ASCII = 1,
    ERLANG_LATIN1 = 2,
    ERLANG_UTF8 = 4
} erlang_char_encoding;
```

The character encodings used for atoms. *ERLANG\_ASCII* represents 7-bit ASCII. Latin-1 and UTF-8 are different extensions of 7-bit ASCII. All 7-bit ASCII characters are valid Latin-1 and UTF-8 characters. ASCII and Latin-1 both represent each character by one byte. An UTF-8 character can consist of 1-4 bytes. Notice that these constants are bit-flags and can be combined with bitwise OR.

- ***erlang\_fun*:**

  Opaque data type representing an Erlang fun.

- ***erlang\_pid*:**

  Opaque data type representing an Erlang process identifier.

- ***erlang\_port*:**

  Opaque data type representing an Erlang port identifier.

- ***erlang\_ref*:**

  Opaque data type representing an Erlang reference.

- ***erlang\_trace*:**

  Opaque data type representing an Erlang sequential trace token.

## EXPORTS

**int ei\_cmp\_pids(erlang\_pid \*a, erlang\_pid \*b)**\

Types:

*erlang\_pid*\

Compare two process identifiers. The comparison is done the same way as Erlang does.

Returns *0* if *a* and *b* are equal. Returns a value less than *0* if *a* compares as less than *b*. Returns a value larger than *0* if *a* compares as larger than *b*.

**int ei\_cmp\_ports(erlang\_port \*a, erlang\_port \*b)**\

Types:

*erlang\_port*\

Compare two port identifiers. The comparison is done the same way as Erlang does.

Returns *0* if *a* and *b* are equal. Returns a value less than *0* if *a* compares as less than *b*. Returns a value larger than *0* if *a* compares as larger than *b*.

**int ei\_cmp\_refs(erlang\_ref \*a, erlang\_ref \*b)**\

Types:

*erlang\_ref*\

Compare two references. The comparison is done the same way as Erlang does.

Returns *0* if *a* and *b* are equal. Returns a value less than *0* if *a* compares as less than *b*. Returns a value larger than *0* if *a* compares as larger than *b*.

**int ei\_decode\_atom(const char \*buf, int \*index, char \*p)**\

Decodes an atom from the binary format. The *NULL*-terminated name of the atom is placed at *p*. At most *MAXATOMLEN* bytes can be placed in the buffer.

**int ei\_decode\_atom\_as(const char \*buf, int \*index, char \*p, int plen, erlang\_char\_encoding want, erlang\_char\_encoding\* was, erlang\_char\_encoding\* result)**\

Types:

*erlang\_char\_encoding*\

Decodes an atom from the binary format. The *NULL*-terminated name of the atom is placed in buffer at *p* of length *plen* bytes.

The wanted string encoding is specified by *want*. The original encoding used in the binary format (Latin-1 or UTF-8) can be obtained from *\*was*. The encoding of the resulting string (7-bit ASCII, Latin-1, or UTF-8) can be obtained from *\*result*. Both *was* and *result* can be *NULL*. *\*result* can differ from *want* if *want* is a bitwise OR'd combination like *ERLANG\_LATIN1\|ERLANG\_UTF8* or if *\*result* turns out to be pure 7-bit ASCII (compatible with both Latin-1 and UTF-8).

This function fails if the atom is too long for the buffer or if it cannot be represented with encoding *want*.

This function was introduced in Erlang/OTP R16 as part of a first step to support UTF-8 atoms.

**int ei\_decode\_bignum(const char \*buf, int \*index, mpz\_t obj)**\

Decodes an integer in the binary format to a GMP *mpz\_t* integer. To use this function, the *ei* library must be configured and compiled to use the GMP library.

**int ei\_decode\_binary(const char \*buf, int \*index, void \*p, long \*len)**\

Decodes a binary from the binary format. Parameter *len* is set to the actual size of the binary. Notice that *ei\_decode\_binary()* assumes that there is enough room for the binary. The size required can be fetched by *ei\_get\_type()*.

**int ei\_decode\_bitstring(const char \*buf, int \*index, const char \*\*pp, unsigned int \*bitoffsp, size\_t \*nbitsp)**\

Decodes a bit string from the binary format.

- ***pp*:**

  Either *NULL* or *\*pp* returns a pointer to the first byte of the bit string. The returned bit string is readable as long as the buffer pointed to by *buf* is readable and not written to.

- ***bitoffsp*:**

  Either *NULL* or *\*bitoffsp* returns the number of unused bits in the first byte pointed to by *\*pp*. The value of *\*bitoffsp* is between 0 and 7. Unused bits in the first byte are the most significant bits.

- ***nbitsp*:**

  Either *NULL* or *\*nbitsp* returns the length of the bit string in *bits*.

Returns *0* if it was a bit string term.

The number of *bytes* pointed to by *\*pp*, which are part of the bit string, is *(\*bitoffsp + \*nbitsp + 7)/8*. If *(\*bitoffsp + \*bitsp)%8 \> 0* then only *(\*bitoffsp + \*bitsp)%8* bits of the last byte are used. Unused bits in the last byte are the least significant bits.

The values of unused bits in the first and last byte are undefined and cannot be relied on.

Number of bits may be divisible by 8, which means a binary decodable by *ei\_decode\_binary* is also decodable by *ei\_decode\_bitstring*.

**int ei\_decode\_boolean(const char \*buf, int \*index, int \*p)**\

Decodes a boolean value from the binary format. A boolean is actually an atom, *true* decodes 1 and *false* decodes 0.

**int ei\_decode\_char(const char \*buf, int \*index, char \*p)**\

Decodes a char (8-bit) integer between 0-255 from the binary format. For historical reasons the returned integer is of type *char*. Your C code is to consider the returned value to be of type *unsigned char* even if the C compilers and system can define *char* to be signed.

**int ei\_decode\_double(const char \*buf, int \*index, double \*p)**\

Decodes a double-precision (64-bit) floating point number from the binary format.

**int ei\_decode\_ei\_term(const char\* buf, int\* index, ei\_term\* term)**\

Types:

*ei\_term*\

Decodes any term, or at least tries to. If the term pointed at by *\*index* in *buf* fits in the *term* union, it is decoded, and the appropriate field in *term-\>value* is set, and *\*index* is incremented by the term size.

The function returns *1* on successful decoding, *-1* on error, and *0* if the term seems alright, but does not fit in the *term* structure. If *1* is returned, the *index* is incremented, and *term* contains the decoded term.

The *term* structure contains the arity for a tuple or list, size for a binary, string, or atom. It contains a term if it is any of the following: integer, float, atom, pid, port, or ref.

**int ei\_decode\_fun(const char \*buf, int \*index, erlang\_fun \*p)**\
 **void free\_fun(erlang\_fun\* f)**\

Types:

*erlang\_fun*\

Decodes a fun from the binary format. Parameter *p* is to be *NULL* or point to an *erlang\_fun* structure. This is the only decode function that allocates memory. When the *erlang\_fun* is no longer needed, it is to be freed with *free\_fun*. (This has to do with the arbitrary size of the environment for a fun.)

**int ei\_decode\_iodata(const char \*buf, int \*index, int \*size, char \*outbuf)**\

Decodes a term of the type *iodata()*. The *iodata()* term will be flattened an written into the buffer pointed to by the *outbuf* argument. The byte size of the *iodata* is written into the integer variable pointed to by the *size* argument. Both *size* and *outbuf* can be set to *NULL*. The integer pointed to by the *index* argument is updated to refer to the term following after the *iodata()* term regardless of the the state of the *size* and the *outbuf* arguments.

Note that the buffer pointed to by the *outbuf* argument must be large enough if a non *NULL* value is passed as *outbuf*. You typically want to call *ei\_decode\_iodata()* twice. First with a non *NULL* *size* argument and a *NULL* *outbuf* argument in order to determine the size of the buffer needed, and then once again in order to do the actual decoding. Note that the integer pointed to by *index* will be updated by the call determining the size as well, so you need to reset it before the second call doing the actual decoding.

Returns *0* on success and *-1* on failure. Failure might be either due to invalid encoding of the term or due to the term not being of the type *iodata()*. On failure, the integer pointed to by the *index* argument will be updated to refer to the sub term where the failure was detected.

**int ei\_decode\_list\_header(const char \*buf, int \*index, int \*arity)**\

Decodes a list header from the binary format. The number of elements is returned in *arity*. The *arity+1* elements follow (the last one is the tail of the list, normally an empty list). If *arity* is *0*, it is an empty list.

Notice that lists are encoded as strings if they consist entirely of integers in the range 0..255. This function do not decode such strings, use *ei\_decode\_string()* instead.

**int ei\_decode\_long(const char \*buf, int \*index, long \*p)**\

Decodes a long integer from the binary format. If the code is 64 bits, the function *ei\_decode\_long()* is the same as *ei\_decode\_longlong()*.

**int ei\_decode\_longlong(const char \*buf, int \*index, long long \*p)**\

Decodes a GCC *long long* or Visual C++ *\_\_int64* (64-bit) integer from the binary format.

**int ei\_decode\_map\_header(const char \*buf, int \*index, int \*arity)**\

Decodes a map header from the binary format. The number of key-value pairs is returned in *\*arity*. Keys and values follow in this order: *K1, V1, K2, V2, ..., Kn, Vn*. This makes a total of *arity\*2* terms. If *arity* is zero, it is an empty map. A correctly encoded map does not have duplicate keys.

**int ei\_decode\_pid(const char \*buf, int \*index, erlang\_pid \*p)**\

Types:

*erlang\_pid*\

Decodes a process identifier (pid) from the binary format.

**int ei\_decode\_port(const char \*buf, int \*index, erlang\_port \*p)**\

Types:

*erlang\_port*\

Decodes a port identifier from the binary format.

**int ei\_decode\_ref(const char \*buf, int \*index, erlang\_ref \*p)**\

Types:

*erlang\_ref*\

Decodes a reference from the binary format.

**int ei\_decode\_string(const char \*buf, int \*index, char \*p)**\

Decodes a string from the binary format. A string in Erlang is a list of integers between 0 and 255. Notice that as the string is just a list, sometimes lists are encoded as strings by *term\_to\_binary/1*, even if it was not intended.

The string is copied to *p*, and enough space must be allocated. The returned string is *NULL*-terminated, so you must add an extra byte to the memory requirement.

**int ei\_decode\_trace(const char \*buf, int \*index, erlang\_trace \*p)**\

Types:

*erlang\_trace*\

Decodes an Erlang trace token from the binary format.

**int ei\_decode\_tuple\_header(const char \*buf, int \*index, int \*arity)**\

Decodes a tuple header, the number of elements is returned in *arity*. The tuple elements follow in order in the buffer.

**int ei\_decode\_ulong(const char \*buf, int \*index, unsigned long \*p)**\

Decodes an unsigned long integer from the binary format. If the code is 64 bits, the function *ei\_decode\_ulong()* is the same as *ei\_decode\_ulonglong()*.

**int ei\_decode\_ulonglong(const char \*buf, int \*index, unsigned long long \*p)**\

Decodes a GCC *unsigned long long* or Visual C++ *unsigned \_\_int64* (64-bit) integer from the binary format.

**int ei\_decode\_version(const char \*buf, int \*index, int \*version)**\

Decodes the version magic number for the Erlang binary term format. It must be the first token in a binary term.

**int ei\_encode\_atom(char \*buf, int \*index, const char \*p)**\
 **int ei\_encode\_atom\_len(char \*buf, int \*index, const char \*p, int len)**\
 **int ei\_x\_encode\_atom(ei\_x\_buff\* x, const char \*p)**\
 **int ei\_x\_encode\_atom\_len(ei\_x\_buff\* x, const char \*p, int len)**\

Types:

*ei\_x\_buff*\

Encodes an atom in the binary format. Parameter *p* is the name of the atom in Latin-1 encoding. Only up to *MAXATOMLEN-1* bytes are encoded. The name is to be *NULL*-terminated, except for the *ei\_x\_encode\_atom\_len()* function.

**int ei\_encode\_atom\_as(char \*buf, int \*index, const char \*p, erlang\_char\_encoding from\_enc, erlang\_char\_encoding to\_enc)**\
 **int ei\_encode\_atom\_len\_as(char \*buf, int \*index, const char \*p, int len, erlang\_char\_encoding from\_enc, erlang\_char\_encoding to\_enc)**\
 **int ei\_x\_encode\_atom\_as(ei\_x\_buff\* x, const char \*p, erlang\_char\_encoding from\_enc, erlang\_char\_encoding to\_enc)**\
 **int ei\_x\_encode\_atom\_len\_as(ei\_x\_buff\* x, const char \*p, int len, erlang\_char\_encoding from\_enc, erlang\_char\_encoding to\_enc)**\

Types:

*ei\_x\_buff*\
 *erlang\_char\_encoding*\

Encodes an atom in the binary format. Parameter *p* is the name of the atom with character encoding *from\_enc* (ASCII, Latin-1, or UTF-8). The name must either be *NULL*-terminated or a function variant with a *len* parameter must be used.

The encoding fails if *p* is not a valid string in encoding *from\_enc*.

Argument *to\_enc* is ignored. As from Erlang/OTP 20 the encoding is always done in UTF-8 which is readable by nodes as old as Erlang/OTP R16.

**int ei\_encode\_bignum(char \*buf, int \*index, mpz\_t obj)**\
 **int ei\_x\_encode\_bignum(ei\_x\_buff \*x, mpz\_t obj)**\

Types:

*ei\_x\_buff*\

Encodes a GMP *mpz\_t* integer to binary format. To use this function, the *ei* library must be configured and compiled to use the GMP library.

**int ei\_encode\_binary(char \*buf, int \*index, const void \*p, long len)**\
 **int ei\_x\_encode\_binary(ei\_x\_buff\* x, const void \*p, long len)**\

Types:

*ei\_x\_buff*\

Encodes a binary in the binary format. The data is at *p*, of *len* bytes length.

**int ei\_encode\_bitstring(char \*buf, int \*index, const char \*p, size\_t bitoffs, size\_t nbits)**\
 **int ei\_x\_encode\_bitstring(ei\_x\_buff\* x, const char \*p, size\_t bitoffs, size\_t nbits)**\

Types:

*ei\_x\_buff*\

Encodes a bit string in the binary format.

The data is at *p*. The length of the bit string is *nbits* bits. The first *bitoffs* bits of the data at *p* are unused. The first byte which is part of the bit string is *p\[bitoffs/8\]*. The *bitoffs%8* most significant bits of the first byte *p\[bitoffs/8\]* are unused.

The number of bytes which is part of the bit string is *(bitoffs + nbits + 7)/8*. If *(bitoffs + nbits)%8 \> 0* then only *(bitoffs + nbits)%8* bits of the last byte are used. Unused bits in the last byte are the least significant bits.

The values of unused bits are disregarded and does not need to be cleared.

**int ei\_encode\_boolean(char \*buf, int \*index, int p)**\
 **int ei\_x\_encode\_boolean(ei\_x\_buff\* x, int p)**\

Types:

*ei\_x\_buff*\

Encodes a boolean value as the atom *true* if *p* is not zero, or *false* if *p* is zero.

**int ei\_encode\_char(char \*buf, int \*index, char p)**\
 **int ei\_x\_encode\_char(ei\_x\_buff\* x, char p)**\

Types:

*ei\_x\_buff*\

Encodes a char (8-bit) as an integer between 0-255 in the binary format. For historical reasons the integer argument is of type *char*. Your C code is to consider the specified argument to be of type *unsigned char* even if the C compilers and system may define *char* to be signed.

**int ei\_encode\_double(char \*buf, int \*index, double p)**\
 **int ei\_x\_encode\_double(ei\_x\_buff\* x, double p)**\

Types:

*ei\_x\_buff*\

Encodes a double-precision (64-bit) floating point number in the binary format.

Returns *-1* if the floating point number is not finite.

**int ei\_encode\_empty\_list(char\* buf, int\* index)**\
 **int ei\_x\_encode\_empty\_list(ei\_x\_buff\* x)**\

Types:

*ei\_x\_buff*\

Encodes an empty list. It is often used at the tail of a list.

**int ei\_encode\_fun(char \*buf, int \*index, const erlang\_fun \*p)**\
 **int ei\_x\_encode\_fun(ei\_x\_buff\* x, const erlang\_fun\* fun)**\

Types:

*ei\_x\_buff*\
 *erlang\_fun*\

Encodes a fun in the binary format. Parameter *p* points to an *erlang\_fun* structure. The *erlang\_fun* is not freed automatically, the *free\_fun* is to be called if the fun is not needed after encoding.

**int ei\_encode\_list\_header(char \*buf, int \*index, int arity)**\
 **int ei\_x\_encode\_list\_header(ei\_x\_buff\* x, int arity)**\

Types:

*ei\_x\_buff*\

Encodes a list header, with a specified arity. The next *arity+1* terms are the elements (actually its *arity* cons cells) and the tail of the list. Lists and tuples are encoded recursively, so that a list can contain another list or tuple.

For example, to encode the list *\[c, d, \[e \| f\]\]*:

```
ei_encode_list_header(buf, &i, 3);
ei_encode_atom(buf, &i, "c");
ei_encode_atom(buf, &i, "d");
ei_encode_list_header(buf, &i, 1);
ei_encode_atom(buf, &i, "e");
ei_encode_atom(buf, &i, "f");
ei_encode_empty_list(buf, &i);
```

**Note:**

It may seem that there is no way to create a list without knowing the number of elements in advance. But indeed there is a way. Notice that the list *\[a, b, c\]* can be written as *\[a \| \[b \| \[c\]\]\]*. Using this, a list can be written as conses.

To encode a list, without knowing the arity in advance:

```
while (something()) {
    ei_x_encode_list_header(&x, 1);
    ei_x_encode_ulong(&x, i); /* just an example */
}
ei_x_encode_empty_list(&x);
```

**int ei\_encode\_long(char \*buf, int \*index, long p)**\
 **int ei\_x\_encode\_long(ei\_x\_buff\* x, long p)**\

Types:

*ei\_x\_buff*\

Encodes a long integer in the binary format. If the code is 64 bits, the function *ei\_encode\_long()* is the same as *ei\_encode\_longlong()*.

**int ei\_encode\_longlong(char \*buf, int \*index, long long p)**\
 **int ei\_x\_encode\_longlong(ei\_x\_buff\* x, long long p)**\

Types:

*ei\_x\_buff*\

Encodes a GCC *long long* or Visual C++ *\_\_int64* (64-bit) integer in the binary format.

**int ei\_encode\_map\_header(char \*buf, int \*index, int arity)**\
 **int ei\_x\_encode\_map\_header(ei\_x\_buff\* x, int arity)**\

Types:

*ei\_x\_buff*\

Encodes a map header, with a specified arity. The next *arity\*2* terms encoded will be the keys and values of the map encoded in the following order: *K1, V1, K2, V2, ..., Kn, Vn*.

For example, to encode the map *#{a =\> "Apple", b =\> "Banana"}*:

```
ei_x_encode_map_header(&x, 2);
ei_x_encode_atom(&x, "a");
ei_x_encode_string(&x, "Apple");
ei_x_encode_atom(&x, "b");
ei_x_encode_string(&x, "Banana");
```

A correctly encoded map cannot have duplicate keys.

**int ei\_encode\_pid(char \*buf, int \*index, const erlang\_pid \*p)**\
 **int ei\_x\_encode\_pid(ei\_x\_buff\* x, const erlang\_pid \*p)**\

Types:

*ei\_x\_buff*\
 *erlang\_pid*\

Encodes an Erlang process identifier (pid) in the binary format. Parameter *p* points to an *erlang\_pid* structure which should either have been obtained earlier with *ei\_decode\_pid()*, *ei\_self()* or created by *ei\_make\_pid()*.

**int ei\_encode\_port(char \*buf, int \*index, const erlang\_port \*p)**\
 **int ei\_x\_encode\_port(ei\_x\_buff\* x, const erlang\_port \*p)**\

Types:

*ei\_x\_buff*\
 *erlang\_port*\

Encodes an Erlang port in the binary format. Parameter *p* points to an *erlang\_port* structure which should have been obtained earlier with *ei\_decode\_port()*,

**int ei\_encode\_ref(char \*buf, int \*index, const erlang\_ref \*p)**\
 **int ei\_x\_encode\_ref(ei\_x\_buff\* x, const erlang\_ref \*p)**\

Types:

*ei\_x\_buff*\
 *erlang\_ref*\

Encodes an Erlang reference in the binary format. Parameter *p* points to an *erlang\_ref* structure which either should have been obtained earlier with *ei\_decode\_ref()*, or created by *ei\_make\_ref()*.

**int ei\_encode\_string(char \*buf, int \*index, const char \*p)**\
 **int ei\_encode\_string\_len(char \*buf, int \*index, const char \*p, int len)**\
 **int ei\_x\_encode\_string(ei\_x\_buff\* x, const char \*p)**\
 **int ei\_x\_encode\_string\_len(ei\_x\_buff\* x, const char\* s, int len)**\

Types:

*ei\_x\_buff*\

Encodes a string in the binary format. (A string in Erlang is a list, but is encoded as a character array in the binary format.) The string is to be *NULL*-terminated, except for the *ei\_x\_encode\_string\_len()* function.

**int ei\_encode\_trace(char \*buf, int \*index, const erlang\_trace \*p)**\
 **int ei\_x\_encode\_trace(ei\_x\_buff\* x, const erlang\_trace \*p)**\

Types:

*ei\_x\_buff*\
 *erlang\_trace*\

Encodes an Erlang trace token in the binary format. Parameter *p* points to a *erlang\_trace* structure which should have been obtained earlier with *ei\_decode\_trace()*.

**int ei\_encode\_tuple\_header(char \*buf, int \*index, int arity)**\
 **int ei\_x\_encode\_tuple\_header(ei\_x\_buff\* x, int arity)**\

Types:

*ei\_x\_buff*\

Encodes a tuple header, with a specified arity. The next *arity* terms encoded will be the elements of the tuple. Tuples and lists are encoded recursively, so that a tuple can contain another tuple or list.

For example, to encode the tuple *{a, {b, {}}}*:

```
ei_encode_tuple_header(buf, &i, 2);
ei_encode_atom(buf, &i, "a");
ei_encode_tuple_header(buf, &i, 2);
ei_encode_atom(buf, &i, "b");
ei_encode_tuple_header(buf, &i, 0);
```

**int ei\_encode\_ulong(char \*buf, int \*index, unsigned long p)**\
 **int ei\_x\_encode\_ulong(ei\_x\_buff\* x, unsigned long p)**\

Types:

*ei\_x\_buff*\

Encodes an unsigned long integer in the binary format. If the code is 64 bits, the function *ei\_encode\_ulong()* is the same as *ei\_encode\_ulonglong()*.

**int ei\_encode\_ulonglong(char \*buf, int \*index, unsigned long long p)**\
 **int ei\_x\_encode\_ulonglong(ei\_x\_buff\* x, unsigned long long p)**\

Types:

*ei\_x\_buff*\

Encodes a GCC *unsigned long long* or Visual C++ *unsigned \_\_int64* (64-bit) integer in the binary format.

**int ei\_encode\_version(char \*buf, int \*index)**\
 **int ei\_x\_encode\_version(ei\_x\_buff\* x)**\

Types:

*ei\_x\_buff*\

Encodes a version magic number for the binary format. Must be the first token in a binary term.

**int ei\_get\_type(const char \*buf, const int \*index, int \*type, int \*size)**\

Returns the type in *\*type* and size in *\*size* of the encoded term. For strings and atoms, size is the number of characters *not* including the terminating *NULL*. For binaries and bitstrings, *\*size* is the number of bytes. For lists, tuples and maps, *\*size* is the arity of the object. For bignum integers, *\*size* is the number of bytes for the absolute value of the bignum. For other types, *\*size* is 0. In all cases, *index* is left unchanged.

Currently *\*type* is one of:

- **ERL\_ATOM\_EXT:**

  Decode using either *ei\_decode\_atom()*, *ei\_decode\_atom\_as()*, or *ei\_decode\_boolean()*.

- **ERL\_BINARY\_EXT:**

  Decode using either *ei\_decode\_binary()*, *ei\_decode\_bitstring()*, or *ei\_decode\_iodata()*.

- **ERL\_BIT\_BINARY\_EXT:**

  Decode using *ei\_decode\_bitstring()*.

- **ERL\_FLOAT\_EXT:**

  Decode using *ei\_decode\_double()*.

- **ERL\_NEW\_FUN\_EXT**

  ERL\_FUN\_EXT\
   ERL\_EXPORT\_EXT: Decode using *ei\_decode\_fun()*.

- **ERL\_SMALL\_INTEGER\_EXT**

  ERL\_INTEGER\_EXT\
   ERL\_SMALL\_BIG\_EXT\
   ERL\_LARGE\_BIG\_EXT: Decode using either *ei\_decode\_char()*, *ei\_decode\_long()*, *ei\_decode\_longlong()*, *ei\_decode\_ulong()*, *ei\_decode\_ulonglong()*, or *ei\_decode\_bignum()*.

- **ERL\_LIST\_EXT**

  ERL\_NIL\_EXT: Decode using either *ei\_decode\_list\_header()*, or *ei\_decode\_iodata()*.

- **ERL\_STRING\_EXT:**

  Decode using either *ei\_decode\_string()*, or *ei\_decode\_iodata()*.

- **ERL\_MAP\_EXT:**

  Decode using *ei\_decode\_map\_header()*.

- **ERL\_PID\_EXT:**

  Decode using *ei\_decode\_pid()*.

- **ERL\_PORT\_EXT:**

  Decode using *ei\_decode\_port()*.

- **ERL\_NEW\_REFERENCE\_EXT:**

  Decode using *ei\_decode\_ref()*.

- **ERL\_SMALL\_TUPLE\_EXT**

  ERL\_LARGE\_TUPLE\_EXT: Decode using *ei\_decode\_tuple\_header()*.

Instead of decoding a term you can also skipped past it if you are not interested in the data by usage of *ei\_skip\_term()*.

**int ei\_init(void)**\

Initialize the *ei* library. This function should be called once (and only once) before calling any other functionality in the *ei* library.

On success zero is returned. On failure a posix error code is returned.

**int ei\_print\_term(FILE\* fp, const char\* buf, int\* index)**\
 **int ei\_s\_print\_term(char\*\* s, const char\* buf, int\* index)**\

Prints a term, in clear text, to the file specified by *fp*, or the buffer pointed to by *s*. It tries to resemble the term printing in the Erlang shell.

In *ei\_s\_print\_term()*, parameter *s* is to point to a dynamically (malloc) allocated string of *BUFSIZ* bytes or a *NULL* pointer. The string can be reallocated (and *\*s* can be updated) by this function if the result is more than *BUFSIZ* characters. The string returned is *NULL*-terminated.

The return value is the number of characters written to the file or string, or *-1* if *buf\[index\]* does not contain a valid term. Unfortunately, I/O errors on *fp* is not checked.

Argument *index* is updated, that is, this function can be viewed as a decode function that decodes a term into a human-readable format.

**void ei\_set\_compat\_rel(unsigned release\_number)**\

In general, the *ei* library is guaranteed to be compatible with other Erlang/OTP components that are 2 major releases older or newer than the *ei* library itself.

Sometimes an exception to the above rule has to be made to make new features (or even bug fixes) possible. A call to *ei\_set\_compat\_rel(release\_number)* sets the *ei* library in compatibility mode of OTP release *release\_number*.

The only useful value for *release\_number* is currently *21*. This will only be useful and have an effect if *bit strings* or *export funs* are received from a connected node. Before OTP 22, bit strings and export funs were not supported by *ei*. They were instead encoded using an undocumented fallback tuple format when sent from the emulator to *ei*:

- ***Bit string*:**

  The term *\<\<42, 1:1\>\>* was encoded as *{\<\<42, 128\>\>, 1}*. The first element of the tuple is a binary and the second element denotes how many bits of the last bytes are part of the bit string. In this example only the most significant bit of the last byte (128) is part of the bit string.

- ***Export fun*:**

  The term *fun lists:map/2* was encoded as *{lists,map}*. A tuple with the module, function and a missing arity.

If *ei\_set\_compat\_rel(21)* is *not* called then a connected emulator will send bit strings and export funs correctly encoded. The functions *ei\_decode\_bitstring* and *ei\_decode\_fun* has to be used to decode such terms. Calling *ei\_set\_compat\_rel(21)* should only be done as a workaround to keep an old implementation alive, which expects to receive the undocumented tuple formats for bit strings and/or export funs.

**Note:**

If this function is called, it can only be called once and must be called before any other functions in the *ei* library are called.

**int ei\_skip\_term(const char\* buf, int\* index)**\

Skips a term in the specified buffer; recursively skips elements of lists and tuples, so that a full term is skipped. This is a way to get the size of an Erlang term.

*buf* is the buffer.

*index* is updated to point right after the term in the buffer.

**Note:**

This can be useful when you want to hold arbitrary terms: skip them and copy the binary term data to some buffer.

Returns *0* on success, otherwise *-1*.

**int ei\_x\_append(ei\_x\_buff\* x, const ei\_x\_buff\* x2)**\
 **int ei\_x\_append\_buf(ei\_x\_buff\* x, const char\* buf, int len)**\

Types:

*ei\_x\_buff*\

Appends data at the end of buffer *x*.

**int ei\_x\_format(ei\_x\_buff\* x, const char\* fmt, ...)**\
 **int ei\_x\_format\_wo\_ver(ei\_x\_buff\* x, const char \*fmt, ... )**\

Types:

*ei\_x\_buff*\
 *erlang\_pid*\

Formats a term, given as a string, to a buffer. Works like a sprintf for Erlang terms. *fmt* contains a format string, with arguments like *~d*, to insert terms from variables. The following formats are supported (with the C types given):

```
~a  An atom, char*
~c  A character, char
~s  A string, char*
~i  An integer, int
~l  A long integer, long int
~u  A unsigned long integer, unsigned long int
~f  A float, float
~d  A double float, double float
~p  An Erlang pid, erlang_pid*
```

For example, to encode a tuple with some stuff:

```
ei_x_format("{~a,~i,~d}", "numbers", 12, 3.14159)
encodes the tuple {numbers,12,3.14159}
```

*ei\_x\_format\_wo\_ver()* formats into a buffer, without the initial version byte.

**int ei\_x\_free(ei\_x\_buff\* x)**\

Types:

*ei\_x\_buff*\

Deallocates the dynamically allocated content of the buffer referred by *x*. After deallocation, the *buff* field is set to *NULL*.

**int ei\_x\_new(ei\_x\_buff\* x)**\
 **int ei\_x\_new\_with\_version(ei\_x\_buff\* x)**\

Types:

*ei\_x\_buff*\

Initialize the dynamically realizable buffer referred to by *x*. The fields of the structure pointed to by parameter *x* is filled in, and a default buffer is allocated. *ei\_x\_new\_with\_version()* also puts an initial version byte, which is used in the binary format (so that *ei\_x\_encode\_version()* will not be needed.)

## DEBUG INFORMATION

Some tips on what to check when the emulator does not seem to receive the terms that you send:

- \*

  Be careful with the version header, use *ei\_x\_new\_with\_version()* when appropriate.

- \*

  Turn on distribution tracing on the Erlang node.

- \*

  Check the result codes from *ei\_decode\_-calls*.
