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.

813 lines
15 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. Wsbdbkey.cpp
  5. Abstract:
  6. The CWsbDbKey class.
  7. Author:
  8. Ron White [ronw] 1-Jul-1997
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "wsbdbkey.h"
  13. // Local stuff
  14. HRESULT
  15. CWsbDbKey::AppendBool(
  16. BOOL value
  17. )
  18. /*++
  19. Implements:
  20. IWsbDbKey::AppendBool
  21. --*/
  22. {
  23. HRESULT hr = S_OK;
  24. ULONG size;
  25. WsbTraceIn(OLESTR("CWsbDbKey::AppendBool"), OLESTR("value = <%ls>"), WsbBoolAsString(value));
  26. try {
  27. WsbAssert(make_key(m_size + WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  28. WsbAffirmHr(WsbConvertToBytes(&m_value[m_size], value, &size));
  29. m_size += size;
  30. } WsbCatch(hr);
  31. WsbTraceOut(OLESTR("CWsbDbKey::AppendBool"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  32. return(hr);
  33. }
  34. HRESULT
  35. CWsbDbKey::AppendBytes(
  36. UCHAR* value,
  37. ULONG size
  38. )
  39. /*++
  40. Implements:
  41. IWsbDbKey::AppendBytes
  42. --*/
  43. {
  44. HRESULT hr = S_OK;
  45. WsbTraceIn(OLESTR("CWsbDbKey::AppendBytes"), OLESTR("size = <%ld>"), size);
  46. try {
  47. WsbAssert(size > 0, E_UNEXPECTED);
  48. WsbAssert(make_key(size + m_size), WSB_E_RESOURCE_UNAVAILABLE);
  49. memcpy(&m_value[m_size], value, size);
  50. m_size += size;
  51. } WsbCatch(hr);
  52. WsbTraceOut(OLESTR("CWsbDbKey::AppendBytes"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  53. return(hr);
  54. }
  55. HRESULT
  56. CWsbDbKey::AppendFiletime(
  57. FILETIME value
  58. )
  59. /*++
  60. Implements:
  61. IWsbDbKey::AppendFiletime
  62. --*/
  63. {
  64. HRESULT hr = S_OK;
  65. ULONG size;
  66. WsbTraceIn(OLESTR("CWsbDbKey::AppendFiletime"), OLESTR(""));
  67. try {
  68. WsbAssert(make_key(m_size + WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  69. WsbAffirmHr(WsbConvertToBytes(&m_value[m_size], value, &size));
  70. m_size += size;
  71. } WsbCatch(hr);
  72. WsbTraceOut(OLESTR("CWsbDbKey::AppendFiletime"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  73. return(hr);
  74. }
  75. HRESULT
  76. CWsbDbKey::AppendGuid(
  77. GUID value
  78. )
  79. /*++
  80. Implements:
  81. IWsbDbKey::AppendGuid
  82. --*/
  83. {
  84. HRESULT hr = S_OK;
  85. ULONG size;
  86. WsbTraceIn(OLESTR("CWsbDbKey::AppendGuid"), OLESTR("value = <%ls>"), WsbGuidAsString(value));
  87. try {
  88. WsbAssert(make_key(m_size + WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  89. WsbAffirmHr(WsbConvertToBytes(&m_value[m_size], value, &size));
  90. m_size += size;
  91. } WsbCatch(hr);
  92. WsbTraceOut(OLESTR("CWsbDbKey::AppendGuid"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  93. return(hr);
  94. }
  95. HRESULT
  96. CWsbDbKey::AppendLonglong(
  97. LONGLONG value
  98. )
  99. /*++
  100. Implements:
  101. IWsbDbKey::AppendLonglong
  102. --*/
  103. {
  104. HRESULT hr = S_OK;
  105. ULONG size;
  106. WsbTraceIn(OLESTR("CWsbDbKey::AppendLonglong"), OLESTR("value = <%ls>"),
  107. WsbLonglongAsString(value));
  108. try {
  109. WsbAssert(make_key(m_size + WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  110. WsbAffirmHr(WsbConvertToBytes(&m_value[m_size], value, &size));
  111. m_size += size;
  112. } WsbCatch(hr);
  113. WsbTraceOut(OLESTR("CWsbDbKey::AppendLonglong"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  114. return(hr);
  115. }
  116. HRESULT
  117. CWsbDbKey::AppendString(
  118. OLECHAR* value
  119. )
  120. /*++
  121. Implements:
  122. IWsbDbKey::AppendString
  123. --*/
  124. {
  125. HRESULT hr = S_OK;
  126. WsbTraceIn(OLESTR("CWsbDbKey::AppendString"), OLESTR(""));
  127. try {
  128. ULONG size;
  129. size = wcslen(value) * sizeof(OLECHAR);
  130. WsbAffirmHr(AppendBytes((UCHAR *)value, size));
  131. } WsbCatch(hr);
  132. WsbTraceOut(OLESTR("CWsbDbKey::AppendString"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  133. return(hr);
  134. }
  135. HRESULT
  136. CWsbDbKey::CompareTo(
  137. IN IUnknown* pCollectable,
  138. OUT SHORT* pResult
  139. )
  140. /*++
  141. Implements:
  142. IWsbCollectable::CompareTo
  143. --*/
  144. {
  145. HRESULT hr = S_FALSE;
  146. WsbTraceIn(OLESTR("CWsbDbKey::CompareTo"), OLESTR(""));
  147. try {
  148. UCHAR* bytes2;
  149. CComPtr<IWsbDbKey> pKey2;
  150. CComPtr<IWsbDbKeyPriv> pKeyPriv2;
  151. SHORT result;
  152. ULONG size2;
  153. // Did they give us a valid item to compare to?
  154. WsbAssert(0 != pCollectable, E_POINTER);
  155. // We need the IWsbDbKey interface to get the value.
  156. WsbAffirmHr(pCollectable->QueryInterface(IID_IWsbDbKey, (void**) &pKey2));
  157. WsbAffirmHr(pKey2->QueryInterface(IID_IWsbDbKeyPriv,
  158. (void**)&pKeyPriv2));
  159. // Get the other key's bytes
  160. bytes2 = NULL;
  161. WsbAffirmHr(pKeyPriv2->GetBytes(&bytes2, &size2));
  162. // Do compare
  163. if (size2 == 0 && m_size == 0) {
  164. result = 0;
  165. } else if (size2 == 0) {
  166. result = 1;
  167. } else if (m_size == 0) {
  168. result = -1;
  169. } else {
  170. result = WsbSign( memcmp(m_value, bytes2, min(m_size, size2)) );
  171. if (result == 0 && m_size != size2) {
  172. result = (m_size > size2) ? (SHORT)1 : (SHORT)-1;
  173. }
  174. }
  175. WsbFree(bytes2);
  176. // If the aren't equal, then return false.
  177. if (result != 0) {
  178. hr = S_FALSE;
  179. }
  180. else {
  181. hr = S_OK;
  182. }
  183. *pResult = result;
  184. } WsbCatch(hr);
  185. WsbTraceOut(OLESTR("CWsbDbKey::CompareTo"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  186. return(hr);
  187. }
  188. HRESULT
  189. CWsbDbKey::GetBytes(
  190. OUT UCHAR** ppBytes,
  191. OUT ULONG* pSize
  192. )
  193. /*++
  194. Implements:
  195. IWsbDbKey::GetBytes
  196. --*/
  197. {
  198. HRESULT hr = S_OK;
  199. WsbTraceIn(OLESTR("CWsbDbKey::GetBytes"), OLESTR(""));
  200. try {
  201. if (ppBytes && m_size) {
  202. if (*ppBytes == NULL) {
  203. *ppBytes = (UCHAR *)WsbAlloc(m_size);
  204. }
  205. if (*ppBytes) {
  206. memcpy(*ppBytes, m_value, m_size);
  207. } else {
  208. WsbThrow(E_OUTOFMEMORY);
  209. }
  210. }
  211. if (pSize) {
  212. *pSize = m_size;
  213. }
  214. } WsbCatch(hr);
  215. WsbTraceOut(OLESTR("CWsbDbKey::GetBytes"), OLESTR("hr = <%ls>"),
  216. WsbHrAsString(hr));
  217. return(hr);
  218. }
  219. HRESULT
  220. CWsbDbKey::GetType(
  221. OUT ULONG* pType
  222. )
  223. /*++
  224. Implements:
  225. IWsbDbKey::GetType
  226. --*/
  227. {
  228. HRESULT hr = S_OK;
  229. WsbTraceIn(OLESTR("CWsbDbKey::GetType"), OLESTR(""));
  230. try {
  231. WsbAffirm(pType != NULL, E_POINTER);
  232. *pType = m_type;
  233. } WsbCatch(hr);
  234. WsbTraceOut(OLESTR("CWsbDbKey::GetType"), OLESTR("hr = <%ls>, value = <%ls>"), WsbHrAsString(hr), m_value);
  235. return(hr);
  236. }
  237. HRESULT
  238. CWsbDbKey::SetToBool(
  239. BOOL value
  240. )
  241. /*++
  242. Implements:
  243. IWsbDbKey::SetToBool
  244. --*/
  245. {
  246. HRESULT hr = S_OK;
  247. WsbTraceIn(OLESTR("CWsbDbKey::SetToBool"), OLESTR("value = <%ls>"),
  248. WsbBoolAsString(value));
  249. try {
  250. WsbAssert(make_key(WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  251. WsbAffirmHr(WsbConvertToBytes(m_value, value, &m_size));
  252. } WsbCatch(hr);
  253. WsbTraceOut(OLESTR("CWsbDbKey::SetToBool"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  254. return(hr);
  255. }
  256. HRESULT
  257. CWsbDbKey::SetToBytes(
  258. UCHAR* value,
  259. ULONG size
  260. )
  261. /*++
  262. Implements:
  263. IWsbDbKey::SetToBytes
  264. --*/
  265. {
  266. HRESULT hr = S_OK;
  267. WsbTraceIn(OLESTR("CWsbDbKey::SetToBytes"), OLESTR("size = <%ld>"), size);
  268. try {
  269. WsbAssert(size > 0, E_UNEXPECTED);
  270. WsbAssert(make_key(size), WSB_E_RESOURCE_UNAVAILABLE);
  271. memcpy(m_value, value, size);
  272. m_size = size;
  273. } WsbCatch(hr);
  274. WsbTraceOut(OLESTR("CWsbDbKey::SetToBytes"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  275. return(hr);
  276. }
  277. HRESULT
  278. CWsbDbKey::SetToFiletime(
  279. FILETIME value
  280. )
  281. /*++
  282. Implements:
  283. IWsbDbKey::SetToFiletime
  284. --*/
  285. {
  286. HRESULT hr = S_OK;
  287. WsbTraceIn(OLESTR("CWsbDbKey::SetToFiletime"), OLESTR(""));
  288. try {
  289. WsbAssert(make_key(WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  290. WsbAffirmHr(WsbConvertToBytes(m_value, value, &m_size));
  291. } WsbCatch(hr);
  292. WsbTraceOut(OLESTR("CWsbDbKey::SetToFiletime"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  293. return(hr);
  294. }
  295. HRESULT
  296. CWsbDbKey::SetToGuid(
  297. GUID value
  298. )
  299. /*++
  300. Implements:
  301. IWsbDbKey::SetToGuid
  302. --*/
  303. {
  304. HRESULT hr = S_OK;
  305. WsbTraceIn(OLESTR("CWsbDbKey::SetToGuid"), OLESTR("value = <%ls>"),
  306. WsbGuidAsString(value));
  307. try {
  308. WsbAssert(make_key(WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  309. WsbAffirmHr(WsbConvertToBytes(m_value, value, &m_size));
  310. } WsbCatch(hr);
  311. WsbTraceOut(OLESTR("CWsbDbKey::SetToGuid"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  312. return(hr);
  313. }
  314. HRESULT
  315. CWsbDbKey::SetToLonglong(
  316. LONGLONG value
  317. )
  318. /*++
  319. Implements:
  320. IWsbDbKey::SetToLonglong
  321. --*/
  322. {
  323. HRESULT hr = S_OK;
  324. WsbTraceIn(OLESTR("CWsbDbKey::SetToLonglong"), OLESTR("value = <%ls>"),
  325. WsbLonglongAsString(value));
  326. try {
  327. WsbAssert(make_key(WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  328. WsbAffirmHr(WsbConvertToBytes(m_value, value, &m_size));
  329. } WsbCatch(hr);
  330. WsbTraceOut(OLESTR("CWsbDbKey::SetToLonglong"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  331. return(hr);
  332. }
  333. HRESULT
  334. CWsbDbKey::SetToUlong(
  335. ULONG value
  336. )
  337. /*++
  338. Implements:
  339. IWsbDbKey::SetToUlong
  340. --*/
  341. {
  342. HRESULT hr = S_OK;
  343. WsbTraceIn(OLESTR("CWsbDbKey::SetToUlong"), OLESTR("value = <%ld>"), value);
  344. try {
  345. WsbAssert(make_key(WsbByteSize(value)), WSB_E_RESOURCE_UNAVAILABLE);
  346. WsbAffirmHr(WsbConvertToBytes(m_value, value, &m_size));
  347. } WsbCatch(hr);
  348. WsbTraceOut(OLESTR("CWsbDbKey::SetToUlong"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  349. return(hr);
  350. }
  351. HRESULT
  352. CWsbDbKey::SetToString(
  353. OLECHAR* value
  354. )
  355. /*++
  356. Implements:
  357. IWsbDbKey::SetToString
  358. --*/
  359. {
  360. HRESULT hr = S_OK;
  361. WsbTraceIn(OLESTR("CWsbDbKey::SetToString"), OLESTR(""));
  362. try {
  363. ULONG size;
  364. size = wcslen(value) * sizeof(OLECHAR);
  365. WsbAffirmHr(SetToBytes((UCHAR *)value, size));
  366. } WsbCatch(hr);
  367. WsbTraceOut(OLESTR("CWsbDbKey::SetToString"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  368. return(hr);
  369. }
  370. HRESULT
  371. CWsbDbKey::FinalConstruct(
  372. void
  373. )
  374. /*++
  375. Implements:
  376. CComObjectRoot::FinalConstruct
  377. --*/
  378. {
  379. HRESULT hr = S_OK;
  380. WsbTraceIn(OLESTR("CWsbDbKey::FinalConstruct"), OLESTR("") );
  381. try {
  382. WsbAffirmHr(CWsbObject::FinalConstruct());
  383. m_value = NULL;
  384. m_size = 0;
  385. m_max = 0;
  386. } WsbCatch(hr);
  387. WsbTraceOut(OLESTR("CWsbDbKey::FinalConstruct"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  388. return(hr);
  389. }
  390. void
  391. CWsbDbKey::FinalRelease(
  392. void
  393. )
  394. /*++
  395. Routine Description:
  396. This method does some cleanup of the object that is necessary
  397. during destruction.
  398. Arguments:
  399. None.
  400. Return Value:
  401. None.
  402. --*/
  403. {
  404. HRESULT hr = S_OK;
  405. WsbTraceIn(OLESTR("CWsbDbKey::FinalRelease"), OLESTR(""));
  406. try {
  407. if (m_value) {
  408. WsbFree(m_value);
  409. m_value = NULL;
  410. }
  411. CWsbObject::FinalRelease();
  412. } WsbCatch(hr);
  413. WsbTraceOut(OLESTR("CWsbDbKey::FinalRelease"), OLESTR("hr =<%ls>"), WsbHrAsString(hr));
  414. }
  415. HRESULT
  416. CWsbDbKey::GetClassID(
  417. OUT CLSID* pClsid
  418. )
  419. /*++
  420. Implements:
  421. IPersist::GetClassID().
  422. --*/
  423. {
  424. HRESULT hr = S_OK;
  425. WsbTraceIn(OLESTR("CWsbDbKey::GetClassID"), OLESTR(""));
  426. try {
  427. WsbAssert(0 != pClsid, E_POINTER);
  428. *pClsid = CLSID_CWsbDbKey;
  429. } WsbCatch(hr);
  430. WsbTraceOut(OLESTR("CWsbDbKey::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  431. return(hr);
  432. }
  433. HRESULT
  434. CWsbDbKey::GetSizeMax(
  435. OUT ULARGE_INTEGER* /*pSize*/
  436. )
  437. /*++
  438. Implements:
  439. IPersistStream::GetSizeMax().
  440. --*/
  441. {
  442. HRESULT hr = E_NOTIMPL;
  443. return(hr);
  444. }
  445. HRESULT
  446. CWsbDbKey::Load(
  447. IN IStream* /*pStream*/
  448. )
  449. /*++
  450. Implements:
  451. IPersistStream::Load().
  452. --*/
  453. {
  454. HRESULT hr = E_NOTIMPL;
  455. return(hr);
  456. }
  457. HRESULT
  458. CWsbDbKey::Save(
  459. IN IStream* /*pStream*/,
  460. IN BOOL /*clearDirty*/
  461. )
  462. /*++
  463. Implements:
  464. IPersistStream::Save().
  465. --*/
  466. {
  467. HRESULT hr = E_NOTIMPL;
  468. return(hr);
  469. }
  470. // CWsbDbKey internal helper functions
  471. // make_key - create a key of the specified size
  472. BOOL
  473. CWsbDbKey::make_key(
  474. ULONG size
  475. )
  476. {
  477. BOOL status = FALSE;
  478. LPVOID pTemp;
  479. if ( (size > IDB_MAX_KEY_SIZE) || (size == 0) ) {
  480. status = FALSE;
  481. } else if (m_value && m_max >= size) {
  482. status = TRUE;
  483. } else {
  484. pTemp = WsbRealloc(m_value, size);
  485. if ( pTemp ) {
  486. m_value = (PUCHAR) pTemp;
  487. status = TRUE;
  488. m_max = size;
  489. }
  490. }
  491. return(status);
  492. }
  493. HRESULT
  494. CWsbDbKey::Test(
  495. OUT USHORT* passed,
  496. OUT USHORT* failed
  497. )
  498. /*++
  499. Implements:
  500. IWsbTestable::Test().
  501. --*/
  502. {
  503. *passed = 0;
  504. *failed = 0;
  505. HRESULT hr = S_OK;
  506. #if !defined(WSB_NO_TEST)
  507. CComPtr<IWsbDbKey> pDbKey1;
  508. WsbTraceIn(OLESTR("CWsbDbKey::Test"), OLESTR(""));
  509. try {
  510. try {
  511. WsbAssertHr(((IUnknown*) (IWsbDbKey*) this)->QueryInterface(IID_IWsbDbKey, (void**) &pDbKey1));
  512. // Set o a ULONG value, and see if it is returned.
  513. hr = S_OK;
  514. try {
  515. WsbAssertHr(pDbKey1->SetToUlong(0xffffffff));
  516. // ULONG value;
  517. // WsbAssertHr(pDbKey1->GetUlong(&value));
  518. // WsbAssert(value == 0xffffffff, E_FAIL);
  519. } WsbCatch(hr);
  520. if (hr == S_OK) {
  521. (*passed)++;
  522. } else {
  523. (*failed)++;
  524. }
  525. } WsbCatch(hr);
  526. if (hr == S_OK) {
  527. (*passed)++;
  528. } else {
  529. (*failed)++;
  530. }
  531. } WsbCatch(hr);
  532. // Tally up the results
  533. if (*failed) {
  534. hr = S_FALSE;
  535. } else {
  536. hr = S_OK;
  537. }
  538. WsbTraceOut(OLESTR("CWsbDbKey::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  539. #endif // WSB_NO_TEST
  540. return(hr);
  541. }