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 ------------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @url_initenc@ --- *
43 * Arguments: @url_ectx *ctx@ = pointer to context block
47 * Use: Initializes a URL encoding context.
50 void url_initenc(url_ectx
*ctx
) { ctx
->f
= 0; }
54 * Arguments: @url_ectx *ctx@ = encoding context
55 * @dstr *d@ = pointer to output string
56 * @const char *p@ = pointer to thing to encode
60 * Use: Encodes the input string into the output string.
63 static void encode(url_ectx
*ctx
, dstr
*d
, const char *p
)
67 case ' ': DPUTC(d
, '+');
70 if (ISSPACE(*p
)) goto unsafe
;
71 else if (ISALNUM(*p
)) goto safe
;
72 else if (ctx
->f
&URLF_LAX
) goto safe
;
75 if (ctx
->f
&URLF_STRICT
) goto unsafe
; /* else fall through... */
76 safe
: case '-': case '.': case '_':
78 unsafe
: case '+': case '%': case '=': case '&': case ';':
79 dstr_putf(d
, "%%%02x", *p
); break;
85 /* --- @url_enc@ --- *
87 * Arguments: @url_ectx *ctx@ = pointer to encoding context
88 * @dstr *d@ = pointer to output string
89 * @const char *name@ = pointer to name
90 * @const char *value@ = pointer to value
94 * Use: Writes an assignment between @name@ and @value@ to the
95 * output string, encoding the values properly.
98 void url_enc(url_ectx
*ctx
, dstr
*d
, const char *name
, const char *value
)
100 if (ctx
->f
& URLF_SEP
)
101 DPUTC(d
, (ctx
->f
& URLF_SEMI
) ?
';' : '&');
102 encode(ctx
, d
, name
);
104 encode(ctx
, d
, value
);
109 /* --- @url_initdec@ --- *
111 * Arguments: @url_dctx *ctx@ = pointer to context block
112 * @const char *p@ = string to read data from
116 * Use: Initializes a URL decoding context.
119 void url_initdec(url_dctx
*ctx
, const char *p
) { ctx
->p
= p
; ctx
->f
= 0; }
121 /* --- @decode@ --- *
123 * Arguments: @url_dctx *ctx@ = pointer to the context
124 * @dstr *d@ = pointer to output string
125 * @const char *p@ = pointer to input data
126 * @int eq@ = whether to stop at `=' characters
128 * Returns: Pointer to next available character.
130 * Use: Does a URL decode.
133 static const char *decode(url_dctx
*ctx
, dstr
*d
, const char *p
, int eq
)
144 if (ctx
->f
& URLF_SEMI
)
156 int x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
172 /* --- @url_dec@ --- *
174 * Arguments: @url_dctx *ctx@ = pointer to decode context
175 * @dstr *n@ = pointer to output string for name
176 * @dstr *v@ = pointer to output string for value
178 * Returns: Nonzero if it read something, zero if there's nothing left
180 * Use: Decodes the next name/value pair from a urlencoded string.
183 int url_dec(url_dctx
*ctx
, dstr
*n
, dstr
*v
)
185 const char *p
= ctx
->p
;
189 if ((p
= decode(ctx
, n
, p
, 1)) == 0 || *p
== 0)
197 if ((p
= decode(ctx
, v
, p
, 0)) == 0)
205 /*----- That's all, folks -------------------------------------------------*/