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.

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