dso_win32.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /* dso_win32.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "cryptlib.h"
  62. #include <openssl/dso.h>
  63. #if !defined(DSO_WIN32)
  64. DSO_METHOD *DSO_METHOD_win32(void)
  65. {
  66. return NULL;
  67. }
  68. #else
  69. # ifdef _WIN32_WCE
  70. # if _WIN32_WCE < 300
  71. static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
  72. {
  73. WCHAR lpProcNameW[64];
  74. int i;
  75. for (i = 0; lpProcName[i] && i < 64; i++)
  76. lpProcNameW[i] = (WCHAR)lpProcName[i];
  77. if (i == 64)
  78. return NULL;
  79. lpProcNameW[i] = 0;
  80. return GetProcAddressW(hModule, lpProcNameW);
  81. }
  82. # endif
  83. # undef GetProcAddress
  84. # define GetProcAddress GetProcAddressA
  85. static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
  86. {
  87. WCHAR *fnamw;
  88. size_t len_0 = strlen(lpLibFileName) + 1, i;
  89. # ifdef _MSC_VER
  90. fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
  91. # else
  92. fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  93. # endif
  94. if (fnamw == NULL) {
  95. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  96. return NULL;
  97. }
  98. # if defined(_WIN32_WCE) && _WIN32_WCE>=101
  99. if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
  100. # endif
  101. for (i = 0; i < len_0; i++)
  102. fnamw[i] = (WCHAR)lpLibFileName[i];
  103. return LoadLibraryW(fnamw);
  104. }
  105. # endif
  106. /* Part of the hack in "win32_load" ... */
  107. # define DSO_MAX_TRANSLATED_SIZE 256
  108. static int win32_load(DSO *dso);
  109. static int win32_unload(DSO *dso);
  110. static void *win32_bind_var(DSO *dso, const char *symname);
  111. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
  112. # if 0
  113. static int win32_unbind_var(DSO *dso, char *symname, void *symptr);
  114. static int win32_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  115. static int win32_init(DSO *dso);
  116. static int win32_finish(DSO *dso);
  117. static long win32_ctrl(DSO *dso, int cmd, long larg, void *parg);
  118. # endif
  119. static char *win32_name_converter(DSO *dso, const char *filename);
  120. static char *win32_merger(DSO *dso, const char *filespec1,
  121. const char *filespec2);
  122. static int win32_pathbyaddr(void *addr, char *path, int sz);
  123. static void *win32_globallookup(const char *name);
  124. static const char *openssl_strnchr(const char *string, int c, size_t len);
  125. static DSO_METHOD dso_meth_win32 = {
  126. "OpenSSL 'win32' shared library method",
  127. win32_load,
  128. win32_unload,
  129. win32_bind_var,
  130. win32_bind_func,
  131. /* For now, "unbind" doesn't exist */
  132. # if 0
  133. NULL, /* unbind_var */
  134. NULL, /* unbind_func */
  135. # endif
  136. NULL, /* ctrl */
  137. win32_name_converter,
  138. win32_merger,
  139. NULL, /* init */
  140. NULL, /* finish */
  141. win32_pathbyaddr,
  142. win32_globallookup
  143. };
  144. DSO_METHOD *DSO_METHOD_win32(void)
  145. {
  146. return (&dso_meth_win32);
  147. }
  148. /*
  149. * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
  150. * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
  151. */
  152. static int win32_load(DSO *dso)
  153. {
  154. HINSTANCE h = NULL, *p = NULL;
  155. /* See applicable comments from dso_dl.c */
  156. char *filename = DSO_convert_filename(dso, NULL);
  157. if (filename == NULL) {
  158. DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
  159. goto err;
  160. }
  161. h = LoadLibraryA(filename);
  162. if (h == NULL) {
  163. DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
  164. ERR_add_error_data(3, "filename(", filename, ")");
  165. goto err;
  166. }
  167. p = (HINSTANCE *) OPENSSL_malloc(sizeof(HINSTANCE));
  168. if (p == NULL) {
  169. DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
  170. goto err;
  171. }
  172. *p = h;
  173. if (!sk_void_push(dso->meth_data, p)) {
  174. DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
  175. goto err;
  176. }
  177. /* Success */
  178. dso->loaded_filename = filename;
  179. return (1);
  180. err:
  181. /* Cleanup ! */
  182. if (filename != NULL)
  183. OPENSSL_free(filename);
  184. if (p != NULL)
  185. OPENSSL_free(p);
  186. if (h != NULL)
  187. FreeLibrary(h);
  188. return (0);
  189. }
  190. static int win32_unload(DSO *dso)
  191. {
  192. HINSTANCE *p;
  193. if (dso == NULL) {
  194. DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  195. return (0);
  196. }
  197. if (sk_void_num(dso->meth_data) < 1)
  198. return (1);
  199. p = sk_void_pop(dso->meth_data);
  200. if (p == NULL) {
  201. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
  202. return (0);
  203. }
  204. if (!FreeLibrary(*p)) {
  205. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
  206. /*
  207. * We should push the value back onto the stack in case of a retry.
  208. */
  209. sk_void_push(dso->meth_data, p);
  210. return (0);
  211. }
  212. /* Cleanup */
  213. OPENSSL_free(p);
  214. return (1);
  215. }
  216. /*
  217. * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
  218. * docs, there's probably a variant for variables.
  219. */
  220. static void *win32_bind_var(DSO *dso, const char *symname)
  221. {
  222. HINSTANCE *ptr;
  223. void *sym;
  224. if ((dso == NULL) || (symname == NULL)) {
  225. DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  226. return (NULL);
  227. }
  228. if (sk_void_num(dso->meth_data) < 1) {
  229. DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
  230. return (NULL);
  231. }
  232. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  233. if (ptr == NULL) {
  234. DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
  235. return (NULL);
  236. }
  237. sym = GetProcAddress(*ptr, symname);
  238. if (sym == NULL) {
  239. DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
  240. ERR_add_error_data(3, "symname(", symname, ")");
  241. return (NULL);
  242. }
  243. return (sym);
  244. }
  245. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
  246. {
  247. HINSTANCE *ptr;
  248. void *sym;
  249. if ((dso == NULL) || (symname == NULL)) {
  250. DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  251. return (NULL);
  252. }
  253. if (sk_void_num(dso->meth_data) < 1) {
  254. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
  255. return (NULL);
  256. }
  257. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  258. if (ptr == NULL) {
  259. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
  260. return (NULL);
  261. }
  262. sym = GetProcAddress(*ptr, symname);
  263. if (sym == NULL) {
  264. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
  265. ERR_add_error_data(3, "symname(", symname, ")");
  266. return (NULL);
  267. }
  268. return ((DSO_FUNC_TYPE)sym);
  269. }
  270. struct file_st {
  271. const char *node;
  272. int nodelen;
  273. const char *device;
  274. int devicelen;
  275. const char *predir;
  276. int predirlen;
  277. const char *dir;
  278. int dirlen;
  279. const char *file;
  280. int filelen;
  281. };
  282. static struct file_st *win32_splitter(DSO *dso, const char *filename,
  283. int assume_last_is_dir)
  284. {
  285. struct file_st *result = NULL;
  286. enum { IN_NODE, IN_DEVICE, IN_FILE } position;
  287. const char *start = filename;
  288. char last;
  289. if (!filename) {
  290. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
  291. /*
  292. * goto err;
  293. */
  294. return (NULL);
  295. }
  296. result = OPENSSL_malloc(sizeof(struct file_st));
  297. if (result == NULL) {
  298. DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
  299. return (NULL);
  300. }
  301. memset(result, 0, sizeof(struct file_st));
  302. position = IN_DEVICE;
  303. if ((filename[0] == '\\' && filename[1] == '\\')
  304. || (filename[0] == '/' && filename[1] == '/')) {
  305. position = IN_NODE;
  306. filename += 2;
  307. start = filename;
  308. result->node = start;
  309. }
  310. do {
  311. last = filename[0];
  312. switch (last) {
  313. case ':':
  314. if (position != IN_DEVICE) {
  315. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
  316. /*
  317. * goto err;
  318. */
  319. OPENSSL_free(result);
  320. return (NULL);
  321. }
  322. result->device = start;
  323. result->devicelen = (int)(filename - start);
  324. position = IN_FILE;
  325. start = ++filename;
  326. result->dir = start;
  327. break;
  328. case '\\':
  329. case '/':
  330. if (position == IN_NODE) {
  331. result->nodelen = (int)(filename - start);
  332. position = IN_FILE;
  333. start = ++filename;
  334. result->dir = start;
  335. } else if (position == IN_DEVICE) {
  336. position = IN_FILE;
  337. filename++;
  338. result->dir = start;
  339. result->dirlen = (int)(filename - start);
  340. start = filename;
  341. } else {
  342. filename++;
  343. result->dirlen += (int)(filename - start);
  344. start = filename;
  345. }
  346. break;
  347. case '\0':
  348. if (position == IN_NODE) {
  349. result->nodelen = (int)(filename - start);
  350. } else {
  351. if (filename - start > 0) {
  352. if (assume_last_is_dir) {
  353. if (position == IN_DEVICE) {
  354. result->dir = start;
  355. result->dirlen = 0;
  356. }
  357. result->dirlen += (int)(filename - start);
  358. } else {
  359. result->file = start;
  360. result->filelen = (int)(filename - start);
  361. }
  362. }
  363. }
  364. break;
  365. default:
  366. filename++;
  367. break;
  368. }
  369. }
  370. while (last);
  371. if (!result->nodelen)
  372. result->node = NULL;
  373. if (!result->devicelen)
  374. result->device = NULL;
  375. if (!result->dirlen)
  376. result->dir = NULL;
  377. if (!result->filelen)
  378. result->file = NULL;
  379. return (result);
  380. }
  381. static char *win32_joiner(DSO *dso, const struct file_st *file_split)
  382. {
  383. int len = 0, offset = 0;
  384. char *result = NULL;
  385. const char *start;
  386. if (!file_split) {
  387. DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
  388. return (NULL);
  389. }
  390. if (file_split->node) {
  391. len += 2 + file_split->nodelen; /* 2 for starting \\ */
  392. if (file_split->predir || file_split->dir || file_split->file)
  393. len++; /* 1 for ending \ */
  394. } else if (file_split->device) {
  395. len += file_split->devicelen + 1; /* 1 for ending : */
  396. }
  397. len += file_split->predirlen;
  398. if (file_split->predir && (file_split->dir || file_split->file)) {
  399. len++; /* 1 for ending \ */
  400. }
  401. len += file_split->dirlen;
  402. if (file_split->dir && file_split->file) {
  403. len++; /* 1 for ending \ */
  404. }
  405. len += file_split->filelen;
  406. if (!len) {
  407. DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
  408. return (NULL);
  409. }
  410. result = OPENSSL_malloc(len + 1);
  411. if (!result) {
  412. DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
  413. return (NULL);
  414. }
  415. if (file_split->node) {
  416. strcpy(&result[offset], "\\\\");
  417. offset += 2;
  418. strncpy(&result[offset], file_split->node, file_split->nodelen);
  419. offset += file_split->nodelen;
  420. if (file_split->predir || file_split->dir || file_split->file) {
  421. result[offset] = '\\';
  422. offset++;
  423. }
  424. } else if (file_split->device) {
  425. strncpy(&result[offset], file_split->device, file_split->devicelen);
  426. offset += file_split->devicelen;
  427. result[offset] = ':';
  428. offset++;
  429. }
  430. start = file_split->predir;
  431. while (file_split->predirlen > (start - file_split->predir)) {
  432. const char *end = openssl_strnchr(start, '/',
  433. file_split->predirlen - (start -
  434. file_split->predir));
  435. if (!end)
  436. end = start
  437. + file_split->predirlen - (start - file_split->predir);
  438. strncpy(&result[offset], start, end - start);
  439. offset += (int)(end - start);
  440. result[offset] = '\\';
  441. offset++;
  442. start = end + 1;
  443. }
  444. # if 0 /* Not needed, since the directory converter
  445. * above already appeneded a backslash */
  446. if (file_split->predir && (file_split->dir || file_split->file)) {
  447. result[offset] = '\\';
  448. offset++;
  449. }
  450. # endif
  451. start = file_split->dir;
  452. while (file_split->dirlen > (start - file_split->dir)) {
  453. const char *end = openssl_strnchr(start, '/',
  454. file_split->dirlen - (start -
  455. file_split->dir));
  456. if (!end)
  457. end = start + file_split->dirlen - (start - file_split->dir);
  458. strncpy(&result[offset], start, end - start);
  459. offset += (int)(end - start);
  460. result[offset] = '\\';
  461. offset++;
  462. start = end + 1;
  463. }
  464. # if 0 /* Not needed, since the directory converter
  465. * above already appeneded a backslash */
  466. if (file_split->dir && file_split->file) {
  467. result[offset] = '\\';
  468. offset++;
  469. }
  470. # endif
  471. strncpy(&result[offset], file_split->file, file_split->filelen);
  472. offset += file_split->filelen;
  473. result[offset] = '\0';
  474. return (result);
  475. }
  476. static char *win32_merger(DSO *dso, const char *filespec1,
  477. const char *filespec2)
  478. {
  479. char *merged = NULL;
  480. struct file_st *filespec1_split = NULL;
  481. struct file_st *filespec2_split = NULL;
  482. if (!filespec1 && !filespec2) {
  483. DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  484. return (NULL);
  485. }
  486. if (!filespec2) {
  487. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  488. if (!merged) {
  489. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  490. return (NULL);
  491. }
  492. strcpy(merged, filespec1);
  493. } else if (!filespec1) {
  494. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  495. if (!merged) {
  496. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  497. return (NULL);
  498. }
  499. strcpy(merged, filespec2);
  500. } else {
  501. filespec1_split = win32_splitter(dso, filespec1, 0);
  502. if (!filespec1_split) {
  503. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  504. return (NULL);
  505. }
  506. filespec2_split = win32_splitter(dso, filespec2, 1);
  507. if (!filespec2_split) {
  508. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  509. OPENSSL_free(filespec1_split);
  510. return (NULL);
  511. }
  512. /* Fill in into filespec1_split */
  513. if (!filespec1_split->node && !filespec1_split->device) {
  514. filespec1_split->node = filespec2_split->node;
  515. filespec1_split->nodelen = filespec2_split->nodelen;
  516. filespec1_split->device = filespec2_split->device;
  517. filespec1_split->devicelen = filespec2_split->devicelen;
  518. }
  519. if (!filespec1_split->dir) {
  520. filespec1_split->dir = filespec2_split->dir;
  521. filespec1_split->dirlen = filespec2_split->dirlen;
  522. } else if (filespec1_split->dir[0] != '\\'
  523. && filespec1_split->dir[0] != '/') {
  524. filespec1_split->predir = filespec2_split->dir;
  525. filespec1_split->predirlen = filespec2_split->dirlen;
  526. }
  527. if (!filespec1_split->file) {
  528. filespec1_split->file = filespec2_split->file;
  529. filespec1_split->filelen = filespec2_split->filelen;
  530. }
  531. merged = win32_joiner(dso, filespec1_split);
  532. }
  533. OPENSSL_free(filespec1_split);
  534. OPENSSL_free(filespec2_split);
  535. return (merged);
  536. }
  537. static char *win32_name_converter(DSO *dso, const char *filename)
  538. {
  539. char *translated;
  540. int len, transform;
  541. len = strlen(filename);
  542. transform = ((strstr(filename, "/") == NULL) &&
  543. (strstr(filename, "\\") == NULL) &&
  544. (strstr(filename, ":") == NULL));
  545. if (transform)
  546. /* We will convert this to "%s.dll" */
  547. translated = OPENSSL_malloc(len + 5);
  548. else
  549. /* We will simply duplicate filename */
  550. translated = OPENSSL_malloc(len + 1);
  551. if (translated == NULL) {
  552. DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  553. return (NULL);
  554. }
  555. if (transform)
  556. sprintf(translated, "%s.dll", filename);
  557. else
  558. sprintf(translated, "%s", filename);
  559. return (translated);
  560. }
  561. static const char *openssl_strnchr(const char *string, int c, size_t len)
  562. {
  563. size_t i;
  564. const char *p;
  565. for (i = 0, p = string; i < len && *p; i++, p++) {
  566. if (*p == c)
  567. return p;
  568. }
  569. return NULL;
  570. }
  571. # include <tlhelp32.h>
  572. # ifdef _WIN32_WCE
  573. # define DLLNAME "TOOLHELP.DLL"
  574. # else
  575. # ifdef MODULEENTRY32
  576. # undef MODULEENTRY32 /* unmask the ASCII version! */
  577. # endif
  578. # define DLLNAME "KERNEL32.DLL"
  579. # endif
  580. typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
  581. typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
  582. typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
  583. static int win32_pathbyaddr(void *addr, char *path, int sz)
  584. {
  585. HMODULE dll;
  586. HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  587. MODULEENTRY32 me32;
  588. CREATETOOLHELP32SNAPSHOT create_snap;
  589. CLOSETOOLHELP32SNAPSHOT close_snap;
  590. MODULE32 module_first, module_next;
  591. if (addr == NULL) {
  592. union {
  593. int (*f) (void *, char *, int);
  594. void *p;
  595. } t = {
  596. win32_pathbyaddr
  597. };
  598. addr = t.p;
  599. }
  600. dll = LoadLibrary(TEXT(DLLNAME));
  601. if (dll == NULL) {
  602. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
  603. return -1;
  604. }
  605. create_snap = (CREATETOOLHELP32SNAPSHOT)
  606. GetProcAddress(dll, "CreateToolhelp32Snapshot");
  607. if (create_snap == NULL) {
  608. FreeLibrary(dll);
  609. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
  610. return -1;
  611. }
  612. /* We take the rest for granted... */
  613. # ifdef _WIN32_WCE
  614. close_snap = (CLOSETOOLHELP32SNAPSHOT)
  615. GetProcAddress(dll, "CloseToolhelp32Snapshot");
  616. # else
  617. close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
  618. # endif
  619. module_first = (MODULE32) GetProcAddress(dll, "Module32First");
  620. module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
  621. hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
  622. if (hModuleSnap == INVALID_HANDLE_VALUE) {
  623. FreeLibrary(dll);
  624. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
  625. return -1;
  626. }
  627. me32.dwSize = sizeof(me32);
  628. if (!(*module_first) (hModuleSnap, &me32)) {
  629. (*close_snap) (hModuleSnap);
  630. FreeLibrary(dll);
  631. DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_FAILURE);
  632. return -1;
  633. }
  634. do {
  635. if ((BYTE *) addr >= me32.modBaseAddr &&
  636. (BYTE *) addr < me32.modBaseAddr + me32.modBaseSize) {
  637. (*close_snap) (hModuleSnap);
  638. FreeLibrary(dll);
  639. # ifdef _WIN32_WCE
  640. # if _WIN32_WCE >= 101
  641. return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
  642. path, sz, NULL, NULL);
  643. # else
  644. {
  645. int i, len = (int)wcslen(me32.szExePath);
  646. if (sz <= 0)
  647. return len + 1;
  648. if (len >= sz)
  649. len = sz - 1;
  650. for (i = 0; i < len; i++)
  651. path[i] = (char)me32.szExePath[i];
  652. path[len++] = 0;
  653. return len;
  654. }
  655. # endif
  656. # else
  657. {
  658. int len = (int)strlen(me32.szExePath);
  659. if (sz <= 0)
  660. return len + 1;
  661. if (len >= sz)
  662. len = sz - 1;
  663. memcpy(path, me32.szExePath, len);
  664. path[len++] = 0;
  665. return len;
  666. }
  667. # endif
  668. }
  669. } while ((*module_next) (hModuleSnap, &me32));
  670. (*close_snap) (hModuleSnap);
  671. FreeLibrary(dll);
  672. return 0;
  673. }
  674. static void *win32_globallookup(const char *name)
  675. {
  676. HMODULE dll;
  677. HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  678. MODULEENTRY32 me32;
  679. CREATETOOLHELP32SNAPSHOT create_snap;
  680. CLOSETOOLHELP32SNAPSHOT close_snap;
  681. MODULE32 module_first, module_next;
  682. FARPROC ret = NULL;
  683. dll = LoadLibrary(TEXT(DLLNAME));
  684. if (dll == NULL) {
  685. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  686. return NULL;
  687. }
  688. create_snap = (CREATETOOLHELP32SNAPSHOT)
  689. GetProcAddress(dll, "CreateToolhelp32Snapshot");
  690. if (create_snap == NULL) {
  691. FreeLibrary(dll);
  692. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  693. return NULL;
  694. }
  695. /* We take the rest for granted... */
  696. # ifdef _WIN32_WCE
  697. close_snap = (CLOSETOOLHELP32SNAPSHOT)
  698. GetProcAddress(dll, "CloseToolhelp32Snapshot");
  699. # else
  700. close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
  701. # endif
  702. module_first = (MODULE32) GetProcAddress(dll, "Module32First");
  703. module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
  704. hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
  705. if (hModuleSnap == INVALID_HANDLE_VALUE) {
  706. FreeLibrary(dll);
  707. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  708. return NULL;
  709. }
  710. me32.dwSize = sizeof(me32);
  711. if (!(*module_first) (hModuleSnap, &me32)) {
  712. (*close_snap) (hModuleSnap);
  713. FreeLibrary(dll);
  714. return NULL;
  715. }
  716. do {
  717. if ((ret = GetProcAddress(me32.hModule, name))) {
  718. (*close_snap) (hModuleSnap);
  719. FreeLibrary(dll);
  720. return ret;
  721. }
  722. } while ((*module_next) (hModuleSnap, &me32));
  723. (*close_snap) (hModuleSnap);
  724. FreeLibrary(dll);
  725. return NULL;
  726. }
  727. #endif /* DSO_WIN32 */