~ubuntu-branches/ubuntu/natty/mysql-5.1/natty-proposed

« back to all changes in this revision

Viewing changes to cmd-line-utils/libedit/np/vis.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 08:30:45 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120222083045-2rd53r4bnyx7qus4
Tags: 5.1.61-0ubuntu0.11.04.1
* SECURITY UPDATE: Update to 5.1.61 to fix multiple security issues
  (LP: #937869)
  - http://www.oracle.com/technetwork/topics/security/cpujan2012-366304.html
  - CVE-2011-2262
  - CVE-2012-0075
  - CVE-2012-0112
  - CVE-2012-0113
  - CVE-2012-0114
  - CVE-2012-0115
  - CVE-2012-0116
  - CVE-2012-0117
  - CVE-2012-0118
  - CVE-2012-0119
  - CVE-2012-0120
  - CVE-2012-0484
  - CVE-2012-0485
  - CVE-2012-0486
  - CVE-2012-0487
  - CVE-2012-0488
  - CVE-2012-0489
  - CVE-2012-0490
  - CVE-2012-0491
  - CVE-2012-0492
  - CVE-2012-0493
  - CVE-2012-0494
  - CVE-2012-0495
  - CVE-2012-0496

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      $NetBSD: vis.c,v 1.38 2008/09/04 09:41:44 lukem Exp $   */
 
1
/*      $NetBSD: vis.c,v 1.44 2011/03/12 19:52:48 christos Exp $        */
2
2
 
3
3
/*-
4
4
 * Copyright (c) 1989, 1993
60
60
#if defined(LIBC_SCCS) && !defined(lint)
61
61
#endif /* LIBC_SCCS and not lint */
62
62
 
 
63
/* XXXMYSQL : Make compiler happy. */
 
64
#ifdef _LIBC
 
65
#include "namespace.h"
 
66
#endif
 
67
 
63
68
#include <sys/types.h>
64
69
 
65
70
#include <assert.h>
66
 
#ifdef HAVE_VIS_H
67
 
#include <vis.h>
68
 
#else
 
71
/*
 
72
  XXXMYSQL : Due to different versions of vis.h available,
 
73
             use the one bundled with libedit.
 
74
*/
69
75
#include "np/vis.h"
70
 
#endif
 
76
#include <errno.h>
71
77
#include <stdlib.h>
72
78
 
73
79
#ifdef __weak_alias
74
 
__weak_alias(strsvis,_strsvis)
75
 
__weak_alias(strsvisx,_strsvisx)
76
 
__weak_alias(strvis,_strvis)
77
80
__weak_alias(strvisx,_strvisx)
78
 
__weak_alias(svis,_svis)
79
 
__weak_alias(vis,_vis)
80
81
#endif
81
82
 
82
83
#if !HAVE_VIS || !HAVE_SVIS
85
86
#include <stdio.h>
86
87
#include <string.h>
87
88
 
88
 
static char *do_svis(char *, int, int, int, const char *);
 
89
static char *do_svis(char *, size_t *, int, int, int, const char *);
89
90
 
90
91
#undef BELL
91
92
#define BELL '\a'
94
95
#define iswhite(c)      (c == ' ' || c == '\t' || c == '\n')
95
96
#define issafe(c)       (c == '\b' || c == BELL || c == '\r')
96
97
#define xtoa(c)         "0123456789abcdef"[c]
 
98
#define XTOA(c)         "0123456789ABCDEF"[c]
97
99
 
98
100
#define MAXEXTRAS       5
99
101
 
120
122
 * This is do_hvis, for HTTP style (RFC 1808)
121
123
 */
122
124
static char *
123
 
do_hvis(char *dst, int c, int flag, int nextc, const char *extra)
 
125
do_hvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
124
126
{
125
 
        if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) {
 
127
 
 
128
        if ((isascii(c) && isalnum(c))
 
129
            /* safe */
 
130
            || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
 
131
            /* extra */
 
132
            || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
 
133
            || c == ',') {
 
134
                dst = do_svis(dst, dlen, c, flag, nextc, extra);
 
135
        } else {
 
136
                if (dlen) {
 
137
                        if (*dlen < 3)
 
138
                                return NULL;
 
139
                        *dlen -= 3;
 
140
                }
126
141
                *dst++ = '%';
127
142
                *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
128
143
                *dst++ = xtoa((unsigned int)c & 0xf);
 
144
        }
 
145
 
 
146
        return dst;
 
147
}
 
148
 
 
149
/*
 
150
 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
 
151
 * NB: No handling of long lines or CRLF.
 
152
 */
 
153
static char *
 
154
do_mvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
 
155
{
 
156
        if ((c != '\n') &&
 
157
            /* Space at the end of the line */
 
158
            ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
 
159
            /* Out of range */
 
160
            (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
 
161
            /* Specific char to be escaped */ 
 
162
            strchr("#$@[\\]^`{|}~", c) != NULL)) {
 
163
                if (dlen) {
 
164
                        if (*dlen < 3)
 
165
                                return NULL;
 
166
                        *dlen -= 3;
 
167
                }
 
168
                *dst++ = '=';
 
169
                *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
 
170
                *dst++ = XTOA((unsigned int)c & 0xf);
129
171
        } else {
130
 
                dst = do_svis(dst, c, flag, nextc, extra);
 
172
                dst = do_svis(dst, dlen, c, flag, nextc, extra);
131
173
        }
132
174
        return dst;
133
175
}
142
184
 *            backslash-protected.
143
185
 */
144
186
static char *
145
 
do_svis(char *dst, int c, int flag, int nextc, const char *extra)
 
187
do_svis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
146
188
{
147
189
        int isextra;
 
190
        size_t odlen = dlen ? *dlen : 0;
 
191
 
148
192
        isextra = strchr(extra, c) != NULL;
 
193
#define HAVE(x) \
 
194
        do { \
 
195
                if (dlen) { \
 
196
                        if (*dlen < (x)) \
 
197
                                goto out; \
 
198
                        *dlen -= (x); \
 
199
                } \
 
200
        } while (/*CONSTCOND*/0)
149
201
        if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
150
202
            ((flag & VIS_SAFE) && issafe(c)))) {
 
203
                HAVE(1);
151
204
                *dst++ = c;
152
205
                return dst;
153
206
        }
