Path: blob/master/drivers/firmware/efi/libstub/string.c
26483 views
// SPDX-License-Identifier: GPL-2.01/*2* Taken from:3* linux/lib/string.c4*5* Copyright (C) 1991, 1992 Linus Torvalds6*/78#include <linux/ctype.h>9#include <linux/kernel.h>10#include <linux/types.h>11#include <linux/string.h>1213#ifndef EFI_HAVE_STRLEN14/**15* strlen - Find the length of a string16* @s: The string to be sized17*/18size_t strlen(const char *s)19{20const char *sc;2122for (sc = s; *sc != '\0'; ++sc)23/* nothing */;24return sc - s;25}26#endif2728#ifndef EFI_HAVE_STRNLEN29/**30* strnlen - Find the length of a length-limited string31* @s: The string to be sized32* @count: The maximum number of bytes to search33*/34size_t strnlen(const char *s, size_t count)35{36const char *sc;3738for (sc = s; count-- && *sc != '\0'; ++sc)39/* nothing */;40return sc - s;41}42#endif4344/**45* strstr - Find the first substring in a %NUL terminated string46* @s1: The string to be searched47* @s2: The string to search for48*/49char *strstr(const char *s1, const char *s2)50{51size_t l1, l2;5253l2 = strlen(s2);54if (!l2)55return (char *)s1;56l1 = strlen(s1);57while (l1 >= l2) {58l1--;59if (!memcmp(s1, s2, l2))60return (char *)s1;61s1++;62}63return NULL;64}6566#ifndef EFI_HAVE_STRCMP67/**68* strcmp - Compare two strings69* @cs: One string70* @ct: Another string71*/72int strcmp(const char *cs, const char *ct)73{74unsigned char c1, c2;7576while (1) {77c1 = *cs++;78c2 = *ct++;79if (c1 != c2)80return c1 < c2 ? -1 : 1;81if (!c1)82break;83}84return 0;85}86#endif8788/**89* strncmp - Compare two length-limited strings90* @cs: One string91* @ct: Another string92* @count: The maximum number of bytes to compare93*/94int strncmp(const char *cs, const char *ct, size_t count)95{96unsigned char c1, c2;9798while (count) {99c1 = *cs++;100c2 = *ct++;101if (c1 != c2)102return c1 < c2 ? -1 : 1;103if (!c1)104break;105count--;106}107return 0;108}109110/* Works only for digits and letters, but small and fast */111#define TOLOWER(x) ((x) | 0x20)112113static unsigned int simple_guess_base(const char *cp)114{115if (cp[0] == '0') {116if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))117return 16;118else119return 8;120} else {121return 10;122}123}124125/**126* simple_strtoull - convert a string to an unsigned long long127* @cp: The start of the string128* @endp: A pointer to the end of the parsed string will be placed here129* @base: The number base to use130*/131132unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)133{134unsigned long long result = 0;135136if (!base)137base = simple_guess_base(cp);138139if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')140cp += 2;141142while (isxdigit(*cp)) {143unsigned int value;144145value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;146if (value >= base)147break;148result = result * base + value;149cp++;150}151if (endp)152*endp = (char *)cp;153154return result;155}156157long simple_strtol(const char *cp, char **endp, unsigned int base)158{159if (*cp == '-')160return -simple_strtoull(cp + 1, endp, base);161162return simple_strtoull(cp, endp, base);163}164165#ifdef CONFIG_EFI_PARAMS_FROM_FDT166#ifndef EFI_HAVE_STRRCHR167/**168* strrchr - Find the last occurrence of a character in a string169* @s: The string to be searched170* @c: The character to search for171*/172char *strrchr(const char *s, int c)173{174const char *last = NULL;175do {176if (*s == (char)c)177last = s;178} while (*s++);179return (char *)last;180}181#endif182#ifndef EFI_HAVE_MEMCHR183/**184* memchr - Find a character in an area of memory.185* @s: The memory area186* @c: The byte to search for187* @n: The size of the area.188*189* returns the address of the first occurrence of @c, or %NULL190* if @c is not found191*/192void *memchr(const void *s, int c, size_t n)193{194const unsigned char *p = s;195while (n-- != 0) {196if ((unsigned char)c == *p++) {197return (void *)(p - 1);198}199}200return NULL;201}202#endif203#endif204205206