| |
| |
| |
| |
| |
| |
| |
| |
| #include <net-snmp/net-snmp-config.h> |
| #include <sys/types.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <ctype.h> |
| #if HAVE_STRING_H |
| #include <string.h> |
| #else |
| #include <strings.h> |
| #endif |
| |
| #include <net-snmp/types.h> |
| #include <net-snmp/library/int64.h> |
| #include <net-snmp/library/snmp_assert.h> |
| #include <net-snmp/library/snmp_debug.h> |
| #include <net-snmp/library/snmp_logging.h> |
| |
| #include <net-snmp/net-snmp-features.h> |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| divBy10(struct counter64 u64, struct counter64 *pu64Q, unsigned int *puR) |
| { |
| unsigned long ulT; |
| unsigned long ulQ; |
| unsigned long ulR; |
| |
| |
| |
| |
| ulT = (u64.high >> 16) & 0x0ffff; |
| ulQ = ulT / 10; |
| ulR = ulT % 10; |
| pu64Q->high = ulQ << 16; |
| |
| |
| |
| |
| ulT = (u64.high & 0x0ffff); |
| ulT += (ulR << 16); |
| ulQ = ulT / 10; |
| ulR = ulT % 10; |
| pu64Q->high = pu64Q->high | ulQ; |
| |
| |
| |
| |
| ulT = ((u64.low >> 16) & 0x0ffff) + (ulR << 16); |
| ulQ = ulT / 10; |
| ulR = ulT % 10; |
| pu64Q->low = ulQ << 16; |
| |
| |
| |
| |
| ulT = (u64.low & 0x0ffff); |
| ulT += (ulR << 16); |
| ulQ = ulT / 10; |
| ulR = ulT % 10; |
| pu64Q->low = pu64Q->low | ulQ; |
| |
| *puR = (unsigned int) (ulR); |
| } |
| |
| |
| |
| |
| |
| |
| |
| void |
| multBy10(struct counter64 u64, struct counter64 *pu64P) |
| { |
| unsigned long ulT; |
| unsigned long ulP; |
| unsigned long ulK; |
| |
| |
| |
| |
| ulT = u64.low & 0x0ffff; |
| ulP = ulT * 10; |
| ulK = ulP >> 16; |
| pu64P->low = ulP & 0x0ffff; |
| |
| |
| |
| |
| ulT = (u64.low >> 16) & 0x0ffff; |
| ulP = (ulT * 10) + ulK; |
| ulK = ulP >> 16; |
| pu64P->low = (ulP & 0x0ffff) << 16 | pu64P->low; |
| |
| |
| |
| |
| ulT = u64.high & 0x0ffff; |
| ulP = (ulT * 10) + ulK; |
| ulK = ulP >> 16; |
| pu64P->high = ulP & 0x0ffff; |
| |
| |
| |
| |
| ulT = (u64.high >> 16) & 0x0ffff; |
| ulP = (ulT * 10) + ulK; |
| ulK = ulP >> 16; |
| pu64P->high = (ulP & 0x0ffff) << 16 | pu64P->high; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| incrByU16(struct counter64 *pu64, unsigned int u16) |
| { |
| incrByU32(pu64, u16); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| incrByU32(struct counter64 *pu64, unsigned int u32) |
| { |
| uint32_t tmp; |
| |
| tmp = pu64->low; |
| pu64->low = (uint32_t)(tmp + u32); |
| if (pu64->low < tmp) |
| pu64->high = (uint32_t)(pu64->high + 1); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| u64Subtract(const struct counter64 *pu64one, const struct counter64 *pu64two, |
| struct counter64 *pu64out) |
| { |
| int carry; |
| |
| carry = pu64one->low < pu64two->low; |
| pu64out->low = (uint32_t)(pu64one->low - pu64two->low); |
| pu64out->high = (uint32_t)(pu64one->high - pu64two->high - carry); |
| } |
| |
| |
| |
| |
| |
| |
| |
| void |
| u64Incr(struct counter64 *pu64out, const struct counter64 *pu64one) |
| { |
| pu64out->high = (uint32_t)(pu64out->high + pu64one->high); |
| incrByU32(pu64out, pu64one->low); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| u64UpdateCounter(struct counter64 *pu64out, const struct counter64 *pu64one, |
| const struct counter64 *pu64two) |
| { |
| struct counter64 tmp; |
| |
| u64Subtract(pu64one, pu64two, &tmp); |
| u64Incr(pu64out, &tmp); |
| } |
| |
| netsnmp_feature_child_of(u64copy, netsnmp_unused) |
| #ifndef NETSNMP_FEATURE_REMOVE_U64COPY |
| |
| |
| |
| |
| |
| |
| void |
| u64Copy(struct counter64 *pu64one, const struct counter64 *pu64two) |
| { |
| *pu64one = *pu64two; |
| } |
| #endif |
| |
| |
| |
| |
| |
| |
| void |
| zeroU64(struct counter64 *pu64) |
| { |
| pu64->low = 0; |
| pu64->high = 0; |
| } |
| |
| |
| |
| |
| |
| |
| int |
| isZeroU64(const struct counter64 *pu64) |
| { |
| return pu64->low == 0 && pu64->high == 0; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int |
| netsnmp_c64_check_for_32bit_wrap(struct counter64 *old_val, |
| struct counter64 *new_val, |
| int adjust) |
| { |
| if( (NULL == old_val) || (NULL == new_val) ) |
| return -1; |
| |
| DEBUGMSGTL(("9:c64:check_wrap", "check wrap 0x%0lx.0x%0lx 0x%0lx.0x%0lx\n", |
| old_val->high, old_val->low, new_val->high, new_val->low)); |
| |
| |
| |
| |
| if ((new_val->low >= old_val->low) && |
| (new_val->high == old_val->high)) { |
| DEBUGMSGTL(("9:c64:check_wrap", "no wrap\n")); |
| return 0; |
| } |
| |
| |
| |
| |
| if (new_val->high == old_val->high) { |
| DEBUGMSGTL(("c64:check_wrap", "32 bit wrap\n")); |
| if (adjust) |
| new_val->high = (uint32_t)(new_val->high + 1); |
| return 32; |
| } |
| else if (new_val->high == (uint32_t)(old_val->high + 1)) { |
| DEBUGMSGTL(("c64:check_wrap", "64 bit wrap\n")); |
| return 64; |
| } |
| |
| return -2; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int |
| netsnmp_c64_check32_and_update(struct counter64 *prev_val, |
| struct counter64 *new_val, |
| struct counter64 *old_prev_val, |
| int *need_wrap_check) |
| { |
| int rc; |
| |
| |
| |
| |
| |
| |
| |
| if ((NULL == need_wrap_check) || (0 != *need_wrap_check)) { |
| rc = netsnmp_c64_check_for_32bit_wrap(old_prev_val,new_val, 1); |
| if (rc < 0) { |
| DEBUGMSGTL(("c64","32 bit check failed\n")); |
| return -1; |
| } |
| } |
| else |
| rc = 0; |
| |
| |
| |
| |
| (void) u64UpdateCounter(prev_val, new_val, old_prev_val); |
| |
| |
| |
| |
| if (32 == rc) { |
| |
| |
| |
| |
| if (1 != new_val->high) |
| DEBUGMSGTL(("c64", "error expanding to 64 bits: new_val->high != 1")); |
| new_val->high = 0; |
| } |
| else if (64 == rc) { |
| |
| |
| |
| |
| if ((prev_val->low != new_val->low) || |
| (prev_val->high != new_val->high)) { |
| DEBUGMSGTL(("c64", "looks like a 64bit wrap, but prev!=new\n")); |
| return -2; |
| } |
| else if (NULL != need_wrap_check) |
| *need_wrap_check = 0; |
| } |
| |
| return 0; |
| } |
| |
| |
| void |
| printU64(char *buf, |
| const struct counter64 *pu64) |
| { |
| struct counter64 u64a; |
| struct counter64 u64b; |
| |
| char aRes[I64CHARSZ + 1]; |
| unsigned int u; |
| int j; |
| |
| u64a = *pu64; |
| aRes[I64CHARSZ] = 0; |
| for (j = 0; j < I64CHARSZ; j++) { |
| divBy10(u64a, &u64b, &u); |
| aRes[(I64CHARSZ - 1) - j] = (char) ('0' + u); |
| u64a = u64b; |
| if (isZeroU64(&u64a)) |
| break; |
| } |
| strcpy(buf, &aRes[(I64CHARSZ - 1) - j]); |
| } |
| |
| |
| void |
| printI64(char *buf, |
| const struct counter64 *pu64) |
| { |
| struct counter64 u64a; |
| |
| if (pu64->high & 0x80000000) { |
| u64a.high = (uint32_t) ~pu64->high; |
| u64a.low = (uint32_t) ~pu64->low; |
| incrByU32(&u64a, 1); |
| buf[0] = '-'; |
| printU64(buf + 1, &u64a); |
| } else { |
| printU64(buf, pu64); |
| } |
| } |
| |
| |
| int |
| read64(struct counter64 *i64, const char *str) |
| { |
| struct counter64 i64p; |
| unsigned int u; |
| int sign = 0; |
| int ok = 0; |
| |
| zeroU64(i64); |
| if (*str == '-') { |
| sign = 1; |
| str++; |
| } |
| |
| while (*str && isdigit((unsigned char)(*str))) { |
| ok = 1; |
| u = *str - '0'; |
| multBy10(*i64, &i64p); |
| *i64 = i64p; |
| incrByU16(i64, u); |
| str++; |
| } |
| if (sign) { |
| i64->high = (uint32_t) ~i64->high; |
| i64->low = (uint32_t) ~i64->low; |
| incrByU16(i64, 1); |
| } |
| return ok; |
| } |