#ifndef ENGMODE_UTILS_H
#define ENGMODE_UTILS_H

int em_is_all_zero(unsigned char *buf, unsigned int size);
int em_get_digit_count(int num);
void em_itoa_for_mode(int val, char *buf);
void em_add_string(uint8_t *buf, uint32_t buf_size, uint32_t *offset, uint8_t *str, uint32_t size);
void print_msg_hex(char *TAG, uint8_t *buf, uint32_t size);
void em_hex_to_byte(const char *str, int size, uint8_t *byteArray);
int em_tolower(int chr);
int em_is_string_int(const char *val);
int em_atoi(char const *c);
char *em_strtok_with_null(char *str, char const *delim);
int em_strncasecmp(const char *s1, const char *s2, size_t n);
void em_get_data_from_raw(uint8_t *data, uint32_t data_max, uint8_t *raw, uint32_t raw_size, uint32_t *raw_offset,
			  uint32_t data_size);
void em_set_data_to_raw(uint8_t *data, uint32_t data_max, uint8_t *raw, uint32_t raw_size, uint32_t *raw_offset,
			uint32_t data_size);

void remove_spaces(const char *input, char *result);
#ifdef EMAS
void em_snprintf(char *buf, int size, char *rgx, ...);
char *em_strtok(char *__restrict s1, const char *__restrict s2);
void em_base64_decode(const char *src, char *result, int *length);
void em_base64_encode(const char *src, char *result, int length);

typedef union {
	struct {
		unsigned char c1, c2, c3;
	};
	struct {
		unsigned int e1 : 6, e2 : 6, e3 : 6, e4 : 6;
	};
} BF;

static const char MimeBase64[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
				  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
				  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
				  'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

static int DecodeMimeBase64[256] = {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00-0F */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10-1F */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 20-2F */
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 30-3F */
    -1, 0,  1,	2,  3,	4,  5,	6,  7,	8,  9,	10, 11, 12, 13, 14, /* 40-4F */
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 50-5F */
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 60-6F */
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 70-7F */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80-8F */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90-9F */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A0-AF */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* B0-BF */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* C0-CF */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* D0-DF */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* E0-EF */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1  /* F0-FF */
};

#endif /* EMAS */
#endif
