1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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)
30 /* mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" */
31 else if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
32 (ch >= '0' && ch <= '9') || strchr("-_.!~*'(|)", ch))
40 sprintf(dst+1, "%02X", (unsigned char ) ch);
44 void yaz_encode_uri_component(char *dst, const char *uri)
48 encode_uri_char(dst, *uri);
54 static unsigned char decode_uri_char(const char *path, size_t *len)
62 else if (*path == '%' && *len >= 3)
64 int d1 = hex_digit(path[1]);
65 int d2 = hex_digit(path[2]);
66 if (d1 >= 0 && d2 >= 0)
85 void yaz_decode_uri_component(char *dst, const char *uri, size_t len)
90 *dst++ = decode_uri_char(uri, &sz);
97 void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
99 size_t i, szp = 0, sz = 1;
100 for(i = 0; name[i]; i++)
101 sz += strlen(name[i]) + 3 + strlen(value[i]) * 3;
102 *path = (char *) odr_malloc(o, sz);
104 for(i = 0; name[i]; i++)
108 (*path)[szp++] = '&';
109 ilen = strlen(name[i]);
110 memcpy(*path+szp, name[i], ilen);
112 (*path)[szp++] = '=';
114 yaz_encode_uri_component(*path + szp, value[i]);
115 szp += strlen(*path + szp);
120 int yaz_uri_to_array(const char *path, ODR o, char ***name, char ***val)
130 while ((cp = strchr(cp, '&')))
135 *name = (char **) odr_malloc(o, no * sizeof(char*));
136 *val = (char **) odr_malloc(o, no * sizeof(char*));
138 for (no = 0; *path; no++)
140 const char *p1 = strchr(path, '=');
146 (*name)[no] = (char *) odr_malloc(o, (p1-path)+1);
147 memcpy((*name)[no], path, p1-path);
148 (*name)[no][p1-path] = '\0';
151 p1 = strchr(path, '&');
153 p1 = strlen(path) + path;
154 (*val)[no] = ret = (char *) odr_malloc(o, p1 - path + 1);
155 while (*path && *path != '&')
158 ret[i++] = decode_uri_char(path, &l);
171 char *yaz_uri_val(const char *path, const char *name, ODR o)
173 size_t nlen = strlen(name);
177 while (path && *path)
179 const char *p1 = strchr(path, '=');
182 if ((size_t)(p1 - path) == nlen && !memcmp(path, name, nlen))
188 p1 = strchr(path, '&');
190 p1 = strlen(path) + path;
191 ret = (char *) odr_malloc(o, p1 - path + 1);
192 while (*path && *path != '&')
195 ret[i++] = decode_uri_char(path, &l);
201 path = strchr(p1, '&');
211 * c-file-style: "Stroustrup"
212 * indent-tabs-mode: nil
214 * vim: shiftwidth=4 tabstop=8 expandtab