Skip to content

Commit

Permalink
[transport] detect and warn about unaligned esc sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
r00t- committed Jan 5, 2023
1 parent 559ca1e commit 50834d7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sml/src/sml_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ size_t sml_transport_read(int fd, unsigned char *buffer, size_t max_len) {
return 0;
}

for (int i=1;i<4;i++){
if (len>i && memcmp(&buf[len-i], esc_seq, 4) == 0){
if (buf[len-i+4] == 0x1a) {
fprintf(stderr, "libsml: warning: unaligned end esc sequence found in data, stream desynced?\n");
} else if (buf[len-i+4] == 0x01) {
fprintf(stderr, "libsml: warning: unaligned (likely) start esc sequence found in data, stream desynced?\n");
} else {
fprintf(stderr, "libsml: warning: unaligned esc sequence found in data, stream desynced?\n");
}
}
}
if (memcmp(&buf[len], esc_seq, 4) == 0) {
// found esc sequence
len += 4;
Expand All @@ -105,6 +116,7 @@ size_t sml_transport_read(int fd, unsigned char *buffer, size_t max_len) {
} else {
// don't read other escaped sequences yet
fprintf(stderr, "libsml: error: unrecognized sequence\n");
fprintf(stderr, "libsml: %xi at %i\n",buf[len],len);
return 0;
}
}
Expand Down

0 comments on commit 50834d7

Please sign in to comment.