chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / codec / url.c
1 /* -*-c-*-
2  *
3  * Parsing and construction of url-encoded name/value pairs
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <string.h>
32
33 #include "dstr.h"
34 #include "macros.h"
35 #include "url.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @url_initenc@ --- *
40  *
41  * Arguments:   @url_ectx *ctx@ = pointer to context block
42  *
43  * Returns:     ---
44  *
45  * Use:         Initializes a URL encoding context.
46  */
47
48 void url_initenc(url_ectx *ctx) { ctx->f = 0; }
49
50 /* --- @encode@ --- *
51  *
52  * Arguments:   @url_ectx *ctx@ = encoding context
53  *              @dstr *d@ = pointer to output string
54  *              @const char *p@ = pointer to thing to encode
55  *
56  * Returns:     ---
57  *
58  * Use:         Encodes the input string into the output string.
59  */
60
61 static void encode(url_ectx *ctx, dstr *d, const char *p)
62 {
63   while (*p) {
64     switch (*p) {
65       case ' ': DPUTC(d, '+');
66         break;
67       default:
68         if (ISSPACE(*p)) goto unsafe;
69         else if (ISALNUM(*p)) goto safe;
70         else if (ctx->f&URLF_LAX) goto safe;
71         else goto unsafe;
72       case '/': case '~':
73         if (ctx->f&URLF_STRICT) goto unsafe; /* else fall through... */
74       safe: case '-': case '.': case '_':
75         DPUTC(d, *p);   break;
76       unsafe: case '+': case '%': case '=': case '&': case ';':
77         dstr_putf(d, "%%%02x", *p); break;
78     }
79     p++;
80   }
81 }
82
83 /* --- @url_enc@ --- *
84  *
85  * Arguments:   @url_ectx *ctx@ = pointer to encoding context
86  *              @dstr *d@ = pointer to output string
87  *              @const char *name@ = pointer to name
88  *              @const char *value@ = pointer to value
89  *
90  * Returns:     ---
91  *
92  * Use:         Writes an assignment between @name@ and @value@ to the
93  *              output string, encoding the values properly.
94  */
95
96 void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
97 {
98   if (ctx->f & URLF_SEP)
99     DPUTC(d, (ctx->f & URLF_SEMI) ? ';' : '&');
100   encode(ctx, d, name);
101   DPUTC(d, '=');
102   encode(ctx, d, value);
103   DPUTZ(d);
104   ctx->f |= URLF_SEP;
105 }
106
107 /* --- @url_initdec@ --- *
108  *
109  * Arguments:   @url_dctx *ctx@ = pointer to context block
110  *              @const char *p@ = string to read data from
111  *
112  * Returns:     ---
113  *
114  * Use:         Initializes a URL decoding context.
115  */
116
117 void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; ctx->f = 0; }
118
119 /* --- @decode@ --- *
120  *
121  * Arguments:   @url_dctx *ctx@ = pointer to the context
122  *              @dstr *d@ = pointer to output string
123  *              @const char *p@ = pointer to input data
124  *              @int eq@ = whether to stop at `=' characters
125  *
126  * Returns:     Pointer to next available character.
127  *
128  * Use:         Does a URL decode.
129  */
130
131 static const char *decode(url_dctx *ctx, dstr *d, const char *p, int eq)
132 {
133   if (!*p)
134     return (0);
135   for (;;) {
136     switch (*p) {
137       case '=':
138         if (eq)
139           return (p);
140         goto boring;
141       case ';':
142         if (ctx->f & URLF_SEMI)
143           return (p);
144         goto boring;
145       case 0:
146       case '&':
147         return (p);
148       case '+':
149         DPUTC(d, ' ');
150         break;
151       case '%': {
152         unsigned int ch;
153         int n;
154         int x = sscanf(p + 1, "%2x%n", &ch, &n);
155         if (x == 1) {
156           DPUTC(d, ch);
157           p += n;
158           break;
159         }
160       }
161       default:
162       boring:
163         DPUTC(d, *p);
164         break;
165     }
166     p++;
167   }
168 }
169
170 /* --- @url_dec@ --- *
171  *
172  * Arguments:   @url_dctx *ctx@ = pointer to decode context
173  *              @dstr *n@ = pointer to output string for name
174  *              @dstr *v@ = pointer to output string for value
175  *
176  * Returns:     Nonzero if it read something, zero if there's nothing left
177  *
178  * Use:         Decodes the next name/value pair from a urlencoded string.
179  */
180
181 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
182 {
183   const char *p = ctx->p;
184   size_t l = n->len;
185
186 again:
187   if ((p = decode(ctx, n, p, 1)) == 0 || *p == 0)
188     return (0);
189   if (*p != '=') {
190     p++;
191     n->len = l;
192     goto again;
193   }
194   p++;
195   if ((p = decode(ctx, v, p, 0)) == 0)
196     return (0);
197   DPUTZ(n);
198   DPUTZ(v);
199   ctx->p = p;
200   return (1);
201 }
202
203 /*----- That's all, folks -------------------------------------------------*/