Function amqp_decode_table

Synopsis

#include <librabbitmq/amqp.h>

int amqp_decode_table(amqp_bytes_t encoded, amqp_pool_t *pool, amqp_table_t *output, size_t *offset)

Description

Deserialize an amqp_table_t from AMQP wireformat

This is an internal function and is not typically used by client applications

Parameters:

[ in ] encoded - the buffer containing the serialized data

[ in ] pool - memory pool used to allocate the table entries from

[ in ] output - the amqp_table_t structure to fill in. Any existing entries will be erased

[ in,out ] offset - The offset into the encoded buffer to start reading the serialized table. It will be updated by this function to end of the table

Return
AMQP_STATUS_OK on success, an amqp_status_enum value on failure Possible error codes:
Since
v0.1

Source

Lines 125-203 in librabbitmq/amqp_table.c. Line 2104 in librabbitmq/amqp.h.

int amqp_decode_table(amqp_bytes_t encoded, amqp_pool_t *pool,
                      amqp_table_t *output, size_t *offset) {
  uint32_t tablesize;
  int num_entries = 0;
  amqp_table_entry_t *entries;
  int allocated_entries = INITIAL_TABLE_SIZE;
  size_t limit;
  int res;

  if (!amqp_decode_32(encoded, offset, &tablesize)) {
    return AMQP_STATUS_BAD_AMQP_DATA;
  }

  if (tablesize + *offset > encoded.len) {
    return AMQP_STATUS_BAD_AMQP_DATA;
  }

  entries = malloc(allocated_entries * sizeof(amqp_table_entry_t));
  if (entries == NULL) {
    return AMQP_STATUS_NO_MEMORY;
  }

  limit = *offset + tablesize;
  while (*offset < limit) {
    uint8_t keylen;

    res = AMQP_STATUS_BAD_AMQP_DATA;
    if (!amqp_decode_8(encoded, offset, &keylen)) {
      goto out;
    }

    if (num_entries >= allocated_entries) {
      void *newentries;
      allocated_entries = allocated_entries * 2;
      newentries =
          realloc(entries, allocated_entries * sizeof(amqp_table_entry_t));
      res = AMQP_STATUS_NO_MEMORY;
      if (newentries == NULL) {
        goto out;
      }

      entries = newentries;
    }

    res = AMQP_STATUS_BAD_AMQP_DATA;
    if (!amqp_decode_bytes(encoded, offset, &entries[num_entries].key,
                           keylen)) {
      goto out;
    }

    res = amqp_decode_field_value(encoded, pool, &entries[num_entries].value,
                                  offset);
    if (res < 0) {
      goto out;
    }

    num_entries++;
  }

  output->num_entries = num_entries;
  output->entries =
      amqp_pool_alloc(pool, num_entries * sizeof(amqp_table_entry_t));
  /* NULL is legitimate if we requested a zero-length block. */
  if (output->entries == NULL) {
    if (num_entries == 0) {
      res = AMQP_STATUS_OK;
    } else {
      res = AMQP_STATUS_NO_MEMORY;
    }
    goto out;
  }

  memcpy(output->entries, entries, num_entries * sizeof(amqp_table_entry_t));
  res = AMQP_STATUS_OK;

out:
  free(entries);
  return res;
}





Add Discussion as Guest

Log in to DocsForge