diff --git a/src/irda.c b/src/irda.c index b9ee35e..c2d792c 100644 --- a/src/irda.c +++ b/src/irda.c @@ -39,6 +39,7 @@ #include "irda.h" #include "utils.h" +#include "array.h" #ifdef _WIN32 #define TRACE(expr) \ @@ -277,10 +278,7 @@ irda_socket_discover (irda *device, irda_callback_t callback, void *userdata) if (callback) { #ifdef _WIN32 for (unsigned int i = 0; i < list->numDevice; ++i) { - unsigned int address = (list->Device[i].irdaDeviceID[0] << 24) + - (list->Device[i].irdaDeviceID[1] << 16) + - (list->Device[i].irdaDeviceID[2] << 8) + - list->Device[i].irdaDeviceID[3]; + unsigned int address = array_uint32_be (list->Device[i].irdaDeviceID); unsigned int hints = (list->Device[i].irdaDeviceHints1 << 8) + list->Device[i].irdaDeviceHints2; callback (address, @@ -291,8 +289,7 @@ irda_socket_discover (irda *device, irda_callback_t callback, void *userdata) } #else for (unsigned int i = 0; i < list->len; ++i) { - unsigned int hints = (list->dev[i].hints[0] << 8) + - list->dev[i].hints[1]; + unsigned int hints = array_uint16_be (list->dev[i].hints); callback (list->dev[i].daddr, list->dev[i].info, list->dev[i].charset, diff --git a/src/mares_nemo.c b/src/mares_nemo.c index 2f164fc..8cc942f 100644 --- a/src/mares_nemo.c +++ b/src/mares_nemo.c @@ -28,6 +28,7 @@ #include "serial.h" #include "utils.h" #include "checksum.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -296,7 +297,7 @@ mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsign assert (size >= MARES_NEMO_MEMORY_SIZE); // Get the end of the profile ring buffer. - unsigned int eop = data[0x6B] + (data[0x6C] << 8); + unsigned int eop = array_uint16_le (data + 0x6B); // Make the ringbuffer linear, to avoid having to deal // with the wrap point. The buffer has extra space to @@ -335,7 +336,7 @@ mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsign } // Get the number of samples in the profile data. - unsigned int nsamples = buffer[offset - 3] + (buffer[offset - 2] << 8); + unsigned int nsamples = array_uint16_le (buffer + offset - 3); // Calculate the total number of bytes for this dive. // If the buffer does not contain that much bytes, we reached the @@ -351,7 +352,7 @@ mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsign // Verify that the length that is stored in the profile data // equals the calculated length. If both values are different, // something is wrong and an error is returned. - unsigned int length = buffer[offset] + (buffer[offset + 1] << 8); + unsigned int length = array_uint16_le (buffer + offset); if (length != nbytes) { WARNING ("Calculated and stored size are not equal."); return DEVICE_STATUS_ERROR; @@ -368,7 +369,7 @@ mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsign count != nsamples) { // Each freedive in the session ends with a zero sample. - unsigned int sample = data[idx] + (data[idx + 1] << 8); + unsigned int sample = array_uint16_le (data + idx); if (sample == 0) count++; diff --git a/src/mares_nemo_parser.c b/src/mares_nemo_parser.c index 93e7846..97a28a5 100644 --- a/src/mares_nemo_parser.c +++ b/src/mares_nemo_parser.c @@ -26,6 +26,7 @@ #include "parser-private.h" #include "units.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -117,12 +118,12 @@ mares_nemo_parser_samples_foreach (parser_t *abstract, sample_callback_t callbac if (size < 5) return PARSER_STATUS_ERROR; - unsigned int length = data[0] + (data[1] << 8); + unsigned int length = array_uint16_le (data); assert (length <= size); unsigned int mode = data[length - 1]; - unsigned int nsamples = data[length - 3] + (data[length - 2] << 8); + unsigned int nsamples = array_uint16_le (data + length - 3); if (mode != 2) { unsigned int time = 0; @@ -130,7 +131,7 @@ mares_nemo_parser_samples_foreach (parser_t *abstract, sample_callback_t callbac parser_sample_value_t sample = {0}; unsigned int idx = 2 + 2 * i; - unsigned int value = data[idx] + (data[idx + 1] << 8); + unsigned int value = array_uint16_le (data + idx); unsigned int depth = value & 0x0FFF; unsigned int ascent = (value & 0xC000) >> 14; unsigned int violation = (value & 0x2000) >> 13; @@ -186,7 +187,7 @@ mares_nemo_parser_samples_foreach (parser_t *abstract, sample_callback_t callbac parser_sample_value_t sample = {0}; unsigned int idx = 2 + 6 * i; - unsigned int maxdepth = data[idx + 0] + (data[idx + 1] << 8); + unsigned int maxdepth = array_uint16_le (data + idx); unsigned int divetime = data[idx + 2] + data[idx + 3] * 60; unsigned int surftime = data[idx + 4] + data[idx + 5] * 60; @@ -213,7 +214,7 @@ mares_nemo_parser_samples_foreach (parser_t *abstract, sample_callback_t callbac // reached, the current freedive profile is complete. unsigned int count = 0; while (offset + 2 <= size) { - unsigned int depth = data[offset] + (data[offset + 1] << 8); + unsigned int depth = array_uint16_le (data + offset); offset += 2; if (depth == 0) diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index 2013b00..b9e5108 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -29,6 +29,7 @@ #include "utils.h" #include "ringbuffer.h" #include "checksum.h" +#include "array.h" #define MAXRETRIES 2 @@ -57,9 +58,6 @@ #define RB_PROFILE_END 0xFFF0 #define RB_PROFILE_DISTANCE(a,b) ringbuffer_distance (a, b, RB_PROFILE_BEGIN, RB_PROFILE_END) -#define PT_LOGBOOK_FIRST(x) ( (x)[4] + ((x)[5] << 8) ) -#define PT_LOGBOOK_LAST(x) ( (x)[6] + ((x)[7] << 8) ) - #define PT_PROFILE_FIRST(x) ( (x)[5] + (((x)[6] & 0x0F) << 8) ) #define PT_PROFILE_LAST(x) ( ((x)[6] >> 4) + ((x)[7] << 4) ) @@ -526,8 +524,8 @@ oceanic_atom2_device_foreach (device_t *abstract, dive_callback_t callback, void } // Get the logbook pointers. - unsigned int logbook_first = PT_LOGBOOK_FIRST (pointers); - unsigned int logbook_last = PT_LOGBOOK_LAST (pointers); + unsigned int logbook_first = array_uint16_le (pointers + 4); + unsigned int logbook_last = array_uint16_le (pointers + 6); message ("logbook: first=%04x, last=%04x\n", logbook_first, logbook_last); // Calculate the total number of logbook entries. diff --git a/src/oceanic_veo250.c b/src/oceanic_veo250.c index 37f3e4b..bcb8eec 100644 --- a/src/oceanic_veo250.c +++ b/src/oceanic_veo250.c @@ -29,6 +29,7 @@ #include "utils.h" #include "ringbuffer.h" #include "checksum.h" +#include "array.h" #define MAXRETRIES 2 @@ -57,9 +58,6 @@ #define RB_PROFILE_END 0x8000 #define RB_PROFILE_DISTANCE(a,b) ringbuffer_distance (a, b, RB_PROFILE_BEGIN, RB_PROFILE_END) -#define PT_LOGBOOK_FIRST(x) ( (x)[4] + ((x)[5] << 8) ) -#define PT_LOGBOOK_LAST(x) ( (x)[6] + ((x)[7] << 8) ) - #define PT_PROFILE_FIRST(x) ( (x)[4] + (((x)[5] & 0x0F) << 8) ) #define PT_PROFILE_LAST(x) ( (x)[6] + (((x)[7] & 0x0F) << 8) ) @@ -503,8 +501,8 @@ oceanic_veo250_device_foreach (device_t *abstract, dive_callback_t callback, voi } // Get the logbook pointers. - unsigned int logbook_first = PT_LOGBOOK_FIRST (pointers); - unsigned int logbook_last = PT_LOGBOOK_LAST (pointers); + unsigned int logbook_first = array_uint16_le (pointers + 4); + unsigned int logbook_last = array_uint16_le (pointers + 6); message ("logbook: first=%04x, last=%04x\n", logbook_first, logbook_last); // Calculate the total number of logbook entries. diff --git a/src/reefnet_sensus.c b/src/reefnet_sensus.c index e47a463..3df33f8 100644 --- a/src/reefnet_sensus.c +++ b/src/reefnet_sensus.c @@ -28,6 +28,7 @@ #include "serial.h" #include "checksum.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -202,7 +203,7 @@ reefnet_sensus_device_set_fingerprint (device_t *abstract, const unsigned char d return DEVICE_STATUS_ERROR; if (size) - device->timestamp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); + device->timestamp = array_uint32_le (data); else device->timestamp = 0; @@ -260,8 +261,8 @@ reefnet_sensus_device_handshake (device_t *abstract, unsigned char *data, unsign handshake[0], handshake[1], handshake[2], handshake[3], handshake[4], handshake[5], - handshake[6] + (handshake[7] << 8), - handshake[8] + (handshake[9] << 8) + (handshake[10] << 16) + (handshake[11] << 24)); + array_uint16_le (handshake + 6), + array_uint16_le (handshake + 8)); #endif memcpy (data, handshake + 2, REEFNET_SENSUS_HANDSHAKE_SIZE); @@ -270,7 +271,7 @@ reefnet_sensus_device_handshake (device_t *abstract, unsigned char *data, unsign device_devinfo_t devinfo; devinfo.model = handshake[2] - '0'; devinfo.firmware = handshake[3] - '0'; - devinfo.serial = handshake[6] + (handshake[7] << 8); + devinfo.serial = array_uint16_le (handshake + 6); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); // Wait at least 10 ms to ensures the data line is @@ -340,9 +341,7 @@ reefnet_sensus_device_dump (device_t *abstract, unsigned char *data, unsigned in } // Verify the checksum of the package. - unsigned short crc = - answer[4 + REEFNET_SENSUS_MEMORY_SIZE + 0] + - (answer[4 + REEFNET_SENSUS_MEMORY_SIZE + 1] << 8); + unsigned short crc = array_uint16_le (answer + 4 + REEFNET_SENSUS_MEMORY_SIZE); unsigned short ccrc = checksum_add_uint16 (answer + 4, REEFNET_SENSUS_MEMORY_SIZE, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); @@ -429,8 +428,7 @@ reefnet_sensus_extract_dives (device_t *abstract, const unsigned char data[], un } // Automatically abort when a dive is older than the provided timestamp. - unsigned int timestamp = data[current + 2] + (data[current + 3] << 8) + - (data[current + 4] << 16) + (data[current + 5] << 24); + unsigned int timestamp = array_uint32_le (data + current + 2); if (device && timestamp <= device->timestamp) return DEVICE_STATUS_SUCCESS; diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index b08d8c4..5756320 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -27,6 +27,7 @@ #include "serial.h" #include "checksum.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -176,7 +177,7 @@ reefnet_sensuspro_device_set_fingerprint (device_t *abstract, const unsigned cha return DEVICE_STATUS_ERROR; if (size) - device->timestamp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); + device->timestamp = array_uint32_le (data); else device->timestamp = 0; @@ -212,9 +213,7 @@ reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, uns serial_set_break (device->port, 0); // Verify the checksum of the handshake packet. - unsigned short crc = - handshake[REEFNET_SENSUSPRO_HANDSHAKE_SIZE + 0] + - (handshake[REEFNET_SENSUSPRO_HANDSHAKE_SIZE + 1] << 8); + unsigned short crc = array_uint16_le (handshake + REEFNET_SENSUSPRO_HANDSHAKE_SIZE); unsigned short ccrc = checksum_crc_ccitt_uint16 (handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); @@ -231,8 +230,8 @@ reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, uns "Current Time: %u\n", handshake[0], handshake[1], handshake[2], handshake[3], - handshake[4] + (handshake[5] << 8), - handshake[6] + (handshake[7] << 8) + (handshake[8] << 16) + (handshake[9] << 24)); + array_uint16_le (handshake + 4), + array_uint32_le (handshake + 6)); #endif memcpy (data, handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); @@ -241,7 +240,7 @@ reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, uns device_devinfo_t devinfo; devinfo.model = handshake[0]; devinfo.firmware = handshake[1]; - devinfo.serial = handshake[4] + (handshake[5] << 8); + devinfo.serial = array_uint16_le (handshake + 4); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); serial_sleep (10); @@ -295,9 +294,7 @@ reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned nbytes += len; } - unsigned short crc = - answer[REEFNET_SENSUSPRO_MEMORY_SIZE + 0] + - (answer[REEFNET_SENSUSPRO_MEMORY_SIZE + 1] << 8); + unsigned short crc = array_uint16_le (answer + REEFNET_SENSUSPRO_MEMORY_SIZE); unsigned short ccrc = checksum_crc_ccitt_uint16 (answer, REEFNET_SENSUSPRO_MEMORY_SIZE); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); @@ -397,8 +394,7 @@ reefnet_sensuspro_extract_dives (device_t *abstract, const unsigned char data[], return DEVICE_STATUS_ERROR; // Automatically abort when a dive is older than the provided timestamp. - unsigned int timestamp = data[current + 6] + (data[current + 7] << 8) + - (data[current + 8] << 16) + (data[current + 9] << 24); + unsigned int timestamp = array_uint32_le (data + current + 6); if (device && timestamp <= device->timestamp) return DEVICE_STATUS_SUCCESS; diff --git a/src/reefnet_sensuspro_parser.c b/src/reefnet_sensuspro_parser.c index ac8469d..979bdbb 100644 --- a/src/reefnet_sensuspro_parser.c +++ b/src/reefnet_sensuspro_parser.c @@ -27,6 +27,7 @@ #include "parser-private.h" #include "units.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -148,13 +149,13 @@ reefnet_sensuspro_parser_samples_foreach (parser_t *abstract, sample_callback_t assert (offset + 10 <= size); unsigned int time = 0; - unsigned int interval = data[offset + 4] + (data[offset + 5] << 8); + unsigned int interval = array_uint16_le (data + offset + 4); offset += 10; while (offset + sizeof (footer) <= size && memcmp (data + offset, footer, sizeof (footer)) != 0) { - unsigned int value = data[offset + 0] + (data[offset + 1] << 8); + unsigned int value = array_uint16_le (data + offset); unsigned int depth = (value & 0x01FF); unsigned int temperature = (value & 0xFE00) >> 9; diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index 2da7540..dc0c418 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -28,6 +28,7 @@ #include "serial.h" #include "checksum.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -196,7 +197,7 @@ reefnet_sensusultra_device_set_fingerprint (device_t *abstract, const unsigned c return DEVICE_STATUS_ERROR; if (size) - device->timestamp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); + device->timestamp = array_uint32_le (data); else device->timestamp = 0; @@ -276,7 +277,7 @@ reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char } // Verify the checksum of the packet. - unsigned short crc = data[size - 2] + (data[size - 1] << 8); + unsigned short crc = array_uint16_le (data + size - 2); unsigned short ccrc = checksum_crc_ccitt_uint16 (data + header, size - header - 2); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); @@ -337,16 +338,16 @@ reefnet_sensusultra_device_handshake (device_t *abstract, unsigned char *data, u "Threshold: %u\n" "End Count: %u\n" "Averaging: %u\n", - handshake[0] + (handshake[1] << 8), - handshake[2] + (handshake[3] << 8), - handshake[4] + (handshake[5] << 8) + (handshake[6] << 16) + (handshake[7] << 24), - handshake[8] + (handshake[9] << 8), - handshake[10] + (handshake[11] << 8) + (handshake[12] << 16) + (handshake[13] << 24), - handshake[14] + (handshake[15] << 8), - handshake[16] + (handshake[17] << 8), - handshake[18] + (handshake[19] << 8), - handshake[20] + (handshake[21] << 8), - handshake[22] + (handshake[23] << 8)); + array_uint16_le (handshake + 0), + array_uint16_le (handshake + 2), + array_uint32_le (handshake + 4), + array_uint16_le (handshake + 8), + array_uint32_le (handshake + 10), + array_uint16_le (handshake + 14), + array_uint16_le (handshake + 16), + array_uint16_le (handshake + 18), + array_uint16_le (handshake + 20), + array_uint16_le (handshake + 22)); #endif memcpy (data, handshake, REEFNET_SENSUSULTRA_HANDSHAKE_SIZE); @@ -355,7 +356,7 @@ reefnet_sensusultra_device_handshake (device_t *abstract, unsigned char *data, u device_devinfo_t devinfo; devinfo.model = handshake[1]; devinfo.firmware = handshake[0]; - devinfo.serial = handshake[2] + (handshake[3] << 8); + devinfo.serial = array_uint16_le (handshake + 2); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); return DEVICE_STATUS_SUCCESS; @@ -387,7 +388,7 @@ reefnet_sensusultra_page (reefnet_sensusultra_device_t *device, unsigned char *d } // Verify the page number. - unsigned int page = package[0] + (package[1] << 8); + unsigned int page = array_uint16_le (package); if (page != pagenum) { WARNING ("Unexpected page number."); return DEVICE_STATUS_PROTOCOL; @@ -661,8 +662,7 @@ reefnet_sensusultra_parse (device_t *abstract, const unsigned char data[], unsig } // Automatically abort when a dive is older than the provided timestamp. - unsigned int timestamp = data[current + 4] + (data[current + 5] << 8) + - (data[current + 6] << 16) + (data[current + 7] << 24); + unsigned int timestamp = array_uint32_le (data + current + 4); if (device && timestamp <= device->timestamp) { if (aborted) *aborted = 1; diff --git a/src/reefnet_sensusultra_parser.c b/src/reefnet_sensusultra_parser.c index 5c0342d..82605c8 100644 --- a/src/reefnet_sensusultra_parser.c +++ b/src/reefnet_sensusultra_parser.c @@ -27,6 +27,7 @@ #include "parser-private.h" #include "units.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -148,7 +149,7 @@ reefnet_sensusultra_parser_samples_foreach (parser_t *abstract, sample_callback_ assert (offset + 16 <= size); unsigned int time = 0; - unsigned int interval = data[offset + 8] + (data[offset + 9] << 8); + unsigned int interval = array_uint16_le (data + offset + 8); offset += 16; while (offset + sizeof (footer) <= size && @@ -161,12 +162,12 @@ reefnet_sensusultra_parser_samples_foreach (parser_t *abstract, sample_callback_ if (callback) callback (SAMPLE_TYPE_TIME, sample, userdata); // Temperature (0.01 °K) - unsigned int temperature = data[offset + 0] + (data[offset + 1] << 8); + unsigned int temperature = array_uint16_le (data + offset); sample.temperature = temperature / 100.0 - 273.15; if (callback) callback (SAMPLE_TYPE_TEMPERATURE, sample, userdata); // Depth (absolute pressure in millibar) - unsigned int depth = data[offset + 2] + (data[offset + 3] << 8); + unsigned int depth = array_uint16_le (data + offset + 2); sample.depth = (depth * BAR / 1000.0 - parser->atmospheric) / parser->hydrostatic; if (callback) callback (SAMPLE_TYPE_DEPTH, sample, userdata); diff --git a/src/suunto_d9.c b/src/suunto_d9.c index ad65076..b83fefa 100644 --- a/src/suunto_d9.c +++ b/src/suunto_d9.c @@ -29,6 +29,7 @@ #include "utils.h" #include "ringbuffer.h" #include "checksum.h" +#include "array.h" #define MAXRETRIES 2 @@ -503,8 +504,8 @@ suunto_d9_device_foreach (device_t *abstract, dive_callback_t callback, void *us // Emit a device info event. device_devinfo_t devinfo; devinfo.model = version[0]; - devinfo.firmware = (version[1] << 16) + (version[2] << 8) + version[3]; - devinfo.serial = (serial[0] << 24) + (serial[1] << 16) + (serial[2] << 8) + serial[3]; + devinfo.firmware = array_uint24_be (version + 1); + devinfo.serial = array_uint32_be (serial); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); // Read the header bytes. @@ -516,10 +517,10 @@ suunto_d9_device_foreach (device_t *abstract, dive_callback_t callback, void *us } // Obtain the pointers from the header. - unsigned int last = header[0] + (header[1] << 8); - unsigned int count = header[2] + (header[3] << 8); - unsigned int end = header[4] + (header[5] << 8); - unsigned int begin = header[6] + (header[7] << 8); + unsigned int last = array_uint16_le (header + 0); + unsigned int count = array_uint16_le (header + 2); + unsigned int end = array_uint16_le (header + 4); + unsigned int begin = array_uint16_le (header + 6); message ("Pointers: begin=%04x, last=%04x, end=%04x, count=%i\n", begin, last, end, count); // Memory buffer to store all the dives. @@ -616,8 +617,8 @@ suunto_d9_device_foreach (device_t *abstract, dive_callback_t callback, void *us remaining -= size; available = nbytes - size; - unsigned int oprevious = data[MINIMUM + remaining + 0] + (data[MINIMUM + remaining + 1] << 8); - unsigned int onext = data[MINIMUM + remaining + 2] + (data[MINIMUM + remaining + 3] << 8); + unsigned int oprevious = array_uint16_le (data + MINIMUM + remaining + 0); + unsigned int onext = array_uint16_le (data + MINIMUM + remaining + 2); message ("Pointers: previous=%04x, next=%04x\n", oprevious, onext); assert (current == onext); diff --git a/src/suunto_d9_parser.c b/src/suunto_d9_parser.c index cca8f4a..d28a122 100644 --- a/src/suunto_d9_parser.c +++ b/src/suunto_d9_parser.c @@ -26,6 +26,7 @@ #include "suunto_d9.h" #include "parser-private.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -158,8 +159,7 @@ suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback unsigned int interval_temperature = data[0x47 - SKIP - pressure_offset]; // Offset to the first marker position. - unsigned int marker = data[0x4C - SKIP - pressure_offset] + - (data[0x4D - SKIP - pressure_offset] << 8); + unsigned int marker = array_uint16_le (data + 0x4C - SKIP - pressure_offset); unsigned int time = 0; unsigned int nsamples = 0; @@ -172,7 +172,7 @@ suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback if (callback) callback (SAMPLE_TYPE_TIME, sample, userdata); // Depth (cm). - unsigned int depth = data[offset + 0] + (data[offset + 1] << 8); + unsigned int depth = array_uint16_le (data + offset); sample.depth = depth / 100.0; if (callback) callback (SAMPLE_TYPE_DEPTH, sample, userdata); offset += 2; @@ -180,7 +180,7 @@ suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback // Tank pressure (1/100 bar). if (pressure_samples) { assert (offset + 2 <= size); - unsigned int pressure = data[offset + 0] + (data[offset + 1] << 8); + unsigned int pressure = array_uint16_le (data + offset); sample.pressure.tank = 0; sample.pressure.value = pressure / 100.0; if (callback) callback (SAMPLE_TYPE_PRESSURE, sample, userdata); @@ -209,8 +209,8 @@ suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback switch (event) { case 0x01: // Next Event Marker assert (offset + 4 <= size); - current = data[offset + 0] + (data[offset + 1] << 8); - next = data[offset + 2] + (data[offset + 3] << 8); + current = array_uint16_le (data + offset + 0); + next = array_uint16_le (data + offset + 2); assert (marker == current); marker += next; offset += 4; @@ -292,7 +292,7 @@ suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback assert (offset + 4 <= size); unknown = data[offset + 0]; seconds = data[offset + 1]; - heading = data[offset + 2] + (data[offset + 3] << 8); + heading = array_uint16_le (data + offset + 2); if (heading == 0xFFFF) { sample.event.type = SAMPLE_EVENT_BOOKMARK; sample.event.value = 0; diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 39932ca..ca2cac1 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -29,6 +29,7 @@ #include "serial.h" #include "checksum.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -246,7 +247,7 @@ suunto_eon_device_foreach (device_t *abstract, dive_callback_t callback, void *u device_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = 0; - devinfo.serial = (data[244 + 0] << 16) + (data[244 + 1] << 8) + data[244 + 2]; + devinfo.serial = array_uint24_be (data + 244); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); return suunto_eon_extract_dives (abstract, data, sizeof (data), callback, userdata); diff --git a/src/suunto_solution.c b/src/suunto_solution.c index c8ecf63..f678068 100644 --- a/src/suunto_solution.c +++ b/src/suunto_solution.c @@ -27,6 +27,7 @@ #include "ringbuffer.h" #include "serial.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -272,7 +273,7 @@ suunto_solution_device_foreach (device_t *abstract, dive_callback_t callback, vo device_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = 0; - devinfo.serial = (data[0x1D + 0] << 16) + (data[0x1D + 1] << 8) + data[0x1D + 2]; + devinfo.serial = array_uint24_be (data + 0x1D); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); return suunto_solution_extract_dives (abstract, data, sizeof (data), callback, userdata); diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index 7302963..b769ff6 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -604,10 +604,7 @@ suunto_vyper_device_foreach (device_t *abstract, dive_callback_t callback, void device_devinfo_t devinfo; devinfo.model = header[hoffset + 0]; devinfo.firmware = header[hoffset + 1]; - devinfo.serial = (header[hoffset + 2] << 24) + - (header[hoffset + 3] << 16) + - (header[hoffset + 4] << 8) + - header[hoffset + 5]; + devinfo.serial = array_uint32_be (header + hoffset + 2); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); // The memory layout of the Spyder is different from the Vyper @@ -667,10 +664,10 @@ suunto_vyper_extract_dives (device_t *abstract, const unsigned char data[], unsi vyper = 0; if (vyper) { - unsigned int eop = (data[0x51] << 8) + data[0x52]; + unsigned int eop = array_uint16_be (data + 0x51); return suunto_common_extract_dives (abstract, data, 0x71, SUUNTO_VYPER_MEMORY_SIZE, eop, 5, fp_compare_vyper, callback, userdata); } else { - unsigned int eop = (data[0x1C] << 8) + data[0x1D]; + unsigned int eop = array_uint16_be (data + 0x1C); return suunto_common_extract_dives (abstract, data, 0x4C, SUUNTO_VYPER_MEMORY_SIZE, eop, 3, fp_compare_spyder, callback, userdata); } } diff --git a/src/suunto_vyper2.c b/src/suunto_vyper2.c index 96ccc1a..7d060d5 100644 --- a/src/suunto_vyper2.c +++ b/src/suunto_vyper2.c @@ -29,6 +29,7 @@ #include "utils.h" #include "ringbuffer.h" #include "checksum.h" +#include "array.h" #define MAXRETRIES 2 @@ -490,8 +491,8 @@ suunto_vyper2_device_foreach (device_t *abstract, dive_callback_t callback, void // Emit a device info event. device_devinfo_t devinfo; devinfo.model = version[0]; - devinfo.firmware = (version[1] << 16) + (version[2] << 8) + version[3]; - devinfo.serial = (serial[0] << 24) + (serial[1] << 16) + (serial[2] << 8) + serial[3]; + devinfo.firmware = array_uint24_be (version + 1); + devinfo.serial = array_uint32_be (serial); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); // Read the header bytes. @@ -503,10 +504,10 @@ suunto_vyper2_device_foreach (device_t *abstract, dive_callback_t callback, void } // Obtain the pointers from the header. - unsigned int last = header[0] + (header[1] << 8); - unsigned int count = header[2] + (header[3] << 8); - unsigned int end = header[4] + (header[5] << 8); - unsigned int begin = header[6] + (header[7] << 8); + unsigned int last = array_uint16_le (header + 0); + unsigned int count = array_uint16_le (header + 2); + unsigned int end = array_uint16_le (header + 4); + unsigned int begin = array_uint16_le (header + 6); message ("Pointers: begin=%04x, last=%04x, end=%04x, count=%i\n", begin, last, end, count); // Memory buffer to store all the dives. @@ -603,8 +604,8 @@ suunto_vyper2_device_foreach (device_t *abstract, dive_callback_t callback, void remaining -= size; available = nbytes - size; - unsigned int oprevious = data[MINIMUM + remaining + 0] + (data[MINIMUM + remaining + 1] << 8); - unsigned int onext = data[MINIMUM + remaining + 2] + (data[MINIMUM + remaining + 3] << 8); + unsigned int oprevious = array_uint16_le (data + MINIMUM + remaining + 0); + unsigned int onext = array_uint16_le (data + MINIMUM + remaining + 2); message ("Pointers: previous=%04x, next=%04x\n", oprevious, onext); assert (current == onext); diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index b3b91ac..6c123d9 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -189,7 +189,7 @@ uwatec_aladin_device_set_fingerprint (device_t *abstract, const unsigned char da return DEVICE_STATUS_ERROR; if (size) - device->timestamp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); + device->timestamp = array_uint32_le (data); else device->timestamp = 0; @@ -251,9 +251,7 @@ uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned in array_reverse_bits (answer, sizeof (answer)); // Verify the checksum of the package. - unsigned short crc = - answer[UWATEC_ALADIN_MEMORY_SIZE + 0] + - (answer[UWATEC_ALADIN_MEMORY_SIZE + 1] << 8); + unsigned short crc = array_uint16_le (answer + UWATEC_ALADIN_MEMORY_SIZE); unsigned short ccrc = checksum_add_uint16 (answer, UWATEC_ALADIN_MEMORY_SIZE, 0x0000); if (ccrc != crc) { WARNING ("Unexpected answer CRC."); @@ -287,7 +285,7 @@ uwatec_aladin_device_foreach (device_t *abstract, dive_callback_t callback, void device_devinfo_t devinfo; devinfo.model = data[HEADER + 0x7bc]; devinfo.firmware = 0; - devinfo.serial = (data[HEADER + 0x7ed] << 16) + (data[HEADER + 0x7ee] << 8) + data[HEADER + 0x7ef]; + devinfo.serial = array_uint24_be (data + HEADER + 0x7ed); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); return uwatec_aladin_extract_dives (abstract, data, sizeof (data), callback, userdata); @@ -308,7 +306,7 @@ uwatec_aladin_extract_dives (device_t *abstract, const unsigned char* data, unsi // The logbook ring buffer can store up to 37 dives. But // if the total number of dives is less, not all logbook // entries contain valid data. - unsigned int ndives = (data[HEADER + 0x7f2] << 8) + data[HEADER + 0x7f3]; + unsigned int ndives = array_uint16_be (data + HEADER + 0x7f2); if (ndives > 37) ndives = 37; @@ -394,8 +392,7 @@ uwatec_aladin_extract_dives (device_t *abstract, const unsigned char* data, unsi } // Automatically abort when a dive is older than the provided timestamp. - unsigned int timestamp = buffer[11] + (buffer[12] << 8) + - (buffer[13] << 16) + (buffer[14] << 24); + unsigned int timestamp = array_uint32_le (buffer + 11); if (device && timestamp <= device->timestamp) return DEVICE_STATUS_SUCCESS; diff --git a/src/uwatec_memomouse.c b/src/uwatec_memomouse.c index 233888b..292874a 100644 --- a/src/uwatec_memomouse.c +++ b/src/uwatec_memomouse.c @@ -189,7 +189,7 @@ uwatec_memomouse_device_set_fingerprint (device_t *abstract, const unsigned char return DEVICE_STATUS_ERROR; if (size) - device->timestamp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); + device->timestamp = array_uint32_le (data); else device->timestamp = 0; @@ -326,7 +326,7 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned } // Calculate the total size of the inner package. - unsigned int total = package[0] + (package[1] << 8) + 3; + unsigned int total = array_uint16_le (package) + 3; // Update and emit a progress event. if (progress) { @@ -558,7 +558,7 @@ uwatec_memomouse_extract_dives (device_t *abstract, const unsigned char data[], break; // Get the length of the profile data. - unsigned int len = data[current + 16] + (data[current + 17] << 8); + unsigned int len = array_uint16_le (data + current + 16); // Check for a buffer overflow. if (current + len + 18 > size) @@ -573,7 +573,7 @@ uwatec_memomouse_extract_dives (device_t *abstract, const unsigned char data[], device_devinfo_t devinfo; devinfo.model = data[current + 3]; devinfo.firmware = 0; - devinfo.serial = (data[current + 0] << 16) + (data[current + 1] << 8) + data[current + 2]; + devinfo.serial = array_uint32_be (data + current); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); } @@ -593,14 +593,14 @@ uwatec_memomouse_extract_dives (device_t *abstract, const unsigned char data[], unsigned int skip = ndives - i - 1; while (skip) { // Get the length of the profile data. - unsigned int len = data[offset + 16] + (data[offset + 17] << 8); + unsigned int len = array_uint16_le (data + offset + 16); // Move to the next dive. offset += len + 18; skip--; } // Get the length of the profile data. - unsigned int length = data[offset + 16] + (data[offset + 17] << 8); + unsigned int length = array_uint16_le (data + offset + 16); if (callback && !callback (data + offset, length + 18, userdata)) return DEVICE_STATUS_SUCCESS; diff --git a/src/uwatec_memomouse_parser.c b/src/uwatec_memomouse_parser.c index 41230a0..d4e8b0c 100644 --- a/src/uwatec_memomouse_parser.c +++ b/src/uwatec_memomouse_parser.c @@ -25,6 +25,7 @@ #include "uwatec_memomouse.h" #include "parser-private.h" #include "utils.h" +#include "array.h" #define WARNING(expr) \ { \ @@ -138,7 +139,7 @@ uwatec_memomouse_parser_samples_foreach (parser_t *abstract, sample_callback_t c while (offset + 2 <= size) { parser_sample_value_t sample = {0}; - unsigned int value = (data[offset + 0] << 8) + data[offset + 1]; + unsigned int value = array_uint16_be (data + offset); unsigned int depth = (value & 0xFFC0) >> 6; unsigned int warnings = (value & 0x3F); offset += 2; diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index 99e215b..12f99a2 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -27,6 +27,7 @@ #include "device-private.h" #include "uwatec_smart.h" #include "irda.h" +#include "array.h" #include "utils.h" #define WARNING(expr) \ @@ -216,7 +217,7 @@ uwatec_smart_device_set_fingerprint (device_t *abstract, const unsigned char dat return DEVICE_STATUS_ERROR; if (size) - device->timestamp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); + device->timestamp = array_uint32_le (data); else device->timestamp = 0; @@ -311,8 +312,7 @@ uwatec_smart_device_version (device_t *abstract, unsigned char data[], unsigned if (rc != DEVICE_STATUS_SUCCESS) return rc; - time_t device_time = answer[0] + (answer[1] << 8) + - (answer[2] << 16) + (answer[3] << 24); + time_t device_time = array_uint32_le (answer); message ("handshake: timestamp=0x%08x\n", device_time); // PC Time and Time Correction. @@ -330,8 +330,7 @@ uwatec_smart_device_version (device_t *abstract, unsigned char data[], unsigned if (rc != DEVICE_STATUS_SUCCESS) return rc; - unsigned int serial = answer[4] + (answer[5] << 8) + - (answer[6] << 16) + (answer[7] << 24); + unsigned int serial = array_uint32_le (answer + 4); message ("handshake: serial=0x%08x\n", serial); // Dive Computer Model. @@ -382,7 +381,7 @@ uwatec_smart_dump (uwatec_smart_device_t *device, unsigned char *data[], unsigne if (rc != DEVICE_STATUS_SUCCESS) return rc; - unsigned int serial = answer[0] + (answer[1] << 8) + (answer[2] << 16) + (answer[3] << 24); + unsigned int serial = array_uint32_le (answer); message ("handshake: serial=0x%08x\n", serial); // Current Timestamp. @@ -392,7 +391,7 @@ uwatec_smart_dump (uwatec_smart_device_t *device, unsigned char *data[], unsigne if (rc != DEVICE_STATUS_SUCCESS) return rc; - unsigned int timestamp = answer[0] + (answer[1] << 8) + (answer[2] << 16) + (answer[3] << 24); + unsigned int timestamp = array_uint32_le (answer); message ("handshake: timestamp=0x%08x\n", timestamp); // Update and emit a progress event. @@ -422,8 +421,7 @@ uwatec_smart_dump (uwatec_smart_device_t *device, unsigned char *data[], unsigne if (rc != DEVICE_STATUS_SUCCESS) return rc; - unsigned int length = answer[0] + (answer[1] << 8) + - (answer[2] << 16) + (answer[3] << 24); + unsigned int length = array_uint32_le (answer); message ("handshake: length=%u\n", length); // Update and emit a progress event. @@ -458,8 +456,7 @@ uwatec_smart_dump (uwatec_smart_device_t *device, unsigned char *data[], unsigne return rc; } - unsigned int total = answer[0] + (answer[1] << 8) + - (answer[2] << 16) + (answer[3] << 24); + unsigned int total = array_uint32_le (answer); message ("handshake: total=%u\n", total); // Update and emit a progress event. @@ -567,8 +564,7 @@ uwatec_smart_extract_dives (device_t *abstract, const unsigned char data[], unsi current--; if (memcmp (data + current, header, sizeof (header)) == 0) { // Get the length of the profile data. - unsigned int len = data[current + 4] + (data[current + 5] << 8) + - (data[current + 6] << 16) + (data[current + 7] << 24); + unsigned int len = array_uint32_le (data + current + 4); // Check for a buffer overflow. if (current + len > previous)