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.

719 lines
16 KiB

  1. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  2. /*-----------------------------------------------------------------
  3. Filename: value.hpp
  4. Purpose : To specify the classes of various Prov values and instance
  5. identifiers. These classes represent the different types of
  6. values for variables that may populate a MIB.
  7. Written By: B.Rajeev
  8. -----------------------------------------------------------------*/
  9. #ifndef __VALUE__
  10. #define __VALUE__
  11. /*-----------------------------------------------------------------
  12. General Overview:
  13. A variable instance refers to a MIB object, e.g.
  14. 1.3.6.1.2.1.1.1.0 or 1.3.6.1.2.1.2.1.2.1. The instance is
  15. encoded as an Prov object identifier and is represented by the
  16. class ProvObjectIdentifier.
  17. The classes derived from ProvValue represent the encoding of the
  18. information stored within the MIB object. The value is encoded
  19. as an implementation of the abstract class ProvValue. The Prov
  20. class library implements the following derivations of ProvValue
  21. which refer to Prov BER encoded types.
  22. ProvNull
  23. ProvInteger
  24. ProvCounter32
  25. ProvCounter64
  26. ProvGauge
  27. ProvTimeTicks
  28. ProvIPAddress
  29. ProvNetworkAddress
  30. ProvBitString
  31. ProvOctetString
  32. ProvOpaque
  33. ProvObjectIdentifier
  34. All the implemented classes provide (in addition to others) -
  35. 1. Constructors to initialize using relevant values or another
  36. instance of the same class.
  37. 2. GetValue, SetValue methods for obtaining and setting
  38. relevant values.
  39. 3. "=" operator to over-ride the default assignment operator and
  40. an Equivalent method to check for equivalence between two instances
  41. of the same (derived) class
  42. 4. Copy methods for obtaining a copy of a specified instance of
  43. the class.
  44. note of caution:
  45. ----------------
  46. Some of the GetValue functions return pointers
  47. to dynamically allocated data. Users of the class must make copies
  48. of the returned values and must not rely on the integrity of this
  49. pointer or values obtained through it in future (because of
  50. SetValue methods, or destruction of corresponding ProvValue class)
  51. -----------------------------------------------------------------*/
  52. #include <provimex.h>
  53. #include <provexpt.h>
  54. // Abstract class at the root of all Prov Values
  55. // maximum length of decimal dot notation addresses
  56. #define MAX_ADDRESS_LEN 100
  57. // end of string character
  58. #define EOS '\0'
  59. #define MIN(a,b) ((a<=b)?a:b)
  60. #define BETWEEN(i, min, max) ( ((i>=min)&&(i<max))?TRUE:FALSE )
  61. #define MAX_FIELDS 100
  62. #define FIELD_SEPARATOR '.'
  63. #define PROV_IP_ADDR_LEN 4
  64. class DllImportExport ProvValue
  65. {
  66. // the "=" operator and the copy constructor have been
  67. // made private to prevent copies of the ProvValue instance
  68. // from being made
  69. ProvValue &operator=(IN const ProvValue &)
  70. {
  71. return *this;
  72. }
  73. ProvValue(IN const ProvValue &Prov_value) {}
  74. protected:
  75. virtual BOOL Equivalent(IN const ProvValue &value) const = 0;
  76. ProvValue() {}
  77. public:
  78. virtual ProvValue *Copy () const = 0 ;
  79. BOOL operator==(IN const ProvValue &value) const
  80. {
  81. return Equivalent(value) ;
  82. }
  83. BOOL operator!=(IN const ProvValue &value) const
  84. {
  85. return !((*this) == value) ;
  86. }
  87. virtual ~ProvValue() {}
  88. } ;
  89. // Enables null values for required variables. Its a concrete class
  90. // with dummy constructor and destructors to enable specification of
  91. // null values
  92. class DllImportExport ProvNull : public ProvValue
  93. {
  94. protected:
  95. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  96. public:
  97. // dummy constructor and destructor
  98. ProvNull() {}
  99. ~ProvNull() {}
  100. ProvValue &operator=(IN const ProvNull &to_copy)
  101. {
  102. return *this;
  103. }
  104. ProvValue *Copy() const { return new ProvNull; }
  105. };
  106. // Allows integer values to be specified
  107. class DllImportExport ProvInteger : public ProvValue
  108. {
  109. private:
  110. LONG val;
  111. protected:
  112. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  113. public:
  114. ProvInteger ( IN const LONG value ) : val(value) {}
  115. ProvInteger ( IN const ProvInteger &value );
  116. ~ProvInteger () {}
  117. LONG GetValue () const;
  118. void SetValue ( IN const LONG value );
  119. ProvValue &operator=(IN const ProvInteger &to_copy)
  120. {
  121. SetValue(to_copy.GetValue());
  122. return *this;
  123. }
  124. BOOL Equivalent(IN const ProvInteger &Prov_integer) const
  125. {
  126. if ( val == Prov_integer.GetValue() )
  127. return TRUE;
  128. else
  129. return FALSE;
  130. }
  131. ProvValue *Copy () const;
  132. } ;
  133. // Encapsulates gauge value
  134. class DllImportExport ProvGauge : public ProvValue
  135. {
  136. private:
  137. ULONG val;
  138. protected:
  139. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  140. public:
  141. ProvGauge ( IN const LONG value ) : val(value) {}
  142. ProvGauge ( IN const ProvGauge &value );
  143. ~ProvGauge () {}
  144. ULONG GetValue () const;
  145. void SetValue ( IN const ULONG value );
  146. ProvValue *Copy () const;
  147. ProvValue &operator=(IN const ProvGauge &to_copy)
  148. {
  149. SetValue(to_copy.GetValue());
  150. return *this;
  151. }
  152. BOOL Equivalent(IN const ProvGauge &Prov_gauge) const
  153. {
  154. if ( val == Prov_gauge.GetValue() )
  155. return TRUE;
  156. else
  157. return FALSE;
  158. }
  159. } ;
  160. // Encapsulates Counter values
  161. class DllImportExport ProvCounter : public ProvValue
  162. {
  163. private:
  164. ULONG val;
  165. protected:
  166. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  167. public:
  168. ProvCounter ( IN const ULONG value ) : val(value) {}
  169. ProvCounter ( IN const ProvCounter &value );
  170. ~ProvCounter () {}
  171. ULONG GetValue () const;
  172. void SetValue ( IN const ULONG value );
  173. ProvValue *Copy () const;
  174. ProvValue &operator=(IN const ProvCounter &to_copy)
  175. {
  176. SetValue(to_copy.GetValue());
  177. return *this;
  178. }
  179. BOOL Equivalent(IN const ProvCounter &Prov_counter) const
  180. {
  181. if ( val == Prov_counter.GetValue() )
  182. return TRUE;
  183. else
  184. return FALSE;
  185. }
  186. } ;
  187. // Encapsulates Time Ticks (since an earlier event)
  188. class DllImportExport ProvTimeTicks : public ProvValue
  189. {
  190. private:
  191. ULONG val;
  192. protected:
  193. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  194. public:
  195. ProvTimeTicks ( IN const ULONG value ) : val(value) {}
  196. ProvTimeTicks ( IN const ProvTimeTicks &value );
  197. ~ProvTimeTicks () {}
  198. ULONG GetValue () const;
  199. void SetValue ( IN const ULONG value );
  200. ProvValue *Copy () const;
  201. ProvValue &operator=(IN const ProvTimeTicks &to_copy)
  202. {
  203. SetValue(to_copy.GetValue());
  204. return *this;
  205. }
  206. BOOL Equivalent(IN const ProvTimeTicks &Prov_time_ticks) const
  207. {
  208. if ( val == Prov_time_ticks.GetValue() )
  209. return TRUE;
  210. else
  211. return FALSE;
  212. }
  213. } ;
  214. // Encapsulates octet strings that do not have any terminator.
  215. // The octet string is specified by the pair (val,length) where
  216. // 'val' is a pointer to heap data and 'length' provides the number
  217. // of octets in the data string.
  218. class DllImportExport ProvOctetString : public ProvValue
  219. {
  220. private:
  221. // in case a new 'value' string has the same length as the stored
  222. // string, the stored string may be overwritten. this avoids
  223. // having to allocate and deallocate heap memory for the purpose.
  224. void OverWrite(IN const UCHAR *value);
  225. protected:
  226. BOOL is_valid;
  227. UCHAR *val;
  228. ULONG length;
  229. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  230. virtual void Initialize(IN const UCHAR *value, IN const ULONG valueLength);
  231. // The Replicate and UnReplicate methods allocate and deallocate
  232. // heap data. Replicate also copies the contents of the parameter
  233. // 'value' onto the allocated memory. This function may be
  234. // implemented different and, thus, the methods have been declared
  235. // virtual.
  236. virtual UCHAR *Replicate(IN const UCHAR *value, IN const ULONG valueLength);
  237. virtual void UnReplicate(UCHAR *value);
  238. public:
  239. ProvOctetString ( IN const UCHAR *value , IN const ULONG valueLength );
  240. ProvOctetString ( IN const ProvOctetString &value );
  241. ~ProvOctetString ();
  242. void SetValue ( IN const UCHAR *value , IN const ULONG valueLength );
  243. ULONG GetValueLength () const;
  244. UCHAR *GetValue () const;
  245. ProvValue *Copy () const;
  246. ProvValue &operator=(IN const ProvOctetString &to_copy)
  247. {
  248. if ( to_copy() )
  249. SetValue(to_copy.GetValue(), to_copy.GetValueLength());
  250. return *this;
  251. }
  252. void * operator()(void) const
  253. {
  254. return ( is_valid?(void *)this:NULL );
  255. }
  256. BOOL Equivalent(IN const ProvOctetString &Prov_octet_string) const;
  257. } ;
  258. // OpaqueValue class encapsulates octet strings
  259. class DllImportExport ProvOpaque : public ProvValue
  260. {
  261. private:
  262. ProvOctetString *octet_string;
  263. protected:
  264. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  265. public:
  266. ProvOpaque ( IN const UCHAR *value , IN const ULONG valueLength ) : octet_string ( NULL )
  267. {
  268. octet_string = new ProvOctetString(value, valueLength);
  269. }
  270. ProvOpaque ( IN const ProvOpaque &value ) : octet_string ( NULL )
  271. {
  272. octet_string = new ProvOctetString(value.GetValue(), value.GetValueLength());
  273. }
  274. ~ProvOpaque()
  275. {
  276. delete octet_string;
  277. }
  278. void SetValue ( IN const UCHAR *value , IN const ULONG valueLength )
  279. {
  280. octet_string->SetValue(value, valueLength);
  281. }
  282. ULONG GetValueLength () const
  283. {
  284. return octet_string->GetValueLength();
  285. }
  286. UCHAR *GetValue () const
  287. {
  288. return octet_string->GetValue();
  289. }
  290. ProvValue &operator=(IN const ProvOpaque &to_copy)
  291. {
  292. if ( to_copy() )
  293. SetValue(to_copy.GetValue(), to_copy.GetValueLength());
  294. return *this;
  295. }
  296. ProvValue *Copy () const
  297. {
  298. return new ProvOpaque(octet_string->GetValue(),
  299. octet_string->GetValueLength());
  300. }
  301. void * operator()(void) const
  302. {
  303. return (*octet_string)();
  304. }
  305. BOOL Equivalent(IN const ProvOpaque &Prov_opaque) const
  306. {
  307. return octet_string->Equivalent(*(Prov_opaque.octet_string));
  308. }
  309. };
  310. #define DEFAULT_OBJECTIDENTIFIER_LENGTH 32
  311. // Encapsulates the object identifier. An object identifier
  312. // identifies a MIB object instance
  313. class DllImportExport ProvObjectIdentifier : public ProvValue
  314. {
  315. // describes the legal values for a comparison
  316. enum Comparison {LESS_THAN, EQUAL_TO, GREATER_THAN};
  317. private:
  318. BOOL is_valid;
  319. ULONG m_value[DEFAULT_OBJECTIDENTIFIER_LENGTH];
  320. ULONG *val;
  321. ULONG length;
  322. // in case a new 'value' string has the same length as the stored
  323. // string, the stored string may be overwritten. this avoids
  324. // having to allocate and deallocate heap memory for the purpose.
  325. void OverWrite(IN const ULONG *value);
  326. protected:
  327. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  328. virtual void Initialize(IN const ULONG *value, IN const ULONG valueLength);
  329. // The Replicate and UnReplicate methods allocate and deallocate
  330. // heap data. Replicate also copies the contents of the parameter
  331. // 'value' onto the allocated memory. This function may be
  332. // implemented different and, thus, the methods have been declared
  333. // virtual.
  334. virtual ULONG *Replicate(IN const ULONG *value, IN const ULONG valueLength) const;
  335. // Allocates enough memory to copy the first value followed by
  336. // the second value to be copied, thus, appending the two values
  337. virtual ULONG *Replicate(IN const ULONG *first_value, IN const ULONG first_length,
  338. IN const ULONG *second_value, IN const ULONG second_length) const;
  339. virtual void UnReplicate(ULONG *value);
  340. // This single function
  341. Comparison Compare(IN const ProvObjectIdentifier &first,
  342. IN const ProvObjectIdentifier &second) const;
  343. BOOL Equivalent(IN const ProvObjectIdentifier &value) const;
  344. public:
  345. ProvObjectIdentifier ( IN const ULONG *value , IN const ULONG valueLength );
  346. ProvObjectIdentifier ( IN const char *value );
  347. ProvObjectIdentifier ( IN const ProvObjectIdentifier &value );
  348. ~ProvObjectIdentifier ();
  349. void SetValue ( IN const ULONG *value , IN const ULONG valueLength );
  350. ULONG GetValueLength () const;
  351. ULONG *GetValue () const;
  352. ProvValue *Copy () const;
  353. BOOL Equivalent(IN const ProvObjectIdentifier &value,
  354. IN ULONG max_length) const;
  355. BOOL operator<(IN const ProvObjectIdentifier &value) const
  356. {
  357. return (Compare(*this,value) == LESS_THAN)?TRUE:FALSE;
  358. }
  359. BOOL operator>(IN const ProvObjectIdentifier &value) const
  360. {
  361. return (Compare(*this,value) == GREATER_THAN)?TRUE:FALSE;
  362. }
  363. BOOL operator<=(IN const ProvObjectIdentifier &value) const
  364. {
  365. return !(*this > value);
  366. }
  367. BOOL operator>=(IN const ProvObjectIdentifier &value) const
  368. {
  369. return !(*this < value);
  370. }
  371. BOOL operator==(IN const ProvObjectIdentifier &value) const
  372. {
  373. if ( this->GetValueLength() == value.GetValueLength() )
  374. return Equivalent(value) ;
  375. else
  376. return FALSE;
  377. }
  378. BOOL operator!=(IN const ProvObjectIdentifier &value) const
  379. {
  380. return !(*this == value);
  381. }
  382. ProvObjectIdentifier operator+ ( IN const ProvObjectIdentifier &value ) const;
  383. BOOL Prefix( IN ULONG index, ProvObjectIdentifier &prefix ) const
  384. {
  385. if ( index >= length )
  386. return FALSE;
  387. prefix.UnReplicate (val) ;
  388. prefix.Initialize (val, index+1) ;
  389. return TRUE ;
  390. }
  391. BOOL Suffix ( IN ULONG index , ProvObjectIdentifier &suffix ) const
  392. {
  393. if ( index >= length )
  394. return FALSE;
  395. suffix.UnReplicate (val) ;
  396. suffix.Initialize ( val+index, length-index ) ;
  397. return TRUE ;
  398. }
  399. ProvObjectIdentifier *Cut (ProvObjectIdentifier &value) const;
  400. ULONG &operator [] ( IN const ULONG index ) const;
  401. ProvValue &operator=(IN const ProvObjectIdentifier &to_copy)
  402. {
  403. if ( to_copy() )
  404. SetValue(to_copy.GetValue(), to_copy.GetValueLength());
  405. return *this;
  406. }
  407. void * operator()(void) const
  408. {
  409. return ( is_valid?(void *)this:NULL );
  410. }
  411. char *GetAllocatedString() const;
  412. } ;
  413. // encapsulates an ip address. represents the 32 bit value in a ULONG
  414. class DllImportExport ProvIpAddress : public ProvValue
  415. {
  416. private:
  417. // if the dotted decimal representation passed to the constructor
  418. // is ill-formed, the instance may be invalid
  419. BOOL is_valid;
  420. ULONG val;
  421. protected:
  422. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  423. public:
  424. ProvIpAddress ( IN const ULONG value )
  425. :val(value), is_valid(TRUE)
  426. {}
  427. // a dotted decimal representation is parsed to obtain the 32 bit value
  428. ProvIpAddress ( IN const char *value ) ;
  429. ProvIpAddress ( IN const ProvIpAddress &value );
  430. ~ProvIpAddress () {}
  431. ULONG GetValue () const;
  432. void SetValue ( IN const ULONG value );
  433. ProvValue *Copy () const;
  434. ProvValue &operator=(IN const ProvIpAddress &to_copy)
  435. {
  436. if ( to_copy() )
  437. SetValue(to_copy.GetValue());
  438. return *this;
  439. }
  440. void * operator()(void) const
  441. {
  442. return ( is_valid?(void *)this:NULL );
  443. }
  444. BOOL Equivalent(IN const ProvIpAddress &Prov_ip_address) const
  445. {
  446. if ( is_valid && Prov_ip_address() )
  447. return ( val == Prov_ip_address.GetValue() );
  448. else
  449. return FALSE;
  450. }
  451. } ;
  452. // Encapsulates UInteger32 value
  453. class DllImportExport ProvUInteger32 : public ProvValue
  454. {
  455. private:
  456. ULONG val;
  457. protected:
  458. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  459. public:
  460. ProvUInteger32 ( IN const LONG value ) : val(value) {}
  461. ProvUInteger32 ( IN const ProvUInteger32 &value );
  462. ~ProvUInteger32 () {}
  463. ULONG GetValue () const;
  464. void SetValue ( IN const ULONG value );
  465. ProvValue *Copy () const;
  466. ProvValue &operator=(IN const ProvUInteger32 &to_copy)
  467. {
  468. SetValue(to_copy.GetValue());
  469. return *this;
  470. }
  471. BOOL Equivalent(IN const ProvUInteger32 &Prov_integer) const
  472. {
  473. if ( val == Prov_integer.GetValue() )
  474. return TRUE;
  475. else
  476. return FALSE;
  477. }
  478. } ;
  479. // Encapsulates Counter64 values
  480. class DllImportExport ProvCounter64 : public ProvValue
  481. {
  482. private:
  483. ULONG lval;
  484. ULONG hval;
  485. protected:
  486. virtual BOOL Equivalent(IN const ProvValue &value) const ;
  487. public:
  488. ProvCounter64 ( IN const ULONG lvalue , IN const ULONG hvalue ) : lval(lvalue),hval(hvalue) {}
  489. ProvCounter64 ( IN const ProvCounter64 &value );
  490. ~ProvCounter64 () {}
  491. ULONG GetLowValue () const;
  492. ULONG GetHighValue () const;
  493. void SetValue ( IN const ULONG lvalue , IN const ULONG hvalue );
  494. ProvValue *Copy () const;
  495. ProvValue &operator=(IN const ProvCounter64 &to_copy)
  496. {
  497. SetValue(to_copy.GetLowValue(),to_copy.GetHighValue());
  498. return *this;
  499. }
  500. BOOL Equivalent(IN const ProvCounter64 &Prov_counter ) const
  501. {
  502. if ( ( lval == Prov_counter.GetLowValue() ) && ( hval == Prov_counter.GetHighValue() ) )
  503. return TRUE;
  504. else
  505. return FALSE;
  506. }
  507. } ;
  508. #endif // __VALUE__