dso_win32.c 20 KB

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