154
207
        if (flag & VIS_CSTYLE) {
 
208
                HAVE(2);
155
209
                switch (c) {
156
210
                case '\n':
157
211
                        *dst++ = '\\'; *dst++ = 'n';
180
234
                case '\0':
181
235
                        *dst++ = '\\'; *dst++ = '0';
182
236
                        if (isoctal(nextc)) {
 
237
                                HAVE(2);
183
238
                                *dst++ = '0';
184
239
                                *dst++ = '0';
185
240
                        }
189
244
                                *dst++ = '\\'; *dst++ = c;
190
245
                                return dst;
191
246
                        }
 
247
                        if (dlen)
 
248
                                *dlen = odlen;
192
249
                }
193
250
        }
194
251
        if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
 
252
                HAVE(4);
195
253
                *dst++ = '\\';
196
254
                *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
197
255
                *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
198
256
                *dst++ =                             (c       & 07) + '0';
199
257
        } else {
200
 
                if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';
 
258
                if ((flag & VIS_NOSLASH) == 0) {
 
259
                        HAVE(1);
 
260
                        *dst++ = '\\';
 
261
                }
 
262
 
201
263
                if (c & 0200) {
 
264
                        HAVE(1);
202
265
                        c &= 0177; *dst++ = 'M';
203
266
                }
 
267
 
204
268
                if (iscntrl(c)) {
 
269
                        HAVE(2);
205
270
                        *dst++ = '^';
206
271
                        if (c == 0177)
207
272
                                *dst++ = '?';
208
273
                        else
209
274
                                *dst++ = c + '@';
210
275
                } else {
 
276
                        HAVE(2);
211
277
                        *dst++ = '-'; *dst++ = c;
212
278
                }
213
279
        }
214
280
        return dst;
215
 
}
216
 
 
217
 
 
218
 
/*
219
 
 * svis - visually encode characters, also encoding the characters
 
281
out:
 
282
        *dlen = odlen;
 
283
        return NULL;
 
284
}
 
285
 
 
286
typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
 
287
 
 
288
/*
 
289
 * Return the appropriate encoding function depending on the flags given.
 
290
 */
 
291
static visfun_t
 
292
getvisfun(int flag)
 
293
{
 
294
        if (flag & VIS_HTTPSTYLE)
 
295
                return do_hvis;
 
296
        if (flag & VIS_MIMESTYLE)
 
297
                return do_mvis;
 
298
        return do_svis;
 
299
}
 
