Source code of Windows XP (NT5)
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.

225 lines
4.3 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 1999 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * blob.c
  7. *
  8. * Abstract:
  9. * This file contains the implementation for r0 blob functions.
  10. *
  11. * Revision History:
  12. * Kanwaljit S Marok ( kmarok ) 05/17/99
  13. * created
  14. *
  15. *****************************************************************************/
  16. #define RING3
  17. #include "common.h"
  18. #include "pathtree.h"
  19. static char * blobTypArr[] = {
  20. "BLOB_TYPE_CONFIG",
  21. "BLOB_TYPE_PATHTREE",
  22. "BLOB_TYPE_HASHLIST",
  23. "BLOB_TYPE_CONTAINER"
  24. };
  25. #define PRINT_BLOB_HEADER( pBlob ) \
  26. printf( "\nBlob: %s, Size: %ld, Version: %ld, Entries: %ld \n",\
  27. blobTypArr[((BlobHeader *)pBlob)->m_dwBlbType], \
  28. ((BlobHeader *)pBlob)->m_dwMaxSize, \
  29. ((BlobHeader *)pBlob)->m_dwVersion, \
  30. ((BlobHeader *)pBlob)->m_dwEntries)
  31. #define COMMON_FILE_HANDLE HANDLE
  32. static __inline COMMON_FILE_HANDLE
  33. COMMON_FILE_OPEN_READ (
  34. PCHAR szFileName
  35. )
  36. {
  37. COMMON_FILE_HANDLE handle;
  38. handle = CreateFile( szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  39. if( handle != INVALID_HANDLE_VALUE )
  40. return handle;
  41. return 0;
  42. }
  43. static __inline COMMON_FILE_HANDLE
  44. COMMON_FILE_OPEN_WRITE (
  45. PCHAR szFileName
  46. )
  47. {
  48. COMMON_FILE_HANDLE handle;
  49. handle = CreateFile( szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  50. if( handle != INVALID_HANDLE_VALUE )
  51. return handle;
  52. return 0;
  53. }
  54. static __inline VOID
  55. COMMON_FILE_CLOSE (
  56. COMMON_FILE_HANDLE fh)
  57. {
  58. if ( fh )
  59. CloseHandle( fh );
  60. }
  61. static __inline DWORD
  62. COMMON_FILE_READ (
  63. COMMON_FILE_HANDLE fh,
  64. DWORD offset,
  65. VOID *buffer,
  66. DWORD dwBytes
  67. )
  68. {
  69. DWORD dwBytesRead = 0;
  70. ReadFile( fh, buffer, dwBytes, &dwBytesRead, NULL );
  71. return dwBytesRead;
  72. }
  73. static __inline DWORD
  74. COMMON_FILE_WRITE (
  75. COMMON_FILE_HANDLE fh,
  76. DWORD offset,
  77. VOID *buffer,
  78. DWORD dwBytes
  79. )
  80. {
  81. DWORD dwBytesWrite = 0;
  82. WriteFile( fh, buffer, dwBytes, &dwBytesWrite, NULL );
  83. return dwBytesWrite;
  84. }
  85. PBYTE
  86. CreateBlobFromFile(
  87. PCHAR pszBlob );
  88. DWORD
  89. CreateCfgBlob(
  90. PCHAR pszBlob,
  91. PCHAR pszTree,
  92. PCHAR pszList );
  93. PBYTE
  94. ReadCfgBlob(
  95. PCHAR pszBlob,
  96. PBYTE * pTree,
  97. PBYTE * pList,
  98. DWORD * pdwDefaultType);
  99. //
  100. // WriteBlobToFile : Writes the given memory blob into the file.
  101. //
  102. DWORD
  103. WriteBlobToFile (
  104. PCHAR pszBlob,
  105. PBYTE pBlob
  106. )
  107. {
  108. COMMON_FILE_HANDLE fh;
  109. BlobHeader * pBlobHeader = (BlobHeader *)pBlob;
  110. fh = COMMON_FILE_OPEN_WRITE( pszBlob ) ;
  111. if ( fh )
  112. {
  113. COMMON_FILE_WRITE( fh, 0, pBlob, pBlobHeader->m_dwMaxSize );
  114. COMMON_FILE_CLOSE( fh );
  115. return BLOB_MAXSIZE( pBlob );
  116. }
  117. return 0;
  118. }
  119. //
  120. // CreateBlobFromFile: This function reads a given file into memory as
  121. // a blob.
  122. //
  123. PBYTE
  124. CreateBlobFromFile(
  125. PCHAR szBlobFile
  126. )
  127. {
  128. BYTE * pBlob = NULL ;
  129. COMMON_FILE_HANDLE fh;
  130. BlobHeader blobHeader;
  131. fh = COMMON_FILE_OPEN_READ( szBlobFile );
  132. //
  133. // TODO : Need to do sanity checking on the dat file.
  134. //
  135. if( fh )
  136. {
  137. if( COMMON_FILE_READ( fh, 0, &blobHeader, sizeof(blobHeader) ) )
  138. {
  139. pBlob = ALLOCATE( blobHeader.m_dwMaxSize );
  140. #ifdef RING3
  141. //
  142. // BUGBUG : need to reset file pointer
  143. //
  144. COMMON_FILE_CLOSE( fh );
  145. fh = COMMON_FILE_OPEN_READ( szBlobFile );
  146. #endif
  147. if(pBlob)
  148. {
  149. COMMON_FILE_READ( fh, 0, pBlob, blobHeader.m_dwMaxSize );
  150. }
  151. }
  152. COMMON_FILE_CLOSE(fh);
  153. }
  154. return pBlob;
  155. }
  156. //
  157. // ReadCfgBlob: Reads the given file a returns pointers to the
  158. // Incl/Excl tree blob and the hashed list of extensions.
  159. //
  160. BYTE *
  161. ReadCfgBlob(
  162. PCHAR pszBlob,
  163. PBYTE * pTree,
  164. PBYTE * pList,
  165. DWORD * pdwDefaultType
  166. )
  167. {
  168. BYTE * pBlob = NULL;
  169. if( !pszBlob || !pTree || !pList || !pdwDefaultType )
  170. return NULL;
  171. *pTree = *pList = NULL;
  172. if( pBlob = CreateBlobFromFile( pszBlob ) )
  173. {
  174. *pTree = pBlob + sizeof(BlobHeader);
  175. *pList = *pTree + BLOB_MAXSIZE( *pTree );
  176. *pdwDefaultType = TREE_HEADER((*pTree))->m_dwDefault;
  177. }
  178. return pBlob;
  179. }