escape.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id: escape.c,v 1.30 2004/03/08 08:38:29 bagder Exp $
  22. ***************************************************************************/
  23. /* Escape and unescape URL encoding in strings. The functions return a new
  24. * allocated string or NULL if an error occurred. */
  25. #include "setup.h"
  26. #include <ctype.h>
  27. #include <curl/curl.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. /* The last #include file should be: */
  32. #ifdef CURLDEBUG
  33. #include "memdebug.h"
  34. #endif
  35. char *curl_escape(const char *string, int length)
  36. {
  37. size_t alloc = (length?(size_t)length:strlen(string))+1;
  38. char *ns = malloc(alloc);
  39. char *testing_ptr = NULL;
  40. unsigned char in;
  41. size_t newlen = alloc;
  42. int strindex=0;
  43. length = alloc-1;
  44. while(length--) {
  45. in = *string;
  46. if(!(in >= 'a' && in <= 'z') &&
  47. !(in >= 'A' && in <= 'Z') &&
  48. !(in >= '0' && in <= '9')) {
  49. /* encode it */
  50. newlen += 2; /* the size grows with two, since this'll become a %XX */
  51. if(newlen > alloc) {
  52. alloc *= 2;
  53. testing_ptr = realloc(ns, alloc);
  54. if(!testing_ptr) {
  55. free( ns );
  56. return NULL;
  57. }
  58. else {
  59. ns = testing_ptr;
  60. }
  61. }
  62. sprintf(&ns[strindex], "%%%02X", in);
  63. strindex+=3;
  64. }
  65. else {
  66. /* just copy this */
  67. ns[strindex++]=in;
  68. }
  69. string++;
  70. }
  71. ns[strindex]=0; /* terminate it */
  72. return ns;
  73. }
  74. #define ishex(in) ((in >= 'a' && in <= 'f') || \
  75. (in >= 'A' && in <= 'F') || \
  76. (in >= '0' && in <= '9'))
  77. char *curl_unescape(const char *string, int length)
  78. {
  79. int alloc = (length?length:(int)strlen(string))+1;
  80. char *ns = malloc(alloc);
  81. unsigned char in;
  82. int strindex=0;
  83. long hex;
  84. if( !ns ) {
  85. return NULL;
  86. }
  87. while(--alloc > 0) {
  88. in = *string;
  89. if(('%' == in) && ishex(string[1]) && ishex(string[2])) {
  90. /* this is two hexadecimal digits following a '%' */
  91. char hexstr[3];
  92. char *ptr;
  93. hexstr[0] = string[1];
  94. hexstr[1] = string[2];
  95. hexstr[2] = 0;
  96. hex = strtol(hexstr, &ptr, 16);
  97. in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
  98. string+=2;
  99. alloc-=2;
  100. }
  101. ns[strindex++] = in;
  102. string++;
  103. }
  104. ns[strindex]=0; /* terminate it */
  105. return ns;
  106. }
  107. /* For operating systems/environments that use different malloc/free
  108. ssystems for the app and for this library, we provide a free that uses
  109. the library's memory system */
  110. void curl_free(void *p)
  111. {
  112. free(p);
  113. }