300
 
 
301
/*
 
302
 * isnvis - visually encode characters, also encoding the characters
220
303
 *        pointed to by `extra'
221
304
 */
 
305
static char *
 
306
isnvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
 
307
{
 
308
        char *nextra = NULL;
 
309
        visfun_t f;
 
310
 
 
311
        _DIAGASSERT(dst != NULL);
 
312
        _DIAGASSERT(extra != NULL);
 
313
        MAKEEXTRALIST(flag, nextra, extra);
 
314
        if (!nextra) {
 
315
                if (dlen && *dlen == 0) {
 
316
                        errno = ENOSPC;
 
317
                        return NULL;
 
318
                }
 
319
                *dst = '\0';            /* can't create nextra, return "" */
 
320
                return dst;
 
321
        }
 
322
        f = getvisfun(flag);
 
323
        dst = (*f)(dst, dlen, c, flag, nextc, nextra);
 
324
        free(nextra);
 
325
        if (dst == NULL || (dlen && *dlen == 0)) {
 
326
                errno = ENOSPC;
 
327
                return NULL;
 
328
        }
 
329
        *dst = '\0';
 
330
        return dst;
 
331
}
 
332
 
222
333
char *
223
334
svis(char *dst, int c, int flag, int nextc, const char *extra)
224
335
{
225
 
        char *nextra = NULL;
 
336
        return isnvis(dst, NULL, c, flag, nextc, extra);
 
337
}
226
338
 
227
 
        _DIAGASSERT(dst != NULL);
228
 
        _DIAGASSERT(extra != NULL);
229
 
        MAKEEXTRALIST(flag, nextra, extra);
230
 
        if (!nextra) {
231
 
                *dst = '\0';            /* can't create nextra, return "" */
232
 
                return dst;
233
 
        }
234
 
        if (flag & VIS_HTTPSTYLE)
235
 
                dst = do_hvis(dst, c, flag, nextc, nextra);
236
 
        else
237
 
                dst = do_svis(dst, c, flag, nextc, nextra);
238
 
        free(nextra);
239
 
        *dst = '\0';
240
 
        return dst;
 
339
char *
 
340
snvis(char *dst, size_t dlen, int c, int flag, int nextc, const char *extra)
 
341
{
 
342
        return isnvis(dst, &dlen, c, flag, nextc, extra);
241
343
}
242
344
 
243
345
 
256
358
 *      Strsvisx encodes exactly len bytes from src into dst.
257
359
 *      This is useful for encoding a block of data.
258
360
 */
 
361
static int
 
362
istrsnvis(char *dst, size_t *dlen, const char *csrc, int flag, const char *extra)
 
363
{
 
364
        int c;
 
365
        char *start;
 
366
        char *nextra = NULL;
 
367
        const unsigned char *src = (const unsigned char *)csrc;
 
368
        visfun_t f;
 
369
 
 
370
        _DIAGASSERT(dst != NULL);
 
371
        _DIAGASSERT(src != NULL);
 
372
        _DIAGASSERT(extra != NULL);
 
373
        MAKEEXTRALIST(flag, nextra, extra);
 
374
        if (!nextra) {
 
375
                *dst = '\0';            /* can't create nextra, return "" */
 
376
                return 0;
 
377
        }
 
378
        f = getvisfun(flag);
 
379
        for (start = dst; (c = *src++) != '\0'; /* empty */) {
 
380
                dst = (*f)(dst, dlen, c, flag, *src, nextra);
 
381
                if (dst == NULL) {
 
382
                        errno = ENOSPC;
 
383
                        return -1;
 
384
                }
 
385
        }
 
386
        free(nextra);
 
387
        if (dlen && *dlen == 0) {
 
388
                errno = ENOSPC;
 
389
                return -1;
 
390
        }
 
391
        *dst = '\0';
 
392
        return (int)(dst - start);
 
393
}
 
394
 
