param.c (1812B)
1 /* 2 * Copy me if you can. 3 * by 20h 4 */ 5 6 #include <unistd.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <string.h> 10 11 #include "ind.h" 12 13 char * 14 paramenc(char *str, int len) 15 { 16 char *ret, add[6]; 17 int i, rlen, alen; 18 19 ret = NULL; 20 for (i = 0, rlen = 0; i < len; i++) { 21 if (str[i] < 0x20 || str[i] > 0x7E) 22 snprintf(add, sizeof(add), "%%%02x", str[i] & 0xFF); 23 else { 24 switch(str[i]) { 25 case ' ': 26 add[0] = '_'; 27 add[1] = '\0'; 28 break; 29 case '\'': 30 case '_': 31 case '%': 32 case '(': 33 case ')': 34 case '<': 35 case '>': 36 case '@': 37 case ',': 38 case ';': 39 case ':': 40 case '\\': 41 case '"': 42 case '/': 43 case '[': 44 case ']': 45 case '?': 46 case '=': 47 snprintf(add, sizeof(add), "%%%02x", 48 str[i] & 0xFF); 49 break; 50 default: 51 add[0] = str[i]; 52 add[1] = '\0'; 53 break; 54 } 55 } 56 alen = strlen(add); 57 ret = memdupcat(ret, rlen, add, alen+1); 58 rlen += alen; 59 } 60 61 return ret; 62 } 63 64 char * 65 paramdec(char *str, int *len) 66 { 67 char *ret, add, abuf[3], *eabuf; 68 int i, rlen, pbreak; 69 long nval; 70 71 ret = NULL; 72 for (i = 0, rlen = 0; i < *len; i++) { 73 add = '\0'; 74 pbreak = 0; 75 switch (str[i]) { 76 case '_': 77 add = ' '; 78 break; 79 case '%': 80 if (str[i+1] == '\n' || str[i+2] == '\0') { 81 pbreak = 1; 82 i++; 83 break; 84 } 85 if (str[i+1] == '\0') 86 break; 87 memmove(&abuf, &str[i+1], 2); 88 abuf[2] = '\0'; 89 90 eabuf = NULL; 91 nval = strtol((char *)&abuf, &eabuf, 16); 92 if (eabuf != NULL && *eabuf != '\0') 93 break; 94 95 add = (char)(nval & 0xFF); 96 i += 2; 97 break; 98 default: 99 add = str[i]; 100 break; 101 } 102 if (pbreak) 103 continue; 104 if (add == '\0') 105 break; 106 107 ret = reallocz(ret, ++rlen+1, 0); 108 ret[rlen-1] = add; 109 ret[rlen] = '\0'; 110 } 111 if (add == '\0' && ret != NULL) { 112 free(ret); 113 return NULL; 114 } 115 116 *len = rlen; 117 return ret; 118 } 119