1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
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.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
29 /*===================================================================
32 Hewlett-Packard Company
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.
46 C O U N T E R 6 4. C P P
48 COUNTER64 CLASS IMPLEMENTATION
50 DESIGN + AUTHOR: Peter E. Mellquist
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 $";
56 #include "snmp_pp/ctr64.h"
57 #include "snmp_pp/asn1.h"
58 #include "snmp_pp/v3.h"
60 #include <stdio.h> // for pretty printing...
62 #ifdef SNMP_PP_NAMESPACE
66 #define MAX32 4294967295u
69 //------------------[ constructor with no value ]------------------------
70 Counter64::Counter64() : m_changed(true)
72 smival.syntax = sNMP_SYNTAX_CNTR64;
73 smival.value.hNumber.hipart = 0;
74 smival.value.hNumber.lopart = 0;
77 //------------------[ constructor with values ]--------------------------
78 Counter64::Counter64(unsigned long hi, unsigned long lo) : m_changed(true)
80 smival.syntax = sNMP_SYNTAX_CNTR64;
81 smival.value.hNumber.hipart = hi;
82 smival.value.hNumber.lopart = lo;
85 //------------------[ constructor with low value only ]------------------
86 Counter64::Counter64(unsigned long lo) : m_changed(true)
88 smival.syntax = sNMP_SYNTAX_CNTR64;
89 smival.value.hNumber.hipart = 0;
90 smival.value.hNumber.lopart = lo;
93 //------------------[ copy constructor ]---------------------------------
94 Counter64::Counter64(const Counter64 &ctr64 ) : m_changed(true)
96 smival.syntax = sNMP_SYNTAX_CNTR64;
97 smival.value.hNumber.hipart = ctr64.high();
98 smival.value.hNumber.lopart = ctr64.low();
101 //------------------[ operator=(const Counter64 &ctr64) ]-------------
102 // assign a ctr64 to a ctr64
103 Counter64& Counter64::operator=(const Counter64 &ctr64)
105 if (this == &ctr64) return *this; // check for self assignment
106 smival.value.hNumber.hipart = ctr64.high();
107 smival.value.hNumber.lopart = ctr64.low();
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)
117 smival.value.hNumber.hipart = 0;
118 smival.value.hNumber.lopart = i;
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)
127 pp_uint64 ll = c64.high();
128 ll *= (pp_uint64)MAX32 + (pp_uint64)1; // gotta be MAX32 + 1 to move it to next pos
133 //-----------[ c64_to_ll( ) ]------------------------------------------
134 pp_uint64 Counter64::c64_to_ll() const
136 pp_uint64 ll = high();
137 ll *= (pp_uint64)MAX32 + (pp_uint64)1; // gotta be MAX32 + 1 to move it to next pos
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)
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)));
151 //----------[ Counter64::operator+(const Counter64 &c) ]---------------
152 // add two Counter64s
153 Counter64 Counter64::operator+(const Counter64 &c) const
155 pp_uint64 llsum = c64_to_ll() + c.c64_to_ll();
156 return ll_to_c64(llsum);
159 //------------[ Counter64::operator-(const Counter64 &c) ]-------------
160 // subtract two Counter64s
161 Counter64 Counter64::operator-(const Counter64 &c) const
163 pp_uint64 lldiff = c64_to_ll() - c.c64_to_ll();
164 return ll_to_c64(lldiff);
167 //------------[ Counter64::operator*(const Counter64 &c) ]-------------
168 // multiply two Counter64s
169 Counter64 Counter64::operator*(const Counter64 &c) const
171 pp_uint64 llmult = c64_to_ll() * c.c64_to_ll();
172 return ll_to_c64(llmult);
175 //------------[ Counter64::operator/(const Counter64 &c) ]--------------
176 // divide two Counter64s
177 Counter64 Counter64::operator/(const Counter64 &c) const
179 pp_uint64 lldiv = c64_to_ll() / c.c64_to_ll();
180 return ll_to_c64(lldiv);
183 //-------[ overloaded equivlence test ]----------------------------------
184 bool operator==(const Counter64 &lhs, const Counter64 &rhs)
186 return ((lhs.high() == rhs.high()) && (lhs.low() == rhs.low()));
189 //-------[ overloaded not equal test ]-----------------------------------
190 bool operator!=(const Counter64 &lhs, const Counter64 &rhs)
192 return ((lhs.high() != rhs.high()) || (lhs.low() != rhs.low()));
195 //--------[ overloaded less than ]---------------------------------------
196 bool operator<(const Counter64 &lhs, const Counter64 &rhs)
198 return ( (lhs.high() < rhs.high()) ||
199 ((lhs.high() == rhs.high()) && (lhs.low() < rhs.low())));
202 //---------[ overloaded less than or equal ]-----------------------------
203 bool operator<=(const Counter64 &lhs, const Counter64 &rhs)
205 return ( (lhs.high() < rhs.high()) ||
206 ((lhs.high() == rhs.high()) && (lhs.low() <= rhs.low())));
209 //---------[ overloaded greater than ]-----------------------------------
210 bool operator>(const Counter64 &lhs, const Counter64 &rhs)
212 return ( (lhs.high() > rhs.high()) ||
213 ((lhs.high() == rhs.high()) && (lhs.low() > rhs.low())));
216 //----------[ overloaded greater than or equal ]-------------------------
217 bool operator>=(const Counter64 &lhs, const Counter64 &rhs)
219 return ( (lhs.high() > rhs.high()) ||
220 ((lhs.high() == rhs.high()) && (lhs.low() >= rhs.low())));
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
229 if (m_changed == false)
230 return output_buffer;
232 char *buf = PP_CONST_CAST(char*, output_buffer);
234 sprintf(buf, "0x%lX%08lX", high(), low());
236 sprintf(buf, "%lu", low());
238 Counter64 *nc_this = PP_CONST_CAST(Counter64*, this);
239 nc_this->m_changed = false;
241 return output_buffer;
245 //----------------[ general Value = operator ]---------------------
246 SnmpSyntax& Counter64::operator=(const SnmpSyntax &val)
248 if (this == &val) return *this; // protect against assignment from itself
250 smival.value.hNumber.lopart = 0; // pessimsitic - assume no mapping
251 smival.value.hNumber.hipart = 0;
253 // try to make assignment valid
256 switch (val.get_syntax())
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;
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;
280 // Return the space needed for serialization
281 int Counter64::get_asn1_length() const
283 if (smival.value.hNumber.hipart == 0)
285 if (smival.value.hNumber.lopart < 0x80)
287 else if (smival.value.hNumber.lopart < 0x8000)
289 else if (smival.value.hNumber.lopart < 0x800000)
291 else if (smival.value.hNumber.lopart < 0x80000000)
295 if (smival.value.hNumber.hipart < 0x80)
297 else if (smival.value.hNumber.hipart < 0x8000)
299 else if (smival.value.hNumber.hipart < 0x800000)
301 else if (smival.value.hNumber.hipart < 0x80000000)
306 #ifdef SNMP_PP_NAMESPACE
307 }; // end of namespace Snmp_pp