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.
38 #include "internal/numbers.h"
44 * We're most likely overcautious here, but let's reserve for broken WinCE
45 * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
46 * builds are compiled with -DUNICODE [as well as -D_UNICODE].
48 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
49 # define FindFirstFile FindFirstFileW
51 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
52 # define FindNextFile FindNextFileW
60 # define CP_DEFAULT CP_UTF8
62 # define CP_DEFAULT CP_ACP
65 struct LP_dir_context_st {
68 char entry_name[NAME_MAX + 1];
71 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
73 if (ctx == NULL || directory == NULL) {
80 size_t dirlen = strlen(directory);
82 if (dirlen == 0 || dirlen > INT_MAX - 3) {
87 *ctx = malloc(sizeof(**ctx));
92 memset(*ctx, 0, sizeof(**ctx));
94 if (sizeof(TCHAR) != sizeof(char)) {
96 /* len_0 denotes string length *with* trailing 0 */
97 size_t index = 0, len_0 = dirlen + 1;
98 #ifdef LP_MULTIBYTE_AVAILABLE
104 if ((sz = MultiByteToWideChar((cp = CP_UTF8), 0,
107 GetLastError() != ERROR_NO_UNICODE_TRANSLATION)
110 sz = MultiByteToWideChar((cp = CP_ACP), 0,
117 * allocate two additional characters in case we need to
118 * concatenate asterisk, |sz| covers trailing '\0'!
120 wdir = _alloca((sz + 2) * sizeof(TCHAR));
121 if (!MultiByteToWideChar(cp, 0, directory, len_0,
122 (WCHAR *)wdir, sz)) {
133 * allocate two additional characters in case we need to
134 * concatenate asterisk, |sz| covers trailing '\0'!
136 wdir = _alloca((sz + 2) * sizeof(TCHAR));
137 for (index = 0; index < len_0; index++)
138 wdir[index] = (TCHAR)directory[index];
141 sz--; /* wdir[sz] is trailing '\0' now */
142 if (wdir[sz - 1] != TEXT('*')) {
143 if (wdir[sz - 1] != TEXT('/') && wdir[sz - 1] != TEXT('\\'))
144 _tcscpy(wdir + sz, TEXT("/*"));
146 _tcscpy(wdir + sz, TEXT("*"));
149 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
151 if (directory[dirlen - 1] != '*') {
152 char *buf = _alloca(dirlen + 3);
154 strcpy(buf, directory);
155 if (buf[dirlen - 1] != '/' && buf[dirlen - 1] != '\\')
156 strcpy(buf + dirlen, "/*");
158 strcpy(buf + dirlen, "*");
163 (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
166 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
173 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
177 if (sizeof(TCHAR) != sizeof(char)) {
178 TCHAR *wdir = (*ctx)->ctx.cFileName;
179 size_t index, len_0 = 0;
181 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
185 #ifdef LP_MULTIBYTE_AVAILABLE
186 if (!WideCharToMultiByte(CP_DEFAULT, 0, (WCHAR *)wdir, len_0,
188 sizeof((*ctx)->entry_name), NULL, 0))
190 for (index = 0; index < len_0; index++)
191 (*ctx)->entry_name[index] = (char)wdir[index];
193 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
194 sizeof((*ctx)->entry_name) - 1);
196 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
198 return (*ctx)->entry_name;
201 int LP_find_file_end(LP_DIR_CTX **ctx)
203 if (ctx != NULL && *ctx != NULL) {
204 FindClose((*ctx)->handle);