/* * string.c * * Copyright (C) 2021 bzt (bztsrc@gitlab) * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * * This file is part of the POSIX-UEFI package. * @brief Implementing functions which are defined in string.h * */ #include void *memcpy(void *dst, const void *src, size_t n) { uint8_t *a=(uint8_t*)dst,*b=(uint8_t*)src; if(src && dst && src != dst && n>0) { while(n--) *a++ = *b++; } return dst; } void *memmove(void *dst, const void *src, size_t n) { uint8_t *a=(uint8_t*)dst,*b=(uint8_t*)src; if(src && dst && src != dst && n>0) { if(a>b && a0) *a--=*b--; } else { while(n--) *a++ = *b++; } } return dst; } void *memset(void *s, int c, size_t n) { uint8_t *p=(uint8_t*)s; if(s && n>0) { while(n--) *p++ = (uint8_t)c; } return s; } int memcmp(const void *s1, const void *s2, size_t n) { uint8_t *a=(uint8_t*)s1,*b=(uint8_t*)s2; if(s1 && s2 && s1 != s2 && n>0) { while(n--) { if(*a != *b) return *a - *b; a++; b++; } } return 0; } void *memchr(const void *s, int c, size_t n) { uint8_t *e, *p=(uint8_t*)s; if(s && n>0) { for(e=p+n; p0) { for(e=p+n; p hl) return NULL; hl -= nl - 1; while(hl) { if(!memcmp(c, needle, nl)) return c; c++; hl--; } return NULL; } void *memrmem(const void *haystack, size_t hl, const void *needle, size_t nl) { uint8_t *c = (uint8_t*)haystack; if(!haystack || !needle || !hl || !nl || nl > hl) return NULL; hl -= nl; c += hl; while(hl) { if(!memcmp(c, needle, nl)) return c; c--; hl--; } return NULL; } char_t *strcpy(char_t *dst, const char_t *src) { char_t *s = dst; if(src && dst && src != dst) { while(*src) {*dst++=*src++;} *dst=0; } return s; } char_t *strncpy(char_t *dst, const char_t *src, size_t n) { char_t *s = dst; const char_t *e = src+n; if(src && dst && src != dst && n>0) { while(*src && src0) { dst += strlen(dst); while(*src && src0) { while(s1