eolib 0.5.0
A core C library for writing applications related to Endless Online
Loading...
Searching...
No Matches
Optional Fields

The EO protocol uses two mechanisms for optional data: chunked delimiters and padded fixed-length strings.

Reading optional fields with chunked mode:

In chunked reading mode, attempting to read past the end of the current chunk returns 0 (or an empty string) rather than EO_BUFFER_UNDERRUN. This lets you safely read fields that may or may not be present without extra length checks.

EoReader reader = eo_reader_init(buf, buf_len);
int32_t required_field;
eo_reader_get_short(&reader, &required_field);
// This field is only present in some versions of the packet.
int32_t optional_field = 0;
eo_reader_get_char(&reader, &optional_field);
// optional_field remains 0 if the chunk ended before it.
// An optional trailing string.
char *optional_str = NULL;
if (eo_reader_remaining(&reader) > 0) {
eo_reader_get_string(&reader, &optional_str);
}
free(optional_str); // safe even if NULL was never reassigned
EoResult eo_reader_get_string(EoReader *reader, char **out_value)
Definition data.c:1073
EoResult eo_reader_get_short(EoReader *reader, int32_t *out_value)
Definition data.c:985
void eo_reader_set_chunked_reading_mode(EoReader *reader, bool enabled)
Definition data.c:851
EoResult eo_reader_next_chunk(EoReader *reader)
Definition data.c:889
EoResult eo_reader_get_char(EoReader *reader, int32_t *out_value)
Definition data.c:958
EoReader eo_reader_init(const uint8_t *data, size_t length)
Definition data.c:834
size_t eo_reader_remaining(const EoReader *reader)
Definition data.c:863

Writing optional fixed-length strings with padding:

Pass padded = true to eo_writer_add_fixed_string() or eo_writer_add_fixed_encoded_string() to right-pad a shorter string with 0xFF bytes up to the declared length.

// Write a name into a 12-byte padded field.
eo_writer_add_fixed_string(&writer, "Bob", 12, true);
// Produces: 'B','o','b', 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
EoResult eo_writer_add_fixed_string(EoWriter *writer, const char *value, size_t length, bool padded)
Definition data.c:665

Passing NULL as the string value is treated as an empty string and writes nothing (the field is simply omitted), which is useful when an optional trailing field is not present.