Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

751 lines
16 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: utils.c
  3. *
  4. * Purpose: Conatains all the utility routines
  5. *
  6. * Created: 1990
  7. *
  8. * Copyright (c) 1990, 1991 Microsoft Corporation
  9. *
  10. * History:
  11. * Raor, srinik (../../1990,91) Designed and coded
  12. * curts created portable version for WIN16/32
  13. *
  14. \***************************************************************************/
  15. #include <windows.h>
  16. #include <reghelp.hxx>
  17. #include "dll.h"
  18. #include "strsafe.h"
  19. #define KB_64 65536
  20. #define NULL_WORD 0x0000
  21. extern ATOM aPackage;
  22. extern OLEOBJECTVTBL vtblMF, vtblBM, vtblDIB, vtblGEN;
  23. // QuerySize API support
  24. DWORD dwObjSize = 0;
  25. OLESTREAMVTBL dllStreamVtbl;
  26. OLESTREAM dllStream;
  27. BOOL PutStrWithLen(
  28. LPOLESTREAM lpstream,
  29. LPSTR lpbytes
  30. ){
  31. LONG len;
  32. len = (LONG) lstrlen(lpbytes) + 1;
  33. if (PutBytes (lpstream, (LPSTR)&len, sizeof(len)))
  34. return TRUE;
  35. return PutBytes(lpstream, lpbytes, len);
  36. }
  37. BOOL GetStrWithLen(
  38. LPOLESTREAM lpstream,
  39. LPSTR lpbytes
  40. ){
  41. if (GetBytes (lpstream, lpbytes, sizeof(LONG)))
  42. return TRUE;
  43. return GetBytes (lpstream, lpbytes + sizeof(LONG), (*(LONG FAR *)lpbytes));
  44. }
  45. ATOM GetAtomFromStream(
  46. LPOLESTREAM lpstream
  47. ){
  48. BOOL err = TRUE;
  49. LONG len;
  50. char str[MAX_STR+1];
  51. if (GetBytes (lpstream, (LPSTR)&len, sizeof(len)))
  52. return (ATOM)0;
  53. if (len == 0)
  54. return (ATOM)0;
  55. if (GetBytes(lpstream, (LPSTR)str, len))
  56. return (ATOM)0;
  57. return GlobalAddAtom(str);
  58. }
  59. BOOL PutAtomIntoStream(
  60. LPOLESTREAM lpstream,
  61. ATOM at
  62. ){
  63. LONG len = 0;
  64. char buf[MAX_STR + 1];
  65. if (at == 0)
  66. return (PutBytes (lpstream, (LPSTR)&len, sizeof(len)));
  67. len = GlobalGetAtomName (at,(LPSTR)buf, MAX_STR) + 1;
  68. if (PutBytes (lpstream, (LPSTR)&len, sizeof(len)))
  69. return TRUE;
  70. return PutBytes(lpstream, buf, len);
  71. }
  72. // DuplicateAtom: Bump the use count up on a global atom.
  73. ATOM FARINTERNAL DuplicateAtom (
  74. ATOM atom
  75. ){
  76. char buffer[MAX_ATOM+1];
  77. Puts("DuplicateAtom");
  78. if (!atom)
  79. return (ATOM)0;
  80. GlobalGetAtomName (atom, buffer, MAX_ATOM);
  81. return GlobalAddAtom (buffer);
  82. }
  83. BOOL GetBytes(
  84. LPOLESTREAM lpstream,
  85. LPSTR lpstr,
  86. LONG len
  87. ){
  88. ASSERT (lpstream->lpstbl->Get , "stream get function is null");
  89. return (((*lpstream->lpstbl->Get)(lpstream, lpstr, (DWORD)len)) != (DWORD)len);
  90. }
  91. BOOL PutBytes(
  92. LPOLESTREAM lpstream,
  93. LPSTR lpstr,
  94. LONG len
  95. ){
  96. ASSERT (lpstream->lpstbl->Put , "stream get function is null");
  97. return (((*lpstream->lpstbl->Put)(lpstream, lpstr, (DWORD)len)) != (DWORD)len);
  98. }
  99. BOOL FARINTERNAL UtilMemCmp (
  100. LPSTR lpmem1,
  101. LPSTR lpmem2,
  102. DWORD dwCount
  103. ){
  104. UINT HUGE_T * hpmem1;
  105. UINT HUGE_T * hpmem2;
  106. DWORD words;
  107. DWORD bytes;
  108. bytes = dwCount % MAPVALUE(2,4);
  109. words = dwCount >> MAPVALUE(1,2);//* we compare DWORDS
  110. //* in the 32 bit version
  111. {
  112. hpmem1 = (UINT HUGE_T *) lpmem1;
  113. hpmem2 = (UINT HUGE_T *) lpmem2;
  114. while (words--) {
  115. if (*hpmem1++ != *hpmem2++)
  116. return FALSE;
  117. }
  118. lpmem1 = (LPSTR)hpmem1;
  119. lpmem2 = (LPSTR)hpmem2;
  120. for (; bytes-- ; ) {
  121. if ( *lpmem1++ != *lpmem2++ )
  122. return FALSE;
  123. }
  124. }
  125. return TRUE;
  126. }
  127. void FARINTERNAL UtilMemCpy (
  128. LPSTR lpdst,
  129. LPSTR lpsrc,
  130. DWORD dwCount
  131. ){
  132. UINT HUGE_T * hpdst;
  133. UINT HUGE_T * hpsrc;
  134. DWORD words;
  135. DWORD bytes;
  136. bytes = dwCount % MAPVALUE(2,4);
  137. words = dwCount >> MAPVALUE(1,2);//* we compare DWORDS
  138. //* in the 32 bit version
  139. {
  140. hpdst = (UINT HUGE_T *) lpdst;
  141. hpsrc = (UINT HUGE_T *) lpsrc;
  142. for(;words--; )
  143. *hpdst++ = *hpsrc++;
  144. lpdst = (LPSTR)hpdst;
  145. lpsrc = (LPSTR)hpsrc;
  146. for (;bytes--;)
  147. *lpdst++ = *lpsrc++;
  148. }
  149. }
  150. //DuplicateData: Duplicates a given Global data handle.
  151. HANDLE FARINTERNAL DuplicateGlobal (
  152. HANDLE hdata,
  153. UINT flags
  154. ){
  155. LPSTR lpdst = NULL;
  156. LPSTR lpsrc = NULL;
  157. HANDLE hdup = NULL;
  158. DWORD size;
  159. BOOL err = TRUE;
  160. if (!hdata)
  161. return NULL;
  162. if(!(lpsrc = GlobalLock (hdata)))
  163. return NULL;
  164. hdup = GlobalAlloc (flags, (size = (DWORD)GlobalSize(hdata)));
  165. if(!(lpdst = GlobalLock (hdup)))
  166. goto errRtn;;
  167. err = FALSE;
  168. UtilMemCpy (lpdst, lpsrc, size);
  169. errRtn:
  170. if(lpsrc)
  171. GlobalUnlock (hdata);
  172. if(lpdst)
  173. GlobalUnlock (hdup);
  174. if (err && hdup) {
  175. GlobalFree (hdup);
  176. hdup = NULL;
  177. }
  178. return hdup;
  179. }
  180. BOOL FARINTERNAL CmpGlobals (
  181. HANDLE hdata1,
  182. HANDLE hdata2
  183. ){
  184. LPSTR lpdata1 = NULL;
  185. LPSTR lpdata2 = NULL;
  186. DWORD size1;
  187. DWORD size2;
  188. BOOL retval = FALSE;
  189. size1 = (DWORD)GlobalSize (hdata1);
  190. size2 = (DWORD)GlobalSize (hdata2);
  191. if (size1 != size2)
  192. return FALSE;
  193. if (!(lpdata1 = GlobalLock (hdata1)))
  194. goto errRtn;
  195. if (!(lpdata2 = GlobalLock (hdata2)))
  196. goto errRtn;
  197. retval = UtilMemCmp (lpdata1, lpdata2, size1);
  198. errRtn:
  199. if (lpdata1)
  200. GlobalUnlock (hdata1);
  201. if (lpdata2)
  202. GlobalUnlock (hdata2);
  203. return retval;
  204. }
  205. int FARINTERNAL GlobalGetAtomLen (
  206. ATOM aItem
  207. ){
  208. // !!! Change this
  209. char buf[MAX_STR];
  210. if (!aItem)
  211. return 0;
  212. return (GlobalGetAtomName (aItem, (LPSTR)buf, MAX_STR));
  213. }
  214. BOOL FARINTERNAL MapExtToClass (
  215. LPSTR lptemplate,
  216. LPSTR lpbuf,
  217. int len
  218. ){
  219. LONG cb;
  220. LPSTR lpstrBack = NULL;
  221. while (*lptemplate)
  222. {
  223. if ((*lptemplate) == '\\'){
  224. lpstrBack = lptemplate ;
  225. }
  226. lptemplate ++ ;
  227. }
  228. while (lpstrBack && *lpstrBack && *lpstrBack != '.')
  229. lpstrBack++ ;
  230. cb = len;
  231. if (lpstrBack == NULL || *(lpstrBack+1) == '\0')
  232. return FALSE;
  233. if (QueryClassesRootValueA (lpstrBack, lpbuf, &cb))
  234. return FALSE;
  235. return TRUE;
  236. }
  237. // Get exe name from aClass and set it as aServer
  238. void INTERNAL SetExeAtom (
  239. LPOBJECT_LE lpobj
  240. ){
  241. char key[MAX_STR];
  242. // if old link object assume the class same as the exe file name.
  243. if (lpobj->bOldLink)
  244. lpobj->aServer = DuplicateAtom (lpobj->app);
  245. else {
  246. if (GlobalGetAtomName (lpobj->app, key, sizeof(key)))
  247. lpobj->aServer = GetAppAtom ((LPSTR)key);
  248. }
  249. }
  250. ATOM FARINTERNAL GetAppAtom (
  251. LPCSTR lpclass
  252. ){
  253. char buf1[MAX_STR];
  254. if (!QueryApp (lpclass, PROTOCOL_EDIT, buf1)) {
  255. return (ATOM)0;
  256. }
  257. return GlobalAddAtom ((LPSTR)buf1);
  258. }
  259. BOOL FARINTERNAL QueryVerb (
  260. LPOBJECT_LE lpobj,
  261. UINT verb,
  262. LPSTR lpbuf,
  263. LONG cbmax
  264. ){
  265. LONG cb = MAX_STR;
  266. char key[MAX_STR];
  267. // do not need 256 bytes buffer
  268. char class[MAX_STR];
  269. int len;
  270. if (!GlobalGetAtomName (lpobj->app, (LPSTR)class, sizeof(class)))
  271. return FALSE;
  272. StringCchCopy(key, sizeof(key)/sizeof(key[0]), (LPSTR)class);
  273. StringCchCat (key, sizeof(key)/sizeof(key[0]), "\\protocol\\StdFileEditing\\verb\\");
  274. len = lstrlen (key);
  275. // If we don't have room to add the char fail
  276. if (len >= sizeof(key)/sizeof(key[0]) - 1)
  277. return FALSE;
  278. key [len++] = (char) ('0' + verb);
  279. key [len++] = 0;
  280. if (QueryClassesRootValueA (key, lpbuf, &cbmax))
  281. return FALSE;
  282. return TRUE;
  283. }
  284. BOOL QueryApp (
  285. LPCSTR lpclass,
  286. LPCSTR lpprotocol,
  287. LPSTR lpbuf
  288. ){
  289. LONG cb = MAX_STR;
  290. char key[MAX_STR];
  291. if (FAILED(StringCchCopy (key, sizeof(key)/sizeof(key[0]), lpclass)))
  292. return FALSE;
  293. if (FAILED(StringCchCat(key, sizeof(key)/sizeof(key[0]), "\\protocol\\")))
  294. return FALSE;
  295. if (FAILED(StringCchCat(key, sizeof(key)/sizeof(key[0]), lpprotocol)))
  296. return FALSE;
  297. if (FAILED(StringCchCat(key, sizeof(key)/sizeof(key[0]), "\\server")))
  298. return FALSE;
  299. if (QueryClassesRootValueA (key, lpbuf, &cb))
  300. return FALSE;
  301. return TRUE;
  302. }
  303. HANDLE MapStrToH (
  304. LPSTR lpstr
  305. ){
  306. HANDLE hdata = NULL;
  307. LPSTR lpdata = NULL;
  308. UINT cch = lstrlen(lpstr) + 1;
  309. hdata = GlobalAlloc (GMEM_DDESHARE, cch);
  310. if (hdata == NULL || (lpdata = (LPSTR)GlobalLock (hdata)) == NULL)
  311. goto errRtn;
  312. StringCchCopy(lpdata, cch, lpstr);
  313. GlobalUnlock (hdata);
  314. return hdata;
  315. errRtn:
  316. if (lpdata)
  317. GlobalUnlock (hdata);
  318. if (hdata)
  319. GlobalFree (hdata);
  320. return NULL;
  321. }
  322. HANDLE FARINTERNAL CopyData (
  323. LPSTR lpsrc,
  324. DWORD dwBytes
  325. ){
  326. HANDLE hnew;
  327. LPSTR lpnew;
  328. BOOL retval = FALSE;
  329. if (hnew = GlobalAlloc (GMEM_MOVEABLE, dwBytes)){
  330. if (lpnew = GlobalLock (hnew)){
  331. UtilMemCpy (lpnew, lpsrc, dwBytes);
  332. GlobalUnlock (hnew);
  333. return hnew;
  334. }
  335. else
  336. GlobalFree (hnew);
  337. }
  338. return NULL;
  339. }
  340. void UtilMemClr (
  341. PSTR pstr,
  342. UINT size
  343. ){
  344. while (size--)
  345. *pstr++ = 0;
  346. }
  347. OLESTATUS FAR PASCAL ObjQueryName (
  348. LPOLEOBJECT lpobj,
  349. LPSTR lpBuf,
  350. UINT FAR * lpcbBuf
  351. ){
  352. if (lpobj->ctype != CT_LINK && lpobj->ctype != CT_EMBEDDED
  353. && lpobj->ctype != CT_STATIC)
  354. return OLE_ERROR_OBJECT;
  355. PROBE_WRITE(lpBuf);
  356. if (!*lpcbBuf)
  357. return OLE_ERROR_SIZE;
  358. if (!CheckPointer(lpBuf+*lpcbBuf-1, WRITE_ACCESS))
  359. return OLE_ERROR_SIZE;
  360. ASSERT(lpobj->aObjName, "object name ATOM is NULL\n");
  361. *lpcbBuf = GlobalGetAtomName (lpobj->aObjName, lpBuf, *lpcbBuf);
  362. return OLE_OK;
  363. }
  364. OLESTATUS FAR PASCAL ObjRename (
  365. LPOLEOBJECT lpobj,
  366. LPCSTR lpNewName
  367. ){
  368. if (lpobj->ctype != CT_LINK && lpobj->ctype != CT_EMBEDDED
  369. && lpobj->ctype != CT_STATIC)
  370. return OLE_ERROR_OBJECT;
  371. PROBE_READ(lpNewName);
  372. if (!lpNewName[0])
  373. return OLE_ERROR_NAME;
  374. if (lpobj->aObjName)
  375. GlobalDeleteAtom (lpobj->aObjName);
  376. lpobj->aObjName = GlobalAddAtom (lpNewName);
  377. return OLE_OK;
  378. }
  379. BOOL QueryHandler(
  380. UINT cfFormat
  381. ){
  382. HANDLE hInfo = NULL;
  383. LPSTR lpInfo = NULL;
  384. BOOL fRet = FALSE, fOpen = FALSE;
  385. LONG cb = MAX_STR;
  386. char str[MAX_STR];
  387. HKEY hKey;
  388. // we don't have the client app window handle, use the screen handle
  389. fOpen = OpenClipboard (NULL);
  390. if (!(hInfo = GetClipboardData (cfFormat)))
  391. goto errRtn;
  392. if (!(lpInfo = GlobalLock(hInfo)))
  393. goto errRtn;
  394. // First string of lpInfo is CLASS. See whether any handler is installed
  395. // for this class.
  396. if (FAILED(StringCchCopy (str, sizeof(str)/sizeof(str[0]), lpInfo)))
  397. goto errRtn;
  398. if (FAILED(StringCchCat (str, sizeof(str)/sizeof(str[0]), "\\protocol\\StdFileEditing\\handler")))
  399. goto errRtn;
  400. if (OpenClassesRootKeyA (str, &hKey))
  401. goto errRtn;
  402. RegCloseKey (hKey);
  403. fRet = TRUE;
  404. errRtn:
  405. if (lpInfo)
  406. GlobalUnlock (hInfo);
  407. if (fOpen)
  408. CloseClipboard();
  409. return fRet;
  410. }
  411. OLESTATUS INTERNAL FileExists (
  412. LPOBJECT_LE lpobj
  413. ){
  414. char filename[MAX_STR];
  415. OFSTRUCT ofstruct;
  416. if (!GlobalGetAtomName (lpobj->topic, filename, MAX_STR))
  417. return OLE_ERROR_MEMORY;
  418. // For package with link we append "/LINK" to the filename. We don't want
  419. // to check for it's existence here.
  420. if (lpobj->app != aPackage) {
  421. // when OF_EXIST is specified, file is opened and closed immediately
  422. if (OpenFile (filename, &ofstruct, OF_EXIST) == -1)
  423. return OLE_ERROR_OPEN;
  424. }
  425. return OLE_OK;
  426. }
  427. BOOL FARINTERNAL UtilQueryProtocol (
  428. LPOBJECT_LE lpobj,
  429. LPCSTR lpprotocol
  430. ){
  431. char buf[MAX_STR];
  432. ATOM aExe;
  433. if (!GlobalGetAtomName (lpobj->app, (LPSTR) buf, MAX_STR))
  434. return FALSE;
  435. if (!QueryApp (buf, lpprotocol, (LPSTR) buf))
  436. return FALSE;
  437. aExe = GlobalAddAtom (buf);
  438. if (aExe)
  439. GlobalDeleteAtom (aExe);
  440. if (aExe != lpobj->aServer)
  441. return FALSE;
  442. return TRUE;
  443. }
  444. DWORD PASCAL FAR DllPut (
  445. LPOLESTREAM lpstream,
  446. OLE_CONST void FAR *lpstr,
  447. DWORD dwSize
  448. ){
  449. UNREFERENCED_PARAMETER(lpstream);
  450. UNREFERENCED_PARAMETER(lpstr);
  451. dwObjSize += dwSize;
  452. return dwSize;
  453. }
  454. OLESTATUS FARINTERNAL ObjQueryType (
  455. LPOLEOBJECT lpobj,
  456. LPLONG lptype
  457. ){
  458. Puts("ObjQueryType");
  459. if (lpobj->ctype != CT_STATIC)
  460. return OLE_ERROR_OBJECT;
  461. *lptype = lpobj->ctype;
  462. return OLE_OK;
  463. }
  464. OLESTATUS FARINTERNAL ObjQuerySize (
  465. LPOLEOBJECT lpobj,
  466. DWORD FAR * lpdwSize
  467. ){
  468. Puts("ObjQuerySize");
  469. *lpdwSize = dwObjSize = 0;
  470. if ((*lpobj->lpvtbl->SaveToStream) (lpobj, &dllStream) == OLE_OK) {
  471. *lpdwSize = dwObjSize;
  472. return OLE_OK;
  473. }
  474. return OLE_ERROR_BLANK;
  475. }
  476. BOOL FARINTERNAL IsObjectBlank (
  477. LPOBJECT_LE lpobj
  478. ){
  479. LPOLEOBJECT lpPictObj;
  480. BOOL retval=FALSE;
  481. // Cleaner way is to provide a method like QueryBlank()
  482. if (!lpobj->hnative)
  483. return TRUE;
  484. if (!(lpPictObj = lpobj->lpobjPict))
  485. return FALSE;
  486. if (lpPictObj->lpvtbl == (LPOLEOBJECTVTBL)&vtblMF)
  487. retval = (((LPOBJECT_MF)lpPictObj)->hmfp != NULL);
  488. else if (lpPictObj->lpvtbl == (LPOLEOBJECTVTBL)&vtblBM)
  489. retval = (((LPOBJECT_BM)lpPictObj)->hBitmap != NULL);
  490. if (lpPictObj->lpvtbl == (LPOLEOBJECTVTBL)&vtblDIB)
  491. retval = (((LPOBJECT_DIB)lpPictObj)->hDIB != NULL);
  492. if (lpPictObj->lpvtbl == (LPOLEOBJECTVTBL)&vtblGEN)
  493. retval = (((LPOBJECT_GEN)lpPictObj)->hData != NULL);
  494. return retval;
  495. }
  496. BOOL FAR PASCAL OleIsDcMeta (HDC hdc)
  497. {
  498. return (GetDeviceCaps (hdc, TECHNOLOGY) == DT_METAFILE);
  499. }
  500. void ConvertBM32to16(
  501. LPBITMAP lpsrc,
  502. LPWIN16BITMAP lpdest
  503. ){
  504. #ifdef WIN32
  505. lpdest->bmType = (short)lpsrc->bmType;
  506. lpdest->bmWidth = (short)lpsrc->bmWidth;
  507. lpdest->bmHeight = (short)lpsrc->bmHeight;
  508. lpdest->bmWidthBytes = (short)lpsrc->bmWidthBytes;
  509. lpdest->bmPlanes = (BYTE)lpsrc->bmPlanes;
  510. lpdest->bmBitsPixel = (BYTE)lpsrc->bmBitsPixel;
  511. #endif
  512. #ifdef WIN16
  513. *lpdest = *lpsrc;
  514. #endif
  515. }
  516. void ConvertBM16to32(
  517. LPWIN16BITMAP lpsrc,
  518. LPBITMAP lpdest
  519. ){
  520. #ifdef WIN32
  521. lpdest->bmType = MAKELONG(lpsrc->bmType,NULL_WORD);
  522. lpdest->bmWidth = MAKELONG(lpsrc->bmWidth,NULL_WORD);
  523. lpdest->bmHeight = MAKELONG(lpsrc->bmHeight,NULL_WORD);
  524. lpdest->bmWidthBytes = MAKELONG(lpsrc->bmWidthBytes,NULL_WORD);
  525. lpdest->bmPlanes = (WORD)lpsrc->bmPlanes;
  526. lpdest->bmBitsPixel = (WORD)lpsrc->bmBitsPixel;
  527. #endif
  528. #ifdef WIN16
  529. *lpdest = *lpsrc;
  530. #endif
  531. }
  532. void ConvertMF16to32(
  533. LPWIN16METAFILEPICT lpsrc,
  534. LPMETAFILEPICT lpdest
  535. ){
  536. #ifdef WIN32
  537. lpdest->mm = (DWORD)lpsrc->mm;
  538. lpdest->xExt = (DWORD)MAKELONG(lpsrc->xExt,NULL_WORD);
  539. lpdest->yExt = (DWORD)MAKELONG(lpsrc->yExt,NULL_WORD);
  540. #endif
  541. #ifdef WIN16
  542. *lpdest = *lpsrc;
  543. #endif
  544. }
  545. void ConvertMF32to16(
  546. LPMETAFILEPICT lpsrc,
  547. LPWIN16METAFILEPICT lpdest
  548. ){
  549. #ifdef WIN32
  550. lpdest->mm = (short)lpsrc->mm;
  551. lpdest->xExt = (short)lpsrc->xExt;
  552. lpdest->yExt = (short)lpsrc->yExt;
  553. #endif
  554. #ifdef WIN16
  555. *lpdest = *lpsrc;
  556. #endif
  557. }
  558. DWORD INTERNAL GetFileVersion(LPOLEOBJECT lpoleobj)
  559. {
  560. if (lpoleobj->lhclientdoc)
  561. return ((LPCLIENTDOC)(lpoleobj->lhclientdoc))->dwFileVer;
  562. if (lpoleobj->lpParent)
  563. return GetFileVersion(lpoleobj->lpParent);
  564. return (DWORD)MAKELONG(wReleaseVer,OS_WIN32);
  565. }