2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
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].
47 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
48 # define FindFirstFile FindFirstFileW
50 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
51 # define FindNextFile FindNextFileW
59 # define CP_DEFAULT CP_UTF8
61 # define CP_DEFAULT CP_ACP
64 struct LP_dir_context_st {
67 char entry_name[NAME_MAX + 1];
70 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
72 if (ctx == NULL || directory == NULL) {
79 size_t dirlen = strlen(directory);
81 if (dirlen == 0 || dirlen > INT_MAX - 3) {
86 *ctx = malloc(sizeof(**ctx));
91 memset(*ctx, 0, sizeof(**ctx));
93 if (sizeof(TCHAR) != sizeof(char)) {
95 /* len_0 denotes string length *with* trailing 0 */
96 size_t index = 0, len_0 = dirlen + 1;
97 #ifdef LP_MULTIBYTE_AVAILABLE
103 if ((sz = MultiByteToWideChar((cp = CP_UTF8), 0,
106 GetLastError() != ERROR_NO_UNICODE_TRANSLATION)
109 sz = MultiByteToWideChar((cp = CP_ACP), 0,
116 * allocate two additional characters in case we need to
117 * concatenate asterisk, |sz| covers trailing '\0'!
119 wdir = _alloca((sz + 2) * sizeof(TCHAR));
120 if (!MultiByteToWideChar(cp, 0, directory, len_0,
121 (WCHAR *)wdir, sz)) {
132 * allocate two additional characters in case we need to
133 * concatenate asterisk, |sz| covers trailing '\0'!
135 wdir = _alloca((sz + 2) * sizeof(TCHAR));
136 for (index = 0; index < len_0; index++)
137 wdir[index] = (TCHAR)directory[index];
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("/*"));
145 _tcscpy(wdir + sz, TEXT("*"));
148 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
150 if (directory[dirlen - 1] != '*') {
151 char *buf = _alloca(dirlen + 3);
153 strcpy(buf, directory);
154 if (buf[dirlen - 1] != '/' && buf[dirlen - 1] != '\\')
155 strcpy(buf + dirlen, "/*");
157 strcpy(buf + dirlen, "*");
162 (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
165 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
172 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
176 if (sizeof(TCHAR) != sizeof(char)) {
177 TCHAR *wdir = (*ctx)->ctx.cFileName;
178 size_t index, len_0 = 0;
180 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
184 #ifdef LP_MULTIBYTE_AVAILABLE
185 if (!WideCharToMultiByte(CP_DEFAULT, 0, (WCHAR *)wdir, len_0,
187 sizeof((*ctx)->entry_name), NULL, 0))
189 for (index = 0; index < len_0; index++)
190 (*ctx)->entry_name[index] = (char)wdir[index];
192 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
193 sizeof((*ctx)->entry_name) - 1);
195 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
197 return (*ctx)->entry_name;
200 int LP_find_file_end(LP_DIR_CTX **ctx)
202 if (ctx != NULL && *ctx != NULL) {
203 FindClose((*ctx)->handle);