A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
5.8 KiB

15 years ago
  1. /*
  2. * Copyright 2003-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <ctype.h>
  10. #include <limits.h>
  11. #include <e_os.h>
  12. #include <openssl/crypto.h>
  13. #include "internal/cryptlib.h"
  14. #include "internal/o_str.h"
  15. #if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \
  16. !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_WINCE) && \
  17. !defined(NETWARE_CLIB)
  18. # include <strings.h>
  19. #endif
  20. int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
  21. {
  22. #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
  23. while (*str1 && *str2 && n) {
  24. int res = toupper(*str1) - toupper(*str2);
  25. if (res)
  26. return res < 0 ? -1 : 1;
  27. str1++;
  28. str2++;
  29. n--;
  30. }
  31. if (n == 0)
  32. return 0;
  33. if (*str1)
  34. return 1;
  35. if (*str2)
  36. return -1;
  37. return 0;
  38. #else
  39. /*
  40. * Recursion hazard warning! Whenever strncasecmp is #defined as
  41. * OPENSSL_strncasecmp, OPENSSL_IMPLEMENTS_strncasecmp must be defined as
  42. * well.
  43. */
  44. return strncasecmp(str1, str2, n);
  45. #endif
  46. }
  47. int OPENSSL_strcasecmp(const char *str1, const char *str2)
  48. {
  49. #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
  50. return OPENSSL_strncasecmp(str1, str2, (size_t)-1);
  51. #else
  52. return strcasecmp(str1, str2);
  53. #endif
  54. }
  55. int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
  56. {
  57. const unsigned char *c1 = v1, *c2 = v2;
  58. int ret = 0;
  59. while (n && (ret = *c1 - *c2) == 0)
  60. n--, c1++, c2++;
  61. return ret;
  62. }
  63. char *CRYPTO_strdup(const char *str, const char* file, int line)
  64. {
  65. char *ret;
  66. size_t size;
  67. if (str == NULL)
  68. return NULL;
  69. size = strlen(str) + 1;
  70. ret = CRYPTO_malloc(size, file, line);
  71. if (ret != NULL)
  72. memcpy(ret, str, size);
  73. return ret;
  74. }
  75. char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
  76. {
  77. size_t maxlen;
  78. char *ret;
  79. if (str == NULL)
  80. return NULL;
  81. maxlen = OPENSSL_strnlen(str, s);
  82. ret = CRYPTO_malloc(maxlen + 1, file, line);
  83. if (ret) {
  84. memcpy(ret, str, maxlen);
  85. ret[maxlen] = '\0';
  86. }
  87. return ret;
  88. }
  89. void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
  90. {
  91. void *ret;
  92. if (data == NULL || siz >= INT_MAX)
  93. return NULL;
  94. ret = CRYPTO_malloc(siz, file, line);
  95. if (ret == NULL) {
  96. CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
  97. return NULL;
  98. }
  99. return memcpy(ret, data, siz);
  100. }
  101. size_t OPENSSL_strnlen(const char *str, size_t maxlen)
  102. {
  103. const char *p;
  104. for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
  105. return p - str;
  106. }
  107. size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
  108. {
  109. size_t l = 0;
  110. for (; size > 1 && *src; size--) {
  111. *dst++ = *src++;
  112. l++;
  113. }
  114. if (size)
  115. *dst = '\0';
  116. return l + strlen(src);
  117. }
  118. size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
  119. {
  120. size_t l = 0;
  121. for (; size > 0 && *dst; size--, dst++)
  122. l++;
  123. return l + OPENSSL_strlcpy(dst, src, size);
  124. }
  125. int OPENSSL_hexchar2int(unsigned char c)
  126. {
  127. #ifdef CHARSET_EBCDIC
  128. c = os_toebcdic[c];
  129. #endif
  130. switch (c) {
  131. case '0':
  132. return 0;
  133. case '1':
  134. return 1;
  135. case '2':
  136. return 2;
  137. case '3':
  138. return 3;
  139. case '4':
  140. return 4;
  141. case '5':
  142. return 5;
  143. case '6':
  144. return 6;
  145. case '7':
  146. return 7;
  147. case '8':
  148. return 8;
  149. case '9':
  150. return 9;
  151. case 'a': case 'A':
  152. return 0x0A;
  153. case 'b': case 'B':
  154. return 0x0B;
  155. case 'c': case 'C':
  156. return 0x0C;
  157. case 'd': case 'D':
  158. return 0x0D;
  159. case 'e': case 'E':
  160. return 0x0E;
  161. case 'f': case 'F':
  162. return 0x0F;
  163. }
  164. return -1;
  165. }
  166. /*
  167. * Give a string of hex digits convert to a buffer
  168. */
  169. unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
  170. {
  171. unsigned char *hexbuf, *q;
  172. unsigned char ch, cl;
  173. int chi, cli;
  174. const unsigned char *p;
  175. size_t s;
  176. s = strlen(str);
  177. if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
  178. CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
  179. return NULL;
  180. }
  181. for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
  182. ch = *p++;
  183. if (ch == ':')
  184. continue;
  185. cl = *p++;
  186. if (!cl) {
  187. CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
  188. CRYPTO_R_ODD_NUMBER_OF_DIGITS);
  189. OPENSSL_free(hexbuf);
  190. return NULL;
  191. }
  192. cli = OPENSSL_hexchar2int(cl);
  193. chi = OPENSSL_hexchar2int(ch);
  194. if (cli < 0 || chi < 0) {
  195. OPENSSL_free(hexbuf);
  196. CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
  197. return NULL;
  198. }
  199. *q++ = (unsigned char)((chi << 4) | cli);
  200. }
  201. if (len)
  202. *len = q - hexbuf;
  203. return hexbuf;
  204. }
  205. /*
  206. * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
  207. * hex representation @@@ (Contents of buffer are always kept in ASCII, also
  208. * on EBCDIC machines)
  209. */
  210. char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
  211. {
  212. const static char hexdig[] = "0123456789ABCDEF";
  213. char *tmp, *q;
  214. const unsigned char *p;
  215. int i;
  216. if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) {
  217. CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
  218. return NULL;
  219. }
  220. q = tmp;
  221. for (i = 0, p = buffer; i < len; i++, p++) {
  222. *q++ = hexdig[(*p >> 4) & 0xf];
  223. *q++ = hexdig[*p & 0xf];
  224. *q++ = ':';
  225. }
  226. q[-1] = 0;
  227. #ifdef CHARSET_EBCDIC
  228. ebcdic2ascii(tmp, tmp, q - tmp - 1);
  229. #endif
  230. return tmp;
  231. }