1
/* Copyright (c) 2007 SNAP Innovation GmbH
3
* BLOB Streaming for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program 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
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Paul McCullagh (H&G2JCtL)
24
* Common definitions that may be required be included at the
25
* top of every header file.
32
#include <sys/types.h>
35
#define u_llong unsigned long long
38
/* This value is at least 32 bits long: */
40
#define u_int unsigned long
43
/* This value is at least 32 bits long: */
47
* A unsigned integer at least 32 bits long.
48
* The size used is whatever is most
49
* convenient to the machine.
52
#define u_int unsigned int
55
* A signed integer at least 32 bits long.
56
* The size used is whatever is most
57
* convenient to the machine.
62
* An unsigned integer 2 bytes long.
65
#define u_short unsigned short
69
* An unsigned integer, 1 byte long:
72
#define u_char unsigned char
76
* An usigned integer, 1 byte long:
78
#define s_char unsigned char
80
/* Forward declartion of a thread: */
85
#define CS_DEFAULT_EOL "\r\n"
86
#define CS_DIR_CHAR '\\'
87
#define CS_DIR_DELIM "\\"
88
#define IS_DIR_CHAR(ch) ((ch) == CS_DIR_CHAR || (ch) == '/')
92
#define CS_DEFAULT_EOL "\n"
93
#define CS_DIR_CHAR '/'
94
#define CS_DIR_DELIM "/"
95
#define IS_DIR_CHAR(ch) ((ch) == CS_DIR_CHAR)
99
#define CS_CALL_STACK_SIZE 100
100
#define CS_RELEASE_STACK_SIZE 200
101
#define CS_JUMP_STACK_SIZE 20
103
/* Fixed length types */
107
#define csInt1 __int8
108
#define csInt2 __int16
109
#define csInt4 __int32
110
#define csInt8 __int64
112
#define csWord1 unsigned __int8
113
#define csWord2 unsigned __int16
114
#define csWord4 unsigned __int32
115
#define csWord8 unsigned __int64
118
#define PATH_MAX MAX_PATH
121
#define NAME_MAX MAX_PATH
124
/* XT actually assumes that off_t is 8 bytes: */
125
#define off_t csWord8
129
#define csInt1 int8_t
130
#define csInt2 int16_t
131
#define csInt4 int32_t
132
#define csInt8 int64_t
134
#define csWord1 u_int8_t
135
#define csWord2 u_int16_t
136
#define csWord4 u_int32_t
137
#define csWord8 u_int64_t
141
/* A pointer sized word value: */
142
#define csWordPS ptrdiff_t
144
#define CS_MAX_INT_1 ((csInt1) 0x7F)
145
#define CS_MIN_INT_1 ((csInt1) 0x80)
146
#define CS_MAX_INT_2 ((csInt2) 0x7FFF)
147
#define CS_MIN_INT_2 ((csInt2) 0x8000)
148
#define CS_MAX_INT_4 ((csInt4) 0x7FFFFFFF)
149
#define CS_MIN_INT_4 ((csInt4) 0x80000000)
151
#define csReal4 float
152
#define csReal8 double
154
typedef csWord1 CSDiskValue1[1];
155
typedef csWord1 CSDiskValue2[2];
156
typedef csWord1 CSDiskValue3[3];
157
typedef csWord1 CSDiskValue4[4];
158
typedef csWord1 CSDiskValue6[6];
159
typedef csWord1 CSDiskValue8[8];
162
* Byte order on the disk is little endian! This is the byte order of the i386.
163
* Little endian byte order starts with the least significan byte.
165
* The reason for choosing this byte order for the disk is 2-fold:
166
* Firstly the i386 is the cheapest and fasted platform today.
167
* Secondly the i386, unlike RISK chips (with big endian) can address
168
* memory that is not aligned!
170
* Since the disk image of PrimeBase XT is not aligned, the second point
171
* is significant. A RISK chip needs to access it byte-wise, so we might as
172
* well do the byte swapping at the same time.
174
* The macros below are of 4 general types:
176
* GET/SET - Get and set 1,2,4,8 byte values (short, int, long, etc).
177
* Values are swapped only on big endian platforms. This makes these
178
* functions very efficient on little-endian platforms.
180
* COPY - Transfer data without swapping regardless of platform. This
181
* function is a bit more efficient on little-endian platforms
182
* because alignment is not an issue.
184
* MOVE - Similar to get and set, but the deals with memory instead
185
* of values. Since no swapping is done on little-endian platforms
186
* this function is identical to COPY on little-endian platforms.
188
* SWAP - Transfer and swap data regardless of the platform type.
189
* Aligment is not assumed.
191
* The DISK component of the macro names indicates that alignment of
192
* the value cannot be assumed.
195
#if BYTE_ORDER == BIG_ENDIAN
196
/* The native order of the machine is big endian. Since the native disk
197
* disk order of XT is little endian, all data to and from disk
200
#define CS_SET_DISK_1(d, s) ((d)[0] = (csWord1) (s))
202
#define CS_SET_DISK_2(d, s) do { (d)[0] = (csWord1) (((csWord2) (s)) & 0xFF); (d)[1] = (csWord1) ((((csWord2) (s)) >> 8 ) & 0xFF); } while (0)
204
#define CS_SET_DISK_3(d, s) do { (d)[0] = (csWord1) (((csWord4) (s)) & 0xFF); (d)[1] = (csWord1) ((((csWord4) (s)) >> 8 ) & 0xFF); \
205
(d)[2] = (csWord1) ((((csWord4) (s)) >> 16) & 0xFF); } while (0)
207
#define CS_SET_DISK_4(d, s) do { (d)[0] = (csWord1) (((csWord4) (s)) & 0xFF); (d)[1] = (csWord1) ((((csWord4) (s)) >> 8 ) & 0xFF); \
208
(d)[2] = (csWord1) ((((csWord4) (s)) >> 16) & 0xFF); (d)[3] = (csWord1) ((((csWord4) (s)) >> 24) & 0xFF); } while (0)
210
#define CS_SET_DISK_6(d, s) do { (d)[0] = (csWord1) (((csWord8) (s)) & 0xFF); (d)[1] = (csWord1) ((((csWord8) (s)) >> 8 ) & 0xFF); \
211
(d)[2] = (csWord1) ((((csWord8) (s)) >> 16) & 0xFF); (d)[3] = (csWord1) ((((csWord8) (s)) >> 24) & 0xFF); \
212
(d)[4] = (csWord1) ((((csWord8) (s)) >> 32) & 0xFF); (d)[5] = (csWord1) ((((csWord8) (s)) >> 40) & 0xFF); } while (0)
214
#define CS_SET_DISK_8(d, s) do { (d)[0] = (csWord1) (((csWord8) (s)) & 0xFF); (d)[1] = (csWord1) ((((csWord8) (s)) >> 8 ) & 0xFF); \
215
(d)[2] = (csWord1) ((((csWord8) (s)) >> 16) & 0xFF); (d)[3] = (csWord1) ((((csWord8) (s)) >> 24) & 0xFF); \
216
(d)[4] = (csWord1) ((((csWord8) (s)) >> 32) & 0xFF); (d)[5] = (csWord1) ((((csWord8) (s)) >> 40) & 0xFF); \
217
(d)[6] = (csWord1) ((((csWord8) (s)) >> 48) & 0xFF); (d)[7] = (csWord1) ((((csWord8) (s)) >> 56) & 0xFF); } while (0)
219
#define CS_GET_DISK_1(s) ((s)[0])
221
#define CS_GET_DISK_2(s) ((csWord2) (((csWord2) (s)[0]) | (((csWord2) (s)[1]) << 8)))
223
#define CS_GET_DISK_3(s) ((csWord4) (((csWord4) (s)[0]) | (((csWord4) (s)[1]) << 8) | (((csWord4) (s)[2]) << 16)))
225
#define CS_GET_DISK_4(s) (((csWord4) (s)[0]) | (((csWord4) (s)[1]) << 8 ) | \
226
(((csWord4) (s)[2]) << 16) | (((csWord4) (s)[3]) << 24))
228
#define CS_GET_DISK_6(s) (((csWord8) (s)[0]) | (((csWord8) (s)[1]) << 8 ) | \
229
(((csWord8) (s)[2]) << 16) | (((csWord8) (s)[3]) << 24) | \
230
(((csWord8) (s)[4]) << 32) | (((csWord8) (s)[5]) << 40))
232
#define CS_GET_DISK_8(s) (((csWord8) (s)[0]) | (((csWord8) (s)[1]) << 8 ) | \
233
(((csWord8) (s)[2]) << 16) | (((csWord8) (s)[3]) << 24) | \
234
(((csWord8) (s)[4]) << 32) | (((csWord8) (s)[5]) << 40) | \
235
(((csWord8) (s)[6]) << 48) | (((csWord8) (s)[7]) << 56))
237
/* Move will copy memory, and swap the bytes on a big endian machine.
238
* On a little endian machine it is the same as COPY.
240
#define CS_MOVE_DISK_1(d, s) ((d)[0] = (s)[0])
241
#define CS_MOVE_DISK_2(d, s) do { (d)[0] = (s)[1]; (d)[1] = (s)[0]; } while (0)
242
#define CS_MOVE_DISK_3(d, s) do { (d)[0] = (s)[2]; (d)[1] = (s)[1]; (d)[2] = (s)[0]; } while (0)
243
#define CS_MOVE_DISK_4(d, s) do { (d)[0] = (s)[3]; (d)[1] = (s)[2]; (d)[2] = (s)[1]; (d)[3] = (s)[0]; } while (0)
244
#define CS_MOVE_DISK_8(d, s) do { (d)[0] = (s)[7]; (d)[1] = (s)[6]; \
245
(d)[2] = (s)[5]; (d)[3] = (s)[4]; \
246
(d)[4] = (s)[3]; (d)[5] = (s)[2]; \
247
(d)[6] = (s)[1]; (d)[7] = (s)[0]; } while (0)
250
* Copy just copies the number of bytes assuming the data is not alligned.
252
#define CS_COPY_DISK_1(d, s) (d)[0] = s
253
#define CS_COPY_DISK_2(d, s) do { (d)[0] = (s)[0]; (d)[1] = (s)[1]; } while (0)
254
#define CS_COPY_DISK_3(d, s) do { (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; } while (0)
255
#define CS_COPY_DISK_4(d, s) do { (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3]; } while (0)
256
#define CS_COPY_DISK_6(d, s) memcpy(&((d)[0]), &((s)[0]), 6)
257
#define CS_COPY_DISK_8(d, s) memcpy(&((d)[0]), &((s)[0]), 8)
258
#define CS_COPY_DISK_10(d, s) memcpy(&((d)[0]), &((s)[0]), 10)
260
#define CS_SET_NULL_DISK_1(d) CS_SET_DISK_1(d, 0)
261
#define CS_SET_NULL_DISK_2(d) do { (d)[0] = 0; (d)[1] = 0; } while (0)
262
#define CS_SET_NULL_DISK_4(d) do { (d)[0] = 0; (d)[1] = 0; (d)[2] = 0; (d)[3] = 0; } while (0)
263
#define CS_SET_NULL_DISK_6(d) do { (d)[0] = 0; (d)[1] = 0; (d)[2] = 0; (d)[3] = 0; (d)[4] = 0; (d)[5] = 0; } while (0)
264
#define CS_SET_NULL_DISK_8(d) do { (d)[0] = 0; (d)[1] = 0; (d)[2] = 0; (d)[3] = 0; (d)[4] = 0; (d)[5] = 0; (d)[6] = 0; (d)[7] = 0; } while (0)
266
#define CS_IS_NULL_DISK_1(d) (!(CS_GET_DISK_1(d)))
267
#define CS_IS_NULL_DISK_4(d) (!(d)[0] && !(d)[1] && !(d)[2] && !(d)[3])
268
#define CS_IS_NULL_DISK_8(d) (!(d)[0] && !(d)[1] && !(d)[2] && !(d)[3] && !(d)[4] && !(d)[5] && !(d)[6] && !(7)[3])
270
#define CS_EQ_DISK_4(d, s) ((d)[0] == (s)[0] && (d)[1] == (s)[1] && (d)[2] == (s)[2] && (d)[3] == (s)[3])
271
#define CS_EQ_DISK_8(d, s) ((d)[0] == (s)[0] && (d)[1] == (s)[1] && (d)[2] == (s)[2] && (d)[3] == (s)[3] && \
272
(d)[4] == (s)[4] && (d)[5] == (s)[5] && (d)[6] == (s)[6] && (d)[7] == (s)[7])
274
#define CS_IS_FF_DISK_4(d) ((d)[0] == 0xFF && (d)[1] == 0xFF && (d)[2] == 0xFF && (d)[3] == 0xFF)
277
* The native order of the machine is little endian. This means the data to
278
* and from disk need not be swapped. In addition to this, since
279
* the i386 can access non-aligned memory we are not required to
280
* handle the data byte-for-byte.
282
#define CS_SET_DISK_1(d, s) ((d)[0] = (csWord1) (s))
283
#define CS_SET_DISK_2(d, s) (*((csWord2 *) &((d)[0])) = (csWord2) (s))
284
#define CS_SET_DISK_3(d, s) do { (*((csWord2 *) &((d)[0])) = (csWord2) (s)); *((csWord1 *) &((d)[2])) = (csWord1) (((csWord4) (s)) >> 16); } while (0)
285
#define CS_SET_DISK_4(d, s) (*((csWord4 *) &((d)[0])) = (csWord4) (s))
286
#define CS_SET_DISK_6(d, s) do { *((csWord4 *) &((d)[0])) = (csWord4) (s); *((csWord2 *) &((d)[4])) = (csWord2) (((csWord8) (s)) >> 32); } while (0)
287
#define CS_SET_DISK_8(d, s) (*((csWord8 *) &((d)[0])) = (csWord8) (s))
289
#define CS_GET_DISK_1(s) ((s)[0])
290
#define CS_GET_DISK_2(s) *((csWord2 *) &((s)[0]))
291
#define CS_GET_DISK_3(s) ((csWord4) *((csWord2 *) &((s)[0])) | (((csWord4) *((csWord1 *) &((s)[2]))) << 16))
292
#define CS_GET_DISK_4(s) *((csWord4 *) &((s)[0]))
293
#define CS_GET_DISK_6(s) ((csWord8) *((csWord4 *) &((s)[0])) | (((csWord8) *((csWord2 *) &((s)[4]))) << 32))
294
#define CS_GET_DISK_8(s) *((csWord8 *) &((s)[0]))
296
#define CS_MOVE_DISK_1(d, s) ((d)[0] = (s)[0])
297
#define CS_MOVE_DISK_2(d, s) CS_COPY_DISK_2(d, s)
298
#define CS_MOVE_DISK_3(d, s) CS_COPY_DISK_3(d, s)
299
#define CS_MOVE_DISK_4(d, s) CS_COPY_DISK_4(d, s)
300
#define CS_MOVE_DISK_8(d, s) CS_COPY_DISK_8(d, s)
302
#define CS_COPY_DISK_1(d, s) (d)[0] = s
303
#define CS_COPY_DISK_2(d, s) (*((csWord2 *) &((d)[0])) = (*((csWord2 *) &((s)[0]))))
304
#define CS_COPY_DISK_3(d, s) do { *((csWord2 *) &((d)[0])) = *((csWord2 *) &((s)[0])); (d)[2] = (s)[2]; } while (0)
305
#define CS_COPY_DISK_4(d, s) (*((csWord4 *) &((d)[0])) = (*((csWord4 *) &((s)[0]))))
306
#define CS_COPY_DISK_6(d, s) do { *((csWord4 *) &((d)[0])) = *((csWord4 *) &((s)[0])); *((csWord2 *) &((d)[4])) = *((csWord2 *) &((s)[4])); } while (0)
307
#define CS_COPY_DISK_8(d, s) (*((csWord8 *) &(d[0])) = (*((csWord8 *) &((s)[0]))))
308
#define CS_COPY_DISK_10(d, s) memcpy(&((d)[0]), &((s)[0]), 10)
310
#define CS_SET_NULL_DISK_1(d) CS_SET_DISK_1(d, 0)
311
#define CS_SET_NULL_DISK_2(d) CS_SET_DISK_2(d, 0)
312
#define CS_SET_NULL_DISK_3(d) CS_SET_DISK_3(d, 0)
313
#define CS_SET_NULL_DISK_4(d) CS_SET_DISK_4(d, 0L)
314
#define CS_SET_NULL_DISK_6(d) CS_SET_DISK_6(d, 0LL)
315
#define CS_SET_NULL_DISK_8(d) CS_SET_DISK_8(d, 0LL)
317
#define CS_IS_NULL_DISK_1(d) (!(CS_GET_DISK_1(d)))
318
#define CS_IS_NULL_DISK_2(d) (!(CS_GET_DISK_2(d)))
319
#define CS_IS_NULL_DISK_3(d) (!(CS_GET_DISK_3(d)))
320
#define CS_IS_NULL_DISK_4(d) (!(CS_GET_DISK_4(d)))
321
#define CS_IS_NULL_DISK_8(d) (!(CS_GET_DISK_8(d)))
323
#define CS_EQ_DISK_4(d, s) (CS_GET_DISK_4(d) == CS_GET_DISK_4(s))
324
#define CS_EQ_DISK_8(d, s) (CS_GET_DISK_8(d) == CS_GET_DISK_8(s))
326
#define CS_IS_FF_DISK_4(d) (CS_GET_DISK_4(d) == 0xFFFFFFFF)
329
#define CS_CMP_DISK_4(a, b) ((csInt4) CS_GET_DISK_4(a) - (csInt4) CS_GET_DISK_4(b))
330
#define CS_CMP_DISK_8(d, s) memcmp(&((d)[0]), &((s)[0]), 8)
331
//#define CS_CMP_DISK_8(d, s) (CS_CMP_DISK_4((d).h_number_4, (s).h_number_4) == 0 ? CS_CMP_DISK_4((d).h_file_4, (s).h_file_4) : CS_CMP_DISK_4((d).h_number_4, (s).h_number_4))
333
#define CS_SWAP_DISK_2(d, s) do { (d)[0] = (s)[1]; (d)[1] = (s)[0]; } while (0)
334
#define CS_SWAP_DISK_3(d, s) do { (d)[0] = (s)[2]; (d)[1] = (s)[1]; (d)[2] = (s)[0]; } while (0)
335
#define CS_SWAP_DISK_4(d, s) do { (d)[0] = (s)[3]; (d)[1] = (s)[2]; (d)[2] = (s)[1]; (d)[3] = (s)[0]; } while (0)
336
#define CS_SWAP_DISK_8(d, s) do { (d)[0] = (s)[7]; (d)[1] = (s)[6]; (d)[2] = (s)[5]; (d)[3] = (s)[4]; \
337
(d)[4] = (s)[3]; (d)[5] = (s)[2]; (d)[6] = (s)[1]; (d)[7] = (s)[0]; } while (0)