1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 1994-2009 Red Hat, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * 3. Neither the name of the copyright holder nor the names of its 17 * contributors may be used to endorse or promote products derived from this 18 * software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* This file is copied from newlib-1.19 */ 34 35 /* 36 FUNCTION 37 <<strlen>>---character string length 38 39 INDEX 40 strlen 41 42 ANSI_SYNOPSIS 43 #include <string.h> 44 size_t strlen(const char *<[str]>); 45 46 TRAD_SYNOPSIS 47 #include <string.h> 48 size_t strlen(<[str]>) 49 char *<[src]>; 50 51 DESCRIPTION 52 The <<strlen>> function works out the length of the string 53 starting at <<*<[str]>>> by counting chararacters until it 54 reaches a <<NULL>> character. 55 56 RETURNS 57 <<strlen>> returns the character count. 58 59 PORTABILITY 60 <<strlen>> is ANSI C. 61 62 <<strlen>> requires no supporting OS subroutines. 63 64 QUICKREF 65 strlen ansi pure 66 */ 67 68 #include "_ansi.h" 69 #include <string.h> 70 #include <limits.h> 71 72 #define LBLOCKSIZE (sizeof(long)) 73 #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1)) 74 75 #if LONG_MAX == 2147483647L 76 #define DETECTNULL(X) (((X) - 0x01010101L) & ~(X) & 0x80808080UL) 77 #else 78 #if LONG_MAX == 9223372036854775807L 79 /* Nonzero if X (a long int) contains a NULL byte. */ 80 #define DETECTNULL(X) (((X) - 0x0101010101010101L) & ~(X) & \ 81 0x8080808080808080UL) 82 #else 83 #error long int is not a 32bit or 64bit type. 84 #endif 85 #endif 86 87 #ifndef DETECTNULL 88 #error long int is not a 32bit or 64bit byte 89 #endif 90 91 size_t _DEFUN(strlen, (str), _CONST char *str) 92 { 93 _CONST char *start = str; 94 95 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) 96 unsigned long *aligned_addr; 97 98 /* Align the pointer, so we can search a word at a time. */ 99 while (UNALIGNED(str)) { 100 if (!*str) 101 return str - start; 102 str++; 103 } 104 105 /* If the string is word-aligned, we can check for the presence of 106 a null in each word-sized block. */ 107 aligned_addr = (unsigned long *)str; 108 while (!DETECTNULL(*aligned_addr)) 109 aligned_addr++; 110 111 /* Once a null is detected, we check each byte in that block for a 112 precise position of the null. */ 113 str = (char *)aligned_addr; 114 115 #endif /* not PREFER_SIZE_OVER_SPEED */ 116 117 while (*str) 118 str++; 119 return str - start; 120 } 121