| 1 | /* Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include "unittest/gunit/skip_trailing.h" |
| 25 | |
| 26 | #include "my_config.h" |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #include "my_byteorder.h" |
| 30 | |
| 31 | namespace skip_trailing_space_unittest { |
| 32 | |
| 33 | /* SPACE_INT is a word that contains only spaces */ |
| 34 | #if SIZEOF_INT == 4 |
| 35 | static const unsigned SPACE_INT = 0x20202020U; |
| 36 | #elif SIZEOF_INT == 8 |
| 37 | static const unsigned SPACE_INT = 0x2020202020202020ULL; |
| 38 | #else |
| 39 | #error define the appropriate constant for a word full of spaces |
| 40 | #endif |
| 41 | |
| 42 | // A copy of the original version of skip_trailing_space |
| 43 | const uchar *skip_trailing_orig(const uchar *ptr, size_t len) { |
| 44 | const uchar *end = ptr + len; |
| 45 | |
| 46 | if (len > 20) { |
| 47 | const uchar *end_words = (const uchar *)(intptr)(((ulonglong)(intptr)end) / |
| 48 | SIZEOF_INT * SIZEOF_INT); |
| 49 | const uchar *start_words = |
| 50 | (const uchar *)(intptr)((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / |
| 51 | SIZEOF_INT * SIZEOF_INT); |
| 52 | |
| 53 | assert(((ulonglong)(intptr)ptr) >= SIZEOF_INT); |
| 54 | if (end_words > ptr) { |
| 55 | while (end > end_words && end[-1] == 0x20) end--; |
| 56 | if (end[-1] == 0x20 && start_words < end_words) |
| 57 | while (end > start_words && |
| 58 | (pointer_cast<const unsigned *>(end))[-1] == SPACE_INT) |
| 59 | end -= SIZEOF_INT; |
| 60 | } |
| 61 | } |
| 62 | while (end > ptr && end[-1] == 0x20) end--; |
| 63 | return (end); |
| 64 | } |
| 65 | |
| 66 | // Read 8 bytes at a time, ignoring alignment. |
| 67 | // We use uint8korr which is fast on all platforms, except sparc. |
| 68 | const uchar *skip_trailing_unalgn(const uchar *ptr, size_t len) { |
| 69 | const uchar *end = ptr + len; |
| 70 | while (end - ptr >= 8) { |
| 71 | if (uint8korr(end - 8) != 0x2020202020202020ULL) break; |
| 72 | end -= 8; |
| 73 | } |
| 74 | while (end > ptr && end[-1] == 0x20) end--; |
| 75 | return (end); |
| 76 | } |
| 77 | |
| 78 | // Same as the original, except we skip a test which is always true. |
| 79 | const uchar *skip_trailing_4byte(const uchar *ptr, size_t len) { |
| 80 | const uchar *end = ptr + len; |
| 81 | |
| 82 | if (len > 20) { |
| 83 | const uchar *end_words = (const uchar *)(intptr)(((ulonglong)(intptr)end) / |
| 84 | SIZEOF_INT * SIZEOF_INT); |
| 85 | const uchar *start_words = |
| 86 | (const uchar *)(intptr)((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / |
| 87 | SIZEOF_INT * SIZEOF_INT); |
| 88 | |
| 89 | assert(end_words > ptr); |
| 90 | { |
| 91 | while (end > end_words && end[-1] == 0x20) end--; |
| 92 | if (end[-1] == 0x20 && start_words < end_words) |
| 93 | while (end > start_words && |
| 94 | (pointer_cast<const unsigned *>(end))[-1] == SPACE_INT) |
| 95 | end -= SIZEOF_INT; |
| 96 | } |
| 97 | } |
| 98 | while (end > ptr && end[-1] == 0x20) end--; |
| 99 | return (end); |
| 100 | } |
| 101 | |
| 102 | // Same as skip_trailing_4byte, except we read 8 bytes at a time (aligned). |
| 103 | const uchar *skip_trailing_8byte(const uchar *ptr, size_t len) { |
| 104 | const uchar *end = ptr + len; |
| 105 | |
| 106 | if (len > 20) { |
| 107 | const uchar *end_words = |
| 108 | (const uchar *)(longlong)(((ulonglong)(longlong)end) / 8 * 8); |
| 109 | const uchar *start_words = |
| 110 | (const uchar *)(longlong)((((ulonglong)(longlong)ptr) + 8 - 1) / 8 * 8); |
| 111 | |
| 112 | assert(end_words > ptr); |
| 113 | { |
| 114 | while (end > end_words && end[-1] == 0x20) end--; |
| 115 | if (end[-1] == 0x20 && start_words < end_words) |
| 116 | while (end > start_words && (pointer_cast<const ulonglong *>( |
| 117 | end))[-1] == 0x2020202020202020ULL) |
| 118 | end -= 8; |
| 119 | } |
| 120 | } |
| 121 | while (end > ptr && end[-1] == 0x20) end--; |
| 122 | return (end); |
| 123 | } |
| 124 | } // namespace skip_trailing_space_unittest |