~ubuntu-branches/ubuntu/hoary/postfix/hoary-security

« back to all changes in this revision

Viewing changes to src/util/unescape.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-10-06 11:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041006115033-ooo6yfg6kmoteu04
Tags: upstream-2.1.3
ImportĀ upstreamĀ versionĀ 2.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      unescape 3
 
4
/* SUMMARY
 
5
/*      translate C-like escape sequences
 
6
/* SYNOPSIS
 
7
/*      #include <stringops.h>
 
8
/*
 
9
/*      VSTRING *unescape(result, input)
 
10
/*      VSTRING *result;
 
11
/*      const char *input;
 
12
/* DESCRIPTION
 
13
/*      unescape() translates C-like escape sequences in the null-terminated
 
14
/*      string \fIinput\fR and places the result in \fIresult\fR. The result
 
15
/*      is null-terminated.
 
16
/*
 
17
/*      Escape sequences and their translations:
 
18
/* .IP \ea
 
19
/*      Bell character.
 
20
/* .IP \eb
 
21
/*      Backspace character.
 
22
/* .IP \ef
 
23
/*      formfeed character.
 
24
/* .IP \en
 
25
/*      newline character
 
26
/* .IP \er
 
27
/*      Carriage-return character.
 
28
/* .IP \et
 
29
/*      Horizontal tab character.
 
30
/* .IP \ev
 
31
/*      Vertical tab character.
 
32
/* .IP \e\e
 
33
/*      Backslash character.
 
34
/* .IP \e\fInum\fR
 
35
/*      8-bit character whose ASCII value is the 1..3 digit
 
36
/*      octal number \fInum\fR.
 
37
/* .IP \e\fIother\fR
 
38
/*      The backslash character is discarded.
 
39
/* LICENSE
 
40
/* .ad
 
41
/* .fi
 
42
/*      The Secure Mailer license must be distributed with this software.
 
43
/* AUTHOR(S)
 
44
/*      Wietse Venema
 
45
/*      IBM T.J. Watson Research
 
46
/*      P.O. Box 704
 
47
/*      Yorktown Heights, NY 10598, USA
 
48
/*--*/
 
49
 
 
50
/* System library. */
 
51
 
 
52
#include <sys_defs.h>
 
53
#include <ctype.h>
 
54
 
 
55
/* Utility library. */
 
56
 
 
57
#include <vstring.h>
 
58
#include <stringops.h>
 
59
 
 
60
/* unescape - process escape sequences */
 
61
 
 
62
VSTRING *unescape(VSTRING *result, const char *data)
 
63
{
 
64
    int     ch;
 
65
    int     oval;
 
66
    int     i;
 
67
 
 
68
#define UCHAR(cp)       ((unsigned char *) (cp))
 
69
#define ISOCTAL(ch)     (ISDIGIT(ch) && (ch) != '8' && (ch) != '9')
 
70
 
 
71
    VSTRING_RESET(result);
 
72
 
 
73
    while ((ch = *UCHAR(data++)) != 0) {
 
74
        if (ch == '\\') {
 
75
            if ((ch = *UCHAR(data++)) == 0)
 
76
                break;
 
77
            switch (ch) {
 
78
            case 'a':                           /* \a -> audible bell */
 
79
                ch = '\a';
 
80
                break;
 
81
            case 'b':                           /* \b -> backspace */
 
82
                ch = '\b';
 
83
                break;
 
84
            case 'f':                           /* \f -> formfeed */
 
85
                ch = '\f';
 
86
                break;
 
87
            case 'n':                           /* \n -> newline */
 
88
                ch = '\n';
 
89
                break;
 
90
            case 'r':                           /* \r -> carriagereturn */
 
91
                ch = '\r';
 
92
                break;
 
93
            case 't':                           /* \t -> horizontal tab */
 
94
                ch = '\t';
 
95
                break;
 
96
            case 'v':                           /* \v -> vertical tab */
 
97
                ch = '\v';
 
98
                break;
 
99
            case '0':                           /* \nnn -> ASCII value */
 
100
            case '1':
 
101
            case '2':
 
102
            case '3':
 
103
            case '4':
 
104
            case '5':
 
105
            case '6':
 
106
            case '7':
 
107
                for (oval = ch - '0', i = 0;
 
108
                     i < 2 && (ch = *UCHAR(data)) != 0 && ISOCTAL(ch);
 
109
                     i++, data++) {
 
110
                    oval = (oval << 3) | (ch - '0');
 
111
                }
 
112
                ch = oval;
 
113
                break;
 
114
            default:                            /* \any -> any */
 
115
                break;
 
116
            }
 
117
        }
 
118
        VSTRING_ADDCH(result, ch);
 
119
    }
 
120
    VSTRING_TERMINATE(result);
 
121
    return (result);
 
122
}
 
123
 
 
124
#ifdef TEST
 
125
 
 
126
#include <vstring_vstream.h>
 
127
 
 
128
int     main(int unused_argc, char **unused_argv)
 
129
{
 
130
    VSTRING *in = vstring_alloc(10);
 
131
    VSTRING *out = vstring_alloc(10);
 
132
 
 
133
    while (vstring_fgets_nonl(in, VSTREAM_IN)) {
 
134
        unescape(out, vstring_str(in));
 
135
        vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
 
136
    }
 
137
    vstream_fflush(VSTREAM_OUT);
 
138
    exit(0);
 
139
}
 
140
 
 
141
#endif