RT 4393: Call EC_GROUP_order_bits in priv2opt.
[openssl.git] / crypto / o_str.c
1 /*
2  * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
3  * 2003.
4  */
5 /* ====================================================================
6  * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <ctype.h>
60 #include <limits.h>
61 #include <e_os.h>
62 #include <openssl/crypto.h>
63 #include "internal/cryptlib.h"
64 #include "internal/o_str.h"
65
66 #if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \
67     !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_WINCE) && \
68     !defined(NETWARE_CLIB)
69 # include <strings.h>
70 #endif
71
72 int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
73 {
74 #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
75     while (*str1 && *str2 && n) {
76         int res = toupper(*str1) - toupper(*str2);
77         if (res)
78             return res < 0 ? -1 : 1;
79         str1++;
80         str2++;
81         n--;
82     }
83     if (n == 0)
84         return 0;
85     if (*str1)
86         return 1;
87     if (*str2)
88         return -1;
89     return 0;
90 #else
91     /*
92      * Recursion hazard warning! Whenever strncasecmp is #defined as
93      * OPENSSL_strncasecmp, OPENSSL_IMPLEMENTS_strncasecmp must be defined as
94      * well.
95      */
96     return strncasecmp(str1, str2, n);
97 #endif
98 }
99
100 int OPENSSL_strcasecmp(const char *str1, const char *str2)
101 {
102 #if defined(OPENSSL_IMPLEMENTS_strncasecmp)
103     return OPENSSL_strncasecmp(str1, str2, (size_t)-1);
104 #else
105     return strcasecmp(str1, str2);
106 #endif
107 }
108
109 int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
110 {
111     const unsigned char *c1 = v1, *c2 = v2;
112     int ret = 0;
113
114     while (n && (ret = *c1 - *c2) == 0)
115         n--, c1++, c2++;
116
117     return ret;
118 }
119
120 char *CRYPTO_strdup(const char *str, const char* file, int line)
121 {
122     char *ret;
123     size_t size;
124
125     if (str == NULL)
126         return NULL;
127     size = strlen(str) + 1;
128     ret = CRYPTO_malloc(size, file, line);
129     if (ret != NULL)
130         memcpy(ret, str, size);
131     return ret;
132 }
133
134 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
135 {
136     size_t maxlen;
137     char *ret;
138
139     if (str == NULL)
140         return NULL;
141
142     maxlen = OPENSSL_strnlen(str, s);
143
144     ret = CRYPTO_malloc(maxlen + 1, file, line);
145     if (ret) {
146         memcpy(ret, str, maxlen);
147         ret[maxlen] = '\0';
148     }
149     return ret;
150 }
151
152 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
153 {
154     void *ret;
155
156     if (data == NULL || siz >= INT_MAX)
157         return NULL;
158
159     ret = CRYPTO_malloc(siz, file, line);
160     if (ret == NULL) {
161         CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
162         return NULL;
163     }
164     return memcpy(ret, data, siz);
165 }
166
167 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
168 {
169     const char *p;
170
171     for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
172
173     return p - str;
174 }
175
176 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
177 {
178     size_t l = 0;
179     for (; size > 1 && *src; size--) {
180         *dst++ = *src++;
181         l++;
182     }
183     if (size)
184         *dst = '\0';
185     return l + strlen(src);
186 }
187
188 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
189 {
190     size_t l = 0;
191     for (; size > 0 && *dst; size--, dst++)
192         l++;
193     return l + OPENSSL_strlcpy(dst, src, size);
194 }