1 /* Copyright (c) 2011, 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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 #ifndef _my_compare_h
24 #define _my_compare_h
25 
26 #include "myisampack.h"
27 #ifdef	__cplusplus
28 extern "C" {
29 #endif
30 
31 #include "m_ctype.h"                            /* CHARSET_INFO */
32 #include "my_icp.h"                             /* ICP_RESULT */
33 
34 /*
35   There is a hard limit for the maximum number of keys as there are only
36   8 bits in the index file header for the number of keys in a table.
37   This means that 0..255 keys can exist for a table. The idea of
38   HA_MAX_POSSIBLE_KEY is to ensure that one can use myisamchk & tools on
39   a MyISAM table for which one has more keys than MyISAM is normally
40   compiled for. If you don't have this, you will get a core dump when
41   running myisamchk compiled for 128 keys on a table with 255 keys.
42 */
43 
44 #define HA_MAX_POSSIBLE_KEY         255         /* For myisamchk */
45 /*
46   The following defines can be increased if necessary.
47   But beware the dependency of MI_MAX_POSSIBLE_KEY_BUFF and HA_MAX_KEY_LENGTH.
48 */
49 
50 #define HA_MAX_KEY_LENGTH           4000        /* Max length in bytes */
51 #define HA_MAX_KEY_SEG              16          /* Max segments for key */
52 
53 #define HA_MAX_POSSIBLE_KEY_BUFF    (HA_MAX_KEY_LENGTH + 24+ 6+6)
54 #define HA_MAX_KEY_BUFF  (HA_MAX_KEY_LENGTH+HA_MAX_KEY_SEG*6+8+8)
55 
56 typedef struct st_HA_KEYSEG		/* Key-portion */
57 {
58   const CHARSET_INFO *charset;
59   uint32 start;				/* Start of key in record */
60   uint32 null_pos;			/* position to NULL indicator */
61   uint16 bit_pos;                       /* Position to bit part */
62   uint16 flag;
63   uint16 length;			/* Keylength */
64   uint16 language;
65   uint8  type;				/* Type of key (for sort) */
66   uint8  null_bit;			/* bitmask to test for NULL */
67   uint8  bit_start,bit_end;		/* if bit field */
68   uint8  bit_length;                    /* Length of bit part */
69 } HA_KEYSEG;
70 
71 #define get_key_length(length,key) \
72 { if (*(uchar*) (key) != 255) \
73     length= (uint) *(uchar*) ((key)++); \
74   else \
75   { length= mi_uint2korr((key)+1); (key)+=3; } \
76 }
77 
78 #define get_key_length_rdonly(length,key) \
79 { if (*(uchar*) (key) != 255) \
80     length= ((uint) *(uchar*) ((key))); \
81   else \
82   { length= mi_uint2korr((key)+1); } \
83 }
84 
85 #define get_key_pack_length(length,length_pack,key) \
86 { if (*(uchar*) (key) != 255) \
87   { length= (uint) *(uchar*) ((key)++); length_pack= 1; }\
88   else \
89   { length=mi_uint2korr((key)+1); (key)+= 3; length_pack= 3; } \
90 }
91 
92 #define store_key_length_inc(key,length) \
93 { if ((length) < 255) \
94   { *(key)++= (length); } \
95   else \
96   { *(key)=255; mi_int2store((key)+1,(length)); (key)+=3; } \
97 }
98 
99 #define size_to_store_key_length(length) ((length) < 255 ? 1 : 3)
100 
101 #define get_rec_bits(bit_ptr, bit_ofs, bit_len) \
102   (((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \
103    ((1 << (bit_len)) - 1))
104 
105 #define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \
106 { \
107   (bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \
108                 ((bits) << (bit_ofs)); \
109   if ((bit_ofs) + (bit_len) > 8) \
110     (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \
111                   ((bits) >> (8 - (bit_ofs))); \
112 }
113 
114 #define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \
115   set_rec_bits(0, bit_ptr, bit_ofs, bit_len)
116 
117 extern int ha_compare_text(const CHARSET_INFO *, uchar *, uint, uchar *, uint ,
118 			   my_bool, my_bool);
119 extern int ha_key_cmp(HA_KEYSEG *keyseg, uchar *a,
120 		      uchar *b, uint key_length, uint nextflag,
121 		      uint *diff_pos);
122 
123 /*
124   Inside an in-memory data record, memory pointers to pieces of the
125   record (like BLOBs) are stored in their native byte order and in
126   this amount of bytes.
127 */
128 #define portable_sizeof_char_ptr 8
129 
130 #ifdef	__cplusplus
131 }
132 #endif
133 
134 #endif /* _my_compare_h */
135