#ifndef __UTIL_H__
#define __UTIL_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include "osal/preproc.h"
typedef enum _file_status
{
file_ok,
file_open_error,
file_read_error,
file_write_error
} file_status_t;
file_status_t read_from_file(const char *filename, void *data, size_t size);
file_status_t write_to_file(const char *filename, const void *data, size_t size);
#ifdef _MSC_VER
#include <stdlib.h>
#endif
static osal_inline unsigned short m64p_swap16(unsigned short x)
{
#ifdef _MSC_VER
return _byteswap_ushort(x);
#else
return ((x & 0x00FF) << 8) |
((x & 0xFF00) >> 8);
#endif
}
static osal_inline unsigned int m64p_swap32(unsigned int x)
{
#ifdef _MSC_VER
return _byteswap_ulong(x); #else
return ((x & 0x000000FF) << 24) |
((x & 0x0000FF00) << 8) |
((x & 0x00FF0000) >> 8) |
((x & 0xFF000000) >> 24);
#endif
}
static osal_inline unsigned long long int m64p_swap64(unsigned long long int x)
{
#ifdef _MSC_VER
return _byteswap_uint64(x);
#else
return ((x & 0x00000000000000FFULL) << 56) |
((x & 0x000000000000FF00ULL) << 40) |
((x & 0x0000000000FF0000ULL) << 24) |
((x & 0x00000000FF000000ULL) << 8) |
((x & 0x000000FF00000000ULL) >> 8) |
((x & 0x0000FF0000000000ULL) >> 24) |
((x & 0x00FF000000000000ULL) >> 40) |
((x & 0xFF00000000000000ULL) >> 56);
#endif
}
#ifdef M64P_BIG_ENDIAN
#define big16(x) (x)
#define big32(x) (x)
#define big64(x) (x)
#define little16(x) m64p_swap16(x)
#define little32(x) m64p_swap32(x)
#define little64(x) m64p_swap64(x)
#else
#define big16(x) m64p_swap16(x)
#define big32(x) m64p_swap32(x)
#define big64(x) m64p_swap64(x)
#define little16(x) (x)
#define little32(x) (x)
#define little64(x) (x)
#endif
void swap_buffer(void *buffer, size_t length, size_t count);
void to_little_endian_buffer(void *buffer, size_t length, size_t count);
void to_big_endian_buffer(void *buffer, size_t length, size_t count);
void countrycodestring(unsigned short countrycode, char *string);
void imagestring(unsigned char imagetype, char *string);
const char* namefrompath(const char* path);
char* combinepath(const char* first, const char *second);
char *trim(char *str);
int string_to_int(const char *str, int *result);
int parse_hex(const char *str, unsigned char *output, size_t output_size);
char* formatstr(const char* fmt, ...);
typedef enum _ini_line_type
{
INI_BLANK,
INI_COMMENT,
INI_SECTION,
INI_PROPERTY,
INI_TRASH
} ini_line_type;
typedef struct _ini_line
{
ini_line_type type;
char *name;
char *value;
} ini_line;
ini_line ini_parse_line(char **lineptr);
#ifdef __cplusplus
}
#endif
#endif