+2009-03-30 Pavel Roskin <proski@gnu.org>
+
+ * fs/hfs.c (grub_hfs_strncasecmp): Integrate into ...
+ (grub_hfs_cmp_catkeys): ... this. Don't assume strings to be
+ zero-terminated, rely only on the strlen value. Fix comparison
+ of strings differing in length.
+
2009-03-30 Robert Millan <rmh@aybabtu.com>
* loader/i386/linux.c (grub_cmd_linux): Check for zImage before
return 0;
}
-/* Case insensitive string comparison using HFS character ordering */
+/* Compare the K1 and K2 catalog file keys using HFS character ordering. */
static int
-grub_hfs_strncasecmp (const unsigned char *s1, const unsigned char *s2,
- grub_size_t n)
+grub_hfs_cmp_catkeys (struct grub_hfs_catalog_key *k1,
+ struct grub_hfs_catalog_key *k2)
{
/* Taken from hfsutils 3.2.6 and converted to a readable form */
static const unsigned char hfs_charorder[256] = {
[0xFE] = 214,
[0xFF] = 215,
};
+ int i;
+ int cmp;
+ int minlen = (k1->strlen < k2->strlen) ? k1->strlen : k2->strlen;
- if (n == 0)
- return 0;
+ cmp = (grub_be_to_cpu32 (k1->parent_dir) - grub_be_to_cpu32 (k2->parent_dir));
+ if (cmp != 0)
+ return cmp;
- while (*s1 && *s2 && --n)
+ for (i = 0; i < minlen; i++)
{
- if (hfs_charorder[*s1] != hfs_charorder[*s2])
- break;
-
- s1++;
- s2++;
+ cmp = (hfs_charorder[k1->str[i]] - hfs_charorder[k2->str[i]]);
+ if (cmp != 0)
+ return cmp;
}
- return (int) hfs_charorder[*s1] - (int) hfs_charorder[*s2];
-}
-
-/* Compare the K1 and K2 catalog file keys. */
-static int
-grub_hfs_cmp_catkeys (struct grub_hfs_catalog_key *k1,
- struct grub_hfs_catalog_key *k2)
-{
- int cmp = (grub_be_to_cpu32 (k1->parent_dir)
- - grub_be_to_cpu32 (k2->parent_dir));
-
- if (cmp != 0)
- return cmp;
-
- cmp = grub_hfs_strncasecmp (k1->str, k2->str, k1->strlen);
-
- /* This is required because the compared strings are not of equal
- length. */
- if (cmp == 0 && k1->strlen < k2->strlen)
- return -1;
- return cmp;
+ /* Shorter strings precede long ones. */
+ return (k1->strlen - k2->strlen);
}