3 * Parsing and construction of url-encoded name/value pairs
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @url_initenc@ --- *
42 * Arguments: @url_ectx *ctx@ = pointer to context block
46 * Use: Initializes a URL encoding context.
49 void url_initenc(url_ectx *ctx) { ctx->f = 0; }
53 * Arguments: @url_ectx *ctx@ = encoding context
54 * @dstr *d@ = pointer to output string
55 * @const char *p@ = pointer to thing to encode
59 * Use: Encodes the input string into the output string.
62 static void encode(url_ectx *ctx, dstr *d, const char *p)
70 if (!isspace((unsigned char)*p) &&
71 ((ctx->f & URLF_LAX) || isalnum((unsigned char)*p)))
77 if (ctx->f & URLF_STRICT)
91 dstr_putf(d, "%%%02x", *p);
98 /* --- @url_enc@ --- *
100 * Arguments: @url_ectx *ctx@ = pointer to encoding context
101 * @dstr *d@ = pointer to output string
102 * @const char *name@ = pointer to name
103 * @const char *value@ = pointer to value
107 * Use: Writes an assignment between @name@ and @value@ to the
108 * output string, encoding the values properly.
111 void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
113 if (ctx->f & URLF_SEP)
114 DPUTC(d, (ctx->f & URLF_SEMI) ? ';' : '&');
115 encode(ctx, d, name);
117 encode(ctx, d, value);
122 /* --- @url_initdec@ --- *
124 * Arguments: @url_dctx *ctx@ = pointer to context block
125 * @const char *p@ = string to read data from
129 * Use: Initializes a URL decoding context.
132 void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; ctx->f = 0; }
134 /* --- @decode@ --- *
136 * Arguments: @url_dctx *ctx@ = pointer to the context
137 * @dstr *d@ = pointer to output string
138 * @const char *p@ = pointer to input data
139 * @int eq@ = whether to stop at `=' characters
141 * Returns: Pointer to next available character.
143 * Use: Does a URL decode.
146 static const char *decode(url_dctx *ctx, dstr *d, const char *p, int eq)
157 if (ctx->f & URLF_SEMI)
169 int x = sscanf(p + 1, "%2x%n", &ch, &n);
185 /* --- @url_dec@ --- *
187 * Arguments: @url_dctx *ctx@ = pointer to decode context
188 * @dstr *n@ = pointer to output string for name
189 * @dstr *v@ = pointer to output string for value
191 * Returns: Nonzero if it read something, zero if there's nothing left
193 * Use: Decodes the next name/value pair from a urlencoded string.
196 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
198 const char *p = ctx->p;
202 if ((p = decode(ctx, n, p, 1)) == 0 || *p == 0)
210 if ((p = decode(ctx, v, p, 0)) == 0)
218 /*----- That's all, folks -------------------------------------------------*/