3 * $Id: url.c,v 1.5 2004/04/08 01:36:13 mdw Exp $
5 * Parsing and construction of url-encoded name/value pairs
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @url_initenc@ --- *
44 * Arguments: @url_ectx *ctx@ = pointer to context block
48 * Use: Initializes a URL encoding context.
51 void url_initenc(url_ectx *ctx) { ctx->f = 0; }
55 * Arguments: @url_ectx *ctx@ = encoding context
56 * @dstr *d@ = pointer to output string
57 * @const char *p@ = pointer to thing to encode
61 * Use: Encodes the input string into the output string.
64 static void encode(url_ectx *ctx, dstr *d, const char *p)
72 if ((ctx->f & URLF_LAX) || isalnum((unsigned char)*p))
78 if (ctx->f & URLF_STRICT)
92 dstr_putf(d, "%%%02x", *p);
99 /* --- @url_enc@ --- *
101 * Arguments: @url_ectx *ctx@ = pointer to encoding context
102 * @dstr *d@ = pointer to output string
103 * @const char *name@ = pointer to name
104 * @const char *value@ = pointer to value
108 * Use: Writes an assignment between @name@ and @value@ to the
109 * output string, encoding the values properly.
112 void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
114 if (ctx->f & URLF_SEP)
116 encode(ctx, d, name);
118 encode(ctx, d, value);
123 /* --- @url_initdec@ --- *
125 * Arguments: @url_dctx *ctx@ = pointer to context block
126 * @const char *p@ = string to read data from
130 * Use: Initializes a URL decoding context.
133 void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; }
135 /* --- @decode@ --- *
137 * Arguments: @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(dstr *d, const char *p, int eq)
166 int x = sscanf(p + 1, "%2x%n", &ch, &n);
181 /* --- @url_dec@ --- *
183 * Arguments: @url_dctx *ctx@ = pointer to decode context
184 * @dstr *n@ = pointer to output string for name
185 * @dstr *v@ = pointer to output string for value
187 * Returns: Nonzero if it read something, zero if there's nothing left
189 * Use: Decodes the next name/value pair from a urlencoded string.
192 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
194 const char *p = ctx->p;
198 if ((p = decode(n, p, 1)) == 0 || *p == 0)
206 if ((p = decode(v, p, 0)) == 0)
214 /*----- That's all, folks -------------------------------------------------*/