[xiph-cvs] cvs commit: libshout/src util.c util.h
Brendan
brendan at xiph.org
Sun Jan 12 20:31:38 PST 2003
brendan 03/01/12 23:31:38
Modified: src util.c util.h
Log:
Add URL encoding function modified from libshout1.
Make indentation consistent.
Revision Changes Path
1.4 +96 -56 libshout/src/util.c
Index: util.c
===================================================================
RCS file: /usr/local/cvsroot/libshout/src/util.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- util.c 17 Aug 2002 04:47:54 -0000 1.3
+++ util.c 13 Jan 2003 04:31:38 -0000 1.4
@@ -11,77 +11,117 @@
char *util_strdup(const char *s)
{
- if (!s) return NULL;
+ if (!s)
+ return NULL;
+
return strdup(s);
}
int util_read_header(int sock, char *buff, unsigned long len)
{
- int read_bytes, ret;
- unsigned long pos;
- char c;
-
- read_bytes = 1;
- pos = 0;
- ret = 0;
-
- while ((read_bytes == 1) && (pos < (len - 1))) {
- read_bytes = 0;
-
- if ((read_bytes = recv(sock, &c, 1, 0))) {
- if (c != '\r') buff[pos++] = c;
- if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {
- ret = 1;
- break;
- }
- } else {
- break;
- }
- }
+ int read_bytes, ret;
+ unsigned long pos;
+ char c;
+
+ read_bytes = 1;
+ pos = 0;
+ ret = 0;
+
+ while ((read_bytes == 1) && (pos < (len - 1))) {
+ read_bytes = 0;
+
+ if ((read_bytes = recv(sock, &c, 1, 0))) {
+ if (c != '\r')
+ buff[pos++] = c;
+ if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {
+ ret = 1;
+ break;
+ }
+ } else {
+ break;
+ }
+ }
- if (ret) buff[pos] = '\0';
+ if (ret) buff[pos] = '\0';
- return ret;
+ return ret;
}
static char base64table[64] = {
- 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
- 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
- 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
- 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
+ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
+ 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
+ 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
+ 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
/* This isn't efficient, but it doesn't need to be */
char *util_base64_encode(char *data)
{
- int len = strlen(data);
- char *out = malloc(len*4/3 + 4);
- char *result = out;
- int chunk;
-
- while(len > 0) {
- chunk = (len >3)?3:len;
- *out++ = base64table[(*data & 0xFC)>>2];
- *out++ = base64table[((*data & 0x03)<<4) | ((*(data+1) & 0xF0) >> 4)];
- switch(chunk) {
- case 3:
- *out++ = base64table[((*(data+1) & 0x0F)<<2) | ((*(data+2) & 0xC0)>>6)];
- *out++ = base64table[(*(data+2)) & 0x3F];
- break;
- case 2:
- *out++ = base64table[((*(data+1) & 0x0F)<<2)];
- *out++ = '=';
- break;
- case 1:
- *out++ = '=';
- *out++ = '=';
- break;
- }
- data += chunk;
- len -= chunk;
- }
- *out = 0;
+ int len = strlen(data);
+ char *out = malloc(len*4/3 + 4);
+ char *result = out;
+ int chunk;
+
+ while(len > 0) {
+ chunk = (len >3)?3:len;
+ *out++ = base64table[(*data & 0xFC)>>2];
+ *out++ = base64table[((*data & 0x03)<<4) | ((*(data+1) & 0xF0) >> 4)];
+
+ switch(chunk) {
+ case 3:
+ *out++ = base64table[((*(data+1) & 0x0F)<<2) | ((*(data+2) & 0xC0)>>6)];
+ *out++ = base64table[(*(data+2)) & 0x3F];
+ break;
+ case 2:
+ *out++ = base64table[((*(data+1) & 0x0F)<<2)];
+ *out++ = '=';
+ break;
+ case 1:
+ *out++ = '=';
+ *out++ = '=';
+ break;
+ }
+ data += chunk;
+ len -= chunk;
+ }
+ *out = 0;
- return result;
+ return result;
}
+static char urltable[16] = {
+ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
+};
+
+/* modified from libshout1, which credits Rick Franchuk <rickf at transpect.net>.
+ * Caller must free result. */
+char *util_url_encode(const char *data) {
+ const char *p;
+ char *q, *dest;
+ int digit;
+ size_t n;
+
+ for (p = data, n = 0; *p; p++) {
+ n++;
+ if (!isalnum((int)*p))
+ n += 2;
+ }
+ if (!(dest = malloc(n)))
+ return NULL;
+
+ for (p = data, q = dest; *p; p++, q++) {
+ if (isalnum((int)*p)) {
+ *q = *p;
+ } else {
+ *q++ = '%';
+ digit = *p >> 4;
+ *q++ = urltable[digit];
+ digit = *p & 0xf;
+ *q = urltable[digit];
+ n += 2;
+ }
+ }
+ *q = '\0';
+
+ return dest;
+}
\ No newline at end of file
<p><p>1.3 +1 -0 libshout/src/util.h
Index: util.h
===================================================================
RCS file: /usr/local/cvsroot/libshout/src/util.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- util.h 16 Aug 2002 14:22:16 -0000 1.2
+++ util.h 13 Jan 2003 04:31:38 -0000 1.3
@@ -5,6 +5,7 @@
char *util_strdup(const char *s);
char *util_base64_encode(char *data);
+char *util_url_encode(const char *data);
int util_read_header(int sock, char *buff, unsigned long len);
#endif /* __LIBSHOUT_UTIL_H__ */
<p><p>--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list