From de32609b9a99cb5e9e92889aa8775cb2f4d58ed7 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 17 Sep 2015 22:35:46 +0200 Subject: [PATCH] Simplify decode_packs() through some pointer arithmetic. --- bmusb.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/bmusb.cpp b/bmusb.cpp index ac73432..926bfa1 100644 --- a/bmusb.cpp +++ b/bmusb.cpp @@ -287,23 +287,18 @@ void decode_packs(const libusb_transfer *xfr, //exit(5); } - const unsigned char *iso_start = xfr->buffer + offset; - for (unsigned iso_offset = 0; iso_offset < pack->actual_length; ) { // Usually runs only one iteration. - const unsigned char* start_next_frame = (const unsigned char *)memmem(iso_start + iso_offset, pack->actual_length - iso_offset, sync_pattern, sync_length); + const uint8_t *start = xfr->buffer + offset; + const uint8_t *limit = start + pack->actual_length; + while (start < limit) { // Usually runs only one iteration. + const unsigned char* start_next_frame = (const unsigned char *)memmem(start, limit - start, sync_pattern, sync_length); if (start_next_frame == nullptr) { // add the rest of the buffer - const uint8_t *start = iso_start + iso_offset; - const uint8_t *end = iso_start + pack->actual_length; - add_to_frame(current_frame, frame_type_name, start, end); + add_to_frame(current_frame, frame_type_name, start, limit); break; } else { - const uint8_t *start = iso_start + iso_offset; - const uint8_t *end = start_next_frame; - add_to_frame(current_frame, frame_type_name, start, end); - start_callback(start_next_frame + sync_length); - - int suboffset = start_next_frame - iso_start; - iso_offset = suboffset + sync_length; // skip sync + add_to_frame(current_frame, frame_type_name, start, start_next_frame); + start = start_next_frame + sync_length; // skip sync + start_callback(start); } } #if 0 -- 2.39.2