示例#1
0
NBBOOL command_packet_stream_ready(command_packet_stream_t *stream)
{
  uint32_t length;

  if(buffer_get_remaining_bytes(stream->buffer) < 4)
  {
    LOG_INFO("Stream only contains 0x%x bytes", buffer_get_remaining_bytes(stream->buffer));
    return FALSE;
  }

  length = buffer_peek_next_int32(stream->buffer);

  if(length > MAX_COMMAND_PACKET_SIZE)
  {
    LOG_FATAL("Command size is too long! (length = %d, max = %d)", length, MAX_COMMAND_PACKET_SIZE);
    exit(1);
  }

  /* I realize some people hate the "if(x) return TRUE else return FALSE"
   * paradigm, but I like it. */
  /* TODO: Is there a sign issue here? */
  if(buffer_get_remaining_bytes(stream->buffer) >= (size_t)(length + 4))
    return TRUE;

  LOG_INFO("Stream only contains 0x%x bytes, we need 0x%x to continue", buffer_get_remaining_bytes(stream->buffer), length+4);
  return FALSE;
}
示例#2
0
NBBOOL command_packet_stream_ready(command_packet_stream_t *stream)
{
  uint32_t length;

  if(buffer_get_remaining_bytes(stream->buffer) < 4)
  {
    LOG_INFO("Stream only contains 0x%x bytes", buffer_get_remaining_bytes(stream->buffer));
    return FALSE;
  }

  length = buffer_peek_next_int32(stream->buffer);

  if(length > MAX_COMMAND_PACKET_SIZE)
  {
    LOG_FATAL("Command size is too long! (length = %d, max = %d)", length, MAX_COMMAND_PACKET_SIZE);
    exit(1);
  }

  /* Check for overflow. */
  if(length + 4 < length)
    return FALSE;

  if(buffer_get_remaining_bytes(stream->buffer) >= (size_t)(length + 4))
    return TRUE;

  LOG_INFO("Stream only contains 0x%x bytes, we need 0x%x to continue", buffer_get_remaining_bytes(stream->buffer), length+4);
  return FALSE;
}
示例#3
0
command_packet_t *command_packet_read(buffer_t *stream)
{
  size_t            remaining_bytes = buffer_get_remaining_bytes(stream);
  uint32_t          needed_bytes    = -1;
  uint8_t          *data;
  command_packet_t *out = NULL;
  size_t            length;

  /* If we don't have a length, we're done. */
  if(remaining_bytes < 4)
    return NULL;

  /* Check for an overflow. */
  needed_bytes = buffer_peek_next_int32(stream);
  if(needed_bytes + 4 < needed_bytes)
  {
    LOG_FATAL("Overflow in command_packet!");
    exit(1);
  }

  /* Make sure there are enough bytes present for the length + data. */
  if(remaining_bytes < needed_bytes + 4)
    return NULL;

  /* Consume the length. */
  buffer_read_next_int32(stream);

  /* Read the data. */
  data = buffer_read_remaining_bytes(stream, &length, needed_bytes, TRUE);

  /* Sanity check. */
  if(length != needed_bytes)
  {
    LOG_FATAL("Something went very wrong with the buffer class; the wrong number of bytes were read!");
    exit(1);
  }

  /* Parse the data and free the buffer. */
  out = command_packet_parse(data, length);
  safe_free(data);

  return out;
}