VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.0g/crypto/LPdir_win.c@ 69890

Last change on this file since 69890 was 69890, checked in by vboxsync, 7 years ago

Added OpenSSL 1.1.0g with unneeded files removed, otherwise unmodified.
bugref:8070: src/libs maintenance

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
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 <[email protected]>
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#include "internal/numbers.h"
39#ifndef LPDIR_H
40# include "LPdir.h"
41#endif
42
43/*
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].
47 */
48#if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
49# define FindFirstFile FindFirstFileW
50#endif
51#if defined(LP_SYS_WINCE) && !defined(FindNextFile)
52# define FindNextFile FindNextFileW
53#endif
54
55#ifndef NAME_MAX
56# define NAME_MAX 255
57#endif
58
59#ifdef CP_UTF8
60# define CP_DEFAULT CP_UTF8
61#else
62# define CP_DEFAULT CP_ACP
63#endif
64
65struct LP_dir_context_st {
66 WIN32_FIND_DATA ctx;
67 HANDLE handle;
68 char entry_name[NAME_MAX + 1];
69};
70
71const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
72{
73 if (ctx == NULL || directory == NULL) {
74 errno = EINVAL;
75 return 0;
76 }
77
78 errno = 0;
79 if (*ctx == NULL) {
80 size_t dirlen = strlen(directory);
81
82 if (dirlen == 0 || dirlen > INT_MAX - 3) {
83 errno = ENOENT;
84 return 0;
85 }
86
87 *ctx = malloc(sizeof(**ctx));
88 if (*ctx == NULL) {
89 errno = ENOMEM;
90 return 0;
91 }
92 memset(*ctx, 0, sizeof(**ctx));
93
94 if (sizeof(TCHAR) != sizeof(char)) {
95 TCHAR *wdir = NULL;
96 /* len_0 denotes string length *with* trailing 0 */
97 size_t index = 0, len_0 = dirlen + 1;
98#ifdef LP_MULTIBYTE_AVAILABLE
99 int sz = 0;
100 UINT cp;
101
102 do {
103# ifdef CP_UTF8
104 if ((sz = MultiByteToWideChar((cp = CP_UTF8), 0,
105 directory, len_0,
106 NULL, 0)) > 0 ||
107 GetLastError() != ERROR_NO_UNICODE_TRANSLATION)
108 break;
109# endif
110 sz = MultiByteToWideChar((cp = CP_ACP), 0,
111 directory, len_0,
112 NULL, 0);
113 } while (0);
114
115 if (sz > 0) {
116 /*
117 * allocate two additional characters in case we need to
118 * concatenate asterisk, |sz| covers trailing '\0'!
119 */
120 wdir = _alloca((sz + 2) * sizeof(TCHAR));
121 if (!MultiByteToWideChar(cp, 0, directory, len_0,
122 (WCHAR *)wdir, sz)) {
123 free(*ctx);
124 *ctx = NULL;
125 errno = EINVAL;
126 return 0;
127 }
128 } else
129#endif
130 {
131 sz = len_0;
132 /*
133 * allocate two additional characters in case we need to
134 * concatenate asterisk, |sz| covers trailing '\0'!
135 */
136 wdir = _alloca((sz + 2) * sizeof(TCHAR));
137 for (index = 0; index < len_0; index++)
138 wdir[index] = (TCHAR)directory[index];
139 }
140
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("/*"));
145 else
146 _tcscpy(wdir + sz, TEXT("*"));
147 }
148
149 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
150 } else {
151 if (directory[dirlen - 1] != '*') {
152 char *buf = _alloca(dirlen + 3);
153
154 strcpy(buf, directory);
155 if (buf[dirlen - 1] != '/' && buf[dirlen - 1] != '\\')
156 strcpy(buf + dirlen, "/*");
157 else
158 strcpy(buf + dirlen, "*");
159
160 directory = buf;
161 }
162
163 (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
164 }
165
166 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
167 free(*ctx);
168 *ctx = NULL;
169 errno = EINVAL;
170 return 0;
171 }
172 } else {
173 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
174 return 0;
175 }
176 }
177 if (sizeof(TCHAR) != sizeof(char)) {
178 TCHAR *wdir = (*ctx)->ctx.cFileName;
179 size_t index, len_0 = 0;
180
181 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
182 len_0++;
183 len_0++;
184
185#ifdef LP_MULTIBYTE_AVAILABLE
186 if (!WideCharToMultiByte(CP_DEFAULT, 0, (WCHAR *)wdir, len_0,
187 (*ctx)->entry_name,
188 sizeof((*ctx)->entry_name), NULL, 0))
189#endif
190 for (index = 0; index < len_0; index++)
191 (*ctx)->entry_name[index] = (char)wdir[index];
192 } else
193 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
194 sizeof((*ctx)->entry_name) - 1);
195
196 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
197
198 return (*ctx)->entry_name;
199}
200
201int LP_find_file_end(LP_DIR_CTX **ctx)
202{
203 if (ctx != NULL && *ctx != NULL) {
204 FindClose((*ctx)->handle);
205 free(*ctx);
206 *ctx = NULL;
207 return 1;
208 }
209 errno = EINVAL;
210 return 0;
211}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette