-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathre_fmt.h
218 lines (177 loc) · 6.75 KB
/
re_fmt.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @file re_fmt.h Interface to formatted text functions
*
* Copyright (C) 2010 Creytiv.com
*/
#include <stdarg.h>
#include <stdio.h>
enum {
ITOA_BUFSZ = 34,
};
struct mbuf;
/** Defines a pointer-length string type */
struct pl {
const char *p; /**< Pointer to string */
size_t l; /**< Length of string */
};
/** Initialise a pointer-length object from a constant string */
#define PL(s) {(s), sizeof((s))-1}
/** Pointer-length Initializer */
#define PL_INIT {NULL, 0}
extern const struct pl pl_null;
struct pl *pl_alloc_str(const char *str);
void pl_set_str(struct pl *pl, const char *str);
void pl_set_mbuf(struct pl *pl, const struct mbuf *mb);
int32_t pl_i32(const struct pl *pl);
int64_t pl_i64(const struct pl *pl);
uint32_t pl_u32(const struct pl *pl);
uint32_t pl_x32(const struct pl *pl);
uint64_t pl_u64(const struct pl *pl);
uint64_t pl_x64(const struct pl *pl);
double pl_float(const struct pl *pl);
int pl_bool(bool *val, const struct pl *pl);
int pl_hex(const struct pl *pl, uint8_t *hex, size_t len);
bool pl_isset(const struct pl *pl);
int pl_strcpy(const struct pl *pl, char *str, size_t size);
int pl_strdup(char **dst, const struct pl *src);
int pl_dup(struct pl *dst, const struct pl *src);
int pl_strcmp(const struct pl *pl, const char *str);
int pl_strncmp(const struct pl *pl, const char *str, size_t n);
int pl_strcasecmp(const struct pl *pl, const char *str);
int pl_cmp(const struct pl *pl1, const struct pl *pl2);
int pl_casecmp(const struct pl *pl1, const struct pl *pl2);
const char *pl_strchr(const struct pl *pl, char c);
const char *pl_strrchr(const struct pl *pl, char c);
const char *pl_strstr(const struct pl *pl, const char *str);
int pl_trim(struct pl *pl);
int pl_ltrim(struct pl *pl);
int pl_rtrim(struct pl *pl);
/** Advance pl position/length by +/- N bytes */
static inline void pl_advance(struct pl *pl, ssize_t n)
{
pl->p += n;
pl->l -= n;
}
/* Formatted printing */
/**
* Defines the re_vhprintf() print handler
*
* @param p String to print
* @param size Size of string to print
* @param arg Handler argument
*
* @return 0 for success, otherwise errorcode
*/
typedef int(re_vprintf_h)(const char *p, size_t size, void *arg);
/** Defines a print backend */
struct re_printf {
re_vprintf_h *vph; /**< Print handler */
void *arg; /**< Handler argument */
};
/**
* Defines the %H print handler
*
* @param pf Print backend
* @param arg Handler argument
*
* @return 0 for success, otherwise errorcode
*/
typedef int(re_printf_h)(struct re_printf *pf, void *arg);
int re_vhprintf(const char *fmt, va_list ap, re_vprintf_h *vph, void *arg);
int re_vfprintf(FILE *stream, const char *fmt, va_list ap);
int re_vprintf(const char *fmt, va_list ap);
int re_vsnprintf(char *re_restrict str, size_t size,
const char *re_restrict fmt, va_list ap);
int re_vsdprintf(char **strp, const char *fmt, va_list ap);
/* Secure va_list print */
int re_vhprintf_s(const char *fmt, va_list ap, re_vprintf_h *vph, void *arg);
int re_vfprintf_s(FILE *stream, const char *fmt, va_list ap);
int re_vprintf_s(const char *fmt, va_list ap);
int re_vsnprintf_s(char *re_restrict str, size_t size,
const char *re_restrict fmt, va_list ap);
int re_vsdprintf_s(char **strp, const char *fmt, va_list ap);
#ifdef HAVE_RE_ARG
#define re_printf(fmt, ...) _re_printf_s((fmt), RE_VA_ARGS(__VA_ARGS__))
#define re_hprintf(pf, fmt, ...) \
_re_hprintf_s((pf), (fmt), RE_VA_ARGS(__VA_ARGS__))
#define re_fprintf(stream, fmt, ...) \
_re_fprintf_s((stream), (fmt), RE_VA_ARGS(__VA_ARGS__))
#define re_snprintf(str, size, fmt, ...) \
_re_snprintf_s((str), (size), (fmt), RE_VA_ARGS(__VA_ARGS__))
#define re_sdprintf(strp, fmt, ...) \
_re_sdprintf_s((strp), (fmt), RE_VA_ARGS(__VA_ARGS__))
#else
#define re_printf(...) _re_printf(__VA_ARGS__)
#define re_hprintf _re_hprintf
#define re_fprintf _re_fprintf
#define re_snprintf _re_snprintf
#define re_sdprintf _re_sdprintf
#endif
int _re_printf(const char *fmt, ...);
int _re_hprintf(struct re_printf *pf, const char *fmt, ...);
int _re_fprintf(FILE *stream, const char *fmt, ...);
int _re_snprintf(char *re_restrict str, size_t size,
const char *re_restrict fmt, ...);
int _re_sdprintf(char **strp, const char *fmt, ...);
int _re_printf_s(const char *fmt, ...);
int _re_hprintf_s(struct re_printf *pf, const char *fmt, ...);
int _re_fprintf_s(FILE *stream, const char *fmt, ...);
int _re_snprintf_s(char *re_restrict str, size_t size,
const char *re_restrict fmt, ...);
int _re_sdprintf_s(char **strp, const char *fmt, ...);
/* Regular expressions */
int re_regex(const char *ptr, size_t len, const char *expr, ...);
/* Character functions */
uint8_t ch_hex(char ch);
/* String functions */
int str_bool(bool *val, const char *str);
int str_hex(uint8_t *hex, size_t len, const char *str);
void str_ncpy(char *dst, const char *src, size_t n);
int str_dup(char **dst, const char *src);
int str_x64dup(char **dst, uint64_t val);
int str_cmp(const char *s1, const char *s2);
int str_ncmp(const char *s1, const char *s2, size_t n);
const char *str_str(const char *s1, const char *s2);
int str_casecmp(const char *s1, const char *s2);
size_t str_len(const char *s);
const char *str_error(int errnum, char *buf, size_t sz);
char *str_itoa(uint32_t val, char *buf, int base);
wchar_t *str_wchar(const char *c);
/**
* Check if string is set
*
* @param s Zero-terminated string
*
* @return true if set, false if not set
*/
static inline bool str_isset(const char *s)
{
return s && s[0] != '\0';
}
/* time */
int fmt_gmtime(struct re_printf *pf, void *ts);
int fmt_timestamp(struct re_printf *pf, void *ts);
int fmt_timestamp_us(struct re_printf *pf, void *arg);
int fmt_human_time(struct re_printf *pf, const uint32_t *seconds);
void hexdump(FILE *f, const void *p, size_t len);
/* param */
typedef void (fmt_param_h)(const struct pl *name, const struct pl *val,
void *arg);
bool fmt_param_exists(const struct pl *pl, const char *pname);
bool fmt_param_sep_get(const struct pl *pl, const char *pname, char sep,
struct pl *val);
bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val);
void fmt_param_apply(const struct pl *pl, fmt_param_h *ph, void *arg);
/* unicode */
int utf8_encode(struct re_printf *pf, const char *str);
int utf8_decode(struct re_printf *pf, const struct pl *pl);
size_t utf8_byteseq(char u[4], unsigned cp);
/* text2pcap */
struct re_text2pcap {
bool in;
const struct mbuf *mb;
const char *id;
};
int re_text2pcap(struct re_printf *pf, struct re_text2pcap *pcap);
void re_text2pcap_trace(const char *name, const char *id, bool in,
const struct mbuf *mb);