2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * This file is dual-licensed and is also available under the following
14 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "internal/numbers.h"
47 * We're most likely overcautious here, but let's reserve for broken WinCE
48 * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
49 * builds are compiled with -DUNICODE [as well as -D_UNICODE].
51 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
52 # define FindFirstFile FindFirstFileW
54 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
55 # define FindNextFile FindNextFileW
63 # define CP_DEFAULT CP_UTF8
65 # define CP_DEFAULT CP_ACP
68 struct LP_dir_context_st
{
71 char entry_name
[NAME_MAX
+ 1];
74 const char *LP_find_file(LP_DIR_CTX
**ctx
, const char *directory
)
76 if (ctx
== NULL
|| directory
== NULL
) {
83 size_t dirlen
= strlen(directory
);
85 if (dirlen
== 0 || dirlen
> INT_MAX
- 3) {
90 *ctx
= malloc(sizeof(**ctx
));
95 memset(*ctx
, 0, sizeof(**ctx
));
97 if (sizeof(TCHAR
) != sizeof(char)) {
99 /* len_0 denotes string length *with* trailing 0 */
100 size_t index
= 0, len_0
= dirlen
+ 1;
101 #ifdef LP_MULTIBYTE_AVAILABLE
107 if ((sz
= MultiByteToWideChar((cp
= CP_UTF8
), 0,
110 GetLastError() != ERROR_NO_UNICODE_TRANSLATION
)
113 sz
= MultiByteToWideChar((cp
= CP_ACP
), 0,
120 * allocate two additional characters in case we need to
121 * concatenate asterisk, |sz| covers trailing '\0'!
123 wdir
= _alloca((sz
+ 2) * sizeof(TCHAR
));
124 if (!MultiByteToWideChar(cp
, 0, directory
, len_0
,
125 (WCHAR
*)wdir
, sz
)) {
136 * allocate two additional characters in case we need to
137 * concatenate asterisk, |sz| covers trailing '\0'!
139 wdir
= _alloca((sz
+ 2) * sizeof(TCHAR
));
140 for (index
= 0; index
< len_0
; index
++)
141 wdir
[index
] = (TCHAR
)directory
[index
];
144 sz
--; /* wdir[sz] is trailing '\0' now */
145 if (wdir
[sz
- 1] != TEXT('*')) {
146 if (wdir
[sz
- 1] != TEXT('/') && wdir
[sz
- 1] != TEXT('\\'))
147 _tcscpy(wdir
+ sz
, TEXT("/*"));
149 _tcscpy(wdir
+ sz
, TEXT("*"));
152 (*ctx
)->handle
= FindFirstFile(wdir
, &(*ctx
)->ctx
);
154 if (directory
[dirlen
- 1] != '*') {
155 char *buf
= _alloca(dirlen
+ 3);
157 strcpy(buf
, directory
);
158 if (buf
[dirlen
- 1] != '/' && buf
[dirlen
- 1] != '\\')
159 strcpy(buf
+ dirlen
, "/*");
161 strcpy(buf
+ dirlen
, "*");
166 (*ctx
)->handle
= FindFirstFile((TCHAR
*)directory
, &(*ctx
)->ctx
);
169 if ((*ctx
)->handle
== INVALID_HANDLE_VALUE
) {
176 if (FindNextFile((*ctx
)->handle
, &(*ctx
)->ctx
) == FALSE
) {
180 if (sizeof(TCHAR
) != sizeof(char)) {
181 TCHAR
*wdir
= (*ctx
)->ctx
.cFileName
;
182 size_t index
, len_0
= 0;
184 while (wdir
[len_0
] && len_0
< (sizeof((*ctx
)->entry_name
) - 1))
188 #ifdef LP_MULTIBYTE_AVAILABLE
189 if (!WideCharToMultiByte(CP_DEFAULT
, 0, (WCHAR
*)wdir
, len_0
,
191 sizeof((*ctx
)->entry_name
), NULL
, 0))
193 for (index
= 0; index
< len_0
; index
++)
194 (*ctx
)->entry_name
[index
] = (char)wdir
[index
];
196 strncpy((*ctx
)->entry_name
, (const char *)(*ctx
)->ctx
.cFileName
,
197 sizeof((*ctx
)->entry_name
) - 1);
199 (*ctx
)->entry_name
[sizeof((*ctx
)->entry_name
) - 1] = '\0';
201 return (*ctx
)->entry_name
;
204 int LP_find_file_end(LP_DIR_CTX
**ctx
)
206 if (ctx
!= NULL
&& *ctx
!= NULL
) {
207 FindClose((*ctx
)->handle
);