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.

399 lines
8.5 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. csc_bmpd.c
  5. Abstract:
  6. This module implements the utility functions of bitmaps associated
  7. with CSC files specifically for the db application. CSC_BMP_U is
  8. an opaque structure. Must use the functions here to
  9. create/modify/destroy a CSC_BMP_U to ensure data integrity. The
  10. 'd' in the filename means "db."
  11. Author:
  12. Nigel Choi [t-nigelc] Sept 3, 1999
  13. --*/
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <assert.h>
  19. #include <winbase.h>
  20. #include "csc_bmpd.h"
  21. // append this to inode file name to get the stream name
  22. LPSTR CscBmpAltStrmName = STRMNAME;
  23. /*++
  24. DBCSC_BitmapCreate()
  25. Routine Description:
  26. Allocates an appropriate in-memory bitmap CSC_BITMAP_DB with size
  27. corresponding to filesize.
  28. Arguments:
  29. Returns:
  30. NULL if memory allocation error.
  31. pointer to the newly allocated bitmap if successful.
  32. Notes:
  33. --*/
  34. LPCSC_BITMAP_DB
  35. DBCSC_BitmapCreate(
  36. DWORD filesize)
  37. {
  38. LPCSC_BITMAP_DB bm;
  39. DWORD i;
  40. bm = (LPCSC_BITMAP_DB)malloc(sizeof(CSC_BITMAP_DB));
  41. if (bm == NULL)
  42. return NULL;
  43. bm->bitmapsize = filesize/BLOCKSIZE;
  44. if (filesize % BLOCKSIZE)
  45. bm->bitmapsize++;
  46. bm->numDWORD = bm->bitmapsize/(8*sizeof(DWORD));
  47. if (bm->bitmapsize % (8*sizeof(DWORD)))
  48. bm->numDWORD++;
  49. if (bm->bitmapsize) {
  50. bm->bitmap = (LPDWORD)malloc(bm->numDWORD*sizeof(DWORD));
  51. if (bm->bitmap == NULL) {
  52. free(bm);
  53. return NULL;
  54. }
  55. for (i = 0; i < bm->numDWORD; i++) {
  56. bm->bitmap[i] = 0;
  57. }
  58. } else {
  59. bm->bitmap = NULL;
  60. }
  61. return bm;
  62. }
  63. /*++
  64. DBCSC_BitmapDelete()
  65. Routine Description:
  66. Arguments:
  67. Returns:
  68. Notes:
  69. --*/
  70. void
  71. DBCSC_BitmapDelete(
  72. LPCSC_BITMAP_DB *lplpbitmap)
  73. {
  74. if (lplpbitmap == NULL)
  75. return;
  76. if (*lplpbitmap == NULL)
  77. return;
  78. if ((*lplpbitmap)->bitmap)
  79. free((*lplpbitmap)->bitmap);
  80. free((*lplpbitmap));
  81. *lplpbitmap = NULL;
  82. }
  83. /*++
  84. DBCSC_BitmapIsMarked()
  85. Routine Description:
  86. Arguments:
  87. Returns:
  88. -1 if lpbitmap is NULL or bitoffset is larger than the bitmap
  89. TRUE if the bit is marked
  90. FALSE if the bit is unmarked
  91. Notes:
  92. --*/
  93. int
  94. DBCSC_BitmapIsMarked(
  95. LPCSC_BITMAP_DB lpbitmap,
  96. DWORD bitoffset)
  97. {
  98. DWORD DWORDnum;
  99. DWORD bitpos;
  100. if (lpbitmap == NULL)
  101. return -1;
  102. if (bitoffset >= lpbitmap->bitmapsize)
  103. return -1;
  104. DWORDnum = bitoffset/(8*sizeof(DWORD));
  105. bitpos = 1 << bitoffset%(8*sizeof(DWORD));
  106. if (lpbitmap->bitmap[DWORDnum] & bitpos)
  107. return TRUE;
  108. return FALSE;
  109. }
  110. /*++
  111. DBCSC_BitmapAppendStreamName()
  112. Routine Description:
  113. Appends the CSC stream name to the existing path/file name fname.
  114. Arguments:
  115. fname is the sting buffer containing the path/file.
  116. bufsize is the buffer size.
  117. Returns:
  118. TRUE if append successful.
  119. FALSE if buffer is too small or other errors.
  120. Notes:
  121. Single-byte strings only.
  122. --*/
  123. int
  124. DBCSC_BitmapAppendStreamName(
  125. LPSTR fname,
  126. DWORD bufsize)
  127. {
  128. int ret = TRUE;
  129. if ((strlen(fname) + strlen(CscBmpAltStrmName) + 1) > bufsize) {
  130. return FALSE;
  131. }
  132. __try {
  133. ret = TRUE;
  134. strcat(fname, CscBmpAltStrmName);
  135. } __except(EXCEPTION_EXECUTE_HANDLER) {
  136. ret = FALSE;
  137. }
  138. return ret;
  139. }
  140. /*++
  141. DBCSC_BitmapRead()
  142. Routine Description:
  143. Reads the on-disk bitmap file, and if it exists, is not in use and valid,
  144. store it in *lplpbitmap. If *lplpbitmap is NULL allocate a new
  145. bitmap data structure. Otherwise, if *lplpbitmap is not NULL, the
  146. existing bitmap will be deleted and assigned the on-disk bitmap
  147. file.
  148. Arguments:
  149. filename is the file that contains the bitmap. If read from a
  150. stream, append the stream name before passing the filename in. The
  151. filename is used as is and no checking of validity of the name is
  152. performed. For default stream name, append the global LPSTR
  153. CscBmpAltStrmName.
  154. Returns:
  155. 1 if read successful
  156. 0 if lplpbitmap is NULL
  157. -1 if error in disk operation (open/read), memory allocating error,
  158. or invalid bitmap file format.
  159. -2 if bitmap not exist
  160. Notes:
  161. CODE.IMPROVEMENT design a better error message propagation mechanism.
  162. Bitmap open for exclusive access.
  163. --*/
  164. int
  165. DBCSC_BitmapRead(
  166. LPCSC_BITMAP_DB *lplpbitmap,
  167. LPCTSTR filename)
  168. {
  169. CscBmpFileHdr hdr;
  170. HANDLE bitmapFile;
  171. DWORD bytesRead;
  172. DWORD bitmapByteSize;
  173. DWORD * bitmapBuf = NULL;
  174. DWORD errCode;
  175. int ret = 1;
  176. if (lplpbitmap == NULL)
  177. return 0;
  178. bitmapFile = CreateFile(
  179. filename,
  180. GENERIC_READ,
  181. 0, // No sharing; exclusive
  182. NULL,
  183. OPEN_EXISTING,
  184. FILE_ATTRIBUTE_NORMAL,
  185. NULL);
  186. if (bitmapFile == INVALID_HANDLE_VALUE) {
  187. errCode = GetLastError();
  188. if (errCode == ERROR_FILE_NOT_FOUND) {
  189. // File does not exist
  190. return -2;
  191. }
  192. return -1;
  193. }
  194. if (!ReadFile(
  195. bitmapFile,
  196. &hdr,
  197. sizeof(CscBmpFileHdr),
  198. &bytesRead,
  199. NULL)
  200. ) {
  201. ret = -1;
  202. goto CLOSEFILE;
  203. }
  204. if (
  205. bytesRead != sizeof(CscBmpFileHdr)
  206. ||
  207. hdr.magicnum != MAGICNUM
  208. ||
  209. !hdr.valid
  210. ||
  211. hdr.inuse
  212. ) {
  213. ret = -1;
  214. goto CLOSEFILE;
  215. }
  216. printf(
  217. "---Header---\n"
  218. "MagicNum: 0x%x\n"
  219. "inuse: 0x%x\n"
  220. "valid: 0x%x\n"
  221. "sizeinbits:0x%x\n"
  222. "numDWORDS:0x%x\n",
  223. hdr.magicnum,
  224. hdr.inuse,
  225. hdr.valid,
  226. hdr.sizeinbits,
  227. hdr.numDWORDs);
  228. if (hdr.sizeinbits > 0) {
  229. bitmapByteSize = hdr.numDWORDs*sizeof(DWORD);
  230. bitmapBuf = (DWORD *)malloc(bitmapByteSize);
  231. if (!bitmapBuf) {
  232. ret = -1;
  233. goto CLOSEFILE;
  234. }
  235. if (!ReadFile(
  236. bitmapFile,
  237. bitmapBuf,
  238. bitmapByteSize,
  239. &bytesRead,
  240. NULL)
  241. ) {
  242. ret = -1;
  243. goto CLOSEFILE;
  244. }
  245. if (bytesRead != bitmapByteSize) {
  246. ret = -1;
  247. goto CLOSEFILE;
  248. }
  249. }
  250. if (*lplpbitmap) {
  251. // bitmap exist, dump old and create new
  252. if ((*lplpbitmap)->bitmap)
  253. free((*lplpbitmap)->bitmap);
  254. (*lplpbitmap)->bitmap = bitmapBuf;
  255. (*lplpbitmap)->numDWORD = hdr.numDWORDs;
  256. (*lplpbitmap)->bitmapsize = hdr.sizeinbits;
  257. } else {
  258. // bitmap not exist, create brand new
  259. *lplpbitmap = (LPCSC_BITMAP_DB)malloc(sizeof(CSC_BITMAP_DB));
  260. if (!(*lplpbitmap)) {
  261. // Error in memory allocation
  262. ret = -1;
  263. goto CLOSEFILE;
  264. }
  265. (*lplpbitmap)->bitmap = bitmapBuf;
  266. (*lplpbitmap)->numDWORD = hdr.numDWORDs;
  267. (*lplpbitmap)->bitmapsize = hdr.sizeinbits;
  268. }
  269. CLOSEFILE:
  270. CloseHandle(bitmapFile);
  271. return ret;
  272. }
  273. /*++
  274. DBCSC_BitmapOutput()
  275. Routine Description:
  276. Outputs the passed in bitmap to the ouput file stream outStrm
  277. Arguments:
  278. Returns:
  279. Notes:
  280. --*/
  281. void
  282. DBCSC_BitmapOutput(
  283. FILE * outStrm,
  284. LPCSC_BITMAP_DB lpbitmap)
  285. {
  286. DWORD i;
  287. if (lpbitmap == NULL) {
  288. fprintf(outStrm, "lpbitmap is NULL\n");
  289. return;
  290. }
  291. fprintf(outStrm, "lpbitmap 0x%08x, bitmapsize %u, numDWORD %u\n",
  292. (ULONG_PTR)lpbitmap, lpbitmap->bitmapsize, lpbitmap->numDWORD);
  293. fprintf(outStrm, "bitmap |0/5 |1/6 |2/7 |3/8 |4/9\n");
  294. fprintf(outStrm, "number |01234|56789|01234|56789|01234|56789|01234|56789|01234|56789");
  295. for (i = 0; i < lpbitmap->bitmapsize; i++) {
  296. if ((i % 50) == 0)
  297. fprintf(outStrm, "\n%08d", i);
  298. if ((i % 5) == 0)
  299. fprintf(outStrm, "|");
  300. fprintf(outStrm, "%1d", DBCSC_BitmapIsMarked(lpbitmap, i));
  301. }
  302. fprintf(outStrm, "\n");
  303. }