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 ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @url_initenc@ --- *
41 * Arguments: @url_ectx *ctx@ = pointer to context block
45 * Use: Initializes a URL encoding context.
48 void url_initenc(url_ectx *ctx) { ctx->f = 0; }
52 * Arguments: @url_ectx *ctx@ = encoding context
53 * @dstr *d@ = pointer to output string
54 * @const char *p@ = pointer to thing to encode
58 * Use: Encodes the input string into the output string.
61 static void encode(url_ectx *ctx, dstr *d, const char *p)
65 case ' ': DPUTC(d, '+');
68 if (ISSPACE(*p)) goto unsafe;
69 else if (ISALNUM(*p)) goto safe;
70 else if (ctx->f&URLF_LAX) goto safe;
73 if (ctx->f&URLF_STRICT) goto unsafe; /* else fall through... */
74 safe: case '-': case '.': case '_':
76 unsafe: case '+': case '%': case '=': case '&': case ';':
77 dstr_putf(d, "%%%02x", *p); break;
83 /* --- @url_enc@ --- *
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
92 * Use: Writes an assignment between @name@ and @value@ to the
93 * output string, encoding the values properly.
96 void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
98 if (ctx->f & URLF_SEP)
99 DPUTC(d, (ctx->f & URLF_SEMI) ? ';' : '&');
100 encode(ctx, d, name);
102 encode(ctx, d, value);
107 /* --- @url_initdec@ --- *
109 * Arguments: @url_dctx *ctx@ = pointer to context block
110 * @const char *p@ = string to read data from
114 * Use: Initializes a URL decoding context.
117 void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; ctx->f = 0; }
119 /* --- @decode@ --- *
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
126 * Returns: Pointer to next available character.
128 * Use: Does a URL decode.
131 static const char *decode(url_dctx *ctx, dstr *d, const char *p, int eq)
142 if (ctx->f & URLF_SEMI)
154 int x = sscanf(p + 1, "%2x%n", &ch, &n);
170 /* --- @url_dec@ --- *
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
176 * Returns: Nonzero if it read something, zero if there's nothing left
178 * Use: Decodes the next name/value pair from a urlencoded string.
181 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
183 const char *p = ctx->p;
187 if ((p = decode(ctx, n, p, 1)) == 0 || *p == 0)
195 if ((p = decode(ctx, v, p, 0)) == 0)
203 /*----- That's all, folks -------------------------------------------------*/