utils.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id$
  36. */
  37. #include <pro/uhttp/utils.h>
  38. #include <memdebug.h>
  39. #include <stdlib.h>
  40. #include <stdarg.h>
  41. #include <string.h>
  42. void HttpUrlUnescape(char *str)
  43. {
  44. char *ptr1, *ptr2, ch;
  45. char hexstr[3] = { 0, 0, 0 };
  46. for (ptr1 = ptr2 = str; *ptr1; ptr1++) {
  47. if (*ptr1 == '+')
  48. *ptr2++ = ' ';
  49. else if (*ptr1 == '%') {
  50. hexstr[0] = ptr1[1];
  51. hexstr[1] = ptr1[2];
  52. ch = (char) strtol(hexstr, 0, 16);
  53. *ptr2++ = ch;
  54. ptr1 += 2;
  55. } else
  56. *ptr2++ = *ptr1;
  57. }
  58. *ptr2 = 0;
  59. }
  60. static int DecodeHex(char c)
  61. {
  62. if (c >= '0' && c <= '9')
  63. return c - '0';
  64. if (c >= 'a' && c <= 'f')
  65. return c - 'a' + 10;
  66. if (c >= 'A' && c <= 'F')
  67. return c - 'A' + 10;
  68. return -1;
  69. }
  70. char *UriUnescape(char *path)
  71. {
  72. int x1;
  73. int x2;
  74. char *src = path;
  75. char *dst = path;
  76. char last = *path;
  77. if (last != '/')
  78. return 0;
  79. while (*++src) {
  80. if (*src == '%' &&
  81. (x1 = DecodeHex(*(src + 1))) >= 0 &&
  82. (x2 = DecodeHex(*(src + 2))) >= 0) {
  83. src += 2;
  84. if ((*src = x1 * 16 + x2) == 0)
  85. break;
  86. }
  87. if (*src == '\\')
  88. *src = '/';
  89. if ((last != '.' && last != '/') || (*src != '.' && *src != '/'))
  90. *dst++ = last = *src;
  91. }
  92. *dst = 0;
  93. return path;
  94. }
  95. static const char base64dtab[96] = {
  96. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 32..47 */
  97. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 48..63 */
  98. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 64..79 */
  99. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 80..95 */
  100. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 96..111 */
  101. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 112..127 */
  102. };
  103. char *HttpDecodeBase64(char *str)
  104. {
  105. char code;
  106. char *sp;
  107. char *tp;
  108. char last = -1;
  109. char step = 0;
  110. for (tp = sp = str; *sp; ++sp) {
  111. if (*sp < 32)
  112. continue;
  113. if ((code = base64dtab[(int) *sp - 32]) == (char)-1)
  114. continue;
  115. switch (step++) {
  116. case 1:
  117. *tp++ = ((last << 2) | ((code & 0x30) >> 4));
  118. break;
  119. case 2:
  120. *tp++ = (((last & 0xf) << 4) | ((code & 0x3c) >> 2));
  121. break;
  122. case 3:
  123. *tp++ = (((last & 0x03) << 6) | code);
  124. step = 0;
  125. break;
  126. }
  127. last = code;
  128. }
  129. *tp = 0;
  130. return str;
  131. }
  132. char *AllocConcatStrings(const char *str, ...)
  133. {
  134. va_list ap;
  135. int len;
  136. char *cp;
  137. char *rp;
  138. va_start(ap, str);
  139. for (len = strlen(str); (cp = va_arg(ap, char *)) != NULL; len += strlen(cp));
  140. va_end(ap);
  141. rp = malloc(len + 1);
  142. if (rp) {
  143. va_start(ap, str);
  144. for (strcpy(rp, str); (cp = va_arg(ap, char *)) != NULL; strcat(rp, cp));
  145. va_end(ap);
  146. }
  147. return rp;
  148. }
  149. char *AllocConcatStringLen(const char *str1, const char *str2, int len2)
  150. {
  151. int len1 = strlen(str1);
  152. char *rp = malloc(len1 + len2 + 1);
  153. if (rp) {
  154. strcpy(rp, str1);
  155. strncpy(rp + len1, str2, len2);
  156. *(rp + len1 + len2) = '\0';
  157. }
  158. return rp;
  159. }