1 /* Copyright (c) 2013, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 #include "skip_trailing.h"
24 
25 namespace skip_trailing_space_unittest {
26 
27 /* SPACE_INT is a word that contains only spaces */
28 #if SIZEOF_INT == 4
29 static const unsigned SPACE_INT= 0x20202020U;
30 #elif SIZEOF_INT == 8
31 static const unsigned SPACE_INT= 0x2020202020202020ULL;
32 #else
33 #error define the appropriate constant for a word full of spaces
34 #endif
35 
36 // A copy of the original version of skip_trailing_space
skip_trailing_orig(const uchar * ptr,size_t len)37 const uchar *skip_trailing_orig(const uchar *ptr,size_t len)
38 {
39   const uchar *end= ptr + len;
40 
41   if (len > 20)
42   {
43     const uchar *end_words= (const uchar *)(intptr)
44       (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
45     const uchar *start_words= (const uchar *)(intptr)
46        ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
47 
48     assert(((ulonglong)(intptr)ptr) >= SIZEOF_INT);
49     if (end_words > ptr)
50     {
51       while (end > end_words && end[-1] == 0x20)
52         end--;
53       if (end[-1] == 0x20 && start_words < end_words)
54         while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
55           end -= SIZEOF_INT;
56     }
57   }
58   while (end > ptr && end[-1] == 0x20)
59     end--;
60   return (end);
61 }
62 
63 
64 // Read 8 bytes at a time, ignoring alignment.
65 // We use uint8korr which is fast on all platforms, except sparc.
skip_trailing_unalgn(const uchar * ptr,size_t len)66 const uchar *skip_trailing_unalgn(const uchar *ptr, size_t len)
67 {
68   const uchar *end= ptr + len;
69   while (end - ptr >= 8)
70   {
71     if (uint8korr(end-8) != 0x2020202020202020ULL)
72       break;
73     end-= 8;
74   }
75   while (end > ptr && end[-1] == 0x20)
76     end--;
77   return (end);
78 }
79 
80 
81 // Same as the original, except we skip a test which is always true.
skip_trailing_4byte(const uchar * ptr,size_t len)82 const uchar *skip_trailing_4byte(const uchar *ptr, size_t len)
83 {
84   const uchar *end= ptr + len;
85 
86   if (len > 20)
87   {
88     const uchar *end_words= (const uchar *)(intptr)
89       (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
90     const uchar *start_words= (const uchar *)(intptr)
91       ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
92 
93     assert(end_words > ptr);
94     {
95       while (end > end_words && end[-1] == 0x20)
96         end--;
97       if (end[-1] == 0x20 && start_words < end_words)
98         while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
99           end -= SIZEOF_INT;
100     }
101   }
102   while (end > ptr && end[-1] == 0x20)
103     end--;
104   return (end);
105 }
106 
107 
108 // Same as skip_trailing_4byte, except we read 8 bytes at a time (aligned).
skip_trailing_8byte(const uchar * ptr,size_t len)109 const uchar *skip_trailing_8byte(const uchar *ptr, size_t len)
110 {
111   const uchar *end= ptr + len;
112 
113   if (len > 20)
114   {
115     const uchar *end_words= (const uchar *)(longlong)
116       (((ulonglong)(longlong)end) / 8 * 8);
117     const uchar *start_words= (const uchar *)(longlong)
118        ((((ulonglong)(longlong)ptr) + 8 - 1) / 8 * 8);
119 
120     assert(end_words > ptr);
121     {
122       while (end > end_words && end[-1] == 0x20)
123         end--;
124       if (end[-1] == 0x20 && start_words < end_words)
125         while (end > start_words &&
126                ((ulonglong *)end)[-1] == 0x2020202020202020ULL)
127           end -= 8;
128     }
129   }
130   while (end > ptr && end[-1] == 0x20)
131     end--;
132   return (end);
133 }
134 
135 }
136