Skip to content

Commit

Permalink
Fix blocks with inner-json mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cardigliano committed Oct 31, 2024
1 parent 8ad34b3 commit 9009d20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/include/ndpi_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,7 @@ typedef enum {
#define NDPI_SERIALIZER_STATUS_LIST (1 << 5)
#define NDPI_SERIALIZER_STATUS_SOL (1 << 6)
#define NDPI_SERIALIZER_STATUS_HDR_DONE (1 << 7)
#define NDPI_SERIALIZER_STATUS_CEOB (1 << 8)

typedef struct {
u_int32_t size_used;
Expand Down
18 changes: 12 additions & 6 deletions src/lib/ndpi_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ void ndpi_reset_serializer(ndpi_serializer *_serializer) {
serializer->status.buffer.size_used = 0;
buff_diff = serializer->buffer.size - serializer->status.buffer.size_used;

if (serializer->multiline_json_array) {
serializer->status.buffer.size_used += ndpi_snprintf((char *) &serializer->buffer.data[serializer->status.buffer.size_used], buff_diff, "{}");
} else {
if (!serializer->multiline_json_array) {
/* Note: please keep a space at the beginning as it is used for arrays when an end-of-record is used */
serializer->status.buffer.size_used += ndpi_snprintf((char *) &serializer->buffer.data[serializer->status.buffer.size_used], buff_diff, " {}");
serializer->buffer.data[serializer->status.buffer.size_used++] = ' ';
}
if (!serializer->inner_json) {
serializer->status.buffer.size_used += ndpi_snprintf((char *) &serializer->buffer.data[serializer->status.buffer.size_used], buff_diff, "{}");
}
} else if(serializer->fmt == ndpi_serialization_format_csv) {
serializer->status.header.size_used = 0;
Expand Down Expand Up @@ -765,8 +766,11 @@ static inline int ndpi_serialize_json_post(ndpi_serializer *_serializer) {

if(serializer->status.buffer.size_used >= serializer->buffer.size)
return -1;
if (!serializer->inner_json)

if (!serializer->inner_json || serializer->status.flags & NDPI_SERIALIZER_STATUS_CEOB) {
serializer->buffer.data[serializer->status.buffer.size_used++] = '}';
serializer->status.flags &= ~NDPI_SERIALIZER_STATUS_CEOB;
}

if (!serializer->multiline_json_array) {
if(serializer->status.flags & NDPI_SERIALIZER_STATUS_ARRAY) {
Expand Down Expand Up @@ -2375,6 +2379,7 @@ int ndpi_serialize_end_of_block(ndpi_serializer *_serializer) {
serializer->status.flags &= ~NDPI_SERIALIZER_STATUS_SOB;

// buff_diff = serializer->buffer.size - serializer->status.buffer.size_used;
serializer->status.flags |= NDPI_SERIALIZER_STATUS_CEOB;
if(ndpi_serialize_json_post(_serializer) != 0)
return(-1);
} else {
Expand Down Expand Up @@ -2409,7 +2414,8 @@ void ndpi_serializer_rollback_snapshot(ndpi_serializer *_serializer) {
} else {
if (!serializer->multiline_json_array)
serializer->buffer.data[0] = ' ';
serializer->buffer.data[serializer->status.buffer.size_used-1] = '}';
if (!serializer->inner_json)
serializer->buffer.data[serializer->status.buffer.size_used-1] = '}';
}
}
}
Expand Down

0 comments on commit 9009d20

Please sign in to comment.