Add back lost copyright and license text in LPdir_win.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 struct LP_dir_context_st {
59     WIN32_FIND_DATA ctx;
60     HANDLE handle;
61     char entry_name[NAME_MAX + 1];
62 };
63
64 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
65 {
66     if (ctx == NULL || directory == NULL) {
67         errno = EINVAL;
68         return 0;
69     }
70
71     errno = 0;
72     if (*ctx == NULL) {
73         const char *extdir = directory;
74         char *extdirbuf = NULL;
75         size_t dirlen = strlen(directory);
76
77         if (dirlen == 0) {
78             errno = ENOENT;
79             return 0;
80         }
81
82         *ctx = malloc(sizeof(**ctx));
83         if (*ctx == NULL) {
84             errno = ENOMEM;
85             return 0;
86         }
87         memset(*ctx, 0, sizeof(**ctx));
88
89         if (directory[dirlen - 1] != '*') {
90             extdirbuf = (char *)malloc(dirlen + 3);
91             if (extdirbuf == NULL) {
92                 free(*ctx);
93                 *ctx = NULL;
94                 errno = ENOMEM;
95                 return 0;
96             }
97             if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
98                 extdir = strcat(strcpy(extdirbuf, directory), "/*");
99             else
100                 extdir = strcat(strcpy(extdirbuf, directory), "*");
101         }
102
103         if (sizeof(TCHAR) != sizeof(char)) {
104             TCHAR *wdir = NULL;
105             /* len_0 denotes string length *with* trailing 0 */
106             size_t index = 0, len_0 = strlen(extdir) + 1;
107
108             wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
109             if (wdir == NULL) {
110                 if (extdirbuf != NULL) {
111                     free(extdirbuf);
112                 }
113                 free(*ctx);
114                 *ctx = NULL;
115                 errno = ENOMEM;
116                 return 0;
117             }
118 #ifdef LP_MULTIBYTE_AVAILABLE
119             if (!MultiByteToWideChar
120                 (CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
121 #endif
122                 for (index = 0; index < len_0; index++)
123                     wdir[index] = (TCHAR)extdir[index];
124
125             (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
126
127             free(wdir);
128         } else {
129             (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
130         }
131         if (extdirbuf != NULL) {
132             free(extdirbuf);
133         }
134
135         if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
136             free(*ctx);
137             *ctx = NULL;
138             errno = EINVAL;
139             return 0;
140         }
141     } else {
142         if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
143             return 0;
144         }
145     }
146     if (sizeof(TCHAR) != sizeof(char)) {
147         TCHAR *wdir = (*ctx)->ctx.cFileName;
148         size_t index, len_0 = 0;
149
150         while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
151             len_0++;
152         len_0++;
153
154 #ifdef LP_MULTIBYTE_AVAILABLE
155         if (!WideCharToMultiByte
156             (CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
157              sizeof((*ctx)->entry_name), NULL, 0))
158 #endif
159             for (index = 0; index < len_0; index++)
160                 (*ctx)->entry_name[index] = (char)wdir[index];
161     } else
162         strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
163                 sizeof((*ctx)->entry_name) - 1);
164
165     (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
166
167     return (*ctx)->entry_name;
168 }
169
170 int LP_find_file_end(LP_DIR_CTX **ctx)
171 {
172     if (ctx != NULL && *ctx != NULL) {
173         FindClose((*ctx)->handle);
174         free(*ctx);
175         *ctx = NULL;
176         return 1;
177     }
178     errno = EINVAL;
179     return 0;
180 }