home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / IJB20OS2 / SOURCE / ENCODE.C < prev    next >
C/C++ Source or Header  |  1997-09-16  |  2KB  |  141 lines

  1. char *encode_rcs = "$Id: encode.c,v 2.4 1997/08/22 12:47:45 ACJC Exp $";
  2. /* Written and copyright by the Anonymous Coders and Junkbusters Corporation.
  3.  * Will be made available under the GNU General Public License.
  4.  * This software comes with NO WARRANTY.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. char *url_code_map[256];
  12. char *cookie_code_map[256];
  13.  
  14. char *
  15. url_encode(char **code_map, unsigned char *s)
  16. {
  17.     char buf[BUFSIZ];
  18.     unsigned char c, *p;
  19.     char *m;
  20.  
  21.     static int one_shot = 1;
  22.  
  23.     if(one_shot) {
  24.  
  25.         /* initialize the code maps */
  26.  
  27.         int i;
  28.  
  29.         one_shot = 0;
  30.  
  31.         /* for cookies, we turn white-space into '+'
  32.          * hex encode comma and semi-colons
  33.          * and leave everything else alone.
  34.          */
  35.  
  36.         cookie_code_map[' '] = "+";
  37.  
  38.         sprintf(buf, "%%%02X", ',');
  39.         cookie_code_map[','] = strdup(buf);
  40.  
  41.         sprintf(buf, "%%%02X", ';');
  42.         cookie_code_map[';'] = strdup(buf);
  43.  
  44.         /* for url's, we do full URL encoding.        */
  45.         /* non-alphanumerics get turned into hex ...    */
  46.         for(i=0; i < 256; i++) {
  47.             if(isalnum(i) == 0) {
  48.                 sprintf(buf, "%%%02X", i);
  49.                 url_code_map[i] = strdup(buf);
  50.             }
  51.         }
  52.  
  53.         /* ... with the following 6 exceptions:        */
  54.         /* white-space gets turned into '+' ...        */
  55.  
  56.         url_code_map[' '] = "+";
  57.  
  58.         /* ... and these punctuation chars map to themselves */
  59.         url_code_map['-'] = "-";
  60.         url_code_map['_'] = "_";
  61.         url_code_map['.'] = ".";
  62.         url_code_map['*'] = "*";
  63.         url_code_map['@'] = "@";
  64.     }
  65.  
  66.     for(p = (unsigned char *) buf; (c = *s); s++) {
  67.         if((m = code_map[c])) {
  68.             strcpy((char *) p, m);
  69.             p += strlen(m);
  70.         } else {
  71.             *p++ = c;
  72.         }
  73.     }
  74.  
  75.     *p = '\0';
  76.  
  77.     return(strdup(buf));
  78. }
  79.  
  80. /* these decode a URL */
  81.  
  82. int
  83. xdtoi(char d)
  84. {
  85.     if((d >= '0') && (d <= '9')) return(d - '0'     );
  86.     if((d >= 'a') && (d <= 'f')) return(d - 'a' + 10);
  87.     if((d >= 'A') && (d <= 'F')) return(d - 'A' + 10);
  88.     return(0);
  89. }
  90.  
  91. int
  92. xtoi(char *s)
  93. {
  94.     char d1, d2;
  95.     int ret = 0;
  96.  
  97.     if(isxdigit(*s)) {
  98.         d1 = *s++;
  99.         if(isxdigit(*s)) {
  100.             d2 = *s++;
  101.             
  102.             ret = (xdtoi(d1) * 16) + xdtoi(d2);
  103.         }
  104.     }
  105.  
  106.     return(ret);
  107. }
  108.  
  109. char *
  110. url_decode(char *str)
  111. {
  112.     char *ret = strdup(str);
  113.     char *p, *q;
  114.  
  115.     p = str;
  116.     q = ret;
  117.  
  118.     while(*p) {
  119.         switch(*p) {
  120.         case '+':
  121.             p++;
  122.             *q++ = ' ';
  123.             break;
  124.         case '%':
  125.             if((*q = xtoi(p+1))) {
  126.                 p += 3;
  127.                 q++;
  128.             } else {
  129.                 /* malformed, just use it */
  130.                 *q++ = *p++;
  131.             }
  132.             break;
  133.         default:
  134.             *q++ = *p++;
  135.             break;
  136.         }
  137.     }
  138.     *q = '\0';
  139.     return(ret);
  140. }
  141.