| 1 | /* memrchr -- find the last occurrence of a byte in a memory block |
| 2 | Copyright (C) 1991-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <string-fzb.h> |
| 20 | #include <string-fzc.h> |
| 21 | #include <string-fzi.h> |
| 22 | #include <string-shift.h> |
| 23 | #include <string.h> |
| 24 | #include <libc-pointer-arith.h> |
| 25 | |
| 26 | #undef __memrchr |
| 27 | #undef memrchr |
| 28 | |
| 29 | #ifdef MEMRCHR |
| 30 | # define __memrchr MEMRCHR |
| 31 | #endif |
| 32 | |
| 33 | void * |
| 34 | __memrchr (const void *s, int c_in, size_t n) |
| 35 | { |
| 36 | if (__glibc_unlikely (n == 0)) |
| 37 | return NULL; |
| 38 | |
| 39 | const op_t *word_ptr = (const op_t *) PTR_ALIGN_UP (s + n, sizeof (op_t)); |
| 40 | uintptr_t s_int = (uintptr_t) s + n; |
| 41 | |
| 42 | op_t word = *--word_ptr; |
| 43 | op_t repeated_c = repeat_bytes (c_in); |
| 44 | |
| 45 | /* Compute the address of the word containing the initial byte. */ |
| 46 | const op_t *sword = (const op_t *) PTR_ALIGN_DOWN (s, sizeof (op_t)); |
| 47 | |
| 48 | /* If the end of buffer is not op_t aligned, mask off the undesirable bits |
| 49 | before find the last byte position. */ |
| 50 | find_t mask = shift_find_last (word: find_eq_all (x1: word, x2: repeated_c), s: s_int); |
| 51 | if (mask != 0) |
| 52 | { |
| 53 | char *ret = (char *) word_ptr + index_last (c: mask); |
| 54 | return ret >= (char *) s ? ret : NULL; |
| 55 | } |
| 56 | if (word_ptr == sword) |
| 57 | return NULL; |
| 58 | word = *--word_ptr; |
| 59 | |
| 60 | while (word_ptr != sword) |
| 61 | { |
| 62 | if (has_eq (x1: word, x2: repeated_c)) |
| 63 | return (char *) word_ptr + index_last_eq (x1: word, x2: repeated_c); |
| 64 | word = *--word_ptr; |
| 65 | } |
| 66 | |
| 67 | if (has_eq (x1: word, x2: repeated_c)) |
| 68 | { |
| 69 | /* We found a match, but it might be in a byte past the end of the |
| 70 | array. */ |
| 71 | char *ret = (char *) word_ptr + index_last_eq (x1: word, x2: repeated_c); |
| 72 | if (ret >= (char *) s) |
| 73 | return ret; |
| 74 | } |
| 75 | return NULL; |
| 76 | } |
| 77 | #ifndef MEMRCHR |
| 78 | libc_hidden_def (__memrchr) |
| 79 | weak_alias (__memrchr, memrchr) |
| 80 | #endif |
| 81 | |