~ubuntu-branches/ubuntu/feisty/dovecot/feisty-security

« back to all changes in this revision

Viewing changes to src/lib/var-expand.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2006-11-27 20:47:11 UTC
  • mfrom: (1.10.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20061127204711-z7alg32xp4tnftjs
Tags: 1.0.rc15-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - snakeoil ssl
  - Remove stop script symlinks from rc0 and rc6

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include <stdlib.h>
12
12
 
13
13
struct var_expand_context {
14
 
        unsigned int offset, width;
 
14
        int offset;
 
15
        unsigned int width;
 
16
        bool zero_padding;
15
17
};
16
18
 
17
19
struct var_expand_modifier {
71
73
        }
72
74
 
73
75
        str_printfa(hash, "%x", value);
74
 
        while (str_len(hash) < ctx->offset)
 
76
        while ((int)str_len(hash) < ctx->offset)
75
77
                str_insert(hash, 0, "0");
76
78
        ctx->offset = 0;
77
79
 
87
89
        return binary_to_hex(digest, sizeof(digest));
88
90
}
89
91
 
 
92
static const char *m_str_ldap_dn(const char *str, struct var_expand_context *ctx __attr_unused__)
 
93
{
 
94
        string_t *ret = t_str_new(256);
 
95
 
 
96
        while (*str) {
 
97
                if (*str == '.')
 
98
                        str_append(ret, ",dc=");
 
99
                else
 
100
                        str_append_c(ret, *str);
 
101
                str++;
 
102
        }
 
103
 
 
104
        return str_free_without_data(&ret);
 
105
}
 
106
 
90
107
#define MAX_MODIFIER_COUNT 10
91
108
static const struct var_expand_modifier modifiers[] = {
92
109
        { 'L', m_str_lcase },
96
113
        { 'R', m_str_reverse },
97
114
        { 'H', m_str_hash },
98
115
        { 'M', m_str_md5 },
 
116
        { 'D', m_str_ldap_dn },
99
117
        { '\0', NULL }
100
118
};
101
119
 
109
127
        const char *(*modifier[MAX_MODIFIER_COUNT])
110
128
                (const char *, struct var_expand_context *);
111
129
        unsigned int i, modifier_count;
112
 
        bool zero_padding = FALSE;
113
130
 
114
131
        memset(&ctx, 0, sizeof(ctx));
115
132
        for (; *str != '\0'; str++) {
116
133
                if (*str != '%')
117
134
                        str_append_c(dest, *str);
118
135
                else {
 
136
                        int sign = 1;
 
137
 
119
138
                        str++;
 
139
                        memset(&ctx, 0, sizeof(ctx));
120
140
 
121
141
                        /* [<offset>.]<width>[<modifiers>]<variable> */
122
 
                        ctx.width = 0;
 
142
                        if (*str == '-') {
 
143
                                sign = -1;
 
144
                                str++;
 
145
                        }
123
146
                        if (*str == '0') {
124
 
                                zero_padding = TRUE;
 
147
                                ctx.zero_padding = TRUE;
125
148
                                str++;
126
149
                        }
127
150
                        while (*str >= '0' && *str <= '9') {
129
152
                                str++;
130
153
                        }
131
154
 
132
 
                        if (*str != '.')
133
 
                                ctx.offset = 0;
134
 
                        else {
135
 
                                ctx.offset = ctx.width;
 
155
                        if (*str == '.') {
 
156
                                ctx.offset = sign * (int)ctx.width;
136
157
                                ctx.width = 0;
137
158
                                str++;
 
159
 
 
160
                                /* if offset was prefixed with zero (or it was
 
161
                                   plain zero), just ignore that. zero padding
 
162
                                   is done with the width. */
 
163
                                ctx.zero_padding = FALSE;
 
164
                                if (*str == '0') {
 
165
                                        ctx.zero_padding = TRUE;
 
166
                                        str++;
 
167
                                }
 
168
 
138
169
                                while (*str >= '0' && *str <= '9') {
139
170
                                        ctx.width = ctx.width*10 + (*str - '0');
140
171
                                        str++;
178
209
                        if (var != NULL) {
179
210
                                for (i = 0; i < modifier_count; i++)
180
211
                                        var = modifier[i](var, &ctx);
181
 
                                while (*var != '\0' && ctx.offset > 0) {
182
 
                                        ctx.offset--;
183
 
                                        var++;
 
212
 
 
213
                                if (ctx.offset < 0) {
 
214
                                        /* if offset is < 0 then we want to
 
215
                                           start at the end */
 
216
                                        size_t len = strlen(var);
 
217
 
 
218
                                        if (len > (size_t)-ctx.offset)
 
219
                                                var += len + ctx.offset;
 
220
                                } else {
 
221
                                        while (*var != '\0' && ctx.offset > 0) {
 
222
                                                ctx.offset--;
 
223
                                                var++;
 
224
                                        }
184
225
                                }
185
226
                                if (ctx.width == 0)
186
227
                                        str_append(dest, var);
187
 
                                else if (!zero_padding)
 
228
                                else if (!ctx.zero_padding)
188
229
                                        str_append_n(dest, var, ctx.width);
189
230
                                else {
190
 
                                        /* %05d -like padding */
 
231
                                        /* %05d -like padding. no truncation. */
191
232
                                        size_t len = strlen(var);
192
233
                                        while (len < ctx.width) {
193
234
                                                str_append_c(dest, '0');
205
246
        const struct var_expand_modifier *m;
206
247
 
207
248
        /* [<offset>.]<width>[<modifiers>]<variable> */
208
 
        while (*str >= '0' && *str <= '9')
 
249
        while ((*str >= '0' && *str <= '9') || *str == '-')
209
250
                str++;
210
251
 
211
252
        if (*str == '.') {