summaryrefslogtreecommitdiff
path: root/src/inc_internal/maat_utils.h
blob: 55c9435dd674996cf8828b0ba1c06476cfc99610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
**********************************************************************************************
*	File: maat_utils.h
*   Description: maat utils entry
*	Authors: Liu WenTan <[email protected]>
*	Date:    2022-10-31
*   Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
***********************************************************************************************
*/

#ifndef _MAAT_UTILS_H_
#define _MAAT_UTILS_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdlib.h>
#include <stddef.h>
#include <arpa/inet.h>
#include <uuid/uuid.h>

#include "uthash/utarray.h"
#include "cJSON/cJSON.h"

#define	TRUE	1
#define	FALSE	0

#ifndef MAX
#define MAX(a, b)  	(((a) > (b)) ? (a) : (b))
#endif

#ifndef MIN
#define MIN(a, b)  	(((a) < (b)) ? (a) : (b))
#endif

#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))

#define FREE(ptr)  \
    {                   \
        if (ptr)        \
        {               \
            free(ptr);  \
            ptr = NULL; \
        }               \
    }

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef container_of
#define container_of(ptr, type, member) ({			 \
	const typeof( ((type *)0)->member ) *__mptr = (ptr); \
	(type *)( (char *)__mptr - offsetof(type,member) );})
#endif

#define MAX_HIT_PATH_NUM     4096
#define	MAX_HIT_RULE_NUM  4096
#define MAX_HIT_OBJECT_NUM    4096
#define MAX_HIT_ITEM_NUM     4096

#define IPV4 4
#define IPV6 6

enum ip_format {
    IP_FORMAT_SINGLE = 1,
	IP_FORMAT_RANGE,
	IP_FORMAT_CIDR,
    IP_FORMAT_MASK,
	IP_FORMAT_UNKNOWN
};

int ip_format2range(const char *ip_str, int ip_type, uint32_t range_begin[], uint32_t range_end[]);

#define UNUSED	__attribute__((unused))

const char *module_name_str(const char *name);

char *maat_strdup(const char *s);

void ipv6_ntoh(unsigned int *v6_addr);

int get_column_pos(const char *line, int column_seq, size_t *offset, size_t *len);

/* the column value must be integer */
long long get_column_value(const char *line, int column_seq);

int load_file_to_memory(const char *file_name, unsigned char **pp_out, size_t *out_sz);

char *strchr_esc(char *s, const char delim);
char *strtok_r_esc(char *s, const char delim, char **save_ptr);

char *str_escape(char *dst, int size, const char *src);
char *str_unescape(char *s);

char *md5_file(const char *filename, char *md5string);

int decrypt_open(const char* file_name, const char* key, const char* algorithm, 
                 unsigned char**pp_out, size_t *out_sz, char* err_str, size_t err_str_sz);

int crypt_memory(const unsigned char *inbuf, size_t inlen, unsigned char **pp_out, size_t *out_sz, 
                 const char *key, const char *algorithm, int do_encrypt, char *err_str, size_t err_str_sz);

int gzip_uncompress(const unsigned char *in_compressed_data, size_t in_compressed_sz, 
                    unsigned char **out_uncompressed_data, size_t *out_uncompressed_sz);

size_t memcat(void **dest, size_t offset, size_t *n_dest, const void *src, size_t n_src);

/* system cmd wrapper */
int system_cmd_mkdir(const char* path);

int system_cmd_rmdir(const char *dir);

int system_cmd_gzip(const char *src_file, const char *dst_file);

int system_cmd_encrypt(const char *src_file, const char *dst_file, const char *password);

int ids_str2longlong_array(const char *ids_str, UT_array *ids_array);
void print_uuid_str(uuid_t uuid);

#ifdef __cplusplus
}
#endif

#endif