Assorted spelling fixes.
[wine.git] / dlls / snmpapi / main.c
blob9e14a1948768d07619f696681b8fbcdb164ddebe
1 /*
2 * Implementation of SNMPAPI.DLL
4 * Copyright 2002 Patrik Stridvall
5 * Copyright 2007 Hans Leidekker
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "snmp.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(snmpapi);
35 static INT asn_any_copy(AsnAny *dst, const AsnAny *src)
37 memset(dst, 0, sizeof(AsnAny));
38 switch (src->asnType)
40 case ASN_INTEGER32: dst->asnValue.number = src->asnValue.number; break;
41 case ASN_UNSIGNED32: dst->asnValue.unsigned32 = src->asnValue.unsigned32; break;
42 case ASN_COUNTER64: dst->asnValue.counter64 = src->asnValue.counter64; break;
43 case ASN_COUNTER32: dst->asnValue.counter = src->asnValue.counter; break;
44 case ASN_GAUGE32: dst->asnValue.gauge = src->asnValue.gauge; break;
45 case ASN_TIMETICKS: dst->asnValue.ticks = src->asnValue.ticks; break;
47 case ASN_OCTETSTRING:
48 case ASN_BITS:
49 case ASN_SEQUENCE:
50 case ASN_IPADDRESS:
51 case ASN_OPAQUE:
53 BYTE *stream;
54 UINT length = src->asnValue.string.length;
56 if (!(stream = HeapAlloc(GetProcessHeap(), 0, length))) return SNMPAPI_ERROR;
57 memcpy(stream, src->asnValue.string.stream, length);
59 dst->asnValue.string.stream = stream;
60 dst->asnValue.string.length = length;
61 dst->asnValue.string.dynamic = TRUE;
62 break;
64 case ASN_OBJECTIDENTIFIER:
66 UINT *ids, i, size = src->asnValue.object.idLength * sizeof(UINT);
68 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
70 dst->asnValue.object.ids = ids;
71 dst->asnValue.object.idLength = src->asnValue.object.idLength;
73 for (i = 0; i < dst->asnValue.object.idLength; i++)
74 dst->asnValue.object.ids[i] = src->asnValue.object.ids[i];
75 break;
77 default:
79 WARN("unknown ASN type: %d\n", src->asnType);
80 return SNMPAPI_ERROR;
83 dst->asnType = src->asnType;
84 return SNMPAPI_NOERROR;
87 static void asn_any_free(AsnAny *any)
89 switch (any->asnType)
91 case ASN_OCTETSTRING:
92 case ASN_BITS:
93 case ASN_SEQUENCE:
94 case ASN_IPADDRESS:
95 case ASN_OPAQUE:
97 if (any->asnValue.string.dynamic)
99 HeapFree(GetProcessHeap(), 0, any->asnValue.string.stream);
100 any->asnValue.string.stream = NULL;
102 break;
104 case ASN_OBJECTIDENTIFIER:
106 HeapFree(GetProcessHeap(), 0, any->asnValue.object.ids);
107 any->asnValue.object.ids = NULL;
108 break;
110 default: break;
112 any->asnType = ASN_NULL;
115 /***********************************************************************
116 * DllMain for SNMPAPI
118 BOOL WINAPI DllMain(
119 HINSTANCE hInstDLL,
120 DWORD fdwReason,
121 LPVOID lpvReserved)
123 TRACE("(%p,%d,%p)\n", hInstDLL, fdwReason, lpvReserved);
125 switch(fdwReason) {
126 case DLL_WINE_PREATTACH:
127 return FALSE; /* prefer native version */
128 case DLL_PROCESS_ATTACH:
129 DisableThreadLibraryCalls(hInstDLL);
130 break;
131 case DLL_PROCESS_DETACH:
132 break;
135 return TRUE;
138 /***********************************************************************
139 * SnmpUtilDbgPrint (SNMPAPI.@)
141 * NOTES
142 * The Microsoft headers claim this function uses the stdcall calling
143 * convention. But stdcall functions cannot take a variable number of
144 * arguments so this does not make sense. The stdcall specification is
145 * probably ignored by Microsoft's compiler in this case. So declare it
146 * correctly in Wine so it works with all compilers.
148 VOID WINAPIV SnmpUtilDbgPrint(INT loglevel, LPSTR format, ...)
150 FIXME("(%d, %s)\n", loglevel, debugstr_a(format));
153 /***********************************************************************
154 * SnmpUtilMemAlloc (SNMPAPI.@)
156 LPVOID WINAPI SnmpUtilMemAlloc(UINT nbytes)
158 TRACE("(%d)\n", nbytes);
159 return HeapAlloc(GetProcessHeap(), 0, nbytes);
162 /***********************************************************************
163 * SnmpUtilMemReAlloc (SNMPAPI.@)
165 LPVOID WINAPI SnmpUtilMemReAlloc(LPVOID mem, UINT nbytes)
167 TRACE("(%p, %d)\n", mem, nbytes);
168 return HeapReAlloc(GetProcessHeap(), 0, mem, nbytes);
171 /***********************************************************************
172 * SnmpUtilMemFree (SNMPAPI.@)
174 VOID WINAPI SnmpUtilMemFree(LPVOID mem)
176 TRACE("(%p)\n", mem);
177 HeapFree(GetProcessHeap(), 0, mem);
180 /***********************************************************************
181 * SnmpUtilAsnAnyCpy (SNMPAPI.@)
183 INT WINAPI SnmpUtilAsnAnyCpy(AsnAny *dst, AsnAny *src)
185 TRACE("(%p, %p)\n", dst, src);
186 return asn_any_copy(dst, src);
189 /***********************************************************************
190 * SnmpUtilAsnAnyFree (SNMPAPI.@)
192 VOID WINAPI SnmpUtilAsnAnyFree(AsnAny *any)
194 TRACE("(%p)\n", any);
195 asn_any_free(any);
198 /***********************************************************************
199 * SnmpUtilOctetsCpy (SNMPAPI.@)
201 INT WINAPI SnmpUtilOctetsCpy(AsnOctetString *dst, AsnOctetString *src)
203 TRACE("(%p, %p)\n", dst, src);
205 if (!dst) return SNMPAPI_ERROR;
206 if (!src)
208 dst->dynamic = FALSE;
209 dst->length = 0;
210 dst->stream = NULL;
211 return SNMPAPI_NOERROR;
213 if ((dst->stream = HeapAlloc(GetProcessHeap(), 0, src->length)))
215 unsigned int i;
217 dst->dynamic = TRUE;
218 dst->length = src->length;
219 for (i = 0; i < dst->length; i++) dst->stream[i] = src->stream[i];
220 return SNMPAPI_NOERROR;
222 return SNMPAPI_ERROR;
225 /***********************************************************************
226 * SnmpUtilOctetsFree (SNMPAPI.@)
228 VOID WINAPI SnmpUtilOctetsFree(AsnOctetString *octets)
230 TRACE("(%p)\n", octets);
232 if (octets)
234 octets->length = 0;
235 if (octets->dynamic) HeapFree(GetProcessHeap(), 0, octets->stream);
236 octets->stream = NULL;
237 octets->dynamic = FALSE;
241 /***********************************************************************
242 * SnmpUtilOctetsNCmp (SNMPAPI.@)
244 INT WINAPI SnmpUtilOctetsNCmp(AsnOctetString *octets1, AsnOctetString *octets2, UINT count)
246 INT ret;
247 unsigned int i;
249 TRACE("(%p, %p, %d)\n", octets1, octets2, count);
251 if (!octets1 || !octets2) return 0;
253 for (i = 0; i < count; i++)
254 if ((ret = octets1->stream[i] - octets2->stream[i])) return ret;
256 return 0;
259 /***********************************************************************
260 * SnmpUtilOctetsCmp (SNMPAPI.@)
262 INT WINAPI SnmpUtilOctetsCmp(AsnOctetString *octets1, AsnOctetString *octets2)
264 TRACE("(%p, %p)\n", octets1, octets2);
266 if (octets1->length < octets2->length) return -1;
267 if (octets1->length > octets2->length) return 1;
269 return SnmpUtilOctetsNCmp(octets1, octets2, octets1->length);
272 /***********************************************************************
273 * SnmpUtilOidAppend (SNMPAPI.@)
275 INT WINAPI SnmpUtilOidAppend(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
277 UINT *ids, i, size;
279 TRACE("(%p, %p)\n", dst, src);
281 if (!dst) return SNMPAPI_ERROR;
282 if (!src) return SNMPAPI_NOERROR;
284 size = (src->idLength + dst->idLength) * sizeof(UINT);
285 if (!(ids = HeapReAlloc(GetProcessHeap(), 0, dst->ids, size)))
287 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size)))
289 SetLastError(SNMP_MEM_ALLOC_ERROR);
290 return SNMPAPI_ERROR;
292 else memcpy(ids, dst->ids, dst->idLength * sizeof(UINT));
295 for (i = 0; i < src->idLength; i++) ids[i + dst->idLength] = src->ids[i];
296 dst->idLength = dst->idLength + src->idLength;
297 dst->ids = ids;
299 return SNMPAPI_NOERROR;
302 /***********************************************************************
303 * SnmpUtilOidCpy (SNMPAPI.@)
305 INT WINAPI SnmpUtilOidCpy(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
307 TRACE("(%p, %p)\n", dst, src);
309 if (!dst) return SNMPAPI_ERROR;
310 if (!src)
312 dst->idLength = 0;
313 dst->ids = NULL;
314 return SNMPAPI_NOERROR;
316 if ((dst->ids = HeapAlloc(GetProcessHeap(), 0, src->idLength * sizeof(UINT))))
318 unsigned int i;
320 dst->idLength = src->idLength;
321 for (i = 0; i < dst->idLength; i++) dst->ids[i] = src->ids[i];
322 return SNMPAPI_NOERROR;
324 return SNMPAPI_ERROR;
327 /***********************************************************************
328 * SnmpUtilOidFree (SNMPAPI.@)
330 VOID WINAPI SnmpUtilOidFree(AsnObjectIdentifier *oid)
332 TRACE("(%p)\n", oid);
334 if (!oid) return;
336 oid->idLength = 0;
337 HeapFree(GetProcessHeap(), 0, oid->ids);
338 oid->ids = NULL;
341 /***********************************************************************
342 * SnmpUtilOidNCmp (SNMPAPI.@)
344 INT WINAPI SnmpUtilOidNCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2, UINT count)
346 unsigned int i, len;
348 TRACE("(%p, %p, %d)\n", oid1, oid2, count);
350 if (!oid1 || !oid2) return 0;
352 len = min(count, oid1->idLength);
353 len = min(len, oid2->idLength);
354 for (i = 0; i < len; i++)
356 if (oid1->ids[i] > oid2->ids[i]) return 1;
357 if (oid1->ids[i] < oid2->ids[i]) return -1;
359 if (i == count) return 0;
360 if (oid1->idLength < oid2->idLength) return -1;
361 if (oid1->idLength > oid2->idLength) return 1;
362 return 0;
365 /***********************************************************************
366 * SnmpUtilOidCmp (SNMPAPI.@)
368 INT WINAPI SnmpUtilOidCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2)
370 TRACE("(%p, %p)\n", oid1, oid2);
372 if (oid1->idLength < oid2->idLength) return -1;
373 if (oid1->idLength > oid2->idLength) return 1;
375 return SnmpUtilOidNCmp(oid1, oid2, oid1->idLength);
378 /***********************************************************************
379 * SnmpUtilVarBindCpy (SNMPAPI.@)
381 INT WINAPI SnmpUtilVarBindCpy(SnmpVarBind *dst, SnmpVarBind *src)
383 unsigned int i, size;
385 TRACE("(%p, %p)\n", dst, src);
387 if (!dst) return SNMPAPI_ERROR;
388 if (!src)
390 dst->value.asnType = ASN_NULL;
391 return SNMPAPI_NOERROR;
394 size = src->name.idLength * sizeof(UINT);
395 if (!(dst->name.ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
397 for (i = 0; i < src->name.idLength; i++) dst->name.ids[i] = src->name.ids[i];
398 dst->name.idLength = src->name.idLength;
400 if (!asn_any_copy(&dst->value, &src->value))
402 HeapFree(GetProcessHeap(), 0, dst->name.ids);
403 return SNMPAPI_ERROR;
405 return SNMPAPI_NOERROR;
408 /***********************************************************************
409 * SnmpUtilVarBindFree (SNMPAPI.@)
411 VOID WINAPI SnmpUtilVarBindFree(SnmpVarBind *vb)
413 TRACE("(%p)\n", vb);
415 if (!vb) return;
417 asn_any_free(&vb->value);
418 HeapFree(GetProcessHeap(), 0, vb->name.ids);
419 vb->name.idLength = 0;
420 vb->name.ids = NULL;
423 /***********************************************************************
424 * SnmpUtilVarBindListCpy (SNMPAPI.@)
426 INT WINAPI SnmpUtilVarBindListCpy(SnmpVarBindList *dst, SnmpVarBindList *src)
428 unsigned int i, size;
429 SnmpVarBind *src_entry, *dst_entry;
431 TRACE("(%p, %p)\n", dst, src);
433 if (!src)
435 dst->list = NULL;
436 dst->len = 0;
437 return SNMPAPI_NOERROR;
439 size = src->len * sizeof(SnmpVarBind);
440 if (!(dst->list = HeapAlloc(GetProcessHeap(), 0, size)))
441 return SNMPAPI_ERROR;
443 src_entry = src->list;
444 dst_entry = dst->list;
445 for (i = 0; i < src->len; i++)
447 if (SnmpUtilVarBindCpy(dst_entry, src_entry))
449 src_entry++;
450 dst_entry++;
452 else
454 for (--i; i > 0; i--) SnmpUtilVarBindFree(--dst_entry);
455 HeapFree(GetProcessHeap(), 0, dst->list);
456 return SNMPAPI_ERROR;
459 dst->len = src->len;
460 return SNMPAPI_NOERROR;
463 /***********************************************************************
464 * SnmpUtilVarBindListFree (SNMPAPI.@)
466 VOID WINAPI SnmpUtilVarBindListFree(SnmpVarBindList *vb)
468 unsigned int i;
469 SnmpVarBind *entry;
471 TRACE("(%p)\n", vb);
473 entry = vb->list;
474 for (i = 0; i < vb->len; i++) SnmpUtilVarBindFree(entry++);
475 HeapFree(GetProcessHeap(), 0, vb->list);
476 vb->list = NULL;
477 vb->len = 0;
480 /***********************************************************************
481 * SnmpUtilIdsToA (SNMPAPI.@)
483 LPSTR WINAPI SnmpUtilIdsToA(UINT *ids, UINT length)
485 static char one[10], oid[514], null_oid[] = "<null oid>";
486 unsigned int i, len, left = sizeof(oid) - 1;
488 TRACE("(%p, %d)\n", ids, length);
490 if (!ids || !length) return null_oid;
492 *oid = 0;
493 for (i = 0; i < length; i++)
495 sprintf(one, "%d", ids[i]);
496 len = strlen(one);
497 if (left >= len)
499 strcat(oid, one);
500 left -= len;
502 else return oid;
504 if (i < length - 1)
506 if (left > 0)
508 strcat(oid, ".");
509 left--;
511 else return oid;
514 return oid;
517 /***********************************************************************
518 * SnmpUtilOidToA (SNMPAPI.@)
520 LPSTR WINAPI SnmpUtilOidToA(AsnObjectIdentifier *oid)
522 static char null_oid[] = "<null oid>";
524 TRACE("(%p)\n", oid);
526 if (oid)
527 return SnmpUtilIdsToA(oid->ids, oid->idLength);
528 else
529 return null_oid;
532 /***********************************************************************
533 * SnmpUtilPrintOid (SNMPAPI.@)
535 VOID WINAPI SnmpUtilPrintOid(AsnObjectIdentifier *oid)
537 unsigned int i;
539 TRACE("(%p)\n", oid);
541 if (!oid) return;
543 for (i = 0; i < oid->idLength; i++)
545 TRACE("%u", oid->ids[i]);
546 if (i < oid->idLength - 1) TRACE(".");
548 TRACE("\n");
551 /***********************************************************************
552 * SnmpUtilPrintAsnAny (SNMPAPI.@)
554 VOID WINAPI SnmpUtilPrintAsnAny(AsnAny *any)
556 unsigned int i;
558 TRACE("(%p)\n", any);
560 switch (any->asnType)
562 case ASN_NULL: TRACE("Null value\n"); return;
563 case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return;
564 case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return;
565 case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return;
566 case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return;
567 case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return;
568 case ASN_COUNTER64:
570 TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
571 return;
573 case ASN_OCTETSTRING:
575 TRACE("String ");
576 for (i = 0; i < any->asnValue.string.length; i++)
577 TRACE("%c", any->asnValue.string.stream[i]);
578 TRACE("\n");
579 return;
581 case ASN_IPADDRESS:
583 TRACE("IpAddress ");
584 if (any->asnValue.string.length < 4)
586 TRACE("Invalid\n");
587 return;
589 for (i = 0; i < 4; i++)
591 TRACE("%u", any->asnValue.string.stream[i]);
592 if (i < 3) TRACE(".");
594 TRACE("\n");
595 return;
597 case ASN_BITS:
599 TRACE("Bits ");
600 for (i = 0; i < any->asnValue.string.length; i++)
602 TRACE("0x%02x", any->asnValue.string.stream[i]);
603 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
605 TRACE("\n");
606 return;
608 case ASN_OPAQUE:
610 TRACE("Opaque ");
611 for (i = 0; i < any->asnValue.string.length; i++)
613 TRACE("0x%02x", any->asnValue.string.stream[i]);
614 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
616 TRACE("\n");
617 return;
619 case ASN_OBJECTIDENTIFIER:
621 TRACE("ObjectID ");
622 for (i = 0; i < any->asnValue.object.idLength; i++)
624 TRACE("%u", any->asnValue.object.ids[i]);
625 if (i < any->asnValue.object.idLength - 1) TRACE(".");
627 TRACE("\n");
628 return;
630 default:
632 TRACE("Invalid type %d\n", any->asnType);
633 return;