HTEscape.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:4k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*      HTEscape.c
  2. ** ESCAPE AND UNESACPE ILLEGAL CHARACTERS IN A URI
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. **
  7. ** history:
  8. ** Nov 13 94 Spawned from HTParse, as it then can be used in utility
  9. ** programs without loading the whole library
  10. */
  11. /* Library include files */
  12. #include "tcp.h"
  13. #include "HTUtils.h"
  14. #include "HTEscape.h"  /* Implemented here */
  15. #define HEX_ESCAPE '%'
  16. #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
  17. /*
  18. **  Not BOTH static AND CONST at the same time in gcc :-(, Henrik 18/03-94 
  19. **  code gen error in gcc when making random access to static CONST table(!!)
  20. */
  21. /*
  22. ** Bit 0 xalpha -- see HTFile.h
  23. ** Bit 1 xpalpha -- as xalpha but with plus.
  24. ** Bit 2 ... path -- as xpalpha but with /
  25. */
  26. PRIVATE unsigned char isAcceptable[96] =
  27. {/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  28.     0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x   !"#$%&'()*+,-./  */
  29.     7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x  0123456789:;<=>?  */
  30.     7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x  @ABCDEFGHIJKLMNO  */
  31.     7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X  PQRSTUVWXYZ[]^_  */
  32.     0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x  `abcdefghijklmno  */
  33.     7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 /* 7X  pqrstuvwxyz{}~ DEL */
  34. };
  35. PRIVATE char *hex = "0123456789ABCDEF";
  36. /* ------------------------------------------------------------------------- */
  37. /* Escape undesirable characters using % HTEscape()
  38. ** -------------------------------------
  39. **
  40. ** This function takes a pointer to a string in which
  41. ** some characters may be unacceptable unescaped.
  42. ** It returns a string which has these characters
  43. ** represented by a '%' character followed by two hex digits.
  44. **
  45. ** In the tradition of being conservative in what you do and liberal
  46. ** in what you accept, we encode some characters which in fact are
  47. ** allowed in URLs unencoded -- so DON'T use the table below for
  48. ** parsing! 
  49. **
  50. ** Unlike HTUnEscape(), this routine returns a malloced string.
  51. **
  52. */
  53. PUBLIC char * HTEscape (CONST char * str, HTURIEncoding mask)
  54. {
  55.     CONST char * p;
  56.     char * q;
  57.     char * result;
  58.     int unacceptable = 0;
  59.     for(p=str; *p; p++)
  60.         if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
  61. unacceptable++;
  62.     if ((result = (char  *) HT_MALLOC(p-str + unacceptable+ unacceptable + 1)) == NULL)
  63.         HT_OUTOFMEM("HTEscape");
  64.     for(q=result, p=str; *p; p++) {
  65.      unsigned char a = TOASCII(*p);
  66. if (!ACCEPTABLE(a)) {
  67.     *q++ = HEX_ESCAPE; /* Means hex commming */
  68.     *q++ = hex[a >> 4];
  69.     *q++ = hex[a & 15];
  70. }
  71. else *q++ = *p;
  72.     }
  73.     *q++ = 0; /* Terminate */
  74.     return result;
  75. }
  76. /* Decode %xx escaped characters HTUnEscape()
  77. ** -----------------------------
  78. **
  79. ** This function takes a pointer to a string in which some
  80. ** characters may have been encoded in %xy form, where xy is
  81. ** the acsii hex code for character 16x+y.
  82. ** The string is converted in place, as it will never grow.
  83. */
  84. PRIVATE char from_hex (char c)
  85. {
  86.     return  c >= '0' && c <= '9' ?  c - '0' 
  87.          : c >= 'A' && c <= 'F'? c - 'A' + 10
  88.          : c - 'a' + 10; /* accept small letters just in case */
  89. }
  90. PUBLIC char * HTUnEscape (char * str)
  91. {
  92.     char * p = str;
  93.     char * q = str;
  94.     if (!str) {       /* Just for safety ;-) */
  95. if (URI_TRACE)
  96.     TTYPrint(TDEST, "HTUnEscape.. Called with NULL argument.n");
  97. return "";
  98.     }
  99.     while(*p) {
  100.         if (*p == HEX_ESCAPE) {
  101.     p++;
  102.     if (*p) *q = from_hex(*p++) * 16;
  103.     if (*p) *q = FROMASCII(*q + from_hex(*p));
  104.     p++, q++;
  105. } else {
  106.     *q++ = *p++; 
  107. }
  108.     }
  109.     
  110.     *q++ = 0;
  111.     return str;
  112.     
  113. } /* HTUnEscape */