]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Add startswith() and endswith() functions with no_case variants 18423/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 2 Feb 2021 22:27:09 +0000 (22:27 +0000)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 2 Feb 2021 22:53:31 +0000 (22:53 +0000)
Adapted from string-util

src/boot/efi/boot.c
src/boot/efi/util.c
src/boot/efi/util.h

index 8b5bcd3683b97369a7eae5e50c27a379dc5654c4..c88a6db9deee71df6a12d500c1b9a943cb11224a 100644 (file)
@@ -1493,7 +1493,6 @@ static VOID config_load_entries(
                         UINTN bufsize;
                         EFI_FILE_INFO *f;
                         _cleanup_freepool_ CHAR8 *content = NULL;
-                        UINTN len;
 
                         bufsize = sizeof(buf);
                         err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
@@ -1506,12 +1505,9 @@ static VOID config_load_entries(
                         if (f->Attribute & EFI_FILE_DIRECTORY)
                                 continue;
 
-                        len = StrLen(f->FileName);
-                        if (len < 6)
-                                continue;
-                        if (StriCmp(f->FileName + len - 5, L".conf") != 0)
+                        if (!endswith_no_case(f->FileName, L".conf"))
                                 continue;
-                        if (StrnCmp(f->FileName, L"auto-", 5) == 0)
+                        if (startswith(f->FileName, L"auto-"))
                                 continue;
 
                         err = file_read(entries_dir, f->FileName, 0, 0, &content, NULL);
@@ -1894,7 +1890,6 @@ static VOID config_entry_add_linux(
                 UINTN szs[ELEMENTSOF(sections)-1] = {};
                 UINTN addrs[ELEMENTSOF(sections)-1] = {};
                 CHAR8 *content = NULL;
-                UINTN len;
                 CHAR8 *line;
                 UINTN pos = 0;
                 CHAR8 *key, *value;
@@ -1914,12 +1909,9 @@ static VOID config_entry_add_linux(
                         continue;
                 if (f->Attribute & EFI_FILE_DIRECTORY)
                         continue;
-                len = StrLen(f->FileName);
-                if (len < 5)
-                        continue;
-                if (StriCmp(f->FileName + len - 4, L".efi") != 0)
+                if (!endswith_no_case(f->FileName, L".efi"))
                         continue;
-                if (StrnCmp(f->FileName, L"auto-", 5) == 0)
+                if (startswith(f->FileName, L"auto-"))
                         continue;
 
                 /* look for .osrel and .cmdline sections in the .efi binary */
index 50ad7575655341ff99ea33205c91cbef743b229e..8adf3f5fe45741f8d48a33319b09247e73c98964 100644 (file)
@@ -375,6 +375,62 @@ CHAR8 *strchra(CHAR8 *s, CHAR8 c) {
         return NULL;
 }
 
+const CHAR16 *startswith(const CHAR16 *s, const CHAR16 *prefix) {
+        UINTN l;
+
+        l = StrLen(prefix);
+        if (StrnCmp(s, prefix, l) == 0)
+                return s + l;
+
+        return NULL;
+}
+
+const CHAR16 *endswith(const CHAR16 *s, const CHAR16 *postfix) {
+        UINTN sl, pl;
+
+        sl = StrLen(s);
+        pl = StrLen(postfix);
+
+        if (pl == 0)
+                return s + sl;
+
+        if (sl < pl)
+                return NULL;
+
+        if (StrnCmp(s + sl - pl, postfix, pl) != 0)
+                return NULL;
+
+        return s + sl - pl;
+}
+
+const CHAR16 *startswith_no_case(const CHAR16 *s, const CHAR16 *prefix) {
+        UINTN l;
+
+        l = StrLen(prefix);
+        if (StriCmp(s, prefix) == 0)
+                return s + l;
+
+        return NULL;
+}
+
+const CHAR16 *endswith_no_case(const CHAR16 *s, const CHAR16 *postfix) {
+        UINTN sl, pl;
+
+        sl = StrLen(s);
+        pl = StrLen(postfix);
+
+        if (pl == 0)
+                return s + sl;
+
+        if (sl < pl)
+                return NULL;
+
+        if (StriCmp(s + sl - pl, postfix) != 0)
+                return NULL;
+
+        return s + sl - pl;
+}
+
 EFI_STATUS file_read(EFI_FILE_HANDLE dir, const CHAR16 *name, UINTN off, UINTN size, CHAR8 **ret, UINTN *ret_size) {
         _cleanup_(FileHandleClosep) EFI_FILE_HANDLE handle = NULL;
         _cleanup_freepool_ CHAR8 *buf = NULL;
index 313d6697d2ec640d5c7353ca9f57d943af8719c4..f2be857d427b6a2c324e0657a091d6edad210164 100644 (file)
@@ -39,6 +39,12 @@ CHAR8 *strchra(CHAR8 *s, CHAR8 c);
 CHAR16 *stra_to_path(CHAR8 *stra);
 CHAR16 *stra_to_str(CHAR8 *stra);
 
+const CHAR16 *startswith(const CHAR16 *s, const CHAR16 *prefix);
+const CHAR16 *endswith(const CHAR16 *s, const CHAR16 *postfix);
+
+const CHAR16 *startswith_no_case(const CHAR16 *s, const CHAR16 *prefix);
+const CHAR16 *endswith_no_case(const CHAR16 *s, const CHAR16 *postfix);
+
 EFI_STATUS file_read(EFI_FILE_HANDLE dir, const CHAR16 *name, UINTN off, UINTN size, CHAR8 **content, UINTN *content_size);
 
 static inline void FreePoolp(void *p) {