crypto/LPdir_win.c: harmonize with o_fopen.c.
[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         const char *extdir = directory;
80         char *extdirbuf = NULL;
81         size_t dirlen = strlen(directory);
82
83         if (dirlen == 0) {
84             errno = ENOENT;
85             return 0;
86         }
87
88         *ctx = malloc(sizeof(**ctx));
89         if (*ctx == NULL) {
90             errno = ENOMEM;
91             return 0;
92         }
93         memset(*ctx, 0, sizeof(**ctx));
94
95         if (directory[dirlen - 1] != '*') {
96             extdirbuf = (char *)malloc(dirlen + 3);
97             if (extdirbuf == NULL) {
98                 free(*ctx);
99                 *ctx = NULL;
100                 errno = ENOMEM;
101                 return 0;
102             }
103             if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
104                 extdir = strcat(strcpy(extdirbuf, directory), "/*");
105             else
106                 extdir = strcat(strcpy(extdirbuf, directory), "*");
107         }
108
109         if (sizeof(TCHAR) != sizeof(char)) {
110             TCHAR *wdir = NULL;
111             /* len_0 denotes string length *with* trailing 0 */
112             size_t index = 0, len_0 = strlen(extdir) + 1;
113 #ifdef LP_MULTIBYTE_AVAILABLE
114             int sz = 0;
115             UINT cp;
116
117             do {
118 # ifdef CP_UTF8
119                 if ((sz = MultiByteToWideChar((cp = CP_UTF8), 0, extdir, len_0,
120                                               NULL, 0)) > 0 ||
121                     GetLastError() != ERROR_NO_UNICODE_TRANSLATION)
122                     break;
123 # endif
124                 sz = MultiByteToWideChar((cp = CP_ACP), 0, extdir, len_0,
125                                          NULL, 0);
126             } while (0);
127
128             if (sz > 0) {
129                 wdir = _alloca(sz * sizeof(TCHAR));
130                 if (!MultiByteToWideChar(cp, 0, extdir, len_0, wdir, sz)) {
131                     if (extdirbuf != NULL) {
132                         free(extdirbuf);
133                     }
134                     free(*ctx);
135                     *ctx = NULL;
136                     errno = EINVAL;
137                     return 0;
138                 }
139             } else
140 #endif
141             {
142                 wdir = _alloca(len_0 * sizeof(TCHAR));
143                 for (index = 0; index < len_0; index++)
144                     wdir[index] = (TCHAR)extdir[index];
145             }
146
147             (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
148         } else {
149             (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
150         }
151         if (extdirbuf != NULL) {
152             free(extdirbuf);
153         }
154
155         if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
156             free(*ctx);
157             *ctx = NULL;
158             errno = EINVAL;
159             return 0;
160         }
161     } else {
162         if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
163             return 0;
164         }
165     }
166     if (sizeof(TCHAR) != sizeof(char)) {
167         TCHAR *wdir = (*ctx)->ctx.cFileName;
168         size_t index, len_0 = 0;
169
170         while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
171             len_0++;
172         len_0++;
173
174 #ifdef LP_MULTIBYTE_AVAILABLE
175         if (!WideCharToMultiByte(CP_DEFAULT, 0, (WCHAR *)wdir, len_0,
176                                  (*ctx)->entry_name,
177                                  sizeof((*ctx)->entry_name), NULL, 0))
178 #endif
179             for (index = 0; index < len_0; index++)
180                 (*ctx)->entry_name[index] = (char)wdir[index];
181     } else
182         strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
183                 sizeof((*ctx)->entry_name) - 1);
184
185     (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
186
187     return (*ctx)->entry_name;
188 }
189
190 int LP_find_file_end(LP_DIR_CTX **ctx)
191 {
192     if (ctx != NULL && *ctx != NULL) {
193         FindClose((*ctx)->handle);
194         free(*ctx);
195         *ctx = NULL;
196         return 1;
197     }
198     errno = EINVAL;
199     return 0;
200 }