<commit_before>#include <lwmqtt.h>
#include <string.h>

#include "packet.h"

typedef union {
  unsigned char byte;
  struct {
    unsigned int retain : 1;
    unsigned int qos : 2;
    unsigned int dup : 1;
    unsigned int type : 4;
  } bits;
} lwmqtt_header_t;

static int lwmqtt_total_header_length(int rem_len) {
  if (rem_len < 128) {
    return 1 + 1;
  } else if (rem_len < 16384) {
    return 1 + 2;
  } else if (rem_len < 2097151) {
    return 1 + 3;
  } else {
    return 1 + 4;
  }
}

lwmqtt_err_t lwmqtt_detect_packet_type(void *buf, lwmqtt_packet_type_t *packet_type) {
  // prepare pointer
  void *ptr = buf;

  // read header
  lwmqtt_header_t header;
  header.byte = lwmqtt_read_char(&ptr);

  // check if packet type is correct and can be received
  switch ((lwmqtt_packet_type_t)header.bits.type) {
    case LWMQTT_CONNACK_PACKET:
    case LWMQTT_PUBLISH_PACKET:
    case LWMQTT_PUBACK_PACKET:
    case LWMQTT_PUBREC_PACKET:
    case LWMQTT_PUBREL_PACKET:
    case LWMQTT_PUBCOMP_PACKET:
    case LWMQTT_SUBACK_PACKET:
    case LWMQTT_UNSUBACK_PACKET:
    case LWMQTT_PINGRESP_PACKET:
      *packet_type = (lwmqtt_packet_type_t)header.bits.type;
      return LWMQTT_SUCCESS;
    default:
      *packet_type = LWMQTT_NO_PACKET;
      return LWMQTT_DECODE_ERROR;
  }
}

lwmqtt_err_t lwmqtt_detect_remaining_length(void *buf, int buf_len, int *rem_len) {
  // prepare pointer
  void *ptr = buf;

  // attempt to decode remaining length
  *rem_len = lwmqtt_read_varnum(&ptr, buf_len);
  if (*rem_len == -1) {
    return LWMQTT_BUFFER_TOO_SHORT;
  } else if (*rem_len == -2) {
    return LWMQTT_REMAINING_LENGTH_OVERFLOW;
  }

  return LWMQTT_SUCCESS;
}

typedef union {
  unsigned char byte;
  struct {
    unsigned int _ : 1;
    unsigned int clean_session : 1;
    unsigned int will : 1;
    unsigned int will_qos : 2;
    unsigned int will_retain : 1;
    unsigned int password : 1;
    unsigned int username : 1;
  } bits;
} lwmqtt_connect_flags_t;
<commit_msg>set to zero before returning<commit_after>#include <lwmqtt.h>
#include <string.h>

#include "packet.h"

typedef union {
  unsigned char byte;
  struct {
    unsigned int retain : 1;
    unsigned int qos : 2;
    unsigned int dup : 1;
    unsigned int type : 4;
  } bits;
} lwmqtt_header_t;

static int lwmqtt_total_header_length(int rem_len) {
  if (rem_len < 128) {
    return 1 + 1;
  } else if (rem_len < 16384) {
    return 1 + 2;
  } else if (rem_len < 2097151) {
    return 1 + 3;
  } else {
    return 1 + 4;
  }
}

lwmqtt_err_t lwmqtt_detect_packet_type(void *buf, lwmqtt_packet_type_t *packet_type) {
  // prepare pointer
  void *ptr = buf;

  // read header
  lwmqtt_header_t header;
  header.byte = lwmqtt_read_char(&ptr);

  // check if packet type is correct and can be received
  switch ((lwmqtt_packet_type_t)header.bits.type) {
    case LWMQTT_CONNACK_PACKET:
    case LWMQTT_PUBLISH_PACKET:
    case LWMQTT_PUBACK_PACKET:
    case LWMQTT_PUBREC_PACKET:
    case LWMQTT_PUBREL_PACKET:
    case LWMQTT_PUBCOMP_PACKET:
    case LWMQTT_SUBACK_PACKET:
    case LWMQTT_UNSUBACK_PACKET:
    case LWMQTT_PINGRESP_PACKET:
      *packet_type = (lwmqtt_packet_type_t)header.bits.type;
      return LWMQTT_SUCCESS;
    default:
      *packet_type = LWMQTT_NO_PACKET;
      return LWMQTT_DECODE_ERROR;
  }
}

lwmqtt_err_t lwmqtt_detect_remaining_length(void *buf, int buf_len, int *rem_len) {
  // prepare pointer
  void *ptr = buf;

  // attempt to decode remaining length
  *rem_len = lwmqtt_read_varnum(&ptr, buf_len);
  if (*rem_len == -1) {
    *rem_len = 0;
    return LWMQTT_BUFFER_TOO_SHORT;
  } else if (*rem_len == -2) {
    *rem_len = 0;
    return LWMQTT_REMAINING_LENGTH_OVERFLOW;
  }

  return LWMQTT_SUCCESS;
}

typedef union {
  unsigned char byte;
  struct {
    unsigned int _ : 1;
    unsigned int clean_session : 1;
    unsigned int will : 1;
    unsigned int will_qos : 2;
    unsigned int will_retain : 1;
    unsigned int password : 1;
    unsigned int username : 1;
  } bits;
} lwmqtt_connect_flags_t;

{'fraction_non_alphanumeric': 0.08830132202544276, 'fraction_numerical': 0.01845846844599651, 'mean_word_length': 3.297963558413719, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '172339', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1586, 'n_words': 370}