db3dfce790f9a87c9e80ac2a4de91ac64606deee
[openssl.git] / crypto / LPdir_win.c
1 /*
2  * Copyright 2004-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
10 /*
11  * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <windows.h>
37 #include <tchar.h>
38 #ifndef LPDIR_H
39 # include "LPdir.h"
40 #endif
41
42 /*
43  * We're most likely overcautious here, but let's reserve for broken WinCE
44  * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
45  * builds are compiled with -DUNICODE [as well as -D_UNICODE].
46  */
47 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
48 # define FindFirstFile FindFirstFileW
49 #endif
50 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
51 # define FindNextFile FindNextFileW
52 #endif
53
54 #ifndef NAME_MAX
55 # define NAME_MAX 255
56 #endif
57
58 #ifdef CP_UTF8
59 # define CP_DEFAULT CP_UTF8
60 #else
61 # define CP_DEFAULT CP_ACP
62 #endif
63
64 struct LP_dir_context_st {
65     WIN32_FIND_DATA ctx;
66     HANDLE handle;
67     char entry_name[NAME_MAX + 1];
68 };
69
70 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
71 {
72     if (ctx == NULL || directory == NULL) {
73         errno = EINVAL;
74         return 0;
75     }
76
77     errno = 0;
78     if (*ctx == NULL) {
79         size_t dirlen = strlen(directory);
80
81         if (dirlen == 0 || dirlen > INT_MAX - 3) {
82             errno = ENOENT;
83             return 0;
84         }
85
86         *ctx = malloc(sizeof(**ctx));
87         if (*ctx == NULL) {
88             errno = ENOMEM;
89             return 0;
90         }
91         memset(*ctx, 0, sizeof(**ctx));
92
93         if (sizeof(TCHAR) != sizeof(char)) {
94             TCHAR *wdir = NULL;
95             /* len_0 denotes string length *with* trailing 0 */
96             size_t index = 0, len_0 = dirlen + 1;
97 #ifdef LP_MULTIBYTE_AVAILABLE
98             int sz = 0;
99             UINT cp;
100
101             do {
102 # ifdef CP_UTF8
103                 if ((sz = MultiByteToWideChar((cp = CP_UTF8), 0,
104                                               directory, len_0,
105                                               NULL, 0)) > 0 ||
106                     GetLastError() != ERROR_NO_UNICODE_TRANSLATION)
107                     break;
108 # endif
109                 sz = MultiByteToWideChar((cp = CP_ACP), 0,
110                                          directory, len_0,
111                                          NULL, 0);
112             } while (0);
113
114             if (sz > 0) {
115                 /*
116                  * allocate two additional characters in case we need to
117                  * concatenate asterisk, |sz| covers trailing '\0'!
118                  */
119                 wdir = _alloca((sz + 2) * sizeof(TCHAR));
120                 if (!MultiByteToWideChar(cp, 0, directory, len_0,
121                                          (WCHAR *)wdir, sz)) {
122                     free(*ctx);
123                     *ctx = NULL;
124                     errno = EINVAL;
125                     return 0;
126                 }
127             } else
128 #endif
129             {
130                 sz = len_0;
131                 /*
132                  * allocate two additional characters in case we need to
133                  * concatenate asterisk, |sz| covers trailing '\0'!
134                  */
135                 wdir = _alloca((sz + 2) * sizeof(TCHAR));
136                 for (index = 0; index < len_0; index++)
137                     wdir[index] = (TCHAR)directory[index];
138             }
139
140             sz--; /* wdir[sz] is trailing '\0' now */
141             if (wdir[sz - 1] != TEXT('*')) {
142                 if (wdir[sz - 1] != TEXT('/') && wdir[sz - 1] != TEXT('\\'))
143                     _tcscpy(wdir + sz, TEXT("/*"));
144                 else
145                     _tcscpy(wdir + sz, TEXT("*"));
146             }
147
148             (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
149         } else {
150             if (directory[dirlen - 1] != '*') {
151                 char *buf = _alloca(dirlen + 3);
152
153                 strcpy(buf, directory);
154                 if (buf[dirlen - 1] != '/' && buf[dirlen - 1] != '\\')
155                     strcpy(buf + dirlen, "/*");
156                 else
157                     strcpy(buf + dirlen, "*");
158
159                 directory = buf;
160             }
161
162             (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
163         }
164
165         if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
166             free(*ctx);
167             *ctx = NULL;
168             errno = EINVAL;
169             return 0;
170         }
171     } else {
172         if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
173             return 0;
174         }
175     }
176     if (sizeof(TCHAR) != sizeof(char)) {
177         TCHAR *wdir = (*ctx)->ctx.cFileName;
178         size_t index, len_0 = 0;
179
180         while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
181             len_0++;
182         len_0++;
183
184 #ifdef LP_MULTIBYTE_AVAILABLE
185         if (!WideCharToMultiByte(CP_DEFAULT, 0, (WCHAR *)wdir, len_0,
186                                  (*ctx)->entry_name,
187                                  sizeof((*ctx)->entry_name), NULL, 0))
188 #endif
189             for (index = 0; index < len_0; index++)
190                 (*ctx)->entry_name[index] = (char)wdir[index];
191     } else
192         strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
193                 sizeof((*ctx)->entry_name) - 1);
194
195     (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
196
197     return (*ctx)->entry_name;
198 }
199
200 int LP_find_file_end(LP_DIR_CTX **ctx)
201 {
202     if (ctx != NULL && *ctx != NULL) {
203         FindClose((*ctx)->handle);
204         free(*ctx);
205         *ctx = NULL;
206         return 1;
207     }
208     errno = EINVAL;
209     return 0;
210 }