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