Subj : Tool wanted To : Angus Mcleod From : Frank Reid Date : Thu Dec 02 2004 06:59 am Re: Tool wanted By: Angus Mcleod to Frank Reid on Wed Dec 01 2004 11:02 pm > > root@freebsd# urlencode 'Hello, World!' > > Hello%2C%20World%21 > > Damn! That's exactly what I need to do. :-/ I cobbled this together based on some code I found on Google for the RFC-1738 stuff. There are absolutely no bounds checking on the main routine, which only "encodes" the input string passed to the program. Maybe you can work something with this? [root@new root]# ./urlcodec 'Hello, World!' Hello%2C%20World%21 Hello, World! /* $Id: $ */ /* * * Copyright(c) 2003, B. Augestad * * COPYING RESTRICTIONS APPLY, see COPYING file. * * * * Implements encode and decode() functions for HTTP URL arguments * * according to RFC 1738. * * * * The short and simple rule is that if a character is A-Za-z0-9 * * it is not encoded, anything else is encoded. The character is * * encoded as a two digit hex number, prefixed with %. * * * * Issues: This version decodes %00, which maps to '\0'. * * Don't know if that's a serious issue or not(security). * */ #include #include /* Local helper * * Returns 1 on success, 0 on illegal input. * */ static int encode(int c, char *dest) { int high, low; /* Don't encode illegal input */ if (c < 0 || c > 255) return 0; high = (c & 0xf0) >> 4; low = (c & 0x0f); if (high > 9) high = 'A' + high - 10; else high = '0' + high; if (low > 9) low = 'A' + low - 10; else low = '0' + low; *dest++ = '%'; *dest++ = high; *dest = low; return 1; } /* Local helper. * * Accepts an alphanumeric character in the range [0-9a-fA-F] * * and returns it converted to an integer in the range 0-15. * */ static int hexchar2int(int c) { if (c >= '0' && c <= '9') return c - '0'; else if (c >= 'a' && c <= 'f') return 10 + c - 'a'; else if (c >= 'A' && c <= 'F') return 10 + c - 'A'; else /* Illegal input. */ return -1; } /* Local helper. * * Decodes an encoded character. Returns -1 on errors, else an * * integer which represents a decoded character. Note that hexchar2int * * will implicitly detect end of string ('\0'). * */ static int decode(const char *src) { int c1, c2; if (*src != '%') return -1; c1 = hexchar2int((unsigned char) *++src); if (c1 == -1) return -1; c2 = hexchar2int((unsigned char) *++src); if (c2 == -1) return -1; return (c1 << 4) + c2; } int clc_rfc1738_encode(char *dest, const char *src, size_t cb) { if ((dest == NULL) || (src == NULL) || (cb <= 0)) return 0; while (*src != '\0' && cb > 0) { /* No isalnum() due to locale */ if ((*src >= 'A' && *src <= 'Z') || (*src >= 'a' && *src <= 'z') || (*src >= '0' && *src <= '9')) { *dest++ = *src++; } else if (cb > 2) { if (!encode((unsigned char) *src, dest)) { printf("\nInvalid Input String.\n"); return 0; } dest += 3; cb -= 3; src++; } else { printf("\nOut of Room.\n"); return 0; } } /* If we ran out of buffer space */ if (*src != '\0') { printf("\nNo Buffer Available."); return 0; } *dest = '\0'; return 1; } int clc_rfc1738_decode(char *dest, const char *src, size_t cb) { int c; if ((src == NULL) || (dest == NULL)) return 0; while (cb > 0 && *src != '\0') { if (*src == '%') { if ((c = decode(src)) == -1) { printf("\nInvalid Input String.\n"); return 0; } *dest++ = c; src += 3; } else *dest++ = *src++; cb--; } if (cb == 0) { printf("\nNo Buffer Available."); return 0; } *dest = '\0'; return 1; } main(int argc, char *argv[]) { char s[1024], s1[1024]; strcpy(s1, argv[1]); clc_rfc1738_encode(s, s1, 1024); printf("%s\n", s); clc_rfc1738_decode(s1, s, 1024); printf("%s\n", s1); return 0; } --- þ Synchronet þ Wireless Neighbors -- telnet://wirelessneighbors.net .