HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/boot/efi-string.c

42,290 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: efe1553696ddfa65ecdd609c90a545616c8ecbe3

Lines 1001–1174 of 1,174

                        push_str(&ctx, &sp);
                }
        }
 
        va_end(ctx.ap);
 
        assert(ctx.n < ctx.n_buf);
        ctx.buf[ctx.n++] = '\0';
 
        if (ret) {
                if (ctx.dyn_buf)
                        return TAKE_PTR(ctx.dyn_buf);
 
                char16_t *ret_buf = xnew(char16_t, ctx.n);
                memcpy(ret_buf, ctx.buf, ctx.n * sizeof(*ctx.buf));
                return ret_buf;
        }
 
#if SD_BOOT
        output_string_safe(ST->ConOut, ctx.buf);
#endif
 
        return mfree(ctx.dyn_buf);
}
 
void printf_status(EFI_STATUS status, const char *format, ...) {
        va_list ap;
        va_start(ap, format);
        printf_internal(status, format, ap, false);
        va_end(ap);
}
 
void vprintf_status(EFI_STATUS status, const char *format, va_list ap) {
        printf_internal(status, format, ap, false);
}
 
char16_t *xasprintf_status(EFI_STATUS status, const char *format, ...) {
        va_list ap;
        va_start(ap, format);
        char16_t *ret = printf_internal(status, format, ap, true);
        va_end(ap);
        return ret;
}
 
char16_t *xvasprintf_status(EFI_STATUS status, const char *format, va_list ap) {
        return printf_internal(status, format, ap, true);
}
 
#if SD_BOOT
/* To provide the actual implementation for these we need to remove the redirection to the builtins. */
#  undef memchr
#  undef memcmp
#  undef memcpy
#  undef memset
// NOLINTBEGIN(misc-use-internal-linkage)
_used_ void *memchr(const void *p, int c, size_t n);
_used_ int memcmp(const void *p1, const void *p2, size_t n);
_used_ void *memcpy(void * restrict dest, const void * restrict src, size_t n);
_used_ void *memset(void *p, int c, size_t n);
// NOLINTEND(misc-use-internal-linkage)
#else
/* And for userspace unit testing we need to give them an efi_ prefix. */
#  undef memchr
#  define memchr efi_memchr
#  define memcmp efi_memcmp
#  define memcpy efi_memcpy
#  define memset efi_memset
#endif
 
void *memchr(const void *p, int c, size_t n) {
        if (!p || n == 0)
                return NULL;
 
        const uint8_t *q = p;
        for (size_t i = 0; i < n; i++)
                if (q[i] == (unsigned char) c)
                        return (void *) (q + i);
 
        return NULL;
}
 
int memcmp(const void *p1, const void *p2, size_t n) {
        const uint8_t *up1 = p1, *up2 = p2;
        int r;
 
        if (!p1 || !p2)
                return CMP(p1, p2);
 
        while (n > 0) {
                r = CMP(*up1, *up2);
                if (r != 0)
                        return r;
 
                up1++;
                up2++;
                n--;
        }
 
        return 0;
}
 
void *memcpy(void * restrict dest, const void * restrict src, size_t n) {
        if (!dest || !src || n == 0)
                return dest;
 
#if SD_BOOT
        /* The firmware-provided memcpy is likely optimized, so use that. The function is guaranteed to be
         * available by the UEFI spec. We still make it depend on the boot services pointer being set just in
         * case the compiler emits a call before it is available. */
        if (_likely_(BS)) {
                BS->CopyMem(dest, (void *) src, n);
                return dest;
        }
#endif
 
        uint8_t *d = dest;
        const uint8_t *s = src;
 
        while (n > 0) {
                *d = *s;
                d++;
                s++;
                n--;
        }
 
        return dest;
}
 
void *memset(void *p, int c, size_t n) {
        if (!p || n == 0)
                return p;
 
#if SD_BOOT
        /* See comment in efi_memcpy. Note that the signature has c and n swapped! */
        if (_likely_(BS)) {
                BS->SetMem(p, n, c);
                return p;
        }
#endif
 
        uint8_t *q = p;
        while (n > 0) {
                *q = c;
                q++;
                n--;
        }
 
        return p;
}
 
size_t strspn16(const char16_t *p, const char16_t *good) {
        assert(p);
        assert(good);
 
        const char16_t *i = p;
        for (; *i != 0; i++)
                if (!strchr16(good, *i))
                        break;
 
        return i - p;
}
 
size_t strcspn16(const char16_t *p, const char16_t *bad) {
        assert(p);
        assert(bad);
 
        const char16_t *i = p;
        for (; *i != 0; i++)
                if (strchr16(bad, *i))
                        break;
 
        return i - p;
}