1#include <linux/init.h>
2#include <linux/module.h>
3#include <linux/types.h>
4#include <linux/kernel.h>
5#include <linux/ctype.h>
6#include <linux/string.h>
7#include <linux/version.h>
8#include "af_utils.h"
9u_int32_t af_get_timestamp_sec(void)
10{
11#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)
12	struct timespec64 ts;
13	ktime_get_real_ts64(&ts);
14	return (u_int32_t)ts.tv_sec;
15#else
16	struct timespec ts;
17	ts = current_kernel_time();
18	return ts.tv_sec;
19#endif
20
21}
22char *k_trim(char *s)
23{
24	char *start, *last, *bk;
25	int len;
26
27	start = s;
28	while (isspace(*start))
29		start++;
30
31	bk = last = s + strlen(s) - 1;
32	while (last > start && isspace(*last))
33		last--;
34
35	if ((s != start) || (bk != last))
36	{
37		len = last - start + 1;
38		strncpy(s, start, len);
39		s[len] = '\0';
40	}
41	return s;
42}
43
44int check_local_network_ip(unsigned int ip)
45{
46	if ((ip & 0xffff0000) == 0xc0a80000)
47		return 1;
48	else if ((ip & 0xfff00000) == 0xac100000)
49		return 1;
50	else if ((ip & 0xff000000) == 0x0a000000)
51		return 1;
52	else
53		return 0;
54}
55
56void dump_str(char *name, unsigned char *p, int len)
57{
58	#define MAX_DUMP_STR_LEN 64
59	char buf[MAX_DUMP_STR_LEN] = {0};
60	if (len > MAX_DUMP_STR_LEN) {
61		len = MAX_DUMP_STR_LEN - 1;
62	}
63	printk("%s: ",name);
64	strncpy(buf, p, len);
65	printk("[%s]\n", buf);
66}
67
68void dump_hex(char *name, unsigned char *p, int len)
69{
70	#define MAX_DUMP_STR_LEN 64
71	int i;
72	if (len > MAX_DUMP_STR_LEN) {
73		len = MAX_DUMP_STR_LEN - 1;
74	}
75	printk("%s: ",name);
76	for (i = 0; i < len; i++) {
77		if (i % 16 == 0)
78			printk("\n");
79		printk("%02X ",*(p + i));
80	}
81	printk("\n");
82}
83
84#ifndef va_arg
85typedef signed int acpi_native_int;
86#ifndef _VALIST
87#define _VALIST
88typedef char *va_list;
89#endif
90
91#define  _AUPBND                (sizeof (acpi_native_int) - 1)
92#define  _ADNBND                (sizeof (acpi_native_int) - 1)
93
94
95#define _bnd(X, bnd)            (((sizeof (X)) + (bnd)) & (~(bnd)))
96#define va_arg(ap, T)           (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))
97#define va_end(ap)              (void) 0
98#define va_start(ap, A)         (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))
99
100#endif
101
102#ifndef TOLOWER
103#define TOLOWER(x) ((x) | 0x20)
104#endif
105
106
107static long long k_simple_strtoll(const char *cp, char **endp, unsigned int base)
108{
109	if (*cp == '-')
110		return -simple_strtoull(cp + 1, endp, base);
111
112	return simple_strtoull(cp, endp, base);
113}
114
115static int skip_atoi(const char **s)
116{
117	int i=0;
118	while (isdigit(**s))
119		i = i*10 + *((*s)++) - '0';
120	return i;
121}
122
123char *skip_spaces(const char *str)
124{
125	while (isspace(*str) && ((unsigned char )*str != 0xa0))
126		++str;
127	return (char *)str;
128}
129static int k_vsscanf(const char *buf, const char *fmt, va_list args)
130{
131	const char *str = buf;
132	char *next;
133	char digit;
134	int num = 0;
135	u8 qualifier;
136	u8 base;
137#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22)
138	int field_width;
139#else
140	s16 field_width;
141#endif
142	bool is_sign;
143	while (*fmt && *str) {
144		if (isspace(*fmt)) {
145			fmt = skip_spaces(++fmt);
146			str = skip_spaces(str);
147		}
148
149		if (*fmt != '%' && *fmt) {
150			if (*fmt++ != *str++)
151				break;
152			continue;
153		}
154		if (!*fmt)
155			break;
156		++fmt;
157
158		if (*fmt == '*') {
159			while (!isspace(*fmt) && *fmt != '%' && *fmt)
160				fmt++;
161			while (!isspace(*str) && *str)
162				str++;
163			continue;
164		}
165		field_width = -1;
166		if (isdigit(*fmt))
167			field_width = skip_atoi(&fmt);
168		qualifier = -1;
169		if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
170		    TOLOWER(*fmt) == 'z') {
171			qualifier = *fmt++;
172			if (unlikely(qualifier == *fmt)) {
173				if (qualifier == 'h') {
174					qualifier = 'H';
175					fmt++;
176				} else if (qualifier == 'l') {
177					qualifier = 'L';
178					fmt++;
179				}
180			}
181		}
182		if (!*fmt || !*str)
183			break;
184		base = 10;
185		is_sign = 0;
186		switch (*fmt++) {
187		case 'c':
188		{
189			char *s = (char *)va_arg(args, char*);
190			if (field_width == -1)
191				field_width = 1;
192			do {
193				*s++ = *str++;
194			} while (--field_width > 0 && *str);
195			num++;
196		}
197		continue;
198		case 's':
199		{
200			char *s = (char *)va_arg(args, char *);
201#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22)
202			if(field_width == -1)
203				field_width = INT_MAX;
204#else
205			if (field_width == -1)
206				field_width = SHRT_MAX;
207#endif
208			str = skip_spaces(str);
209
210			while (*str && (!isspace(*str)  || ((unsigned char )*str == 0xA0) )&& field_width--)
211				*s++ = *str++;
212			*s = '\0';
213			num++;
214		}
215		continue;
216		case 'n':
217		{
218			int *i = (int *)va_arg(args, int*);
219			*i = str - buf;
220		}
221		continue;
222		case 'o':
223			base = 8;
224			break;
225		case 'x':
226		case 'X':
227			base = 16;
228			break;
229		case 'i':
230			base = 0;
231		case 'd':
232			is_sign = 1;
233		case 'u':
234			break;
235		case '%':
236			if (*str++ != '%')
237				return num;
238			continue;
239		default:
240			return num;
241		}
242		str = skip_spaces(str);
243		digit = *str;
244		if (is_sign && digit == '-')
245			digit = *(str + 1);
246		if (!digit
247		    || (base == 16 && !isxdigit(digit))
248		    || (base == 10 && !isdigit(digit))
249		    || (base == 8 && (!isdigit(digit) || digit > '7'))
250		    || (base == 0 && !isdigit(digit)))
251			break;
252
253		switch (qualifier) {
254		case 'H':
255			if (is_sign) {
256				signed char *s = (signed char *)va_arg(args, signed char *);
257				*s = (signed char)simple_strtol(str, &next, base);
258			} else {
259				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
260				*s = (unsigned char)simple_strtoul(str, &next, base);
261			}
262			break;
263		case 'h':
264			if (is_sign) {
265				short *s = (short *)va_arg(args, short *);
266				*s = (short)simple_strtol(str, &next, base);
267			} else {
268				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
269				*s = (unsigned short)simple_strtoul(str, &next, base);
270			}
271			break;
272		case 'l':
273			if (is_sign) {
274				long *l = (long *)va_arg(args, long *);
275				*l = simple_strtol(str, &next, base);
276			} else {
277				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
278				*l = simple_strtoul(str, &next, base);
279			}
280			break;
281		case 'L':
282			if (is_sign) {
283				long long *l = (long long *)va_arg(args, long long *);
284				*l = k_simple_strtoll(str, &next, base);
285			} else {
286				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
287				*l = simple_strtoull(str, &next, base);
288			}
289			break;
290		case 'Z':
291		case 'z':
292		{
293			size_t *s = (size_t *)va_arg(args, size_t *);
294			*s = (size_t)simple_strtoul(str, &next, base);
295		}
296		break;
297		default:
298			if (is_sign) {
299				int *i = (int *)va_arg(args, int *);
300				*i = (int)simple_strtol(str, &next, base);
301			} else {
302				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
303				*i = (unsigned int)simple_strtoul(str, &next, base);
304			}
305			break;
306		}
307		num++;
308		if (!next)
309			break;
310		str = next;
311	}
312	if (*fmt == '%' && *(fmt + 1) == 'n') {
313		int *p = (int *)va_arg(args, int *);
314		*p = str - buf;
315	}
316	return num;
317}
318
319
320int k_sscanf(const char *buf, const char *fmt, ...)
321{
322	va_list args;
323	int i;
324	va_start(args, fmt);
325	i = k_vsscanf(buf, fmt, args);
326	va_end(args);
327	return i;
328}
329
330
331