escape.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. unsigned char in;
  40. int newlen = alloc;
  41. int index=0;
  42. length = alloc-1;
  43. while(length--) {
  44. in = *string;
  45. if(!(in >= 'a' && in <= 'z') &&
  46. !(in >= 'A' && in <= 'Z') &&
  47. !(in >= '0' && in <= '9')) {
  48. /* encode it */
  49. newlen += 2; /* the size grows with two, since this'll become a %XX */
  50. if(newlen > alloc) {
  51. alloc *= 2;
  52. ns = realloc(ns, alloc);
  53. if(!ns)
  54. return NULL;
  55. }
  56. sprintf(&ns[index], "%%%02X", in);
  57. index+=3;
  58. }
  59. else {
  60. /* just copy this */
  61. ns[index++]=in;
  62. }
  63. string++;
  64. }
  65. ns[index]=0; /* terminate it */
  66. return ns;
  67. }
  68. char *curl_unescape(const char *string, int length)
  69. {
  70. int alloc = (length?length:(int)strlen(string))+1;
  71. char *ns = malloc(alloc);
  72. unsigned char in;
  73. int index=0;
  74. unsigned int hex;
  75. while(--alloc > 0) {
  76. in = *string;
  77. if('%' == in) {
  78. /* encoded part */
  79. if(sscanf(string+1, "%02X", &hex)) {
  80. in = hex;
  81. string+=2;
  82. alloc-=2;
  83. }
  84. }
  85. ns[index++] = in;
  86. string++;
  87. }
  88. ns[index]=0; /* terminate it */
  89. return ns;
  90. }
  91. void curl_free(void *p)
  92. {
  93. free(p);
  94. }
  95. /*
  96. * local variables:
  97. * eval: (load-file "../curl-mode.el")
  98. * end:
  99. * vim600: fdm=marker
  100. * vim: et sw=2 ts=2 sts=2 tw=78
  101. */