1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SRW/SRU utilities.
12 #include <yaz/matchstr.h>
13 #include <yaz/yaz-iconv.h>
15 static int hex_digit (int ch)
17 if (ch >= '0' && ch <= '9')
19 else if (ch >= 'a' && ch <= 'f')
21 else if (ch >= 'A' && ch <= 'F')
26 static void encode_uri_char(char *dst, char ch)
28 /* mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" */
29 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
30 (ch >= '0' && ch <= '9') || strchr("-_.!~*'(|)", ch))
38 sprintf(dst+1, "%02X", (unsigned char ) ch);
42 void yaz_encode_uri_component(char *dst, const char *uri)
46 encode_uri_char(dst, *uri);
52 static unsigned char decode_uri_char(const char *path, size_t *len)
60 else if (*path == '%' && *len >= 3)
62 int d1 = hex_digit(path[1]);
63 int d2 = hex_digit(path[2]);
64 if (d1 >= 0 && d2 >= 0)
83 void yaz_decode_uri_component(char *dst, const char *uri, size_t len)
88 *dst++ = decode_uri_char(uri, &sz);
95 void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
97 size_t i, szp = 0, sz = 1;
98 for(i = 0; name[i]; i++)
99 sz += strlen(name[i]) + 3 + strlen(value[i]) * 3;
100 *path = (char *) odr_malloc(o, sz);
102 for(i = 0; name[i]; i++)
106 (*path)[szp++] = '&';
107 ilen = strlen(name[i]);
108 memcpy(*path+szp, name[i], ilen);
110 (*path)[szp++] = '=';
112 yaz_encode_uri_component(*path + szp, value[i]);
113 szp += strlen(*path + szp);
118 int yaz_uri_to_array(const char *path, ODR o, char ***name, char ***val)
128 while ((cp = strchr(cp, '&')))
133 *name = (char **) odr_malloc(o, no * sizeof(char*));
134 *val = (char **) odr_malloc(o, no * sizeof(char*));
136 for (no = 0; *path; no++)
138 const char *p1 = strchr(path, '=');
144 (*name)[no] = (char *) odr_malloc(o, (p1-path)+1);
145 memcpy((*name)[no], path, p1-path);
146 (*name)[no][p1-path] = '\0';
149 p1 = strchr(path, '&');
151 p1 = strlen(path) + path;
152 (*val)[no] = ret = (char *) odr_malloc(o, p1 - path + 1);
153 while (*path && *path != '&')
156 ret[i++] = decode_uri_char(path, &l);
169 char *yaz_uri_val(const char *path, const char *name, ODR o)
171 size_t nlen = strlen(name);
175 while (path && *path)
177 const char *p1 = strchr(path, '=');
180 if ((size_t)(p1 - path) == nlen && !memcmp(path, name, nlen))
186 p1 = strchr(path, '&');
188 p1 = strlen(path) + path;
189 ret = (char *) odr_malloc(o, p1 - path + 1);
190 while (*path && *path != '&')
193 ret[i++] = decode_uri_char(path, &l);
199 path = strchr(p1, '&');
209 * c-file-style: "Stroustrup"
210 * indent-tabs-mode: nil
212 * vim: shiftwidth=4 tabstop=8 expandtab