diff --git a/include/string.h b/include/string.h index 3a86e9eb24..7ddfb0df05 100644 --- a/include/string.h +++ b/include/string.h @@ -110,4 +110,24 @@ static inline int strcmp(const char *s1, const char *s2) return c1 - c2; } +/** + * Compare two strings with fixed length. + * + * @param s1 The first string. + * @param s2 The second string. + * @param maxlen Return at most maxlen characters as length of the string. + * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2. + */ +static inline int strncmp(const char *s1, const char *s2, int maxlen) +{ + int i; + + for (i = 0; i < maxlen; i++) { + if (s1[i] != s2[i]) + return s1[i] - s2[i]; + } + + return 0; +} + #endif /* STRING_H */ diff --git a/lib/lar.c b/lib/lar.c index fd079619b5..da0ec44eb6 100644 --- a/lib/lar.c +++ b/lib/lar.c @@ -92,7 +92,7 @@ int find_file(struct mem_file *archive, const char *filename, struct mem_file *r for (walk = archive->start; (walk < (char *)(archive->start + archive->len - sizeof(struct lar_header))) && (walk >= (char *)archive->start); walk += 16) { - if (strcmp(walk, MAGIC) != 0) + if (strncmp(walk, MAGIC, 8) != 0) continue; header = (struct lar_header *)walk;