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
)
66 case ' ': DPUTC(d
, '+');
69 if (isspace((unsigned char)*p
)) goto unsafe
;
70 else if (isalnum((unsigned char)*p
)) goto safe
;
71 else if (ctx
->f
&URLF_LAX
) goto safe
;
74 if (ctx
->f
&URLF_STRICT
) goto unsafe
; /* else fall through... */
75 safe
: case '-': case '.': case '_':
77 unsafe
: case '+': case '%': case '=': case '&': case ';':
78 dstr_putf(d
, "%%%02x", *p
); break;
84 /* --- @url_enc@ --- *
86 * Arguments: @url_ectx *ctx@ = pointer to encoding context
87 * @dstr *d@ = pointer to output string
88 * @const char *name@ = pointer to name
89 * @const char *value@ = pointer to value
93 * Use: Writes an assignment between @name@ and @value@ to the
94 * output string, encoding the values properly.
97 void url_enc(url_ectx
*ctx
, dstr
*d
, const char *name
, const char *value
)
99 if (ctx
->f
& URLF_SEP
)
100 DPUTC(d
, (ctx
->f
& URLF_SEMI
) ?
';' : '&');
101 encode(ctx
, d
, name
);
103 encode(ctx
, d
, value
);
108 /* --- @url_initdec@ --- *
110 * Arguments: @url_dctx *ctx@ = pointer to context block
111 * @const char *p@ = string to read data from
115 * Use: Initializes a URL decoding context.
118 void url_initdec(url_dctx
*ctx
, const char *p
) { ctx
->p
= p
; ctx
->f
= 0; }
120 /* --- @decode@ --- *
122 * Arguments: @url_dctx *ctx@ = pointer to the context
123 * @dstr *d@ = pointer to output string
124 * @const char *p@ = pointer to input data
125 * @int eq@ = whether to stop at `=' characters
127 * Returns: Pointer to next available character.
129 * Use: Does a URL decode.
132 static const char *decode(url_dctx
*ctx
, dstr
*d
, const char *p
, int eq
)
143 if (ctx
->f
& URLF_SEMI
)
155 int x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
171 /* --- @url_dec@ --- *
173 * Arguments: @url_dctx *ctx@ = pointer to decode context
174 * @dstr *n@ = pointer to output string for name
175 * @dstr *v@ = pointer to output string for value
177 * Returns: Nonzero if it read something, zero if there's nothing left
179 * Use: Decodes the next name/value pair from a urlencoded string.
182 int url_dec(url_dctx
*ctx
, dstr
*n
, dstr
*v
)
184 const char *p
= ctx
->p
;
188 if ((p
= decode(ctx
, n
, p
, 1)) == 0 || *p
== 0)
196 if ((p
= decode(ctx
, v
, p
, 0)) == 0)
204 /*----- That's all, folks -------------------------------------------------*/