]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/ctr64.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / ctr64.cpp
1 /*_############################################################################
2   _## 
3   _##  ctr64.cpp  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
14   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15   _##  Permission to use, copy, modify, distribute and/or sell this software 
16   _##  and/or its documentation is hereby granted without fee. User agrees 
17   _##  to display the above copyright notice and this license notice in all 
18   _##  copies of the software and any documentation of the software. User 
19   _##  agrees to assume all liability for the use of the software; 
20   _##  Hewlett-Packard and Jochen Katz make no representations about the 
21   _##  suitability of this software for any purpose. It is provided 
22   _##  "AS-IS" without warranty of any kind, either express or implied. User 
23   _##  hereby grants a royalty-free license to any and all derivatives based
24   _##  upon this software code base. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*===================================================================
30
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
34   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35   Permission to use, copy, modify, distribute and/or sell this software
36   and/or its documentation is hereby granted without fee. User agrees
37   to display the above copyright notice and this license notice in all
38   copies of the software and any documentation of the software. User
39   agrees to assume all liability for the use of the software; Hewlett-Packard
40   makes no representations about the suitability of this software for any
41   purpose. It is provided "AS-IS" without warranty of any kind,either express
42   or implied. User hereby grants a royalty-free license to any and all
43   derivatives based upon this software code base.
44
45
46   C O U N T E R 6 4. C P P
47
48   COUNTER64 CLASS IMPLEMENTATION
49
50   DESIGN + AUTHOR:     Peter E. Mellquist
51
52   DESCRIPTION:         Implementation for Counter64 (64 bit counter class).
53 =====================================================================*/
54 char counter64_cpp_version[]="@(#) SNMP++ $Id: ctr64.cpp 1558 2009-07-03 20:16:53Z katz $";
55
56 #include "snmp_pp/ctr64.h"
57 #include "snmp_pp/asn1.h"
58 #include "snmp_pp/v3.h"
59
60 #include <stdio.h>   // for pretty printing...
61
62 #ifdef SNMP_PP_NAMESPACE
63 namespace Snmp_pp {
64 #endif
65
66 #define MAX32 4294967295u
67
68
69 //------------------[ constructor with no value ]------------------------
70 Counter64::Counter64() : m_changed(true)
71 {
72   smival.syntax = sNMP_SYNTAX_CNTR64;
73   smival.value.hNumber.hipart = 0;
74   smival.value.hNumber.lopart = 0;
75 }
76
77 //------------------[ constructor with values ]--------------------------
78 Counter64::Counter64(unsigned long hi, unsigned long lo) : m_changed(true)
79 {
80   smival.syntax = sNMP_SYNTAX_CNTR64;
81   smival.value.hNumber.hipart = hi;
82   smival.value.hNumber.lopart = lo;
83 }
84
85 //------------------[ constructor with low value only ]------------------
86 Counter64::Counter64(unsigned long lo) : m_changed(true)
87 {
88   smival.syntax = sNMP_SYNTAX_CNTR64;
89   smival.value.hNumber.hipart = 0;
90   smival.value.hNumber.lopart = lo;
91 }
92
93 //------------------[ copy constructor ]---------------------------------
94 Counter64::Counter64(const Counter64 &ctr64 ) : m_changed(true)
95 {
96   smival.syntax = sNMP_SYNTAX_CNTR64;
97   smival.value.hNumber.hipart = ctr64.high();
98   smival.value.hNumber.lopart = ctr64.low();
99 }
100
101 //------------------[ operator=(const Counter64 &ctr64) ]-------------
102 // assign a ctr64 to a ctr64
103 Counter64& Counter64::operator=(const Counter64 &ctr64)
104 {
105   if (this == &ctr64) return *this;  // check for self assignment
106   smival.value.hNumber.hipart = ctr64.high();
107   smival.value.hNumber.lopart = ctr64.low();
108   m_changed = true;
109   return *this;
110 }
111
112 //-------------------[ operator=(const unsigned long int i) ]---------
113 // assign a ul to a ctr64, clears the high part
114 // and assugns the low part
115 Counter64& Counter64::operator=(const unsigned long i)
116 {
117   smival.value.hNumber.hipart = 0;
118   smival.value.hNumber.lopart = i;
119   m_changed = true;
120   return *this;
121 }
122
123 //-----------[ c64_to_ll(Counter64 c64) ]-----------------------------
124 // convert a Counter 64 to a 64 bit integer
125 pp_uint64 Counter64::c64_to_ll(const Counter64 &c64)
126 {
127   pp_uint64 ll = c64.high();
128   ll *= (pp_uint64)MAX32 + (pp_uint64)1; // gotta be MAX32 + 1 to move it to next pos
129   ll += c64.low();
130   return ll;
131 }
132
133 //-----------[ c64_to_ll( ) ]------------------------------------------
134 pp_uint64 Counter64::c64_to_ll() const
135 {
136   pp_uint64 ll = high();
137   ll *= (pp_uint64)MAX32 + (pp_uint64)1; // gotta be MAX32 + 1 to move it to next pos
138   ll += low();
139   return ll;
140 }
141
142 //-----------[ ll_to_c64(pp_uint64 ll) ]----------------------------
143 // convert a 64 bit integer to a Counter64
144 Counter64 Counter64::ll_to_c64(const pp_uint64 &ll)
145 {
146   pp_uint64 high = (pp_uint64)MAX32 + (pp_uint64)1; // look above
147   unsigned long h = (unsigned long)(ll / high);
148   return Counter64(h, (unsigned long)(ll - (h * high)));
149 }
150
151 //----------[ Counter64::operator+(const Counter64 &c) ]---------------
152 // add two Counter64s
153 Counter64 Counter64::operator+(const Counter64 &c) const
154 {
155   pp_uint64 llsum = c64_to_ll() + c.c64_to_ll();
156   return ll_to_c64(llsum);
157 }
158
159 //------------[ Counter64::operator-(const Counter64 &c) ]-------------
160 // subtract two Counter64s
161 Counter64 Counter64::operator-(const Counter64 &c) const
162 {
163   pp_uint64 lldiff = c64_to_ll() - c.c64_to_ll();
164   return ll_to_c64(lldiff);
165 }
166
167 //------------[ Counter64::operator*(const Counter64 &c) ]-------------
168 // multiply two Counter64s
169 Counter64 Counter64::operator*(const Counter64 &c) const
170 {
171   pp_uint64 llmult = c64_to_ll() * c.c64_to_ll();
172   return ll_to_c64(llmult);
173 }
174
175 //------------[ Counter64::operator/(const Counter64 &c) ]--------------
176 // divide two Counter64s
177 Counter64 Counter64::operator/(const Counter64 &c) const
178 {
179   pp_uint64 lldiv = c64_to_ll() / c.c64_to_ll();
180   return ll_to_c64(lldiv);
181 }
182
183 //-------[ overloaded equivlence test ]----------------------------------
184 bool operator==(const Counter64 &lhs, const Counter64 &rhs)
185 {
186   return ((lhs.high() == rhs.high()) && (lhs.low() == rhs.low()));
187 }
188
189 //-------[ overloaded not equal test ]-----------------------------------
190 bool operator!=(const Counter64 &lhs, const Counter64 &rhs)
191 {
192   return ((lhs.high() != rhs.high()) || (lhs.low() != rhs.low()));
193 }
194
195 //--------[ overloaded less than ]---------------------------------------
196 bool operator<(const Counter64 &lhs, const Counter64 &rhs)
197 {
198   return ( (lhs.high() < rhs.high()) ||
199            ((lhs.high() == rhs.high()) && (lhs.low() < rhs.low())));
200 }
201
202 //---------[ overloaded less than or equal ]-----------------------------
203 bool operator<=(const Counter64 &lhs, const Counter64 &rhs)
204 {
205   return ( (lhs.high() < rhs.high()) ||
206            ((lhs.high() == rhs.high()) && (lhs.low() <= rhs.low())));
207 }
208
209 //---------[ overloaded greater than ]-----------------------------------
210 bool operator>(const Counter64 &lhs, const Counter64 &rhs)
211 {
212   return ( (lhs.high() > rhs.high()) ||
213            ((lhs.high() == rhs.high()) && (lhs.low() > rhs.low())));
214 }
215
216 //----------[ overloaded greater than or equal ]-------------------------
217 bool operator>=(const Counter64 &lhs, const Counter64 &rhs)
218 {
219   return ( (lhs.high() > rhs.high()) ||
220            ((lhs.high() == rhs.high()) && (lhs.low() >= rhs.low())));
221 }
222
223 //----------[ return ASCII format ]-------------------------
224 // TODO:  Fix up to do real 64bit decimal value printing...
225 //        For now, print > 32-bit values in hex
226 // 26Nov2002 M.Evstiounin - this method is not thread safe!
227 const char *Counter64::get_printable() const
228 {
229   if (m_changed == false)
230     return output_buffer;
231
232   char *buf = PP_CONST_CAST(char*, output_buffer);
233   if ( high() != 0 )
234     sprintf(buf, "0x%lX%08lX", high(), low());
235   else
236     sprintf(buf, "%lu", low());
237
238   Counter64 *nc_this = PP_CONST_CAST(Counter64*, this);
239   nc_this->m_changed = false;
240
241   return output_buffer;
242 }
243
244
245 //----------------[ general Value = operator ]---------------------
246 SnmpSyntax& Counter64::operator=(const SnmpSyntax &val)
247 {
248   if (this == &val) return *this;  // protect against assignment from itself
249
250   smival.value.hNumber.lopart = 0;      // pessimsitic - assume no mapping
251   smival.value.hNumber.hipart = 0;
252
253   // try to make assignment valid
254   if (val.valid())
255   {
256     switch (val.get_syntax())
257     {
258       case sNMP_SYNTAX_CNTR64:
259         smival.value.hNumber.hipart =
260                 ((Counter64 &)val).smival.value.hNumber.hipart;
261         smival.value.hNumber.lopart =
262                 ((Counter64 &)val).smival.value.hNumber.lopart;
263         break;
264
265       case sNMP_SYNTAX_CNTR32:
266       case sNMP_SYNTAX_TIMETICKS:
267       case sNMP_SYNTAX_GAUGE32:
268    // case sNMP_SYNTAX_UINT32:          .. indistinguishable from GAUGE32
269       case sNMP_SYNTAX_INT32:
270         // take advantage of union...
271         smival.value.hNumber.lopart = ((Counter64 &)val).smival.value.uNumber;
272         smival.value.hNumber.hipart = 0;
273         break;
274     }
275   }
276   m_changed = true;
277   return *this;
278 }
279
280 // Return the space needed for serialization
281 int Counter64::get_asn1_length() const
282 {
283   if (smival.value.hNumber.hipart == 0)
284   {
285     if (smival.value.hNumber.lopart < 0x80)
286       return 3;
287     else if (smival.value.hNumber.lopart < 0x8000)
288       return 4;
289     else if (smival.value.hNumber.lopart < 0x800000)
290       return 5;
291     else if (smival.value.hNumber.lopart < 0x80000000)
292       return 6;
293     return 7;
294   }
295   if (smival.value.hNumber.hipart < 0x80)
296     return 7;
297   else if (smival.value.hNumber.hipart < 0x8000)
298     return 8;
299   else if (smival.value.hNumber.hipart < 0x800000)
300     return 9;
301   else if (smival.value.hNumber.hipart < 0x80000000)
302     return 10;
303   return 11;
304 }
305
306 #ifdef SNMP_PP_NAMESPACE
307 }; // end of namespace Snmp_pp
308 #endif