codec/url.c (encode): Fix bungled cast in previous refactoring.
[mLib] / codec / url.c
CommitLineData
0f2a8846 1/* -*-c-*-
2 *
0f2a8846 3 * Parsing and construction of url-encoded name/value pairs
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
0f2a8846 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
0f2a8846 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
d4efbcd9 21 *
0f2a8846 22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
0f2a8846 28/*----- Header files ------------------------------------------------------*/
29
78b1464e 30#include <ctype.h>
0f2a8846 31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "dstr.h"
36#include "url.h"
37
38/*----- Main code ---------------------------------------------------------*/
39
40/* --- @url_initenc@ --- *
41 *
42 * Arguments: @url_ectx *ctx@ = pointer to context block
43 *
44 * Returns: ---
45 *
46 * Use: Initializes a URL encoding context.
47 */
48
0ddebb8f 49void url_initenc(url_ectx *ctx) { ctx->f = 0; }
0f2a8846 50
51/* --- @encode@ --- *
52 *
7e4708e4
MW
53 * Arguments: @url_ectx *ctx@ = encoding context
54 * @dstr *d@ = pointer to output string
0f2a8846 55 * @const char *p@ = pointer to thing to encode
56 *
57 * Returns: ---
58 *
59 * Use: Encodes the input string into the output string.
60 */
61
7e4708e4 62static void encode(url_ectx *ctx, dstr *d, const char *p)
0f2a8846 63{
64 while (*p) {
65 switch (*p) {
96e10f28 66 case ' ': DPUTC(d, '+');
d4efbcd9 67 break;
0f2a8846 68 default:
96e10f28 69 if (isspace((unsigned char)*p)) goto unsafe;
962e9e9c 70 else if (isalnum((unsigned char)*p)) goto safe;
96e10f28
MW
71 else if (ctx->f&URLF_LAX) goto safe;
72 else goto unsafe;
73 case '/': case '~':
74 if (ctx->f&URLF_STRICT) goto unsafe; /* else fall through... */
75 safe: case '-': case '.': case '_':
76 DPUTC(d, *p); break;
77 unsafe: case '+': case '%': case '=': case '&': case ';':
78 dstr_putf(d, "%%%02x", *p); break;
0f2a8846 79 }
80 p++;
81 }
82}
83
84/* --- @url_enc@ --- *
85 *
86 * Arguments: @url_ectx *ctx@ = pointer to encoding context
87 * @dstr *d@ = pointer to output string
88 * @const char *name@ = pointer to name
89 * @const char *value@ = pointer to value
90 *
91 * Returns: ---
92 *
93 * Use: Writes an assignment between @name@ and @value@ to the
94 * output string, encoding the values properly.
95 */
96
97void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
98{
99 if (ctx->f & URLF_SEP)
9dc511e4 100 DPUTC(d, (ctx->f & URLF_SEMI) ? ';' : '&');
7e4708e4 101 encode(ctx, d, name);
0f2a8846 102 DPUTC(d, '=');
7e4708e4 103 encode(ctx, d, value);
0f2a8846 104 DPUTZ(d);
105 ctx->f |= URLF_SEP;
106}
107
108/* --- @url_initdec@ --- *
109 *
110 * Arguments: @url_dctx *ctx@ = pointer to context block
111 * @const char *p@ = string to read data from
112 *
113 * Returns: ---
114 *
115 * Use: Initializes a URL decoding context.
116 */
117
9dc511e4 118void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; ctx->f = 0; }
0f2a8846 119
120/* --- @decode@ --- *
121 *
9dc511e4
MW
122 * Arguments: @url_dctx *ctx@ = pointer to the context
123 * @dstr *d@ = pointer to output string
0f2a8846 124 * @const char *p@ = pointer to input data
125 * @int eq@ = whether to stop at `=' characters
126 *
127 * Returns: Pointer to next available character.
128 *
129 * Use: Does a URL decode.
130 */
131
9dc511e4 132static const char *decode(url_dctx *ctx, dstr *d, const char *p, int eq)
0f2a8846 133{
134 if (!*p)
135 return (0);
136 for (;;) {
137 switch (*p) {
138 case '=':
139 if (eq)
140 return (p);
9dc511e4
MW
141 goto boring;
142 case ';':
143 if (ctx->f & URLF_SEMI)
144 return (p);
145 goto boring;
0f2a8846 146 case 0:
147 case '&':
148 return (p);
149 case '+':
150 DPUTC(d, ' ');
151 break;
152 case '%': {
153 unsigned int ch;
154 int n;
155 int x = sscanf(p + 1, "%2x%n", &ch, &n);
156 if (x == 1) {
157 DPUTC(d, ch);
158 p += n;
159 break;
160 }
161 }
162 default:
9dc511e4 163 boring:
0f2a8846 164 DPUTC(d, *p);
165 break;
166 }
167 p++;
168 }
169}
170
171/* --- @url_dec@ --- *
172 *
173 * Arguments: @url_dctx *ctx@ = pointer to decode context
174 * @dstr *n@ = pointer to output string for name
175 * @dstr *v@ = pointer to output string for value
176 *
177 * Returns: Nonzero if it read something, zero if there's nothing left
178 *
179 * Use: Decodes the next name/value pair from a urlencoded string.
180 */
181
182int url_dec(url_dctx *ctx, dstr *n, dstr *v)
183{
184 const char *p = ctx->p;
185 size_t l = n->len;
186
187again:
9dc511e4 188 if ((p = decode(ctx, n, p, 1)) == 0 || *p == 0)
0f2a8846 189 return (0);
190 if (*p != '=') {
191 p++;
192 n->len = l;
193 goto again;
194 }
195 p++;
9dc511e4 196 if ((p = decode(ctx, v, p, 0)) == 0)
0f2a8846 197 return (0);
198 DPUTZ(n);
199 DPUTZ(v);
200 ctx->p = p;
201 return (1);
202}
203
204/*----- That's all, folks -------------------------------------------------*/