summaryrefslogtreecommitdiff
blob: 04f264d52ddd821d45759588e0c1552231a76925 (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
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
From 60e3ac6b39cb25c974785b8ccc49edaae458a891 Mon Sep 17 00:00:00 2001
From: Luca Deri <deri@ntop.org>
Date: Fri, 3 Apr 2020 15:31:23 +0200
Subject: [PATCH] Fix for serialization of strings where the first element is a
 zero len string

---
 src/include/ndpi_typedefs.h | 10 ++++++----
 src/lib/ndpi_serializer.c   | 36 +++++++++++++++++++++++++++++++-----
 2 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h
index 98b8ccf8..2c513e26 100644
--- a/src/include/ndpi_typedefs.h
+++ b/src/include/ndpi_typedefs.h
@@ -1446,10 +1446,12 @@ typedef enum {
 #define NDPI_SERIALIZER_DEFAULT_BUFFER_SIZE 8192
 #define NDPI_SERIALIZER_DEFAULT_BUFFER_INCR 1024
 
-#define NDPI_SERIALIZER_STATUS_COMMA (1 << 0)
-#define NDPI_SERIALIZER_STATUS_ARRAY (1 << 1)
-#define NDPI_SERIALIZER_STATUS_EOR   (1 << 2)
-#define NDPI_SERIALIZER_STATUS_SOB   (1 << 3)
+#define NDPI_SERIALIZER_STATUS_COMMA     (1 << 0)
+#define NDPI_SERIALIZER_STATUS_ARRAY     (1 << 1)
+#define NDPI_SERIALIZER_STATUS_EOR       (1 << 2)
+#define NDPI_SERIALIZER_STATUS_SOB       (1 << 3)
+#define NDPI_SERIALIZER_STATUS_NOT_EMPTY (1 << 4)
+
 
 typedef struct {
   u_int32_t flags;
diff --git a/src/lib/ndpi_serializer.c b/src/lib/ndpi_serializer.c
index 80221005..57c61604 100644
--- a/src/lib/ndpi_serializer.c
+++ b/src/lib/ndpi_serializer.c
@@ -43,6 +43,14 @@
 
 /* ********************************** */
 
+static u_int8_t ndpi_serialize_is_not_empty(ndpi_serializer *_serializer) {
+  ndpi_private_serializer *serializer = (ndpi_private_serializer*)_serializer;
+  
+  return(((serializer->status.flags & NDPI_SERIALIZER_STATUS_NOT_EMPTY) == NDPI_SERIALIZER_STATUS_NOT_EMPTY) ? 1 : 0);
+}
+
+/* ********************************** */
+
 static u_int64_t ndpi_htonll(u_int64_t v) {
   union { u_int32_t lv[2]; u_int64_t llv; } u;
   
@@ -442,14 +450,17 @@ int ndpi_serialize_end_of_record(ndpi_serializer *_serializer) {
     if(!(serializer->status.flags & NDPI_SERIALIZER_STATUS_ARRAY)) {
       serializer->buffer[0] = '[';
       serializer->status.size_used += snprintf((char *) &serializer->buffer[serializer->status.size_used],
-					buff_diff, "]");
+					       buff_diff, "]");
     }
+    
     serializer->status.flags |= NDPI_SERIALIZER_STATUS_ARRAY | NDPI_SERIALIZER_STATUS_EOR;
     serializer->status.flags &= ~NDPI_SERIALIZER_STATUS_COMMA;
   } else {
     serializer->buffer[serializer->status.size_used++] = ndpi_serialization_end_of_record;
   }
 
+  serializer->status.flags &= ~NDPI_SERIALIZER_STATUS_NOT_EMPTY;
+  
   return(0);
 }
 
@@ -556,6 +567,7 @@ int ndpi_serialize_uint32_uint32(ndpi_serializer *_serializer,
     serializer->buffer[type_offset] = type;
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -607,6 +619,7 @@ int ndpi_serialize_uint32_uint64(ndpi_serializer *_serializer,
     }
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -660,6 +673,7 @@ int ndpi_serialize_uint32_int32(ndpi_serializer *_serializer,
     serializer->buffer[type_offset] = type;
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -713,6 +727,7 @@ int ndpi_serialize_uint32_int64(ndpi_serializer *_serializer,
     }
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -761,6 +776,7 @@ int ndpi_serialize_uint32_float(ndpi_serializer *_serializer,
     serializer->buffer[type_offset] = type;
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -811,6 +827,7 @@ static int ndpi_serialize_uint32_binary(ndpi_serializer *_serializer,
     serializer->buffer[type_offset] = type;
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -851,6 +868,7 @@ int ndpi_serialize_uint32_boolean(ndpi_serializer *_serializer,
       value ? "true" : "false");
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -908,6 +926,8 @@ static int ndpi_serialize_binary_int32(ndpi_serializer *_serializer,
     }
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
+  
   return(0);
 }
 
@@ -967,6 +987,7 @@ int ndpi_serialize_binary_int64(ndpi_serializer *_serializer,
     }
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -1030,6 +1051,7 @@ static int ndpi_serialize_binary_uint32(ndpi_serializer *_serializer,
     }
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -1056,7 +1078,7 @@ int ndpi_serialize_string_uint32_format(ndpi_serializer *_serializer,
     return(ndpi_serialize_string_uint32(_serializer, key, value));
   } else {
     char buf[16];
-    
+
     snprintf(buf, sizeof(buf), format, value);
     return(ndpi_serialize_string_string(_serializer, key, buf));
   }
@@ -1111,6 +1133,7 @@ static int ndpi_serialize_binary_uint64(ndpi_serializer *_serializer,
     }
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -1174,6 +1197,7 @@ static int ndpi_serialize_binary_float(ndpi_serializer *_serializer,
     ndpi_serialize_single_float(serializer, value);
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -1232,8 +1256,8 @@ static int ndpi_serialize_binary_raw(ndpi_serializer *_serializer,
     ndpi_serialize_json_post(_serializer);
   } else if(serializer->fmt == ndpi_serialization_format_csv) {
     serializer->status.size_used += snprintf((char *) &serializer->buffer[serializer->status.size_used], buff_diff,
-				      "%s%s", (serializer->status.size_used > 0) ? serializer->csv_separator : "",
-				      value);
+					     "%s%s", ndpi_serialize_is_not_empty(_serializer) ? serializer->csv_separator : "",
+					     value);
   } else {
     serializer->buffer[serializer->status.size_used++] = (ndpi_serialization_string << 4) | ndpi_serialization_string;
 
@@ -1241,6 +1265,7 @@ static int ndpi_serialize_binary_raw(ndpi_serializer *_serializer,
     ndpi_serialize_single_string(serializer, value, vlen);
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }
 
@@ -1320,10 +1345,11 @@ int ndpi_serialize_string_boolean(ndpi_serializer *_serializer,
     ndpi_serialize_json_post(_serializer);
   } else if(serializer->fmt == ndpi_serialization_format_csv) {
     serializer->status.size_used += snprintf((char *) &serializer->buffer[serializer->status.size_used], buff_diff,
-      "%s%s", (serializer->status.size_used > 0) ? serializer->csv_separator : "",
+      "%s%s", ndpi_serialize_is_not_empty(_serializer) ? serializer->csv_separator : "",
       value ? "true" : "false");
   }
 
+  serializer->status.flags |= NDPI_SERIALIZER_STATUS_NOT_EMPTY;
   return(0);
 }