octet.cpp
来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 688 行 · 第 1/2 页
CPP
688 行
if ( lhs.nCompare( to.smival.value.string.len,to)!=0)
return TRUE;
else
return FALSE;
};
//===============[ less than < operator overloaded ]==================
int operator<( const OctetStr &lhs,const char *rhs)
{
OctetStr to( rhs);
if ( lhs.nCompare( to.smival.value.string.len,to)<0)
return TRUE;
else
return FALSE;
};
//===============[ less than <= operator overloaded ]=================
int operator<=( const OctetStr &lhs,char *rhs)
{
OctetStr to( rhs);
if (( lhs.nCompare( to.smival.value.string.len,to)<0) ||
( lhs.nCompare( to.smival.value.string.len,to)==0))
return TRUE;
else
return FALSE;
};
//===============[ greater than > operator overloaded ]===============
int operator>( const OctetStr &lhs,const char *rhs)
{
OctetStr to( rhs);
if ( lhs.nCompare( to.smival.value.string.len,to)>0)
return TRUE;
else
return FALSE;
};
//===============[ greater than >= operator overloaded ]==============
int operator>=( const OctetStr &lhs,const char *rhs)
{
OctetStr to( rhs);
if (( lhs.nCompare( to.smival.value.string.len,to)>0) ||
( lhs.nCompare( to.smival.value.string.len,to)==0))
return TRUE;
else
return FALSE;
};
//===============[ append operator, appends a string ]================
OctetStr& OctetStr::operator+=( const char *a)
{
unsigned char *tmp; // temp pointer
size_t slen,nlen;
// get len of string
if ( !a || ((slen = (size_t) STRLEN( a)) == 0))
return *this;
// total len of new octet
nlen = slen + (size_t) smival.value.string.len;
// get mem needed
tmp = (SmiLPBYTE) new unsigned char [ nlen];
// if not null, fill it up
if ( tmp != 0)
{
// copy in the original 1st
MEMCPY ( tmp, smival.value.string.ptr, (size_t) smival.value.string.len);
// copy in the string
MEMCPY( tmp + smival.value.string.len, a, (size_t) slen);
// delete the original
if ( smival.value.string.ptr )
delete [] smival.value.string.ptr;
// point to the new one
smival.value.string.ptr = tmp;
smival.value.string.len = nlen;
}
return *this;
};
//================[ append one OctetStr to another ]==================
OctetStr& OctetStr::operator+=( const OctetStr& octetstr)
{
unsigned char *tmp; // temp pointer
size_t slen,nlen;
if (!octetstr.validity ||
!(slen = (size_t)octetstr.len()))
return *this;
// total len of new octet
nlen = slen + (size_t) smival.value.string.len;
// get mem needed
tmp = (SmiLPBYTE) new unsigned char [ nlen];
// if not null, fill it up
if ( tmp != 0)
{
// copy in the original 1st
MEMCPY ( tmp, smival.value.string.ptr, (size_t) smival.value.string.len);
// copy in the string
MEMCPY( tmp + smival.value.string.len, octetstr.data(), (size_t) slen);
// delete the original
if ( smival.value.string.ptr )
delete [] smival.value.string.ptr;
// point to the new one
smival.value.string.ptr = tmp;
smival.value.string.len = nlen;
}
return *this;
};
//================[ appends an int ]==================================
OctetStr& OctetStr::operator+=( const unsigned char c)
{
unsigned char *tmp;
// get the memory needed plus one extra byte
tmp = (SmiLPBYTE) new unsigned char [ smival.value.string.len + 1];
// if not null, fill it up
if ( tmp != 0)
{
MEMCPY ( tmp, // dest
smival.value.string.ptr, // source
(size_t) smival.value.string.len); // len of original
tmp[ smival.value.string.len ] = c; // assign in new byte
if ( smival.value.string.ptr ) // delete the original
delete [] smival.value.string.ptr;
smival.value.string.ptr = tmp; // point to new one
smival.value.string.len++; // up the len
}
return *this; // return self reference
};
//================[ compare n elements of an Octet ]==================
int OctetStr::nCompare( const unsigned long n,
const OctetStr &o) const
{
unsigned long z,w;
// both are empty, they are equal
if (( smival.value.string.len == 0) &&
( o.smival.value.string.len == 0))
return 0; // equal
// self is empty and param has something
if (( smival.value.string.len == 0) &&
( o.smival.value.string.len >0) &&
(n>0))
return -1;
// self has something and param has nothing
if (( smival.value.string.len > 0) &&
( o.smival.value.string.len ==0) &&
(n>0))
return 1;
// special case
if (( smival.value.string.len == 0) &&
( o.smival.value.string.len > 0) &&
( n == 0))
return 0;
// pick the Min of n, this and the param len
// this is the maximum # to iterate a search
w = smival.value.string.len < o.smival.value.string.len
? smival.value.string.len : o.smival.value.string.len;
if (n<w) w=n;
z=0;
while( z<w)
{
if ( smival.value.string.ptr[z] < o.smival.value.string.ptr[z])
return -1; // less than
if ( smival.value.string.ptr[z] > o.smival.value.string.ptr[z])
return 1; // greater than
z++;
}
if (( z == 0) &&
( smival.value.string.len == 0) &&
( o.smival.value.string.len > 0))
return -1;
if (( z == 0) &&
( o.smival.value.string.len == 0) &&
( smival.value.string.len > 0))
return 1;
return 0;
};
//================[ return the len of the oid ]=======================
unsigned long OctetStr::len() const
{
return smival.value.string.len;
};
//================[ operator[]: access as if array ]==================
unsigned char& OctetStr::operator[]( int position)
{
return smival.value.string.ptr[position];
};
//===============[ reuturns pointer to internal data ]===============
unsigned char * OctetStr::data() const
{
return smival.value.string.ptr;
};
//================[ returns validity ]================================
int OctetStr::valid() const
{
return validity;
};
//================[ clone() ]=========================================
SnmpSyntax * OctetStr::clone() const
{
return ( SnmpSyntax *) new OctetStr(*this);
};
//================[ ASCII format return ]=============================
char * OctetStr::get_printable()
{
for ( unsigned long i=0; i < smival.value.string.len; i++){
if (( smival.value.string.ptr[i] != '\r')&&
( smival.value.string.ptr[i] != '\n')&&
(isprint((int) (smival.value.string.ptr[i]))==0))
return(get_printable_hex());
}
if ( output_buffer != NULL)
delete [] output_buffer;
output_buffer = new char[smival.value.string.len + 1];
if (smival.value.string.len)
MEMCPY(output_buffer, smival.value.string.ptr, (unsigned int) smival.value.string.len);
output_buffer[smival.value.string.len] = '\0';
return(output_buffer);
}
//================[ general Value = operator ]========================
SnmpSyntax& OctetStr::operator=( SnmpSyntax &val)
{
// protect against assignment from self
if ( this == &val )
return *this;
// blow away the old value
if (smival.value.string.ptr) {
delete [] smival.value.string.ptr;
smival.value.string.ptr = NULL;
}
smival.value.string.len = 0;
validity=FALSE;
if (val.valid()){
switch (val.get_syntax()){
case sNMP_SYNTAX_OCTETS:
case sNMP_SYNTAX_IPADDR:
set_data( ((OctetStr &)val).smival.value.string.ptr,
((OctetStr &)val).smival.value.string.len);
break;
}
}
return *this;
};
//================[ format the output into hex ]========================
char *OctetStr::get_printable_hex()
{
int cnt;
char char_buf[80]; // holds ASCII representation of data
char *buf_ptr; // pointer into ASCII listing
char *line_ptr; // pointer into Hex listing
int storageNeeded; // how much space do we need ?
int local_len = (int) smival.value.string.len;
unsigned char *bytes = smival.value.string.ptr;
storageNeeded = (int) ((smival.value.string.len/16)+1) * 72 + 1;
if ( output_buffer != NULL)
delete [] output_buffer;
output_buffer = new char[storageNeeded];
line_ptr = output_buffer;
/*----------------------------------------*/
/* processing loop for entire data buffer */
/*----------------------------------------*/
while (local_len > 0) {
cnt = 16; /* print 16 bytes per line */
buf_ptr = char_buf;
sprintf(line_ptr, " ");
line_ptr += 2; /* indent */
/*-----------------------*/
/* process a single line */
/*-----------------------*/
while (cnt-- > 0 && local_len-- > 0) {
sprintf(line_ptr, "%2.2X ", *bytes);
line_ptr +=3; /* the display of a byte always 3 chars long */
if (isprint(*bytes))
sprintf(buf_ptr, "%c", *bytes);
else
sprintf(buf_ptr, ".");
bytes++;
buf_ptr++;
}
cnt++;
/*----------------------------------------------------------*/
/* this is to make sure that the ASCII displays line up for */
/* incomplete lines of hex */
/*----------------------------------------------------------*/
while (cnt-- > 0){
sprintf(line_ptr," ");
line_ptr += 3;
}
/*------------------------------------------*/
/* append the ASCII display to the Hex line */
/*------------------------------------------*/
#ifndef __unix
sprintf(line_ptr," %s\n", char_buf);
#else
sprintf(line_ptr," %s\r\n", char_buf);
#endif // __unix
line_ptr += 3 + strlen(char_buf);
}
return(output_buffer);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?