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.

393 lines
8.4 KiB

  1. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. oid.c
  5. Abstract:
  6. Contains routines to manipulatee object identifiers.
  7. SnmpUtilOidCpy
  8. SnmpUtilOidAppend
  9. SnmpUtilOidNCmp
  10. SnmpUtilOidCmp
  11. SnmpUtilOidFree
  12. Environment:
  13. User Mode - Win32
  14. Revision History:
  15. --*/
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // //
  18. // Include files //
  19. // //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. #include <snmp.h>
  22. #include <snmputil.h>
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // //
  25. // Public Procedures //
  26. // //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. SNMPAPI
  29. SNMP_FUNC_TYPE
  30. SnmpUtilOidCpy(
  31. AsnObjectIdentifier * pOidDst,
  32. AsnObjectIdentifier * pOidSrc
  33. )
  34. /*++
  35. Routine Description:
  36. Copy an object identifier.
  37. Arguments:
  38. pOidDst - pointer to structure to receive OID.
  39. pOidSrc - pointer to OID to copy.
  40. Return Values:
  41. Returns SNMPAPI_NOERROR if successful.
  42. --*/
  43. {
  44. SNMPAPI nResult = SNMPAPI_ERROR;
  45. // validate pointers
  46. if (pOidDst != NULL) {
  47. // initialize
  48. pOidDst->ids = NULL;
  49. pOidDst->idLength = 0;
  50. // check for subids
  51. if ((pOidSrc != NULL) &&
  52. (pOidSrc->ids != NULL) &&
  53. (pOidSrc->idLength != 0)) {
  54. // attempt to allocate the subids
  55. pOidDst->ids = (UINT *)SnmpUtilMemAlloc(
  56. pOidSrc->idLength * sizeof(UINT)
  57. );
  58. // validate pointer
  59. if (pOidDst->ids != NULL) {
  60. // transfer the oid length
  61. pOidDst->idLength = pOidSrc->idLength;
  62. // transfer subids
  63. memcpy(pOidDst->ids,
  64. pOidSrc->ids,
  65. pOidSrc->idLength * sizeof(UINT)
  66. );
  67. nResult = SNMPAPI_NOERROR; // success...
  68. } else {
  69. SNMPDBG((
  70. SNMP_LOG_ERROR,
  71. "SNMP: API: could not allocate oid.\n"
  72. ));
  73. SetLastError(SNMP_MEM_ALLOC_ERROR);
  74. }
  75. } else {
  76. SNMPDBG((
  77. SNMP_LOG_WARNING,
  78. "SNMP: API: copying a null oid.\n"
  79. ));
  80. nResult = SNMPAPI_NOERROR; // success...
  81. }
  82. } else {
  83. SNMPDBG((
  84. SNMP_LOG_ERROR,
  85. "SNMP: API: null oid pointer during copy.\n"
  86. ));
  87. SetLastError(ERROR_INVALID_PARAMETER);
  88. }
  89. return nResult;
  90. }
  91. SNMPAPI
  92. SNMP_FUNC_TYPE
  93. SnmpUtilOidAppend(
  94. AsnObjectIdentifier * pOidDst,
  95. AsnObjectIdentifier * pOidSrc
  96. )
  97. /*++
  98. Routine Description:
  99. Append source OID to destination OID
  100. Arguments:
  101. pOidDst - pointer to structure to receive combined OID.
  102. pOidSrc - pointer to OID to append.
  103. Return Values:
  104. Returns SNMPAPI_NOERROR if successful.
  105. --*/
  106. {
  107. SNMPAPI nResult = SNMPAPI_ERROR;
  108. // validate pointers
  109. if (pOidDst != NULL) {
  110. // check if there are subids
  111. if ((pOidSrc != NULL) &&
  112. (pOidSrc->ids != NULL) &&
  113. (pOidSrc->idLength != 0)) {
  114. // calculate the total number of subidentifiers
  115. UINT nIds = pOidDst->idLength + pOidSrc->idLength;
  116. // validate number of subids
  117. if (nIds <= SNMP_MAX_OID_LEN) {
  118. // attempt to allocate the subidentifiers
  119. UINT * pIds = (UINT *)SnmpUtilMemReAlloc(
  120. pOidDst->ids,
  121. nIds * sizeof(UINT)
  122. );
  123. // validate pointer
  124. if (pIds != NULL) {
  125. // transfer pointer
  126. pOidDst->ids = pIds;
  127. // transfer subids
  128. memcpy(&pOidDst->ids[pOidDst->idLength],
  129. pOidSrc->ids,
  130. pOidSrc->idLength * sizeof(UINT)
  131. );
  132. // transfer oid length
  133. pOidDst->idLength = nIds;
  134. nResult = SNMPAPI_NOERROR; // success...
  135. } else {
  136. SNMPDBG((
  137. SNMP_LOG_ERROR,
  138. "SNMP: API: could not allocate oid.\n"
  139. ));
  140. SetLastError(SNMP_MEM_ALLOC_ERROR);
  141. }
  142. } else {
  143. SNMPDBG((
  144. SNMP_LOG_ERROR,
  145. "SNMP: API: combined oid too large.\n"
  146. ));
  147. SetLastError(SNMP_BERAPI_OVERFLOW);
  148. }
  149. } else {
  150. SNMPDBG((
  151. SNMP_LOG_WARNING,
  152. "SNMP: API: appending a null oid.\n"
  153. ));
  154. nResult = SNMPAPI_NOERROR; // success...
  155. }
  156. } else {
  157. SNMPDBG((
  158. SNMP_LOG_ERROR,
  159. "SNMP: API: null oid pointer during copy.\n"
  160. ));
  161. SetLastError(ERROR_INVALID_PARAMETER);
  162. }
  163. return nResult;
  164. }
  165. SNMPAPI
  166. SNMP_FUNC_TYPE
  167. SnmpUtilOidNCmp(
  168. AsnObjectIdentifier * pOid1,
  169. AsnObjectIdentifier * pOid2,
  170. UINT nSubIds
  171. )
  172. /*++
  173. Routine Description:
  174. Compares two OIDs up to a certain subidentifier.
  175. Arguments:
  176. pOid1 - pointer to first OID.
  177. pOid2 - pointer to second OID.
  178. nSubIds - maximum subidentifiers to compare.
  179. Return Values:
  180. < 0 first parameter is 'less than' second.
  181. 0 first parameter is 'equal to' second.
  182. > 0 first parameter is 'greater than' second.
  183. --*/
  184. {
  185. UINT i = 0;
  186. // INT nResult = 0;
  187. // validate pointers
  188. if ((pOid1 != NULL) &&
  189. (pOid2 != NULL)) {
  190. // calculate maximum number of subidentifiers to compare
  191. UINT nMaxIds = min(nSubIds, min(pOid1->idLength, pOid2->idLength));
  192. /*
  193. // loop through the subidentifiers
  194. while((nResult == 0) && (i < nMaxIds)) {
  195. // compare each subidentifier
  196. nResult = pOid1->ids[i] - pOid2->ids[i++];
  197. }
  198. */
  199. while(i < nMaxIds)
  200. {
  201. if (pOid1->ids[i] != pOid2->ids[i])
  202. break;
  203. i++;
  204. }
  205. // comparision length less then either OID lengths; components equals
  206. if (i == nSubIds)
  207. return 0;
  208. // difference encountered before either OID endings and before the
  209. // requested comparision length
  210. if (i < nMaxIds)
  211. return (pOid1->ids[i] < pOid2->ids[i])? -1 : 1;
  212. // one OID is shorter than the requested comparision length
  213. return pOid1->idLength - pOid2->idLength;
  214. /*
  215. // check for second being subset
  216. if ((nResult == 0) && (i < nSubIds)) {
  217. // determine order by length of oid
  218. nResult = pOid1->idLength - pOid2->idLength;
  219. }
  220. */
  221. }
  222. // return nResult;
  223. return 0;
  224. }
  225. SNMPAPI
  226. SNMP_FUNC_TYPE
  227. SnmpUtilOidCmp(
  228. AsnObjectIdentifier * pOid1,
  229. AsnObjectIdentifier * pOid2
  230. )
  231. /*++
  232. Routine Description:
  233. Compares two OIDs.
  234. Arguments:
  235. pOid1 - pointer to first OID.
  236. pOid2 - pointer to second OID.
  237. Return Values:
  238. < 0 first parameter is 'less than' second.
  239. 0 first parameter is 'equal to' second.
  240. > 0 first parameter is 'greater than' second.
  241. --*/
  242. {
  243. // forward request to the function above
  244. return SnmpUtilOidNCmp(pOid1,pOid2,max(pOid1->idLength,pOid2->idLength));
  245. }
  246. VOID
  247. SNMP_FUNC_TYPE
  248. SnmpUtilOidFree(
  249. AsnObjectIdentifier * pOid
  250. )
  251. /*++
  252. Routine Description:
  253. Releases memory associated with OID.
  254. Arguments:
  255. pOid - pointer to OID to free.
  256. Return Values:
  257. None.
  258. --*/
  259. {
  260. // validate
  261. if (pOid != NULL) {
  262. // release subids memory
  263. SnmpUtilMemFree(pOid->ids);
  264. // re-initialize
  265. pOid->idLength = 0;
  266. pOid->ids = NULL;
  267. }
  268. }