]> git.draconx.ca Git - upkg.git/blobdiff - src/libupkg.c
libupkg: Make decodeindex test accept an empty string.
[upkg.git] / src / libupkg.c
index 178e235f4ef29b9c31038a3ad184adc259c4d8ce..5a9dce7f29ad0d8ec6936827f01b5181d6d89c67 100644 (file)
@@ -110,12 +110,11 @@ const struct upkg_file_ops upkg_default_fops = {
  * Stores the result in *val and returns the number of input bytes read (or 0
  * if the input is invalid, in which case *val is undefined).
  */
-size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
+size_t upkg_decode_index(long *val, const unsigned char *bytes, size_t n)
 {
-       size_t i = 0;
-
        *val = 0;
-       while (i < MIN(n, 5)) {
+
+       for (size_t i = 0; i < MIN(n, 5); i++) {
                /*
                 * Least significant bytes are first, so we need to do this
                 * nonsense.
@@ -127,18 +126,14 @@ size_t upkg_decode_index(long *val, unsigned char *bytes, size_t n)
                *val += tmp;
 
                if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
-                       i++;
-                       break;
+                       if (bytes[0] & 0x80)
+                               *val = -*val;
+                       return i+1;
                }
-
-               i++;
        }
 
-       if (i > MIN(n, 5) || n == 0)
-               return 0;
-       if (bytes[0] & 0x80)
-               *val = -*val;
-       return i;
+       /* Error */
+       return 0;
 }
 
 static struct upkg_priv *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])