visomat-data-downloader: more precise data validation in decode_eeprom()
[visomat-utils.git] / src / visomat-data-downloader.c
index c2b8590..a53f72c 100644 (file)
@@ -71,7 +71,7 @@ struct pressure  {
        unsigned int pulses;
 };
 
-static inline int extract_datetime(unsigned char *buffer, struct datetime *d)
+static inline int extract_datetime(uint8_t *buffer, struct datetime *d)
 {
        int ret;
 
@@ -120,7 +120,7 @@ static void print_record_csv_compat(struct datetime *d, struct pressure *p)
 }
 
 /* TODO: it would be better to separate decoding data from printing it */
-static int decode_eeprom(unsigned char *buffer,
+static int decode_eeprom(uint8_t *buffer,
                         unsigned int len,
                         unsigned int user_mask)
 {
@@ -133,8 +133,15 @@ static int decode_eeprom(unsigned char *buffer,
        struct datetime d;
        struct pressure p;
 
-       if (buffer[0] != STX || buffer[1] != 'M' || buffer[len - 1] != ETX)
+       if (buffer[0] != STX || buffer[1] != 'M') {
+               fprintf(stderr, "Usupported data.\n");
                return -EINVAL;
+       }
+
+       if (buffer[len - 1] != ETX) {
+               fprintf(stderr, "Bad terminator in data. Buffer too small?\n");
+               return -EINVAL;
+       }
 
        /* skip the initial STX */
        i = 1;
@@ -156,6 +163,13 @@ static int decode_eeprom(unsigned char *buffer,
                        /* user_id and num_records take 3 bytes */
                        i += 3;
 
+                       /*
+                        * when there are no records, there is a dummy byte
+                        * which has to be consumed
+                        */
+                       if (num_records == 0)
+                               i += 1;
+
                        for (j = 0; j < num_records; j++) {
                                ret = extract_datetime(buffer + i, &d);
                                if (ret < 0)
@@ -189,12 +203,12 @@ static int decode_eeprom(unsigned char *buffer,
        return 0;
 }
 
-static int decode_datetime(unsigned char *buffer, unsigned int len)
+static int decode_datetime(uint8_t *buffer, unsigned int len)
 {
        int ret;
-       unsigned char code[4] = { 0 };
+       uint8_t code[4] = { 0 };
        struct datetime d;
-       unsigned char *pbuffer = buffer;
+       uint8_t *pbuffer = buffer;
 
        if (len != 15)
                return -EINVAL;
@@ -217,7 +231,7 @@ static int send_command(libusb_device_handle *dev, visomat_command cmd)
 {
        int ret;
        int transferred;
-       unsigned char request[5];
+       uint8_t request[5];
 
        request[0] = STX;
        request[1] = command_codes[cmd][0];
@@ -236,12 +250,12 @@ static int send_command(libusb_device_handle *dev, visomat_command cmd)
 }
 
 static int get_response(libusb_device_handle *dev,
-                       unsigned char *buffer,
+                       uint8_t *buffer,
                        unsigned int len)
 {
        int ret;
        int transferred;
-       unsigned char response[64] = { 0 };
+       uint8_t response[64] = { 0 };
        unsigned int i;
 
        i = 0;
@@ -275,7 +289,7 @@ static int get_response(libusb_device_handle *dev,
 static int visomat_dump_eeprom(visomat_device *dev, unsigned int user_mask)
 {
        /* Assuming an EEPROM of 1 KiB  */
-       unsigned char buffer[1024] = { 0 };
+       uint8_t buffer[1024] = { 0 };
        int ret;
 
        ret = send_command(dev, VISOMAT_CMD_DUMP_EEPROM);
@@ -295,7 +309,7 @@ static int visomat_dump_eeprom(visomat_device *dev, unsigned int user_mask)
 
 static int visomat_get_datetime(visomat_device *dev)
 {
-       unsigned char buffer[255] = { 0 };
+       uint8_t buffer[255] = { 0 };
        int ret;
 
        ret = send_command(dev, VISOMAT_CMD_GET_DATETIME);
@@ -350,7 +364,7 @@ int main(void)
                if (ret < 0) {
                        fprintf(stderr, "libusb_set_configuration failed: %s\n",
                                libusb_error_name(ret));
-                       fprintf(stderr, "Cannot set configuration %hhu\n",
+                       fprintf(stderr, "Cannot set configuration %d\n",
                                VISOMAT_CONFIGURATION);
                        goto out_libusb_close;
                }
@@ -362,7 +376,7 @@ int main(void)
        if (ret < 0) {
                fprintf(stderr, "libusb_claim_interface failed: %s\n",
                        libusb_error_name(ret));
-               fprintf(stderr, "Cannot claim interface %hhu\n",
+               fprintf(stderr, "Cannot claim interface %d\n",
                        VISOMAT_INTERFACE);
                goto out_libusb_close;
        }
@@ -379,7 +393,7 @@ int main(void)
        }
 
        if (current_configuration != VISOMAT_CONFIGURATION) {
-               fprintf(stderr, "libusb configuration changed (expected: %hhu, current: %hhu\n",
+               fprintf(stderr, "libusb configuration changed (expected: %d, current: %d)\n",
                        VISOMAT_CONFIGURATION, current_configuration);
                ret = -EINVAL;
                goto out_libusb_release_interface;