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.

305 lines
8.5 KiB

  1. #include <windows.h>
  2. #include "common.h"
  3. #include "clipfile.h"
  4. #include "render.h"
  5. #include "debugout.h"
  6. #include "dib.h"
  7. /*
  8. * RenderFormat() -
  9. *
  10. * Read the data from fh and SetClipboardData() with it.
  11. */
  12. HANDLE RenderFormat(
  13. FORMATHEADER *pfmthdr,
  14. register HANDLE fh)
  15. {
  16. HANDLE hBitmap;
  17. register HANDLE hData;
  18. LPSTR lpData;
  19. DWORD MetaOffset; /* special case hack for metafiles */
  20. BITMAP bitmap;
  21. HPALETTE hPalette;
  22. LPLOGPALETTE lpLogPalette;
  23. DWORD dwBytesRead;
  24. DWORD dwDataOffset;
  25. if (PRIVATE_FORMAT(pfmthdr->FormatID))
  26. pfmthdr->FormatID = RegisterClipboardFormatW(pfmthdr->Name);
  27. // Special case hack for metafiles to get hData referencing
  28. // the metafile bits, not the METAFILEPICT structure.
  29. switch (pfmthdr->FormatID)
  30. {
  31. case CF_METAFILEPICT:
  32. if (!fNTReadFileFormat)
  33. {
  34. MetaOffset = sizeof(WIN31METAFILEPICT);
  35. }
  36. else
  37. {
  38. MetaOffset = sizeof(METAFILEPICT);
  39. }
  40. break;
  41. case CF_BITMAP:
  42. if (!fNTReadFileFormat)
  43. {
  44. MetaOffset = sizeof(WIN31BITMAP);
  45. }
  46. else
  47. {
  48. MetaOffset = sizeof(BITMAP);
  49. }
  50. break;
  51. default:
  52. MetaOffset = 0;
  53. break;
  54. }
  55. if (!(hData = GlobalAlloc(GHND, pfmthdr->DataLen - MetaOffset)))
  56. {
  57. PERROR(TEXT("GlobalAlloc failure in RenderFormat\n\r"));
  58. return NULL;
  59. }
  60. if (!(lpData = GlobalLock(hData)))
  61. {
  62. PERROR(TEXT("GlobalLock failure in RenderFormat\n\r"));
  63. GlobalFree(hData);
  64. return NULL;
  65. }
  66. dwDataOffset = pfmthdr->DataOffset + MetaOffset;
  67. PINFO("Getting data for %ws at offset %ld\r\n",pfmthdr->Name, dwDataOffset);
  68. SetFilePointer(fh, dwDataOffset, NULL, FILE_BEGIN);
  69. ReadFile (fh, lpData, pfmthdr->DataLen - MetaOffset, &dwBytesRead, NULL);
  70. if(pfmthdr->DataLen - MetaOffset != dwBytesRead)
  71. {
  72. // Error in reading the file
  73. GlobalUnlock(hData);
  74. GlobalFree(hData);
  75. PERROR(TEXT("RenderFormat: Read err, expected %d bytes, got %d\r\n"),
  76. pfmthdr->DataLen - MetaOffset, dwBytesRead);
  77. return (NULL);
  78. }
  79. // As when we write these, we have to special case a few of them
  80. // This code and the write code should match in terms of the sizes
  81. // and positions of data blocks being written out.
  82. // EVERY case in this switch should have a GlobalUnlock(hData);
  83. // statement. We go in with the block locked, but should come
  84. // out with the block unlocked.
  85. switch (pfmthdr->FormatID)
  86. {
  87. case CF_ENHMETAFILE:
  88. {
  89. HENHMETAFILE hemf;
  90. hemf = SetEnhMetaFileBits(pfmthdr->DataLen, lpData);
  91. GlobalUnlock(hData);
  92. GlobalFree(hData);
  93. hData = hemf;
  94. break;
  95. }
  96. case CF_METAFILEPICT:
  97. {
  98. HANDLE hMF;
  99. HANDLE hMFP;
  100. LPMETAFILEPICT lpMFP;
  101. /* Create the METAFILE with the bits we read in. */
  102. hMF = SetMetaFileBitsEx(pfmthdr->DataLen, lpData);
  103. GlobalUnlock(hData);
  104. GlobalFree(hData);
  105. hData = NULL;
  106. if (hMF)
  107. {
  108. /* Alloc a METAFILEPICT header. */
  109. if (hMFP = GlobalAlloc(GHND, (DWORD)sizeof(METAFILEPICT)))
  110. {
  111. if (!(lpMFP = (LPMETAFILEPICT)GlobalLock(hMFP)))
  112. {
  113. GlobalFree(hMFP);
  114. }
  115. else
  116. {
  117. /* Reposition to the start of the METAFILEPICT header. */
  118. SetFilePointer(fh, pfmthdr->DataOffset, NULL, FILE_BEGIN);
  119. /* Read in the data */
  120. if (fNTReadFileFormat)
  121. {
  122. ReadFile(fh, lpMFP, sizeof(METAFILEPICT),
  123. &dwBytesRead, NULL);
  124. }
  125. else
  126. {
  127. WIN31METAFILEPICT w31mfp;
  128. ReadFile(fh, &w31mfp, sizeof(w31mfp), &dwBytesRead, NULL);
  129. if (sizeof(w31mfp) == dwBytesRead)
  130. {
  131. lpMFP->mm = w31mfp.mm;
  132. lpMFP->xExt = w31mfp.xExt;
  133. lpMFP->yExt = w31mfp.yExt;
  134. }
  135. }
  136. lpMFP->hMF = hMF; /* Update the METAFILE handle */
  137. GlobalUnlock(hMFP); /* Unlock the header */
  138. hData = hMFP; /* Stuff this in the clipboard */
  139. }
  140. }
  141. }
  142. break;
  143. }
  144. case CF_BITMAP:
  145. // Reposition to the start of the METAFILEPICT header.
  146. SetFilePointer(fh, pfmthdr->DataOffset, NULL, FILE_BEGIN);
  147. /* Read in the BITMAP struct */
  148. if (fNTReadFileFormat)
  149. {
  150. if (!ReadFile(fh, &bitmap, sizeof(BITMAP), &dwBytesRead, NULL))
  151. memset(&bitmap, 0, sizeof(bitmap));
  152. }
  153. else
  154. {
  155. // Read in an old-style BITMAP struct, and set the fields
  156. // of the new-style BITMAP from that.
  157. WIN31BITMAP w31bm;
  158. if (ReadFile(fh, &w31bm, sizeof(w31bm), &dwBytesRead, NULL))
  159. {
  160. bitmap.bmType = w31bm.bmType;
  161. bitmap.bmWidth = w31bm.bmWidth;
  162. bitmap.bmHeight = w31bm.bmHeight;
  163. bitmap.bmWidthBytes = w31bm.bmWidthBytes;
  164. bitmap.bmPlanes = w31bm.bmPlanes;
  165. bitmap.bmBitsPixel = w31bm.bmBitsPixel;
  166. }
  167. else
  168. {
  169. memset(&bitmap, 0, sizeof(bitmap));
  170. }
  171. }
  172. // Set the bmBits member of the BITMAP to point to our existing
  173. // bits and make the bitmap.
  174. bitmap.bmBits = lpData;
  175. hBitmap = CreateBitmapIndirect(&bitmap);
  176. // Dump the original data (which was just the bitmap bits) and
  177. // make the bitmap handle our data handle.
  178. GlobalUnlock(hData);
  179. GlobalFree(hData);
  180. hData = hBitmap; // Stuff this in the clipboard
  181. break;
  182. case CF_PALETTE:
  183. lpLogPalette = (LPLOGPALETTE)lpData;
  184. hPalette = CreatePalette(lpLogPalette);
  185. GlobalUnlock(hData);
  186. GlobalFree(hData);
  187. hData = hPalette;
  188. break;
  189. default:
  190. GlobalUnlock(hData);
  191. break;
  192. }
  193. return(hData);
  194. }
  195. HANDLE RenderFormatDibToBitmap(
  196. FORMATHEADER *pfmthdr,
  197. register HANDLE fh,
  198. HPALETTE hPalette)
  199. {
  200. HANDLE hBitmap;
  201. register HANDLE hData;
  202. LPSTR lpData;
  203. DWORD dwBytesRead;
  204. DWORD dwDataOffset;
  205. if (PRIVATE_FORMAT(pfmthdr->FormatID))
  206. pfmthdr->FormatID = RegisterClipboardFormatW(pfmthdr->Name);
  207. if (!(hData = GlobalAlloc(GHND, pfmthdr->DataLen)))
  208. {
  209. PERROR(TEXT("GlobalAlloc failure in RenderFormat\n\r"));
  210. return NULL;
  211. }
  212. if (!(lpData = GlobalLock(hData)))
  213. {
  214. PERROR(TEXT("GlobalLock failure in RenderFormat\n\r"));
  215. GlobalFree(hData);
  216. return NULL;
  217. }
  218. dwDataOffset = pfmthdr->DataOffset;
  219. PINFO("Getting data for %ws at offset %ld\r\n",pfmthdr->Name, dwDataOffset);
  220. SetFilePointer(fh, dwDataOffset, NULL, FILE_BEGIN);
  221. ReadFile (fh, lpData, pfmthdr->DataLen, &dwBytesRead, NULL);
  222. if(pfmthdr->DataLen != dwBytesRead)
  223. {
  224. // Error in reading the file
  225. GlobalUnlock(hData);
  226. GlobalFree(hData);
  227. PERROR (TEXT("RenderFormat: Read err, expected %d bytes, got %d\r\n"),
  228. pfmthdr->DataLen, dwBytesRead);
  229. return (NULL);
  230. }
  231. GlobalUnlock(hData);
  232. hBitmap = BitmapFromDib (hData, hPalette);
  233. GlobalFree (hData);
  234. return (hBitmap);
  235. }