escape.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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. /* The last #include file should be: */
  32. #ifdef MALLOCDEBUG
  33. #include "memdebug.h"
  34. #endif
  35. char *curl_escape(const char *string, int length)
  36. {
  37. int alloc = (length?length:(int)strlen(string))+1;
  38. char *ns = malloc(alloc);
  39. char *testing_ptr = NULL;
  40. unsigned char in;
  41. int newlen = alloc;
  42. int index=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[index], "%%%02X", in);
  63. index+=3;
  64. }
  65. else {
  66. /* just copy this */
  67. ns[index++]=in;
  68. }
  69. string++;
  70. }
  71. ns[index]=0; /* terminate it */
  72. return ns;
  73. }
  74. char *curl_unescape(const char *string, int length)
  75. {
  76. int alloc = (length?length:(int)strlen(string))+1;
  77. char *ns = malloc(alloc);
  78. unsigned char in;
  79. int index=0;
  80. unsigned int hex;
  81. if( !ns ) {
  82. return NULL;
  83. }
  84. while(--alloc > 0) {
  85. in = *string;
  86. if('%' == in) {
  87. /* encoded part */
  88. if(sscanf(string+1, "%02X", &hex)) {
  89. in = hex;
  90. string+=2;
  91. alloc-=2;
  92. }
  93. }
  94. ns[index++] = in;
  95. string++;
  96. }
  97. ns[index]=0; /* terminate it */
  98. return ns;
  99. }
  100. void curl_free(void *p)
  101. {
  102. free(p);
  103. }
  104. /*
  105. * local variables:
  106. * eval: (load-file "../curl-mode.el")
  107. * end:
  108. * vim600: fdm=marker
  109. * vim: et sw=2 ts=2 sts=2 tw=78
  110. */