[444] | 1 | /* |
---|
| 2 | FUNCTION |
---|
| 3 | <<memrchr>>---reverse search for character in memory |
---|
| 4 | |
---|
| 5 | INDEX |
---|
| 6 | memrchr |
---|
| 7 | |
---|
| 8 | SYNOPSIS |
---|
| 9 | #include <string.h> |
---|
| 10 | void *memrchr(const void *<[src]>, int <[c]>, size_t <[length]>); |
---|
| 11 | |
---|
| 12 | DESCRIPTION |
---|
| 13 | This function searches memory starting at <[length]> bytes |
---|
| 14 | beyond <<*<[src]>>> backwards for the character <[c]>. |
---|
| 15 | The search only ends with the first occurrence of <[c]>; in |
---|
| 16 | particular, <<NUL>> does not terminate the search. |
---|
| 17 | |
---|
| 18 | RETURNS |
---|
| 19 | If the character <[c]> is found within <[length]> characters |
---|
| 20 | of <<*<[src]>>>, a pointer to the character is returned. If |
---|
| 21 | <[c]> is not found, then <<NULL>> is returned. |
---|
| 22 | |
---|
| 23 | PORTABILITY |
---|
| 24 | <<memrchr>> is a GNU extension. |
---|
| 25 | |
---|
| 26 | <<memrchr>> requires no supporting OS subroutines. |
---|
| 27 | |
---|
| 28 | QUICKREF |
---|
| 29 | memrchr |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | #include <_ansi.h> |
---|
| 33 | #include <string.h> |
---|
| 34 | #include <limits.h> |
---|
| 35 | |
---|
| 36 | /* Nonzero if X is not aligned on a "long" boundary. */ |
---|
| 37 | #define UNALIGNED(X) ((long)(X + 1) & (sizeof (long) - 1)) |
---|
| 38 | |
---|
| 39 | /* How many bytes are loaded each iteration of the word copy loop. */ |
---|
| 40 | #define LBLOCKSIZE (sizeof (long)) |
---|
| 41 | |
---|
| 42 | /* Threshhold for punting to the bytewise iterator. */ |
---|
| 43 | #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE) |
---|
| 44 | |
---|
| 45 | #if LONG_MAX == 2147483647L |
---|
| 46 | #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) |
---|
| 47 | #else |
---|
| 48 | #if LONG_MAX == 9223372036854775807L |
---|
| 49 | /* Nonzero if X (a long int) contains a NULL byte. */ |
---|
| 50 | #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) |
---|
| 51 | #else |
---|
| 52 | #error long int is not a 32bit or 64bit type. |
---|
| 53 | #endif |
---|
| 54 | #endif |
---|
| 55 | |
---|
| 56 | #ifndef DETECTNULL |
---|
| 57 | #error long int is not a 32bit or 64bit byte |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | /* DETECTCHAR returns nonzero if (long)X contains the byte used |
---|
| 61 | to fill (long)MASK. */ |
---|
| 62 | #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK)) |
---|
| 63 | |
---|
| 64 | void * |
---|
| 65 | memrchr (const void *src_void, |
---|
| 66 | int c, |
---|
| 67 | size_t length) |
---|
| 68 | { |
---|
| 69 | const unsigned char *src = (const unsigned char *) src_void + length - 1; |
---|
| 70 | unsigned char d = c; |
---|
| 71 | |
---|
| 72 | #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) |
---|
| 73 | unsigned long *asrc; |
---|
| 74 | unsigned long mask; |
---|
| 75 | unsigned int i; |
---|
| 76 | |
---|
| 77 | while (UNALIGNED (src)) |
---|
| 78 | { |
---|
| 79 | if (!length--) |
---|
| 80 | return NULL; |
---|
| 81 | if (*src == d) |
---|
| 82 | return (void *) src; |
---|
| 83 | src--; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (!TOO_SMALL (length)) |
---|
| 87 | { |
---|
| 88 | /* If we get this far, we know that length is large and src is |
---|
| 89 | word-aligned. */ |
---|
| 90 | /* The fast code reads the source one word at a time and only |
---|
| 91 | performs the bytewise search on word-sized segments if they |
---|
| 92 | contain the search character, which is detected by XORing |
---|
| 93 | the word-sized segment with a word-sized block of the search |
---|
| 94 | character and then detecting for the presence of NUL in the |
---|
| 95 | result. */ |
---|
| 96 | asrc = (unsigned long *) (src - LBLOCKSIZE + 1); |
---|
| 97 | mask = d << 8 | d; |
---|
| 98 | mask = mask << 16 | mask; |
---|
| 99 | for (i = 32; i < LBLOCKSIZE * 8; i <<= 1) |
---|
| 100 | mask = (mask << i) | mask; |
---|
| 101 | |
---|
| 102 | while (length >= LBLOCKSIZE) |
---|
| 103 | { |
---|
| 104 | if (DETECTCHAR (*asrc, mask)) |
---|
| 105 | break; |
---|
| 106 | length -= LBLOCKSIZE; |
---|
| 107 | asrc--; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /* If there are fewer than LBLOCKSIZE characters left, |
---|
| 111 | then we resort to the bytewise loop. */ |
---|
| 112 | |
---|
| 113 | src = (unsigned char *) asrc + LBLOCKSIZE - 1; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
---|
| 117 | |
---|
| 118 | while (length--) |
---|
| 119 | { |
---|
| 120 | if (*src == d) |
---|
| 121 | return (void *) src; |
---|
| 122 | src--; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | return NULL; |
---|
| 126 | } |
---|