259
395
int
260
396
strsvis(char *dst, const char *csrc, int flag, const char *extra)
261
397
{
262
 
        int c;
263
 
        char *start;
264
 
        char *nextra = NULL;
265
 
        const unsigned char *src = (const unsigned char *)csrc;
266
 
 
267
 
        _DIAGASSERT(dst != NULL);
268
 
        _DIAGASSERT(src != NULL);
269
 
        _DIAGASSERT(extra != NULL);
270
 
        MAKEEXTRALIST(flag, nextra, extra);
271
 
        if (!nextra) {
272
 
                *dst = '\0';            /* can't create nextra, return "" */
273
 
                return 0;
274
 
        }
275
 
        if (flag & VIS_HTTPSTYLE) {
276
 
                for (start = dst; (c = *src++) != '\0'; /* empty */)
277
 
                        dst = do_hvis(dst, c, flag, *src, nextra);
278
 
        } else {
279
 
                for (start = dst; (c = *src++) != '\0'; /* empty */)
280
 
                        dst = do_svis(dst, c, flag, *src, nextra);
281
 
        }
282
 
        free(nextra);
283
 
        *dst = '\0';
284
 
        return (dst - start);
 
398
        return istrsnvis(dst, NULL, csrc, flag, extra);
285
399
}
286
400
 
287
 
 
288
401
int
289
 
strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
 
402
strsnvis(char *dst, size_t dlen, const char *csrc, int flag, const char *extra)
 
403
{
 
404
        return istrsnvis(dst, &dlen, csrc, flag, extra);
 
405
}
 
406
 
 
407
static int
 
408
istrsnvisx(char *dst, size_t *dlen, const char *csrc, size_t len, int flag,
 
409
    const char *extra)
