Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. octets.c
  5. Abstract:
  6. Contains routines to manipulate octet strings.
  7. SnmpUtilOctetsCpy
  8. SnmpUtilOctetsFree
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // //
  15. // Include files //
  16. // //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <snmp.h>
  19. #include <snmputil.h>
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // //
  22. // Public Procedures //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. SNMPAPI
  26. SNMP_FUNC_TYPE
  27. SnmpUtilOctetsCpy(
  28. AsnOctetString * pOctetsDst,
  29. AsnOctetString * pOctetsSrc
  30. )
  31. /*++
  32. Routine Description:
  33. Copy an octet string.
  34. Arguments:
  35. pOctetsDst - pointer to structure to receive octets.
  36. pOctetsSrc - pointer to octets to copy.
  37. Return Values:
  38. Returns SNMPAPI_NOERROR if successful.
  39. --*/
  40. {
  41. SNMPAPI nResult = SNMPAPI_ERROR;
  42. // validate pointers
  43. if (pOctetsDst != NULL) {
  44. // initialize
  45. pOctetsDst->stream = NULL;
  46. pOctetsDst->length = 0;
  47. pOctetsDst->dynamic = FALSE;
  48. // make sure there are bytes
  49. if ((pOctetsSrc != NULL) &&
  50. (pOctetsSrc->stream != NULL) &&
  51. (pOctetsSrc->length != 0)) {
  52. // attempt to allocate octet string
  53. pOctetsDst->stream = SnmpUtilMemAlloc(pOctetsSrc->length);
  54. // validate pointer
  55. if (pOctetsDst->stream != NULL) {
  56. // denote allocated type
  57. pOctetsDst->dynamic = TRUE;
  58. // transfer octet string length
  59. pOctetsDst->length = pOctetsSrc->length;
  60. // copy
  61. memcpy(pOctetsDst->stream,
  62. pOctetsSrc->stream,
  63. pOctetsSrc->length
  64. );
  65. nResult = SNMPAPI_NOERROR; // success...
  66. } else {
  67. SNMPDBG((
  68. SNMP_LOG_ERROR,
  69. "SNMP: API: could not allocate octet string.\n"
  70. ));
  71. SetLastError(SNMP_MEM_ALLOC_ERROR);
  72. }
  73. } else {
  74. // copying null string
  75. nResult = SNMPAPI_NOERROR;
  76. }
  77. } else {
  78. SNMPDBG((
  79. SNMP_LOG_ERROR,
  80. "SNMP: API: null octet string pointer.\n"
  81. ));
  82. SetLastError(ERROR_INVALID_PARAMETER);
  83. }
  84. return nResult;
  85. }
  86. VOID
  87. SNMP_FUNC_TYPE
  88. SnmpUtilOctetsFree(
  89. AsnOctetString * pOctets
  90. )
  91. /*++
  92. Routine Description:
  93. Releases memory for an octet string.
  94. Arguments:
  95. pOctets - pointer to octets to release.
  96. Return Values:
  97. None.
  98. --*/
  99. {
  100. // validate pointers
  101. if ((pOctets != NULL) &&
  102. (pOctets->stream != NULL) &&
  103. (pOctets->dynamic == TRUE)) {
  104. // release memory for octets
  105. SnmpUtilMemFree(pOctets->stream);
  106. // re-initialize
  107. pOctets->dynamic = FALSE;
  108. pOctets->stream = NULL;
  109. pOctets->length = 0;
  110. }
  111. }
  112. SNMPAPI
  113. SNMP_FUNC_TYPE
  114. SnmpUtilOctetsNCmp(
  115. AsnOctetString * pOctets1,
  116. AsnOctetString * pOctets2,
  117. UINT nChars
  118. )
  119. /*++
  120. Routine Description:
  121. Compares two octet strings up to a certain number of characters.
  122. Arguments:
  123. pOctets1 - pointer to first octet string.
  124. pOctets2 - pointer to second octet string.
  125. nChars - maximum characters to compare.
  126. Return Values:
  127. < 0 first parameter is 'less than' second.
  128. 0 first parameter is 'equal to' second.
  129. > 0 first parameter is 'greater than' second.
  130. --*/
  131. {
  132. UINT i = 0;
  133. INT nResult = 0;
  134. // validate pointers
  135. if ((pOctets1 != NULL) &&
  136. (pOctets2 != NULL)) {
  137. // calculate maximum number of subidentifiers to compare
  138. UINT nMaxChars = min(nChars, min(pOctets1->length, pOctets2->length));
  139. // loop through the subidentifiers
  140. while((nResult == 0) && (i < nMaxChars)) {
  141. // compare each subidentifier
  142. nResult = pOctets1->stream[i] - pOctets2->stream[i++];
  143. }
  144. // check for second being subset
  145. if ((nResult == 0) && (i < nChars)) {
  146. // determine order by length of oid
  147. nResult = pOctets1->length - pOctets2->length;
  148. }
  149. }
  150. return nResult;
  151. }
  152. SNMPAPI
  153. SNMP_FUNC_TYPE
  154. SnmpUtilOctetsCmp(
  155. AsnOctetString * pOctets1,
  156. AsnOctetString * pOctets2
  157. )
  158. /*++
  159. Routine Description:
  160. Compares two octet strings.
  161. Arguments:
  162. pOctets1 - pointer to first octet string.
  163. pOctets2 - pointer to second octet string.
  164. Return Values:
  165. < 0 first parameter is 'less than' second.
  166. 0 first parameter is 'equal to' second.
  167. > 0 first parameter is 'greater than' second.
  168. --*/
  169. {
  170. // forward request above
  171. return SnmpUtilOctetsNCmp(
  172. pOctets1,
  173. pOctets2,
  174. max(pOctets1->length,pOctets2->length)
  175. );
  176. }