-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_parser.c
More file actions
201 lines (160 loc) · 6.45 KB
/
data_parser.c
File metadata and controls
201 lines (160 loc) · 6.45 KB
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
#include <string.h>
#include <stdio.h>
#include "data_parser.h"
#include "config.h"
#include "log.h"
int parse_buf_data_raw(RingBuffer *ring_buffer, int *frame, float *fps, int *size, size_t *parsed_bytes) {
// Ensure the ring buffer has sufficient data to parse
if (ring_buffer->data_size < 16) {
return -1; // Not enough data to parse
}
// Copy data from the ring buffer into a temporary buffer for easier parsing
char temp_buffer[ring_buffer->data_size + 1]; // +1 for null terminator
size_t i;
for (i = 0; i < ring_buffer->data_size; i++) {
temp_buffer[i] = ring_buffer->data[(ring_buffer->start + i) % ring_buffer->size];
}
temp_buffer[ring_buffer->data_size] = '\0'; // Null-terminate the string
// Variables to track parsing results
int frame_found = 0;
int fps_found = 0;
int size_found = 0;
// Tokenize the input buffer by newline
char *token = strtok(temp_buffer, "\n");
while (token) {
LOG_DEBUG("token: %s\n", token);
//safety measurement 1
extern int required_data_size;
if (token - temp_buffer > required_data_size*0.8) {
LOG_DEBUG("SM 1: %ld:%s\n", token - temp_buffer, token);
break;
}
//safety measurement 2
if (strlen(token) > 55) {
LOG_DEBUG("SM 2: %s\n", token);
return -5;
}
// Parse the frame number from lines starting with "Viewfinder frame"
if (strncmp(token, "Viewfinder frame", 16) == 0) {
if (sscanf(token, "Viewfinder frame %d", frame) == 1) {
frame_found = 1;
}
}
// Parse FPS from lines starting with "#"
if (strncmp(token, "#", 1) == 0) {
if (sscanf(token, "#%*d (%f fps)", fps) == 1) {
fps_found = 1;
}
}
// Parse size from lines containing "FileOutput: output buffer"
if (strstr(token, "FileOutput: output buffer") != NULL) {
if (sscanf(token, "FileOutput: output buffer %*s size %d", size) == 1) {
size_found = 1;
}
}
// Move to the next line
token = strtok(NULL, "\n");
}
// Check if all necessary components were successfully parsed
if (frame_found && fps_found && size_found) {
*parsed_bytes = ring_buffer->data_size; // Assume all bytes were parsed
return 0; // Success
}
// Return appropriate error code based on what failed
if (!frame_found) return -2; // Failed to parse frame
if (!fps_found) return -3; // Failed to parse FPS
if (!size_found) return -4; // Failed to parse size
return -10; // Unknown error
}
int parse_buf_data_arithmetic(RingBuffer *ring_buffer, int *frame, float *fps, int *size, size_t *parsed_bytes) {
// Ensure token operation will NOT overflow
char temp_buffer[RING_BUFFER_SIZE+1]; // +1 for null terminator
if (ring_buffer->data_size < 16) {
return -1; // Not enough data to parse
}
size_t buffer_limit = sizeof(temp_buffer) - 1;
size_t bytes_to_copy = ring_buffer->data_size > buffer_limit ? buffer_limit : ring_buffer->data_size;
size_t i;
for (i = 0; i < bytes_to_copy; i++) {
temp_buffer[i] = ring_buffer->data[(ring_buffer->start + i) % ring_buffer->size];
}
temp_buffer[ring_buffer->data_size] = '\0'; // Null-terminate the string
int frame_found = 0, fps_found = 0, size_found = 0;
int temp_frame = 0, temp_size = 0, data_count = 0;
float temp_fps = 0, total_fps = 0;
int total_size = 0;
size_t current_offset = 0;
char *token = strtok(temp_buffer, "\n");
while (token) {
size_t token_length = strlen(token);
// Debugging to verify offsets
LOG_DEBUG("token: %s\n", token);
LOG_DEBUG("Current offset: %ld, token: %s\n", current_offset, token);
// Safety measurement 1
extern int required_data_size;
if (current_offset > required_data_size * 0.8) {
LOG_DEBUG("SM 1: %ld:%s\n", current_offset, token);
break;
}
// Safety measurement 2
if (token_length > 55) {
LOG_DEBUG("SM 2: %s\n", token);
return -5;
}
// Parse the frame number from lines starting with "Viewfinder frame"
if (strncmp(token, "Viewfinder frame", 16) == 0) {
if (sscanf(token, "Viewfinder frame %d", &temp_frame) == 1) {
frame_found = 1;
*frame = temp_frame;
}
}
// Parse FPS from lines starting with "#"
if (frame_found && strncmp(token, "#", 1) == 0) {
int frame_number = 0;
if (sscanf(token, "#%d (%f fps)", &frame_number, &temp_fps) == 2) {
if (frame_number <= temp_frame) {
fps_found = 1;
} else {
frame_found = fps_found = size_found = 0;
token = strtok(NULL, "\n");
continue;
}
}
}
// Parse size from lines containing "FileOutput: output buffer"
if (frame_found && strstr(token, "FileOutput: output buffer") != NULL) {
if (sscanf(token, "FileOutput: output buffer %*s size %d", &temp_size) == 1) {
size_found = 1;
} else {
frame_found = fps_found = size_found = 0;
token = strtok(NULL, "\n");
continue;
}
}
current_offset += token_length + 1; // Account for '\n'
if (frame_found && fps_found && size_found) {
total_fps += temp_fps;
total_size += temp_size;
data_count++;
*parsed_bytes = current_offset;
LOG_DEBUG("Count: %d Frame: %d Offset: %ld\n", data_count, temp_frame, *parsed_bytes);
frame_found = fps_found = size_found = 0;
}
token = strtok(NULL, "\n");
}
// Multi-frame detected
if (data_count > 1) {
*frame = -data_count;
}
// Compute the average FPS if at least one valid FPS was found
if (data_count > 0) {
*fps = total_fps / data_count;
*size = total_size / data_count;
return 0; // Success
}
// Return appropriate error code based on what failed
if (!frame_found) return -2; // Failed to parse frame
if (!fps_found) return -3; // Failed to parse FPS
if (!size_found) return -4; // Failed to parse size
return -10; // Unknown error
}