290
410
{
291
411
        unsigned char c;
292
412
        char *start;
293
413
        char *nextra = NULL;
294
414
        const unsigned char *src = (const unsigned char *)csrc;
 
415
        visfun_t f;
295
416
 
296
417
        _DIAGASSERT(dst != NULL);
297
418
        _DIAGASSERT(src != NULL);
298
419
        _DIAGASSERT(extra != NULL);
299
420
        MAKEEXTRALIST(flag, nextra, extra);
300
421
        if (! nextra) {
 
422
                if (dlen && *dlen == 0) {
 
423
                        errno = ENOSPC;
 
424
                        return -1;
 
425
                }
301
426
                *dst = '\0';            /* can't create nextra, return "" */
302
427
                return 0;
303
428
        }
304
429
 
305
 
        if (flag & VIS_HTTPSTYLE) {
306
 
                for (start = dst; len > 0; len--) {
307
 
                        c = *src++;
308
 
                        dst = do_hvis(dst, c, flag,
309
 
                            len > 1 ? *src : '\0', nextra);
310
 
                }
311
 
        } else {
312
 
                for (start = dst; len > 0; len--) {
313
 
                        c = *src++;
314
 
                        dst = do_svis(dst, c, flag,
315
 
                            len > 1 ? *src : '\0', nextra);
 
430
        f = getvisfun(flag);
 
431
        for (start = dst; len > 0; len--) {
 
432
                c = *src++;
 
433
                dst = (*f)(dst, dlen, c, flag, len > 1 ? *src : '\0', nextra);
 
434
                if (dst == NULL) {
 
435
                        errno = ENOSPC;
 
436
                        return -1;
316
437
                }
317
438
        }
318
439
        free(nextra);
 
440
        if (dlen && *dlen == 0) {
 
441
                errno = ENOSPC;
 
442
                return -1;
 
443
        }
319
444
        *dst = '\0';
320
 
        return (dst - start);
 
445
        return (int)(dst - start);
 
446
}
 
447
 
 
448
int
 
449
strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
 
450
{
 
451
        return istrsnvisx(dst, NULL, csrc, len, flag, extra);
 
452
}
 
453
 
 
454
int
 
455
strsnvisx(char *dst, size_t dlen, const char *csrc, size_t len, int flag,
 
456
    const char *extra)
 
457
{
 
458
        return istrsnvisx(dst, &dlen, csrc, len, flag, extra);
321
459
}
322
460
#endif
323
461
 
325
463
/*
326
464
 * vis - visually encode characters
327
465
 */
328
 
char *
329
 
vis(char *dst, int c, int flag, int nextc)
 
466
static char *
 
467
invis(char *dst, size_t *dlen, int c, int flag, int nextc)
330
468
{
331
469
        char *extra = NULL;
332
470
        unsigned char uc = (unsigned char)c;
 
471
        visfun_t f;
333
472
 
334
473
        _DIAGASSERT(dst != NULL);
335
474
 
336
475
        MAKEEXTRALIST(flag, extra, "");
337
476
        if (! extra) {
 
477
                if (dlen && *dlen == 0) {
 
478
                        errno = ENOSPC;
 
479
                        return NULL;
 
480
                }
338
481
                *dst = '\0';            /* can't create extra, return "" */
339
482
                return dst;
340
483
        }
341
 
        if (flag & VIS_HTTPSTYLE)
342
 
                dst = do_hvis(dst, uc, flag, nextc, extra);
343
 
        else
344
 
                dst = do_svis(dst, uc, flag, nextc, extra);
 
484
        f = getvisfun(flag);
 
485
        dst = (*f)(dst, dlen, uc, flag, nextc, extra);
345
486
        free(extra);
 
487
        if (dst == NULL || (dlen && *dlen == 0)) {
 
488
                errno = ENOSPC;
 
489
                return NULL;
 
490
        }
346
491
        *dst = '\0';
347
492
        return dst;
348
493
}
349
494
 
 
495
char *
 
496
vis(char *dst, int c, int flag, int nextc)
 
497
{
 
498
        return invis(dst, NULL, c, flag, nextc);
 
499
}
 
500
 
 
501
char *
 
502
nvis(char *dst, size_t dlen, int c, int flag, int nextc)
 
503
{
 
504
        return invis(dst, &dlen, c, flag, nextc);
 
505
}
 
506
 
350
507
 
351
508
/*
352
509
 * strvis, strvisx - visually encode characters from src into dst
358
515
 *      Strvisx encodes exactly len bytes from src into dst.
359
516
 *      This is useful for encoding a block of data.
360
517
 */
 
518
static int
 
519
istrnvis(char *dst, size_t *dlen, const char *src, int flag)
 
520
{
 
521
        char *extra = NULL;
 
522
        int rv;
 
523
 
 
524
        MAKEEXTRALIST(flag, extra, "");
 
525
        if (!extra) {
 
526
                if (dlen && *dlen == 0) {
 
527
                        errno = ENOSPC;
 
528
                        return -1;
 
529
                }
 
530
                *dst = '\0';            /* can't create extra, return "" */
 
531
                return 0;
 
532
        }
 
533
        rv = istrsnvis(dst, dlen, src, flag, extra);
 
534
        free(extra);
 
535
        return rv;
 
536
}
 
537
 
361
538
int
362
539
strvis(char *dst, const char *src, int flag)
363
540
{
 
541
        return istrnvis(dst, NULL, src, flag);
 
542
}
 
543
 
 
544
int
 
545
strnvis(char *dst, size_t dlen, const char *src, int flag)
 
546
{
 
547
        return istrnvis(dst, &dlen, src, flag);
 
548
}
 
549
 
 
550
static int
 
551
istrnvisx(char *dst, size_t *dlen, const char *src, size_t len, int flag)
 
552
{
364
553
        char *extra = NULL;
365
554
        int rv;
366
555
 
367
556
        MAKEEXTRALIST(flag, extra, "");
368
557
        if (!extra) {
 
558
                if (dlen && *dlen == 0) {
 
559
                        errno = ENOSPC;
 
560
                        return -1;
 
561
                }
369
562
                *dst = '\0';            /* can't create extra, return "" */
370
563
                return 0;
371
564
        }
372
 
        rv = strsvis(dst, src, flag, extra);
 
565
        rv = istrsnvisx(dst, dlen, src, len, flag, extra);
373
566
        free(extra);
374
567
        return rv;
375
568
}
376
569
 
377
 
 
378
570
int
379
571
strvisx(char *dst, const char *src, size_t len, int flag)
380
572
{
381
 
        char *extra = NULL;
382
 
        int rv;
383
 
 
384
 
        MAKEEXTRALIST(flag, extra, "");
385
 
        if (!extra) {
386
 
                *dst = '\0';            /* can't create extra, return "" */
387
 
                return 0;
388
 
        }
389
 
        rv = strsvisx(dst, src, len, flag, extra);
390
 
        free(extra);
391
 
        return rv;
392
 
}
 
573
        return istrnvisx(dst, NULL, src, len, flag);
 
574
}
 
575
 
 
576
int
 
577
strnvisx(char *dst, size_t dlen, const char *src, size_t len, int flag)
 
578
{
 
579
        return istrnvisx(dst, &dlen, src, len, flag);
 
580
}
 
581
 
393
582
#endif