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.
 
  48   OCTETSTR CLASS DEFINITION
 
  50   DESIGN + AUTHOR:  Peter E Mellquist
 
  53   This class is fully contained and does not rely on or any other
 
  54   SNMP libraries. This class is portable across any platform
 
  56 =====================================================================*/
 
  57 // $Id: octet.h 1824 2010-08-29 19:47:08Z katz $
 
  62 #include "snmp_pp/smival.h"
 
  64 #ifdef SNMP_PP_NAMESPACE
 
  68 //------------[ SNMP++ OCTETSTR CLASS DEF  ]-----------------------------
 
  69 class DLLOPT OctetStr: public  SnmpSyntax
 
  74    * Enum for setting the hex output format.
 
  83   //-----------[ Constructors and Destrucotr ]----------------------
 
  86    * Constructs a valid OctetStr with zero length.
 
  91    * Constructs a OctetStr with the given value.
 
  92    * The OctetStr will be valid unless a call to new fails.
 
  94    * @param str - Null terminated string
 
  96   OctetStr(const char *str);
 
  99    * Constructs a OctetStr with the given value.
 
 100    * The OctetStr will be valid unless a call to new fails.
 
 102    * @param str - string that may contain null bytes
 
 103    * @param len - length of the string
 
 105   OctetStr(const unsigned char *str, unsigned long len);
 
 108    * Construct a OctetStr from another OctetStr.
 
 109    * The OctetStr will be valid unless a call to new fails.
 
 111    * @param octet - Value for the new object
 
 113   OctetStr(const OctetStr &octet);
 
 116    * Destructor, frees allocated space.
 
 120   //-----------[ Overloaded operators ]----------------------
 
 123    * Assign a char string to a OctetStr.
 
 125   OctetStr& operator=(const char *str);
 
 128    * Assign a OctetStr to a OctetStr.
 
 130   OctetStr& operator=(const OctetStr &octet);
 
 133    * Equal operator for two OctetStr.
 
 135   DLLOPT friend int operator==(const OctetStr &lhs, const OctetStr &rhs);
 
 138    * Not equal operator for two OctetStr.
 
 140   DLLOPT friend int operator!=(const OctetStr &lhs, const OctetStr &rhs);
 
 143    * Not equal operator for two OctetStr.
 
 145   DLLOPT friend int operator<(const OctetStr &lhs, const OctetStr &rhs);
 
 148    * Less than operator for two OctetStr.
 
 150   DLLOPT friend int operator<=(const OctetStr &lhs,const OctetStr &rhs);
 
 153    * Greater than operator for two OctetStr.
 
 155   DLLOPT friend int operator>(const OctetStr &lhs, const OctetStr &rhs);
 
 158    * Greater than or equal operator for two OctetStr.
 
 160   DLLOPT friend int operator>=(const OctetStr &lhs, const OctetStr &rhs);
 
 163    * Equal operator for OctetStr and char string.
 
 165   DLLOPT friend int operator==(const OctetStr &lhs, const char *rhs);
 
 168    * Not equal operator for OctetStr and char string.
 
 170   DLLOPT friend int operator!=(const OctetStr &lhs, const char *rhs);
 
 173    * Less than operator for OctetStr and char string.
 
 175   DLLOPT friend int operator<(const OctetStr &lhs, const char *rhs);
 
 178    * Less than or equal operator for OctetStr and char string.
 
 180   DLLOPT friend int operator<=(const OctetStr &lhs, const char *rhs);
 
 183    * Greater than operator for OctetStr and char string.
 
 185   DLLOPT friend int operator>(const OctetStr &lhs, const char *rhs);
 
 188    * Greater than or equal operator for OctetStr and char string.
 
 190   DLLOPT friend int operator>=(const OctetStr &lhs, const char *rhs);
 
 193    * Append a char string to this OctetStr.
 
 195   OctetStr& operator+=(const char *a);
 
 198    * Append a single char to this OctetStr.
 
 200   OctetStr& operator+=(const unsigned char c);
 
 203    * Append another OctetStr to this OctetStr.
 
 205   OctetStr& operator+=(const OctetStr& octet);
 
 208    * Allow access as if it was an array.
 
 210    * @note The given param is not checked for validity.
 
 212   unsigned char &operator[](int i)
 
 213     { m_changed = true; return smival.value.string.ptr[i]; };
 
 216    * Allow access as if it was an array for const OctetStr objects.
 
 218    * @note The given param is not checked for validity.
 
 220   unsigned char operator[](int i) const { return smival.value.string.ptr[i]; };
 
 225    * @return This method always returns sNMP_SYNTAX_OCTETS.
 
 227   SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; };
 
 230    * Return the space needed for serialization.
 
 232   int get_asn1_length() const;
 
 235    * Return validity of the object.
 
 237   bool valid() const { return validity; };
 
 242    * @return Pointer to the newly created object (allocated through new).
 
 244   SnmpSyntax *clone() const { return (SnmpSyntax *) new OctetStr(*this); };
 
 247    * Map other SnmpSyntax objects to OctetStr.
 
 249   SnmpSyntax& operator=(const SnmpSyntax &val);
 
 252    * Get a printable ASCII value of the string.
 
 254    * @note Depending on the selected output format, this method will
 
 255    *       return get_printable_hex() or get_printable_clear() if the
 
 256    *       string contains not printable characters.
 
 258    * @return Printable, null terminated string
 
 260   const char *get_printable() const;
 
 263    * Get an ASCII formatted hex dump of the contents.
 
 264    * If the output format was set to OctetStr::OutputHexAndClear,
 
 265    * the produced string of this method will look like this:
 
 267    * 09 4F 63 74 65 74 53 74 72 3A 3A 67 65 74 5F 70    .OctetStr::get_p
 
 268    * 72 69 6E 74 61 62 6C 65 5F 68 65 78 28 29          rintable_hex()
 
 270    * If the output format was set to OctetStr::OutputHex the
 
 271    * produced string will look like this:
 
 273    * 09 4F 63 74 65 74 53 74 72 3A 3A 67 65 74 5F 70
 
 274    * 72 69 6E 74 61 62 6C 65 5F 68 65 78 28 29
 
 276    * @return Printable, null terminated string.
 
 278   const char *get_printable_hex() const;
 
 281    * Get the contents with all non printable characters replaced.
 
 283    * @return Printable, null terminated string.
 
 285   const char *get_printable_clear() const;
 
 288    * Set the output format for get_pritable_hex().
 
 290   static void set_hex_output_type(const enum OutputType ot)
 
 291     { hex_output_type = ot; };
 
 294    * Set the char get_printable_hex() and get_printable_clear()
 
 295    * will use for non printable characters.
 
 297   static void set_np_char(const char np) { nonprintable_char = np; };
 
 300    * Set the data on an already constructed OctetStr.
 
 301    * The given string is copied to an internal member var, so the
 
 302    * params can be destroyed afterwards.
 
 304    * @param str - The new string value
 
 305    * @param len - Length of the given string
 
 307   void set_data(const unsigned char *str, unsigned long len);
 
 310    * Get the length of the string.
 
 312   unsigned long len() const { return smival.value.string.len; };
 
 315    * Get a pointer to internal data.
 
 317   unsigned char *data() const { return smival.value.string.ptr; };
 
 319   // compare n elements of an octet
 
 320   int nCompare(const unsigned long n, const OctetStr &o) const;
 
 323    * Build an OctetStr from a hex string.
 
 324    * Called with "5465  737469 6e672074686973206D657468 6f 64 21"
 
 325    * the returned value will be "Testing this method!"
 
 327    * @param hex_string - The hex string (may contain spaces)
 
 328    * @return created string
 
 330   static OctetStr from_hex_string(const OctetStr &hex_string);
 
 333    * Set the character for linefeeds in get_printable() functions.
 
 335    * The default linefeeds are \n for Unix and \r\n on other systems.
 
 337    * @param lf_chars - string less than 3 bytes
 
 338    * @return true on success
 
 340   static bool set_linefeed_chars(const char* lf_chars);
 
 343    * Null out the contents of the string. The string will be empty
 
 344    * after calling this method
 
 349    * Append or shorten the internal data buffer.
 
 351    * The buffer will either be shortened or extended. In the second case
 
 352    * zeroes are added to the end of the string.
 
 354    * @param new_len - The new length for the string
 
 355    * @return true on success
 
 357   bool set_len(const unsigned long new_len);
 
 363       OutputFunctionDefault,
 
 368   SNMP_PP_MUTABLE char *output_buffer;   // formatted Octet value
 
 369   SNMP_PP_MUTABLE unsigned int output_buffer_len; // allocated space for string
 
 370   SNMP_PP_MUTABLE bool m_changed;
 
 371   SNMP_PP_MUTABLE enum OutputType output_last_type;
 
 372   SNMP_PP_MUTABLE char output_last_np_char;
 
 373   SNMP_PP_MUTABLE enum OutputFunction output_last_function;
 
 376   bool validity;                         // validity boolean
 
 378   static enum OutputType hex_output_type;
 
 379   static char nonprintable_char;
 
 380   static char linefeed_chars[3];
 
 383 //-----------[ End OctetStr Class ]-------------------------------------
 
 386  * The OpaqueStr class represents the Opaque SNMP type. It is derived from
 
 387  * the SNMP++ class OctetStr and has the same interfaces and behavior,
 
 388  * except that its syntax is sNMP_SYNTAX_OPAQUE.
 
 390 class OpaqueStr: public OctetStr
 
 394    * Constructor creating a valid zero length OpaqueStr.
 
 396   OpaqueStr(): OctetStr()
 
 397     { smival.syntax = sNMP_SYNTAX_OPAQUE; };
 
 400    * Constructs a OpaqueStr with the given value.
 
 401    * The OpaqueStr will be valid unless a call to new fails.
 
 403    * @param str - Null terminated string
 
 405   OpaqueStr(const char *str) : OctetStr(str)
 
 406     { smival.syntax = sNMP_SYNTAX_OPAQUE; };
 
 409    * Constructs a OpaqueStr with the given value.
 
 410    * The OpaqueStr will be valid unless a call to new fails.
 
 412    * @param str - string that may contain null bytes
 
 413    * @param len - length of the string
 
 415   OpaqueStr(const unsigned char *str, unsigned long length)
 
 416     : OctetStr(str, length) { smival.syntax = sNMP_SYNTAX_OPAQUE; };
 
 419    * Construct a OpaqueStr from an OctetStr.
 
 420    * The OpaqueStr will be valid unless a call to new fails.
 
 422    * @param octet - Value for the new object
 
 424   OpaqueStr(const OctetStr &octet) : OctetStr(octet)
 
 425     { smival.syntax = sNMP_SYNTAX_OPAQUE; };
 
 428    * Construct a OpaqueStr from another OpaqueStr.
 
 429    * The OpaqueStr will be valid unless a call to new fails.
 
 431    * @param opaque - Value for the new object
 
 433   OpaqueStr(const OpaqueStr& opaque) : OctetStr(opaque)
 
 434     { smival.syntax = sNMP_SYNTAX_OPAQUE; };
 
 439    * @return Pointer to the newly created object (allocated through new).
 
 441   virtual SnmpSyntax *clone() const { return new OpaqueStr(*this); }
 
 446    * @return This method always returns sNMP_SYNTAX_OPAQUE.
 
 448   virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OPAQUE; };
 
 451    * Map other SnmpSyntax objects to OpaqueStr.
 
 453   SnmpSyntax& operator=(const SnmpSyntax &val) 
 
 454    { return OctetStr::operator=(val); }
 
 458 #ifdef SNMP_PP_NAMESPACE
 
 459 } // end of namespace Snmp_pp