~ubuntu-branches/ubuntu/vivid/mariadb-5.5/vivid

« back to all changes in this revision

Viewing changes to storage/tokudb/ft-index/tools/tokudb_common_funcs.h

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2014-11-14 21:04:24 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20141114210424-xlyna0ozl11647o5
Tags: 5.5.40-0ubuntu0.14.10.1
* SECURITY UPDATE: Update to 5.5.40 to fix security issues (LP: #1391676)
  - CVE-2014-6507
  - CVE-2014-6491
  - CVE-2014-6500
  - CVE-2014-6469
  - CVE-2014-6555
  - CVE-2014-6559
  - CVE-2014-6494
  - CVE-2014-6496
  - CVE-2014-6464
* Add bsdutils as mariadb-server dependency like upstream does in 5.5.40.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
 
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3
 
#ident "$Id$"
4
 
#if !defined(TOKUDB_COMMON_FUNCS_H)
5
 
#define TOKUDB_COMMON_FUNCS_H
6
 
 
7
 
/*
8
 
COPYING CONDITIONS NOTICE:
9
 
 
10
 
  This program is free software; you can redistribute it and/or modify
11
 
  it under the terms of version 2 of the GNU General Public License as
12
 
  published by the Free Software Foundation, and provided that the
13
 
  following conditions are met:
14
 
 
15
 
      * Redistributions of source code must retain this COPYING
16
 
        CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
17
 
        DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
18
 
        PATENT MARKING NOTICE (below), and the PATENT RIGHTS
19
 
        GRANT (below).
20
 
 
21
 
      * Redistributions in binary form must reproduce this COPYING
22
 
        CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
23
 
        DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
24
 
        PATENT MARKING NOTICE (below), and the PATENT RIGHTS
25
 
        GRANT (below) in the documentation and/or other materials
26
 
        provided with the distribution.
27
 
 
28
 
  You should have received a copy of the GNU General Public License
29
 
  along with this program; if not, write to the Free Software
30
 
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31
 
  02110-1301, USA.
32
 
 
33
 
COPYRIGHT NOTICE:
34
 
 
35
 
  TokuDB, Tokutek Fractal Tree Indexing Library.
36
 
  Copyright (C) 2007-2013 Tokutek, Inc.
37
 
 
38
 
DISCLAIMER:
39
 
 
40
 
  This program is distributed in the hope that it will be useful, but
41
 
  WITHOUT ANY WARRANTY; without even the implied warranty of
42
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 
  General Public License for more details.
44
 
 
45
 
UNIVERSITY PATENT NOTICE:
46
 
 
47
 
  The technology is licensed by the Massachusetts Institute of
48
 
  Technology, Rutgers State University of New Jersey, and the Research
49
 
  Foundation of State University of New York at Stony Brook under
50
 
  United States of America Serial No. 11/760379 and to the patents
51
 
  and/or patent applications resulting from it.
52
 
 
53
 
PATENT MARKING NOTICE:
54
 
 
55
 
  This software is covered by US Patent No. 8,185,551.
56
 
  This software is covered by US Patent No. 8,489,638.
57
 
 
58
 
PATENT RIGHTS GRANT:
59
 
 
60
 
  "THIS IMPLEMENTATION" means the copyrightable works distributed by
61
 
  Tokutek as part of the Fractal Tree project.
62
 
 
63
 
  "PATENT CLAIMS" means the claims of patents that are owned or
64
 
  licensable by Tokutek, both currently or in the future; and that in
65
 
  the absence of this license would be infringed by THIS
66
 
  IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
67
 
 
68
 
  "PATENT CHALLENGE" shall mean a challenge to the validity,
69
 
  patentability, enforceability and/or non-infringement of any of the
70
 
  PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
71
 
 
72
 
  Tokutek hereby grants to you, for the term and geographical scope of
73
 
  the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
74
 
  irrevocable (except as stated in this section) patent license to
75
 
  make, have made, use, offer to sell, sell, import, transfer, and
76
 
  otherwise run, modify, and propagate the contents of THIS
77
 
  IMPLEMENTATION, where such license applies only to the PATENT
78
 
  CLAIMS.  This grant does not include claims that would be infringed
79
 
  only as a consequence of further modifications of THIS
80
 
  IMPLEMENTATION.  If you or your agent or licensee institute or order
81
 
  or agree to the institution of patent litigation against any entity
82
 
  (including a cross-claim or counterclaim in a lawsuit) alleging that
83
 
  THIS IMPLEMENTATION constitutes direct or contributory patent
84
 
  infringement, or inducement of patent infringement, then any rights
85
 
  granted to you under this License shall terminate as of the date
86
 
  such litigation is filed.  If you or your agent or exclusive
87
 
  licensee institute or order or agree to the institution of a PATENT
88
 
  CHALLENGE, then Tokutek may terminate any rights granted to you
89
 
  under this License.
90
 
*/
91
 
 
92
 
#ident "Copyright (c) 2007-2013 Tokutek Inc.  All rights reserved."
93
 
 
94
 
#include "tokudb_common.h"
95
 
 
96
 
//DB_ENV->err disabled since it does not use db_strerror
97
 
#define PRINT_ERROR(retval, ...)                                  \
98
 
do {                                                              \
99
 
if (0) g.dbenv->err(g.dbenv, retval, __VA_ARGS__);                \
100
 
else {                                                            \
101
 
   fprintf(stderr, "\tIn %s:%d %s()\n", __FILE__, __LINE__, __FUNCTION__); \
102
 
   fprintf(stderr, "%s: %s:", g.progname, db_strerror(retval));   \
103
 
   fprintf(stderr, __VA_ARGS__);                                  \
104
 
   fprintf(stderr, "\n");                                         \
105
 
   fflush(stderr);                                                \
106
 
}                                                                 \
107
 
} while (0)
108
 
 
109
 
//DB_ENV->err disabled since it does not use db_strerror, errx does not exist.
110
 
#define PRINT_ERRORX(...)                                               \
111
 
do {                                                              \
112
 
if (0) g.dbenv->err(g.dbenv, 0, __VA_ARGS__);                     \
113
 
else {                                                            \
114
 
   fprintf(stderr, "\tIn %s:%d %s()\n", __FILE__, __LINE__, __FUNCTION__); \
115
 
   fprintf(stderr, "%s: ", g.progname);                           \
116
 
   fprintf(stderr, __VA_ARGS__);                                  \
117
 
   fprintf(stderr, "\n");                                         \
118
 
   fflush(stderr);                                                \
119
 
}                                                                 \
120
 
} while (0)
121
 
 
122
 
int   strtoint32  (char* str,  int32_t* num,  int32_t min,  int32_t max, int base);
123
 
int   strtouint32 (char* str, uint32_t* num, uint32_t min, uint32_t max, int base);
124
 
int   strtoint64  (char* str,  int64_t* num,  int64_t min,  int64_t max, int base);
125
 
int   strtouint64 (char* str, uint64_t* num, uint64_t min, uint64_t max, int base);
126
 
 
127
 
/*
128
 
 * Convert a string to an integer of type "type".
129
 
 *
130
 
 *
131
 
 * Sets errno and returns:
132
 
 *    EINVAL: str == NULL, num == NULL, or string not of the form [ \t]*[+-]?[0-9]+
133
 
 *    ERANGE: value out of range specified. (Range of [min, max])
134
 
 *
135
 
 * *num is unchanged on error.
136
 
 * Returns:
137
 
 *
138
 
 */
139
 
#define DEF_STR_TO(name, type, bigtype, strtofunc, frmt)       \
140
 
int name(char* str, type* num, type min, type max, int base)   \
141
 
{                                                              \
142
 
   char* test;                                                 \
143
 
   bigtype value;                                              \
144
 
                                                               \
145
 
   assert(str);                                                \
146
 
   assert(num);                                                \
147
 
   assert(min <= max);                                         \
148
 
   assert(g.dbenv || g.progname);                              \
149
 
   assert(base == 0 || (base >= 2 && base <= 36));             \
150
 
                                                               \
151
 
   errno = 0;                                                  \
152
 
   while (isspace(*str)) str++;                                \
153
 
   value = strtofunc(str, &test, base);                        \
154
 
   if ((*test != '\0' && *test != '\n') || test == str) {      \
155
 
      PRINT_ERRORX("%s: Invalid numeric argument\n", str);           \
156
 
      errno = EINVAL;                                          \
157
 
      goto error;                                              \
158
 
   }                                                           \
159
 
   if (errno != 0) {                                           \
160
 
      PRINT_ERROR(errno, "%s\n", str);                               \
161
 
   }                                                           \
162
 
   if (value < min) {                                          \
163
 
      PRINT_ERRORX("%s: Less than minimum value (%" frmt ")\n", str, min); \
164
 
      goto error;                                              \
165
 
   }                                                           \
166
 
   if (value > max) {                                          \
167
 
      PRINT_ERRORX("%s: Greater than maximum value (%" frmt ")\n", str, max); \
168
 
      goto error;                                              \
169
 
   }                                                           \
170
 
   *num = value;                                               \
171
 
   return EXIT_SUCCESS;                                        \
172
 
error:                                                         \
173
 
   return errno;                                               \
174
 
}
175
 
 
176
 
DEF_STR_TO(strtoint32,  int32_t,  int64_t,  strtoll,  PRId32)
177
 
DEF_STR_TO(strtouint32, uint32_t, uint64_t, strtoull, PRIu32)
178
 
DEF_STR_TO(strtoint64,  int64_t,  int64_t,  strtoll,  PRId64)
179
 
DEF_STR_TO(strtouint64, uint64_t, uint64_t, strtoull, PRIu64)
180
 
 
181
 
static inline void
182
 
outputbyte(uint8_t ch)
183
 
{
184
 
   if (g.plaintext) {
185
 
      if (ch == '\\')         printf("\\\\");
186
 
      else if (isprint(ch))   printf("%c", ch);
187
 
      else                    printf("\\%02x", ch);
188
 
   }
189
 
   else printf("%02x", ch);
190
 
}
191
 
 
192
 
static inline void
193
 
outputstring(char* str)
194
 
{
195
 
   char* p;
196
 
 
197
 
   for (p = str; *p != '\0'; p++) {
198
 
      outputbyte((uint8_t)*p);
199
 
   }
200
 
}
201
 
 
202
 
static inline void
203
 
outputplaintextstring(char* str)
204
 
{
205
 
   bool old_plaintext = g.plaintext;
206
 
   g.plaintext = true;
207
 
   outputstring(str);
208
 
   g.plaintext = old_plaintext;
209
 
}
210
 
 
211
 
static inline int
212
 
hextoint(int ch)
213
 
{
214
 
   if (ch >= '0' && ch <= '9') {
215
 
      return ch - '0';
216
 
   }
217
 
   if (ch >= 'a' && ch <= 'z') {
218
 
      return ch - 'a' + 10;
219
 
   }
220
 
   if (ch >= 'A' && ch <= 'Z') {
221
 
      return ch - 'A' + 10;
222
 
   }
223
 
   return EOF;
224
 
}
225
 
 
226
 
static inline int
227
 
printabletocstring(char* inputstr, char** poutputstr)
228
 
{
229
 
   char highch;
230
 
   char lowch;
231
 
   char nextch;
232
 
   char* cstring;
233
 
 
234
 
   assert(inputstr);
235
 
   assert(poutputstr);
236
 
   assert(*poutputstr == NULL);
237
 
 
238
 
   cstring = (char*)toku_malloc((strlen(inputstr) + 1) * sizeof(char));
239
 
   if (cstring == NULL) {
240
 
      PRINT_ERROR(errno, "printabletocstring");
241
 
      goto error;
242
 
   }
243
 
 
244
 
   for (*poutputstr = cstring; *inputstr != '\0'; inputstr++) {
245
 
      if (*inputstr == '\\') {
246
 
         if ((highch = *++inputstr) == '\\') {
247
 
            *cstring++ = '\\';
248
 
            continue;
249
 
         }
250
 
         if (highch == '\0' || (lowch = *++inputstr) == '\0') {
251
 
            PRINT_ERROR(0, "unexpected end of input data or key/data pair");
252
 
            goto error;
253
 
         }
254
 
         if (!isxdigit(highch)) {
255
 
            PRINT_ERROR(0, "Unexpected '%c' (non-hex) input.\n", highch);
256
 
            goto error;
257
 
         }
258
 
         if (!isxdigit(lowch)) {
259
 
            PRINT_ERROR(0, "Unexpected '%c' (non-hex) input.\n", lowch);
260
 
            goto error;
261
 
         }
262
 
         nextch = (char)((hextoint(highch) << 4) | hextoint(lowch));
263
 
         if (nextch == '\0') {
264
 
            /* Database names are c strings, and cannot have extra NULL terminators. */
265
 
            PRINT_ERROR(0, "Unexpected '\\00' in input.\n");
266
 
            goto error;
267
 
         }
268
 
         *cstring++ = nextch;
269
 
      }
270
 
      else *cstring++ = *inputstr;
271
 
   }
272
 
   /* Terminate the string. */
273
 
   *cstring = '\0';
274
 
   return EXIT_SUCCESS;
275
 
 
276
 
error:
277
 
   PRINT_ERROR(0, "Quitting out due to errors.\n");
278
 
   return EXIT_FAILURE;
279
 
}
280
 
 
281
 
static inline int
282
 
verify_library_version(void)
283
 
{
284
 
   int major;
285
 
   int minor;
286
 
   
287
 
   db_version(&major, &minor, NULL);
288
 
   if (major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR) {
289
 
      PRINT_ERRORX("version %d.%d doesn't match library version %d.%d\n",
290
 
             DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
291
 
      return EXIT_FAILURE;
292
 
   }
293
 
   return EXIT_SUCCESS;
294
 
}
295
 
 
296
 
static int last_caught = 0;
297
 
 
298
 
static void catch_signal(int which_signal) {
299
 
    last_caught = which_signal;
300
 
    if (last_caught == 0) last_caught = SIGINT;
301
 
}
302
 
 
303
 
static inline void
304
 
init_catch_signals(void) {
305
 
    signal(SIGINT, catch_signal);
306
 
    signal(SIGTERM, catch_signal);
307
 
#ifdef SIGHUP
308
 
    signal(SIGHUP, catch_signal);
309
 
#endif
310
 
#ifdef SIGPIPE
311
 
    signal(SIGPIPE, catch_signal);
312
 
#endif
313
 
}
314
 
 
315
 
static inline int
316
 
caught_any_signals(void) {
317
 
    return last_caught != 0;
318
 
}
319
 
 
320
 
static inline void
321
 
resend_signals(void) {
322
 
    if (last_caught) {
323
 
        signal(last_caught, SIG_DFL);
324
 
        raise(last_caught);
325
 
    }
326
 
}
327
 
 
328
 
#include <memory.h>
329
 
static int test_main (int argc, char *const argv[]);
330
 
int
331
 
main(int argc, char *const argv[]) {
332
 
    int r;
333
 
    r = test_main(argc, argv);
334
 
    return r;
335
 
}
336
 
 
337
 
#endif /* #if !defined(TOKUDB_COMMON_H) */