From 043be07af480843b74672b1ba75ede382073bd81 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 19 Jun 2009 08:43:42 +0000 Subject: [PATCH] Improve the detection of an empty logbook ringbuffer. Some devices do not appear to set the ringbuffer pointers to their normal empty values (e.g. pointing outside the ringbuffer memory). In that case, there appears to be a single entry. But since that entry contains uninitialized memory (e.g. all 0xFF bytes), we are able to detect this special situation. --- src/array.c | 12 ++++++++++++ src/array.h | 3 +++ src/oceanic_common.c | 11 +++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/array.c b/src/array.c index 8484ecd..5c1763e 100644 --- a/src/array.c +++ b/src/array.c @@ -50,6 +50,18 @@ array_reverse_bits (unsigned char data[], unsigned int size) } +int +array_isequal (const unsigned char data[], unsigned int size, unsigned char value) +{ + for (unsigned int i = 0; i < size; ++i) { + if (data[i] != value) + return 0; + } + + return 1; +} + + unsigned int array_uint32_be (const unsigned char data[]) { diff --git a/src/array.h b/src/array.h index cc53dc2..2739e70 100644 --- a/src/array.h +++ b/src/array.h @@ -32,6 +32,9 @@ array_reverse_bytes (unsigned char data[], unsigned int size); void array_reverse_bits (unsigned char data[], unsigned int size); +int +array_isequal (const unsigned char data[], unsigned int size, unsigned char value); + unsigned int array_uint32_be (const unsigned char data[]); diff --git a/src/oceanic_common.c b/src/oceanic_common.c index 0d17f64..2307bee 100644 --- a/src/oceanic_common.c +++ b/src/oceanic_common.c @@ -306,6 +306,17 @@ oceanic_common_device_foreach (oceanic_common_device_t *device, const oceanic_co // Move to the start of the current entry. current -= PAGESIZE / 2; + // Check for uninitialized entries. Normally, such entries are + // never present, except when the ringbuffer is actually empty, + // but the ringbuffer pointers are not set to their empty values. + // This appears to happen on some devices, and we attempt to + // fix this here. + if (array_isequal (logbooks + current, PAGESIZE / 2, 0xFF)) { + begin = current + PAGESIZE / 2; + abort = 1; + break; + } + // Compare the fingerprint to identify previously downloaded entries. if (memcmp (logbooks + current, device->fingerprint, PAGESIZE / 2) == 0) { begin = current + PAGESIZE / 2;