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.

819 lines
16 KiB

  1. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  2. /*---------------------------------------------------------
  3. Filename: value.cpp
  4. Written By: B.Rajeev
  5. ----------------------------------------------------------*/
  6. #include <precomp.h>
  7. #include <typeinfo.h>
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <strstrea.h>
  11. #include <provimex.h>
  12. #include <provexpt.h>
  13. #include <provval.h>
  14. BOOL ProvNull :: Equivalent (IN const ProvValue &value) const
  15. {
  16. BOOL bResult = FALSE;
  17. if (typeid(*this) == typeid(value))
  18. {
  19. bResult = TRUE;
  20. }
  21. return bResult;
  22. }
  23. // Copy constructor
  24. ProvInteger::ProvInteger ( IN const ProvInteger &value )
  25. {
  26. val = value.GetValue();
  27. }
  28. BOOL ProvInteger :: Equivalent (IN const ProvValue &value) const
  29. {
  30. BOOL bResult = FALSE;
  31. if (typeid(*this) == typeid(value))
  32. {
  33. bResult = Equivalent((const ProvInteger &)value);
  34. }
  35. return bResult;
  36. }
  37. LONG ProvInteger::GetValue () const
  38. {
  39. return val;
  40. }
  41. void ProvInteger::SetValue ( IN const LONG value )
  42. {
  43. val = value;
  44. }
  45. ProvValue *ProvInteger::Copy () const
  46. {
  47. return new ProvInteger(val);
  48. }
  49. // Copy constructor
  50. ProvGauge::ProvGauge ( IN const ProvGauge &value )
  51. {
  52. val = value.GetValue();
  53. }
  54. BOOL ProvGauge :: Equivalent (IN const ProvValue &value) const
  55. {
  56. BOOL bResult = FALSE;
  57. if (typeid(*this) == typeid(value))
  58. {
  59. bResult = Equivalent((const ProvGauge &)value);
  60. }
  61. return bResult;
  62. }
  63. ULONG ProvGauge::GetValue () const
  64. {
  65. return val;
  66. }
  67. void ProvGauge::SetValue ( IN const ULONG value )
  68. {
  69. val = value;
  70. }
  71. ProvValue *ProvGauge::Copy () const
  72. {
  73. return new ProvGauge(val);
  74. }
  75. // Copy constructor
  76. ProvCounter::ProvCounter ( IN const ProvCounter &value )
  77. {
  78. val = value.GetValue();
  79. }
  80. BOOL ProvCounter :: Equivalent (IN const ProvValue &value) const
  81. {
  82. BOOL bResult = FALSE;
  83. if (typeid(*this) == typeid(value))
  84. {
  85. bResult = Equivalent((const ProvCounter &)value);
  86. }
  87. return bResult;
  88. }
  89. ULONG ProvCounter::GetValue () const
  90. {
  91. return val;
  92. }
  93. void ProvCounter::SetValue ( IN const ULONG value )
  94. {
  95. val = value;
  96. }
  97. ProvValue *ProvCounter::Copy () const
  98. {
  99. return new ProvCounter(val);
  100. }
  101. // Copy constructor
  102. ProvTimeTicks::ProvTimeTicks ( IN const ProvTimeTicks &value )
  103. {
  104. val = value.GetValue();
  105. }
  106. BOOL ProvTimeTicks :: Equivalent (IN const ProvValue &value) const
  107. {
  108. BOOL bResult = FALSE;
  109. if (typeid(*this) == typeid(value))
  110. {
  111. bResult = Equivalent((const ProvTimeTicks &)value);
  112. }
  113. return bResult;
  114. }
  115. ULONG ProvTimeTicks::GetValue () const
  116. {
  117. return val;
  118. }
  119. void ProvTimeTicks::SetValue ( IN const ULONG value )
  120. {
  121. val = value;
  122. }
  123. ProvValue *ProvTimeTicks::Copy () const
  124. {
  125. return new ProvTimeTicks(val);
  126. }
  127. void ProvOctetString::OverWrite(IN const UCHAR *value)
  128. {
  129. if ( value && length )
  130. {
  131. memcpy(val, value, sizeof(UCHAR)*length);
  132. }
  133. }
  134. void ProvOctetString::Initialize(IN const UCHAR *value, IN const ULONG valueLength)
  135. {
  136. is_valid = FALSE;
  137. if ( (value == NULL) && (valueLength != 0) )
  138. return;
  139. length = valueLength;
  140. val = Replicate(value, valueLength);
  141. is_valid = TRUE;
  142. }
  143. void ProvOctetString::UnReplicate(UCHAR *value)
  144. {
  145. if ( is_valid == TRUE )
  146. delete[] val;
  147. }
  148. ProvOctetString::ProvOctetString ( IN const UCHAR *value , IN const ULONG valueLength ) : is_valid ( FALSE )
  149. {
  150. Initialize(value, valueLength);
  151. }
  152. ProvOctetString::ProvOctetString ( IN const ProvOctetString &value ) : is_valid ( FALSE )
  153. {
  154. Initialize(value.GetValue(), value.GetValueLength());
  155. }
  156. ProvOctetString::~ProvOctetString ()
  157. {
  158. UnReplicate(val);
  159. }
  160. ULONG ProvOctetString::GetValueLength () const
  161. {
  162. return length;
  163. }
  164. UCHAR *ProvOctetString::GetValue () const
  165. {
  166. return val;
  167. }
  168. ProvValue *ProvOctetString::Copy () const
  169. {
  170. return new ProvOctetString(val, length);
  171. }
  172. UCHAR *ProvOctetString::Replicate(IN const UCHAR *value, IN const ULONG valueLength)
  173. {
  174. if ( value )
  175. {
  176. UCHAR *temp = new UCHAR[valueLength];
  177. memcpy(temp, value, sizeof(UCHAR)*valueLength);
  178. return temp;
  179. }
  180. else
  181. {
  182. return NULL ;
  183. }
  184. }
  185. void ProvOctetString::SetValue ( IN const UCHAR *value , IN const ULONG valueLength )
  186. {
  187. if (length != valueLength)
  188. {
  189. UnReplicate(val);
  190. Initialize(value, valueLength);
  191. }
  192. else
  193. OverWrite(value);
  194. }
  195. BOOL ProvOctetString :: Equivalent (IN const ProvValue &value) const
  196. {
  197. BOOL bResult = FALSE;
  198. if (typeid(*this) == typeid(value))
  199. {
  200. bResult = Equivalent((const ProvOctetString &)value);
  201. }
  202. return bResult;
  203. }
  204. BOOL ProvOctetString::Equivalent(IN const ProvOctetString &Prov_octet_string) const
  205. {
  206. if ( is_valid && Prov_octet_string() )
  207. {
  208. if ( length != Prov_octet_string.GetValueLength() )
  209. return FALSE;
  210. UCHAR *octet_values = Prov_octet_string.GetValue();
  211. for( UINT i=0; i < length; i++)
  212. {
  213. if ( val[i] != octet_values[i] )
  214. return FALSE;
  215. }
  216. return TRUE;
  217. }
  218. else
  219. return FALSE;
  220. }
  221. void ProvObjectIdentifier::OverWrite(IN const ULONG *value)
  222. {
  223. if ( value )
  224. {
  225. memcpy(val, value, sizeof(ULONG)*length);
  226. }
  227. }
  228. void ProvObjectIdentifier::Initialize(IN const ULONG *value, IN const ULONG valueLength)
  229. {
  230. if ( ( (value == NULL) && (valueLength != 0) ) || ( valueLength == 0 ) )
  231. {
  232. length = 0 ;
  233. val = NULL ;
  234. return;
  235. }
  236. length = valueLength;
  237. if ( length <= DEFAULT_OBJECTIDENTIFIER_LENGTH )
  238. {
  239. val = m_value ;
  240. memcpy(val , value, sizeof(ULONG)*length);
  241. is_valid = TRUE;
  242. }
  243. else
  244. {
  245. val = new ULONG[length];
  246. memcpy(val , value, sizeof(ULONG)*length);
  247. is_valid = TRUE;
  248. }
  249. }
  250. void ProvObjectIdentifier::UnReplicate(ULONG *value)
  251. {
  252. if ( ( is_valid == TRUE ) & ( length > DEFAULT_OBJECTIDENTIFIER_LENGTH ) )
  253. {
  254. delete[] val;
  255. }
  256. }
  257. ProvObjectIdentifier::ProvObjectIdentifier ( IN const ULONG *value , IN const ULONG valueLength ) : val ( NULL ) , length ( 0 ) , is_valid ( TRUE )
  258. {
  259. Initialize(value, valueLength);
  260. }
  261. ProvObjectIdentifier::ProvObjectIdentifier ( IN const ProvObjectIdentifier &value ) : val ( NULL ) , length ( 0 ) , is_valid ( TRUE )
  262. {
  263. Initialize(value.GetValue(), value.GetValueLength());
  264. }
  265. ProvObjectIdentifier::~ProvObjectIdentifier ()
  266. {
  267. UnReplicate(val);
  268. }
  269. ULONG ProvObjectIdentifier::GetValueLength () const
  270. {
  271. return length;
  272. }
  273. ULONG *ProvObjectIdentifier::GetValue () const
  274. {
  275. return val;
  276. }
  277. ProvValue *ProvObjectIdentifier::Copy () const
  278. {
  279. return new ProvObjectIdentifier(val, length);
  280. }
  281. ULONG *ProvObjectIdentifier::Replicate(IN const ULONG *value, IN const ULONG valueLength) const
  282. {
  283. if ( value )
  284. {
  285. ULONG *temp = new ULONG[valueLength];
  286. memcpy(temp, value, sizeof(ULONG)*valueLength);
  287. return temp;
  288. }
  289. else
  290. {
  291. return NULL ;
  292. }
  293. }
  294. ULONG *ProvObjectIdentifier::Replicate(IN const ULONG *first_value,
  295. IN const ULONG first_length,
  296. IN const ULONG *second_value,
  297. IN const ULONG second_length) const
  298. {
  299. if ( first_value && second_value )
  300. {
  301. ULONG new_length = first_length + second_length;
  302. ULONG *temp = new ULONG[new_length];
  303. int first_value_size = sizeof(ULONG)*first_length;
  304. memcpy(temp, first_value, first_value_size);
  305. memcpy(temp + first_length, second_value,
  306. sizeof(ULONG)*second_length);
  307. return temp;
  308. }
  309. else if ( first_value )
  310. {
  311. ULONG *temp = new ULONG [ first_length];
  312. memcpy(temp, first_value, sizeof(ULONG)*first_length);
  313. return temp;
  314. }
  315. else if ( second_value )
  316. {
  317. ULONG *temp = new ULONG [ second_length];
  318. memcpy(temp, second_value, sizeof(ULONG)*second_length);
  319. return temp;
  320. }
  321. else
  322. {
  323. return NULL ;
  324. }
  325. }
  326. ProvObjectIdentifier::Comparison ProvObjectIdentifier::Compare(IN const ProvObjectIdentifier &first,
  327. IN const ProvObjectIdentifier &second) const
  328. {
  329. ULONG *first_string = first.GetValue();
  330. ULONG *second_string = second.GetValue();
  331. int first_length = first.GetValueLength();
  332. int second_length = second.GetValueLength();
  333. int min_length = MIN(first_length,second_length);
  334. for(int i=0; i < min_length; i++)
  335. {
  336. if ( first_string[i] < second_string[i] )
  337. return LESS_THAN;
  338. else if ( first_string[i] > second_string[i] )
  339. return GREATER_THAN;
  340. else
  341. continue;
  342. }
  343. if ( first_length < second_length )
  344. return LESS_THAN;
  345. else if ( first_length > second_length )
  346. return GREATER_THAN;
  347. else
  348. return EQUAL_TO;
  349. }
  350. void ProvObjectIdentifier::SetValue ( IN const ULONG *value , IN const ULONG valueLength )
  351. {
  352. if (valueLength)
  353. {
  354. if ( length != valueLength)
  355. {
  356. UnReplicate(val);
  357. Initialize(value, valueLength);
  358. }
  359. else
  360. {
  361. OverWrite(value);
  362. }
  363. }
  364. else
  365. {
  366. UnReplicate(val);
  367. val = NULL ;
  368. length = 0 ;
  369. }
  370. }
  371. // A null terminated dot-separated string representing the
  372. // object identifer value is passed and the private fields
  373. // and length are set from it
  374. ProvObjectIdentifier::ProvObjectIdentifier(IN const char *value)
  375. {
  376. is_valid = FALSE;
  377. UINT str_len = strlen(value);
  378. if ( str_len <= 0 )
  379. return;
  380. ULONG temp_field[MAX_FIELDS];
  381. // create an input stream from the string
  382. istrstream input_stream((char *)value);
  383. // consecutive fields must be separated by a
  384. // FIELD_SEPARATOR
  385. char separator;
  386. input_stream >> temp_field[0];
  387. if ( input_stream.bad() || input_stream.fail() )
  388. return;
  389. // while the stream still has something,
  390. // read (FIELD_SEPARATOR, ULONG) pairs from the input stream
  391. // and set the temp_fields
  392. // check if the read was bad or failed after the event
  393. for( int i = 1 ; (i < MAX_FIELDS) && (!input_stream.eof()); i++)
  394. {
  395. input_stream >> separator;
  396. if ( input_stream.bad() || input_stream.fail() )
  397. return;
  398. if ( separator != FIELD_SEPARATOR )
  399. return;
  400. input_stream >> temp_field[i];
  401. if ( input_stream.bad() || input_stream.fail() )
  402. return;
  403. }
  404. is_valid = TRUE;
  405. // set the length
  406. length = i;
  407. val = NULL ;
  408. // create memory for the fields and copy temp_fields into it
  409. Initialize(temp_field, length);
  410. }
  411. BOOL ProvObjectIdentifier::Equivalent(IN const ProvObjectIdentifier &value,
  412. IN ULONG max_length) const
  413. {
  414. if ( (!is_valid) || (!value()) )
  415. return FALSE;
  416. if ( (length < max_length) || (value.GetValueLength() < max_length) )
  417. return FALSE;
  418. ULONG *value_string = value.GetValue();
  419. for( UINT i=0; i < max_length; i++ )
  420. if ( val[i] != value_string[i] )
  421. return FALSE;
  422. return TRUE;
  423. }
  424. BOOL ProvObjectIdentifier::Equivalent(IN const ProvObjectIdentifier &value) const
  425. {
  426. if ( (!is_valid) || (!value()) )
  427. return FALSE;
  428. ULONG *value_string = value.GetValue();
  429. for( UINT i=length; i ; i-- )
  430. {
  431. if ( val[i-1] != value_string[i-1] )
  432. return FALSE;
  433. }
  434. return TRUE;
  435. }
  436. BOOL ProvObjectIdentifier :: Equivalent (IN const ProvValue &value) const
  437. {
  438. BOOL bResult = FALSE;
  439. if (typeid(*this) == typeid(value))
  440. {
  441. bResult = Equivalent((const ProvObjectIdentifier &)value);
  442. }
  443. return bResult;
  444. }
  445. ProvObjectIdentifier ProvObjectIdentifier::operator+ ( IN const ProvObjectIdentifier &value ) const
  446. {
  447. ULONG *temp_plus_array = Replicate(val, length, value.GetValue(), value.GetValueLength());
  448. ProvObjectIdentifier local_identifier(temp_plus_array, length+value.GetValueLength());
  449. delete[] temp_plus_array;
  450. return ProvObjectIdentifier(local_identifier);
  451. }
  452. // Determines the fields (starting from left), common to the
  453. // two object identifiers and returns a new object identifier
  454. // with only these fields. If nothing is shared, NULL is returned
  455. ProvObjectIdentifier *ProvObjectIdentifier::Cut( ProvObjectIdentifier &value ) const
  456. {
  457. // determine the smaller of the two lengths
  458. int min_length = MIN(length, value.GetValueLength());
  459. ULONG *other_field = value.GetValue();
  460. // compare the fields
  461. for(int index=0; index < min_length; index++)
  462. if ( val[index] != other_field[index] )
  463. break;
  464. // if nothing in common - return NULL
  465. if ( index == 0 )
  466. return NULL;
  467. // they must have the fields in the range [0..(index-1)] common
  468. // therefore, a common length of "index"
  469. return new ProvObjectIdentifier(other_field, index);
  470. }
  471. ULONG &ProvObjectIdentifier::operator [] ( IN const ULONG index ) const
  472. {
  473. if ( index < length )
  474. return val[index];
  475. // should never reach here if the user checks the
  476. // index value before
  477. return val[0];
  478. }
  479. //returns an allocated char* representation of the OID.
  480. //The return value must be freed by the caller i.e. delete []
  481. char *ProvObjectIdentifier::GetAllocatedString() const
  482. {
  483. char * retVal = NULL ;
  484. if (length)
  485. {
  486. retVal = new char [ length * 18 ] ;
  487. ostrstream s ( retVal , length * 18 ) ;
  488. s << val[0];
  489. UINT i = 1;
  490. char dot = '.';
  491. while (i < length)
  492. {
  493. s << dot << val[i++] ;
  494. }
  495. s << ends ;
  496. }
  497. return retVal;
  498. }
  499. ProvIpAddress::ProvIpAddress ( IN const char *value )
  500. {
  501. // create a stream to read the fields from
  502. istrstream address_stream((char *)value);
  503. // store the values [0..255] separated by FIELD_SEPARATORs
  504. // in the value string
  505. UCHAR field[PROV_IP_ADDR_LEN];
  506. // contains the maximum value for a UCHAR. used
  507. // for comparison with the field values read
  508. const UCHAR max_uchar = -1;
  509. // consecutive fields must be separated by a
  510. // FIELD_SEPARATOR
  511. char separator;
  512. // a field is first read into this for comparison
  513. // with max_uchar
  514. ULONG temp_field;
  515. is_valid = FALSE;
  516. // read the first three (UCHAR,FIELD_SEPARATOR) pairs
  517. // check if the stream is good before each read
  518. for(int i=0; i < (PROV_IP_ADDR_LEN-1); i++)
  519. {
  520. if ( !address_stream.good() )
  521. return;
  522. address_stream >> temp_field;
  523. if ( temp_field > max_uchar )
  524. return;
  525. field[i] = (UCHAR)temp_field;
  526. if ( !address_stream.good() )
  527. return;
  528. address_stream >> separator;
  529. if ( separator != FIELD_SEPARATOR )
  530. return;
  531. }
  532. if ( !address_stream.good() )
  533. return;
  534. address_stream >> temp_field;
  535. if (temp_field > max_uchar)
  536. return;
  537. field[PROV_IP_ADDR_LEN-1] = (UCHAR)temp_field;
  538. // make sure that there are is nothing more left in the
  539. // stream
  540. if ( !address_stream.eof() )
  541. return;
  542. ULONG byteA = field [ 0 ] ;
  543. ULONG byteB = field [ 1 ] ;
  544. ULONG byteC = field [ 2 ] ;
  545. ULONG byteD = field [ 3 ] ;
  546. val = ( byteA << 24 ) + ( byteB << 16 ) + ( byteC << 8 ) + byteD ;
  547. is_valid = TRUE;
  548. }
  549. // Copy constructor
  550. ProvIpAddress::ProvIpAddress ( IN const ProvIpAddress &value )
  551. {
  552. if ( value() )
  553. {
  554. val = value.GetValue();
  555. is_valid = TRUE;
  556. }
  557. else
  558. is_valid = FALSE;
  559. }
  560. BOOL ProvIpAddress :: Equivalent (IN const ProvValue &value) const
  561. {
  562. BOOL bResult = FALSE;
  563. if (typeid(*this) == typeid(value))
  564. {
  565. bResult = Equivalent((const ProvIpAddress &)value);
  566. }
  567. return bResult;
  568. }
  569. ULONG ProvIpAddress::GetValue () const
  570. {
  571. return val;
  572. }
  573. void ProvIpAddress::SetValue ( IN const ULONG value )
  574. {
  575. val = value;
  576. is_valid = TRUE;
  577. }
  578. ProvValue *ProvIpAddress::Copy () const
  579. {
  580. return new ProvIpAddress(val);
  581. }
  582. // Copy constructor
  583. ProvUInteger32::ProvUInteger32 ( IN const ProvUInteger32 &value )
  584. {
  585. val = value.GetValue();
  586. }
  587. ULONG ProvUInteger32::GetValue () const
  588. {
  589. return val;
  590. }
  591. void ProvUInteger32::SetValue ( IN const ULONG value )
  592. {
  593. val = value;
  594. }
  595. ProvValue *ProvUInteger32::Copy () const
  596. {
  597. return new ProvUInteger32(val);
  598. }
  599. BOOL ProvUInteger32 :: Equivalent (IN const ProvValue &value) const
  600. {
  601. BOOL bResult = FALSE;
  602. if (typeid(*this) == typeid(value))
  603. {
  604. bResult = Equivalent((const ProvUInteger32 &)value);
  605. }
  606. return bResult;
  607. }
  608. // Copy constructor
  609. ProvCounter64::ProvCounter64( IN const ProvCounter64 &value )
  610. {
  611. lval = value.GetLowValue();
  612. hval = value.GetHighValue();
  613. }
  614. ULONG ProvCounter64::GetLowValue () const
  615. {
  616. return lval;
  617. }
  618. ULONG ProvCounter64::GetHighValue () const
  619. {
  620. return hval;
  621. }
  622. void ProvCounter64::SetValue ( IN const ULONG lvalue , IN const ULONG hvalue )
  623. {
  624. lval = lvalue;
  625. hval = hvalue ;
  626. }
  627. ProvValue *ProvCounter64::Copy () const
  628. {
  629. return new ProvCounter64(lval,hval);
  630. }
  631. BOOL ProvCounter64 :: Equivalent (IN const ProvValue &value) const
  632. {
  633. BOOL bResult = FALSE;
  634. if (typeid(*this) == typeid(value))
  635. {
  636. bResult = Equivalent((const ProvCounter64 &)value);
  637. }
  638. return bResult;
  639. }
  640. BOOL ProvOpaque :: Equivalent (IN const ProvValue &value) const
  641. {
  642. BOOL bResult = FALSE;
  643. if (typeid(*this) == typeid(value))
  644. {
  645. bResult = Equivalent((const ProvOpaque &)value);
  646. }
  647. return bResult;
  648. }