1
0

escape.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <[email protected]>, 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$
  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. #include "curl_memory.h"
  32. #define _MPRINTF_REPLACE /* use our functions only */
  33. #include <curl/mprintf.h>
  34. /* The last #include file should be: */
  35. #include "memdebug.h"
  36. char *curl_escape(const char *string, int inlength)
  37. {
  38. size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
  39. char *ns;
  40. char *testing_ptr;
  41. unsigned char in;
  42. size_t newlen = alloc;
  43. int strindex=0;
  44. size_t length;
  45. ns = malloc(alloc);
  46. if(!ns)
  47. return NULL;
  48. length = alloc-1;
  49. while(length--) {
  50. in = *string;
  51. if(!(in >= 'a' && in <= 'z') &&
  52. !(in >= 'A' && in <= 'Z') &&
  53. !(in >= '0' && in <= '9')) {
  54. /* encode it */
  55. newlen += 2; /* the size grows with two, since this'll become a %XX */
  56. if(newlen > alloc) {
  57. alloc *= 2;
  58. testing_ptr = realloc(ns, alloc);
  59. if(!testing_ptr) {
  60. free( ns );
  61. return NULL;
  62. }
  63. else {
  64. ns = testing_ptr;
  65. }
  66. }
  67. snprintf(&ns[strindex], 4, "%%%02X", in);
  68. strindex+=3;
  69. }
  70. else {
  71. /* just copy this */
  72. ns[strindex++]=in;
  73. }
  74. string++;
  75. }
  76. ns[strindex]=0; /* terminate it */
  77. return ns;
  78. }
  79. #define ishex(in) ((in >= 'a' && in <= 'f') || \
  80. (in >= 'A' && in <= 'F') || \
  81. (in >= '0' && in <= '9'))
  82. char *curl_unescape(const char *string, int length)
  83. {
  84. int alloc = (length?length:(int)strlen(string))+1;
  85. char *ns = malloc(alloc);
  86. unsigned char in;
  87. int strindex=0;
  88. long hex;
  89. if( !ns )
  90. return NULL;
  91. while(--alloc > 0) {
  92. in = *string;
  93. if(('%' == in) && ishex(string[1]) && ishex(string[2])) {
  94. /* this is two hexadecimal digits following a '%' */
  95. char hexstr[3];
  96. char *ptr;
  97. hexstr[0] = string[1];
  98. hexstr[1] = string[2];
  99. hexstr[2] = 0;
  100. hex = strtol(hexstr, &ptr, 16);
  101. in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
  102. string+=2;
  103. alloc-=2;
  104. }
  105. ns[strindex++] = in;
  106. string++;
  107. }
  108. ns[strindex]=0; /* terminate it */
  109. return ns;
  110. }
  111. /* For operating systems/environments that use different malloc/free
  112. ssystems for the app and for this library, we provide a free that uses
  113. the library's memory system */
  114. void curl_free(void *p)
  115. {
  116. if(p)
  117. free(p);
  118. }