chiark / gitweb /
Insert missing `NAME' section. Use a pleasant `>=' sign when doing good
[mLib] / url.c
CommitLineData
0f2a8846 1/* -*-c-*-
2 *
000b4322 3 * $Id: url.c,v 1.3 2001/01/20 12:06:21 mdw Exp $
0f2a8846 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/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: url.c,v $
000b4322 33 * Revision 1.3 2001/01/20 12:06:21 mdw
34 * Be more conservative in base-64 encoding.
35 *
d16e1bac 36 * Revision 1.2 1999/09/03 08:02:05 mdw
37 * Make `#' a special character which needs escaping.
38 *
0f2a8846 39 * Revision 1.1 1999/06/01 09:49:48 mdw
40 * New files for url-encoding and decoding.
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49
50#include "dstr.h"
51#include "url.h"
52
53/*----- Main code ---------------------------------------------------------*/
54
55/* --- @url_initenc@ --- *
56 *
57 * Arguments: @url_ectx *ctx@ = pointer to context block
58 *
59 * Returns: ---
60 *
61 * Use: Initializes a URL encoding context.
62 */
63
64void url_initenc(url_ectx *ctx)
65{
66 ctx->f = 0;
67}
68
69/* --- @encode@ --- *
70 *
71 * Arguments: @dstr *d@ = pointer to output string
72 * @const char *p@ = pointer to thing to encode
73 *
74 * Returns: ---
75 *
76 * Use: Encodes the input string into the output string.
77 */
78
79static void encode(dstr *d, const char *p)
80{
81 while (*p) {
82 switch (*p) {
83 case ' ':
84 DPUTC(d, '+');
85 break;
86 default:
000b4322 87 if (isalnum((unsigned char)*p))
0f2a8846 88 DPUTC(d, *p);
89 else
000b4322 90 dstr_putf(d, "%%%02x", *p);
0f2a8846 91 break;
92 }
93 p++;
94 }
95}
96
97/* --- @url_enc@ --- *
98 *
99 * Arguments: @url_ectx *ctx@ = pointer to encoding context
100 * @dstr *d@ = pointer to output string
101 * @const char *name@ = pointer to name
102 * @const char *value@ = pointer to value
103 *
104 * Returns: ---
105 *
106 * Use: Writes an assignment between @name@ and @value@ to the
107 * output string, encoding the values properly.
108 */
109
110void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
111{
112 if (ctx->f & URLF_SEP)
113 DPUTC(d, '&');
114 encode(d, name);
115 DPUTC(d, '=');
116 encode(d, value);
117 DPUTZ(d);
118 ctx->f |= URLF_SEP;
119}
120
121/* --- @url_initdec@ --- *
122 *
123 * Arguments: @url_dctx *ctx@ = pointer to context block
124 * @const char *p@ = string to read data from
125 *
126 * Returns: ---
127 *
128 * Use: Initializes a URL decoding context.
129 */
130
131void url_initdec(url_dctx *ctx, const char *p)
132{
133 ctx->p = p;
134}
135
136/* --- @decode@ --- *
137 *
138 * Arguments: @dstr *d@ = pointer to output string
139 * @const char *p@ = pointer to input data
140 * @int eq@ = whether to stop at `=' characters
141 *
142 * Returns: Pointer to next available character.
143 *
144 * Use: Does a URL decode.
145 */
146
147static const char *decode(dstr *d, const char *p, int eq)
148{
149 if (!*p)
150 return (0);
151 for (;;) {
152 switch (*p) {
153 case '=':
154 if (eq)
155 return (p);
156 DPUTC(d, *p);
157 break;
158 case 0:
159 case '&':
160 return (p);
161 case '+':
162 DPUTC(d, ' ');
163 break;
164 case '%': {
165 unsigned int ch;
166 int n;
167 int x = sscanf(p + 1, "%2x%n", &ch, &n);
168 if (x == 1) {
169 DPUTC(d, ch);
170 p += n;
171 break;
172 }
173 }
174 default:
175 DPUTC(d, *p);
176 break;
177 }
178 p++;
179 }
180}
181
182/* --- @url_dec@ --- *
183 *
184 * Arguments: @url_dctx *ctx@ = pointer to decode context
185 * @dstr *n@ = pointer to output string for name
186 * @dstr *v@ = pointer to output string for value
187 *
188 * Returns: Nonzero if it read something, zero if there's nothing left
189 *
190 * Use: Decodes the next name/value pair from a urlencoded string.
191 */
192
193int url_dec(url_dctx *ctx, dstr *n, dstr *v)
194{
195 const char *p = ctx->p;
196 size_t l = n->len;
197
198again:
199 if ((p = decode(n, p, 1)) == 0 || *p == 0)
200 return (0);
201 if (*p != '=') {
202 p++;
203 n->len = l;
204 goto again;
205 }
206 p++;
207 if ((p = decode(v, p, 0)) == 0)
208 return (0);
209 DPUTZ(n);
210 DPUTZ(v);
211 ctx->p = p;
212 return (1);
213}
214
215/*----- That's all, folks -------------------------------------------------*/