This repository has been archived by the owner. It is now read-only.
forked from Daniel-Abrecht/honeypot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log2json.c
99 lines (84 loc) · 1.93 KB
/
log2json.c
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
#include <stdio.h>
#include <stdbool.h>
enum state {
STATE_NEW,
STATE_NEW_KEY,
STATE_KEY_START,
STATE_KEY_ECHO,
STATE_VALUE_ECHO,
STATE_INIT = STATE_NEW
};
void eputc( char c ){
switch(c){
case '"': putchar('\\'); putchar('"'); break;
case '\\': putchar('\\'); putchar('\\'); break;
case '\f': putchar('\\'); putchar('f'); break;
case '\b': putchar('\\'); putchar('b'); break;
case '\r': putchar('\\'); putchar('r'); break;
case '\n': putchar('\\'); putchar('n'); break;
case '\t': putchar('\\'); putchar('t'); break;
case '\0': putchar('\\'); putchar('0'); break;
default: if( (unsigned char)c < ' ' ){
printf("\\u%.4hhx",c);
}else{
putchar(c);
} break;
}
}
int main(){
int c;
enum state state = STATE_INIT;
while( ( c = getchar() ) != EOF ){
start: switch( state ){
case STATE_NEW: {
putchar('{');
state = STATE_NEW_KEY;
goto start;
} break;
case STATE_NEW_KEY: {
if( c == '\n' ){
putchar('}');
putchar('\n');
state = STATE_NEW;
continue;
}else{
state = STATE_KEY_ECHO;
putchar('"');
goto start;
}
} break;
case STATE_KEY_START: {
if( c == '\n' ){
putchar('}');
putchar('\n');
state = STATE_NEW;
continue;
}else{
state = STATE_KEY_ECHO;
putchar(',');
putchar('"');
goto start;
}
} break;
case STATE_KEY_ECHO: {
if( c == '\0' ){
state = STATE_VALUE_ECHO;
putchar('"');
putchar(':');
putchar('"');
continue;
}
eputc(c);
} break;
case STATE_VALUE_ECHO: {
if( c == '\0' ){
putchar('"');
state = STATE_KEY_START;
continue;
}
eputc(c);
} break;
}
}
return 0;
}