dotdot.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "dotdot.h"
  24. #include "curl_memory.h"
  25. /* The last #include file should be: */
  26. #include "memdebug.h"
  27. /*
  28. * "Remove Dot Segments"
  29. * http://tools.ietf.org/html/rfc3986#section-5.2.4
  30. */
  31. /*
  32. * Curl_dedotdotify()
  33. *
  34. * This function gets a zero-terminated path with dot and dotdot sequences
  35. * passed in and strips them off according to the rules in RFC 3986 section
  36. * 5.2.4.
  37. *
  38. * The function handles a query part ('?' + stuff) appended but it expects
  39. * that fragments ('#' + stuff) have already been cut off.
  40. *
  41. * RETURNS
  42. *
  43. * an allocated dedotdotified output string
  44. */
  45. char *Curl_dedotdotify(char *input)
  46. {
  47. size_t inlen = strlen(input);
  48. char *clone;
  49. size_t clen = inlen; /* the length of the cloned input */
  50. char *out = malloc(inlen+1);
  51. char *outptr;
  52. char *orgclone;
  53. char *queryp;
  54. if(!out)
  55. return NULL; /* out of memory */
  56. /* get a cloned copy of the input */
  57. clone = strdup(input);
  58. if(!clone) {
  59. free(out);
  60. return NULL;
  61. }
  62. orgclone = clone;
  63. outptr = out;
  64. /*
  65. * To handle query-parts properly, we must find it and remove it during the
  66. * dotdot-operation and then append it again at the end to the output
  67. * string.
  68. */
  69. queryp = strchr(clone, '?');
  70. if(queryp)
  71. *queryp = 0;
  72. do {
  73. /* A. If the input buffer begins with a prefix of "../" or "./", then
  74. remove that prefix from the input buffer; otherwise, */
  75. if(!strncmp("./", clone, 2)) {
  76. clone+=2;
  77. clen-=2;
  78. }
  79. else if(!strncmp("../", clone, 3)) {
  80. clone+=3;
  81. clen-=3;
  82. }
  83. /* B. if the input buffer begins with a prefix of "/./" or "/.", where
  84. "." is a complete path segment, then replace that prefix with "/" in
  85. the input buffer; otherwise, */
  86. else if(!strncmp("/./", clone, 3)) {
  87. clone+=2;
  88. clen-=2;
  89. }
  90. else if(!strcmp("/.", clone)) {
  91. clone[1]='/';
  92. clone++;
  93. clen-=1;
  94. }
  95. /* C. if the input buffer begins with a prefix of "/../" or "/..", where
  96. ".." is a complete path segment, then replace that prefix with "/" in
  97. the input buffer and remove the last segment and its preceding "/" (if
  98. any) from the output buffer; otherwise, */
  99. else if(!strncmp("/../", clone, 4)) {
  100. clone+=3;
  101. clen-=3;
  102. /* remove the last segment from the output buffer */
  103. while(outptr > out) {
  104. outptr--;
  105. if(*outptr == '/')
  106. break;
  107. }
  108. *outptr = 0; /* zero-terminate where it stops */
  109. }
  110. else if(!strcmp("/..", clone)) {
  111. clone[2]='/';
  112. clone+=2;
  113. clen-=2;
  114. /* remove the last segment from the output buffer */
  115. while(outptr > out) {
  116. outptr--;
  117. if(*outptr == '/')
  118. break;
  119. }
  120. *outptr = 0; /* zero-terminate where it stops */
  121. }
  122. /* D. if the input buffer consists only of "." or "..", then remove
  123. that from the input buffer; otherwise, */
  124. else if(!strcmp(".", clone) || !strcmp("..", clone)) {
  125. *clone=0;
  126. }
  127. else {
  128. /* E. move the first path segment in the input buffer to the end of
  129. the output buffer, including the initial "/" character (if any) and
  130. any subsequent characters up to, but not including, the next "/"
  131. character or the end of the input buffer. */
  132. do {
  133. *outptr++ = *clone++;
  134. clen--;
  135. } while(*clone && (*clone != '/'));
  136. *outptr = 0;
  137. }
  138. } while(*clone);
  139. if(queryp) {
  140. size_t qlen;
  141. /* There was a query part, append that to the output. The 'clone' string
  142. may now have been altered so we copy from the original input string
  143. from the correct index. */
  144. size_t oindex = queryp - orgclone;
  145. qlen = strlen(&input[oindex]);
  146. memcpy(outptr, &input[oindex], qlen+1); /* include the ending zero byte */
  147. }
  148. free(orgclone);
  149. return out;
  150. }