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.

354 lines
6.6 KiB

  1. // RegFile.cpp: implementation of the CRegFile class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "RegFile.h"
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CRegFile::CRegFile()
  11. : m_pFile(NULL), m_pTempLine(NULL), m_TempNameBuf(256)
  12. {
  13. }
  14. CRegFile::~CRegFile()
  15. {
  16. if (m_pFile != NULL)
  17. fclose(m_pFile);
  18. for (int i=0; i < m_TempNameBuf.GetNumElementsStored(); i++)
  19. {
  20. delete m_TempNameBuf.Access()[i];
  21. }
  22. // m_TempName.OverideBuffer(NULL);
  23. }
  24. void CRegFile::WriteString(LPCTSTR str)
  25. {
  26. // DWORD bw;
  27. // WriteFile(m_hFile, str, _tcsclen(str)*sizeof(TCHAR), &bw, NULL);
  28. if (m_pFile == NULL)
  29. return;
  30. fwrite(str,sizeof(TCHAR),_tcsclen(str),m_pFile);
  31. }
  32. void CRegFile::WriteData(LPBYTE pData, DWORD NumBytes)
  33. {
  34. // DWORD bw;
  35. // WriteFile(m_hFile, pData, NumBytes, &bw, NULL);
  36. if (m_pFile == NULL)
  37. return;
  38. fwrite(pData,sizeof(BYTE),NumBytes,m_pFile);
  39. }
  40. void CRegFile::WriteNewLine()
  41. {
  42. //TCHAR nl[] = {13,10,0};
  43. WriteString(TEXT("\r\n"));
  44. }
  45. bool CRegFile::Init(LPCTSTR FileName, BOOL bForReading)
  46. {
  47. /* m_hFile = CreateFile(FileName,
  48. GENERIC_WRITE, 0, NULL,
  49. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
  50. return (m_hFile != INVALID_HANDLE_VALUE);*/
  51. CHAR signature[4] = "\xFF\xFE";
  52. CHAR buffer[4];
  53. DWORD rc;
  54. if(bForReading)
  55. {
  56. m_pFile = _tfopen(FileName, TEXT("rb"));
  57. if (m_pFile) {
  58. if (fread (buffer, 1, 2, m_pFile) < 2) {
  59. rc = GetLastError ();
  60. fclose (m_pFile);
  61. m_pFile = NULL;
  62. }
  63. }
  64. }
  65. else
  66. {
  67. m_pFile = _tfopen(FileName, TEXT("wb"));
  68. if (m_pFile) {
  69. if (fwrite (signature, 1, 2, m_pFile) < 2) {
  70. rc = GetLastError ();
  71. fclose (m_pFile);
  72. m_pFile = NULL;
  73. }
  74. }
  75. }
  76. if (m_pFile == NULL) {
  77. SetLastError (rc);
  78. }
  79. return (m_pFile != NULL);
  80. }
  81. LPCTSTR CRegFile::GetNextLine()
  82. {
  83. if (m_pFile == NULL)
  84. return NULL;
  85. LPTSTR result;
  86. if (m_pTempLine != NULL)
  87. {
  88. result = (LPTSTR)m_pTempLine;
  89. m_pTempLine = NULL;
  90. return (LPCTSTR)result;
  91. }
  92. result = new TCHAR[1024];
  93. if (result == NULL)
  94. {
  95. LOG0(LOG_ERROR, "Could not allocate array in CRegFile::GetNextLine()");
  96. //ASSERT(0);
  97. return NULL;
  98. }
  99. LPTSTR code = _fgetts(result, 1024, m_pFile);
  100. if (code == NULL)
  101. {
  102. delete[] result;
  103. return NULL;
  104. }
  105. else
  106. {
  107. //UNICODE HERE???
  108. int pos = _tcsclen(result);
  109. result[pos-2] = NULL;
  110. return result;
  111. }
  112. }
  113. LPCTSTR CRegFile::GetNextSubKey(LPCTSTR KeyName)
  114. {
  115. if (m_pFile == NULL)
  116. return NULL;
  117. LPCTSTR SubKey;
  118. int keyLen = _tcsclen(KeyName);
  119. // long oldPos;
  120. // oldPos = GetPos();
  121. if (m_pTempLine == NULL)
  122. {
  123. SubKey = GetNextLine();
  124. }
  125. else
  126. {
  127. SubKey = m_pTempLine;
  128. m_pTempLine = NULL;
  129. }
  130. if (SubKey != NULL)
  131. {
  132. if (_tcsncmp(KeyName, SubKey, keyLen) != 0)
  133. {
  134. //delete SubKey;
  135. //SeekToPos(oldPos);
  136. m_pTempLine = SubKey;
  137. //put SubKey back on the stream, since it
  138. //is no longer a sub key of Key.
  139. }
  140. else
  141. {
  142. return SubKey;
  143. }
  144. }
  145. return NULL;
  146. }
  147. //for unicode
  148. #define ERR_VALUE WEOF
  149. CRegDataItemPtr CRegFile::GetNextDataItem()
  150. {
  151. if (m_pFile == NULL)
  152. return new CRegDataItem();
  153. TCHAR c = (TCHAR)_fgettc(m_pFile);
  154. if ((c == EOF) || (c == WEOF))
  155. return new CRegDataItem();
  156. if (c != TEXT('S'))
  157. {
  158. _ungettc(c, m_pFile);
  159. return new CRegDataItem(); //no data item - only reg key on the current line
  160. }
  161. CRegDataItem* result = new CRegDataItem;
  162. if (!result)
  163. {
  164. LOG0(LOG_ERROR,"Could not allocate CRegDataItem");
  165. return NULL;
  166. }
  167. TCHAR type;
  168. //Scan in the length of the variable name
  169. if (_ftscanf(m_pFile, TEXT("%u:"), &result->m_NameLen) == ERR_VALUE)
  170. {
  171. delete result;
  172. LOG0(LOG_ERROR,"CRegDataItem - could not read result->m_NameLen");
  173. return new CRegDataItem();
  174. }
  175. result->m_NameLen++; //names of variables always forget the blank at end
  176. //may be introducing memory leak here!
  177. //***********************************
  178. /* //Scan in the variable name
  179. CStr name(new TCHAR[result->m_NameLen+1]);
  180. _fgetts(name.get(), result->m_NameLen, m_pFile);
  181. result->m_Name = name;
  182. //************************************
  183. */
  184. //Scan in the variable name
  185. TCHAR* temp = new TCHAR[result->m_NameLen+1];
  186. if(_fgetts(temp, result->m_NameLen, m_pFile) == NULL)
  187. {
  188. delete result;
  189. LOG0(LOG_ERROR,"CRegDataItem - could not read result->m_Name");
  190. return new CRegDataItem();
  191. }
  192. result->m_Name = temp;
  193. m_TempNameBuf.AddElement(temp);
  194. //delete[] temp;
  195. //************************************
  196. /* m_TempNameBuf.Allocate(result->m_NameLen+1);
  197. _fgetts(m_TempNameBuf.Access(), result->m_NameLen, m_pFile);
  198. m_TempName.OverideBuffer(m_TempNameBuf.Access());
  199. result->m_Name = m_TempName;
  200. */
  201. //Scan in the type of variable and the length of its data
  202. if (_ftscanf(m_pFile, TEXT(" = %c(%u)%u:"), &type, &result->m_Type, &result->m_DataLen)
  203. == ERR_VALUE)
  204. {
  205. delete result;
  206. LOG0(LOG_ERROR,"CRegDataItem - could not scan in other data values");
  207. return new CRegDataItem();
  208. }
  209. //read in the null byte (for unicode compatibility, can't have odd # of bytes)
  210. if ((result->m_DataLen % 2) != 0)
  211. {
  212. BYTE nullByte;
  213. if (fread(&nullByte,1,1,m_pFile)==0)
  214. {
  215. delete result;
  216. LOG0(LOG_ERROR,"CRegDataItem - could not read null byte");
  217. return new CRegDataItem();
  218. }
  219. }
  220. result->m_bIsEmpty = false;
  221. if (result->m_DataLen > 0)
  222. {
  223. result->m_pDataBuf = new BYTE[result->m_DataLen];
  224. if (fread(result->m_pDataBuf,result->m_DataLen,1,m_pFile) ==0)
  225. {
  226. delete result;
  227. LOG0(LOG_ERROR,"CRegDataItem - could not read null byte");
  228. return new CRegDataItem();
  229. }
  230. }
  231. else
  232. {
  233. result->m_pDataBuf =NULL;
  234. }
  235. //read in cr/lf
  236. c = (TCHAR)_fgettc(m_pFile);
  237. if (c != 13)
  238. {LOG0(LOG_ERROR,"CRegDataItem - could not read char 13");}
  239. c = (TCHAR)_fgettc(m_pFile);
  240. if (c != 10)
  241. {LOG0(LOG_ERROR,"CRegDataItem - could not read char 10");}
  242. return result;
  243. }
  244. TCHAR CRegFile::PeekNextChar()
  245. {
  246. TCHAR c = (TCHAR)_fgettc(m_pFile);
  247. _ungettc(c, m_pFile);
  248. return c;
  249. }
  250. void CRegFile::WriteDataItem(enum SectionType t, CRegDataItemPtr r)
  251. {
  252. //ignore section type
  253. if (!r->m_Name.IsEmpty())
  254. r->m_KeyName = NULL;
  255. r->WriteToFile(*this);
  256. }
  257. bool CRegFile::NeedStorageOfValueData()
  258. {
  259. return false;
  260. }