TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / snmpapi / main.c
blob2101ec8d1047f9338ad5a25d207e8c809af38243
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 static ULONGLONG startTime;
117 /***********************************************************************
118 * DllMain for SNMPAPI
120 BOOL WINAPI DllMain(
121 HINSTANCE hInstDLL,
122 DWORD fdwReason,
123 LPVOID lpvReserved)
125 TRACE("(%p,%d,%p)\n", hInstDLL, fdwReason, lpvReserved);
127 switch(fdwReason) {
128 case DLL_WINE_PREATTACH:
129 return FALSE; /* prefer native version */
130 case DLL_PROCESS_ATTACH:
131 DisableThreadLibraryCalls(hInstDLL);
132 startTime = GetTickCount64();
133 break;
136 return TRUE;
139 /***********************************************************************
140 * SnmpSvcGetUptime (SNMPAPI.@)
142 * BUGS
143 * This returns the number of centiseconds since the DLL was loaded,
144 * rather than the number of centiseconds since the SNMP service was
145 * started, since there isn't yet any SNMP service in Wine.
147 DWORD WINAPI SnmpSvcGetUptime(void)
149 ULONGLONG now = GetTickCount64();
151 return (now - startTime) / 10;
154 /***********************************************************************
155 * SnmpUtilDbgPrint (SNMPAPI.@)
157 * NOTES
158 * The Microsoft headers claim this function uses the stdcall calling
159 * convention. But stdcall functions cannot take a variable number of
160 * arguments so this does not make sense. The stdcall specification is
161 * probably ignored by Microsoft's compiler in this case. So declare it
162 * correctly in Wine so it works with all compilers.
164 VOID WINAPIV SnmpUtilDbgPrint(INT loglevel, LPSTR format, ...)
166 FIXME("(%d, %s)\n", loglevel, debugstr_a(format));
169 /***********************************************************************
170 * SnmpUtilMemAlloc (SNMPAPI.@)
172 LPVOID WINAPI SnmpUtilMemAlloc(UINT nbytes)
174 TRACE("(%d)\n", nbytes);
175 return HeapAlloc(GetProcessHeap(), 0, nbytes);
178 /***********************************************************************
179 * SnmpUtilMemReAlloc (SNMPAPI.@)
181 LPVOID WINAPI SnmpUtilMemReAlloc(LPVOID mem, UINT nbytes)
183 TRACE("(%p, %d)\n", mem, nbytes);
184 return HeapReAlloc(GetProcessHeap(), 0, mem, nbytes);
187 /***********************************************************************
188 * SnmpUtilMemFree (SNMPAPI.@)
190 VOID WINAPI SnmpUtilMemFree(LPVOID mem)
192 TRACE("(%p)\n", mem);
193 HeapFree(GetProcessHeap(), 0, mem);
196 /***********************************************************************
197 * SnmpUtilAsnAnyCpy (SNMPAPI.@)
199 INT WINAPI SnmpUtilAsnAnyCpy(AsnAny *dst, AsnAny *src)
201 TRACE("(%p, %p)\n", dst, src);
202 return asn_any_copy(dst, src);
205 /***********************************************************************
206 * SnmpUtilAsnAnyFree (SNMPAPI.@)
208 VOID WINAPI SnmpUtilAsnAnyFree(AsnAny *any)
210 TRACE("(%p)\n", any);
211 asn_any_free(any);
214 /***********************************************************************
215 * SnmpUtilOctetsCpy (SNMPAPI.@)
217 INT WINAPI SnmpUtilOctetsCpy(AsnOctetString *dst, AsnOctetString *src)
219 TRACE("(%p, %p)\n", dst, src);
221 if (!dst) return SNMPAPI_ERROR;
222 if (!src)
224 dst->dynamic = FALSE;
225 dst->length = 0;
226 dst->stream = NULL;
227 return SNMPAPI_NOERROR;
229 if ((dst->stream = HeapAlloc(GetProcessHeap(), 0, src->length)))
231 unsigned int i;
233 dst->dynamic = TRUE;
234 dst->length = src->length;
235 for (i = 0; i < dst->length; i++) dst->stream[i] = src->stream[i];
236 return SNMPAPI_NOERROR;
238 return SNMPAPI_ERROR;
241 /***********************************************************************
242 * SnmpUtilOctetsFree (SNMPAPI.@)
244 VOID WINAPI SnmpUtilOctetsFree(AsnOctetString *octets)
246 TRACE("(%p)\n", octets);
248 if (octets)
250 octets->length = 0;
251 if (octets->dynamic) HeapFree(GetProcessHeap(), 0, octets->stream);
252 octets->stream = NULL;
253 octets->dynamic = FALSE;
257 /***********************************************************************
258 * SnmpUtilOctetsNCmp (SNMPAPI.@)
260 INT WINAPI SnmpUtilOctetsNCmp(AsnOctetString *octets1, AsnOctetString *octets2, UINT count)
262 INT ret;
263 unsigned int i;
265 TRACE("(%p, %p, %d)\n", octets1, octets2, count);
267 if (!octets1 || !octets2) return 0;
269 for (i = 0; i < count; i++)
270 if ((ret = octets1->stream[i] - octets2->stream[i])) return ret;
272 return 0;
275 /***********************************************************************
276 * SnmpUtilOctetsCmp (SNMPAPI.@)
278 INT WINAPI SnmpUtilOctetsCmp(AsnOctetString *octets1, AsnOctetString *octets2)
280 TRACE("(%p, %p)\n", octets1, octets2);
282 if (octets1->length < octets2->length) return -1;
283 if (octets1->length > octets2->length) return 1;
285 return SnmpUtilOctetsNCmp(octets1, octets2, octets1->length);
288 /***********************************************************************
289 * SnmpUtilOidAppend (SNMPAPI.@)
291 INT WINAPI SnmpUtilOidAppend(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
293 UINT *ids, i, size;
295 TRACE("(%p, %p)\n", dst, src);
297 if (!dst) return SNMPAPI_ERROR;
298 if (!src) return SNMPAPI_NOERROR;
300 size = (src->idLength + dst->idLength) * sizeof(UINT);
301 if (!(ids = HeapReAlloc(GetProcessHeap(), 0, dst->ids, size)))
303 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size)))
305 SetLastError(SNMP_MEM_ALLOC_ERROR);
306 return SNMPAPI_ERROR;
308 else memcpy(ids, dst->ids, dst->idLength * sizeof(UINT));
311 for (i = 0; i < src->idLength; i++) ids[i + dst->idLength] = src->ids[i];
312 dst->idLength = dst->idLength + src->idLength;
313 dst->ids = ids;
315 return SNMPAPI_NOERROR;
318 /***********************************************************************
319 * SnmpUtilOidCpy (SNMPAPI.@)
321 INT WINAPI SnmpUtilOidCpy(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
323 TRACE("(%p, %p)\n", dst, src);
325 if (!dst) return SNMPAPI_ERROR;
326 if (!src)
328 dst->idLength = 0;
329 dst->ids = NULL;
330 return SNMPAPI_NOERROR;
332 if ((dst->ids = HeapAlloc(GetProcessHeap(), 0, src->idLength * sizeof(UINT))))
334 unsigned int i;
336 dst->idLength = src->idLength;
337 for (i = 0; i < dst->idLength; i++) dst->ids[i] = src->ids[i];
338 return SNMPAPI_NOERROR;
340 return SNMPAPI_ERROR;
343 /***********************************************************************
344 * SnmpUtilOidFree (SNMPAPI.@)
346 VOID WINAPI SnmpUtilOidFree(AsnObjectIdentifier *oid)
348 TRACE("(%p)\n", oid);
350 if (!oid) return;
352 oid->idLength = 0;
353 HeapFree(GetProcessHeap(), 0, oid->ids);
354 oid->ids = NULL;
357 /***********************************************************************
358 * SnmpUtilOidNCmp (SNMPAPI.@)
360 INT WINAPI SnmpUtilOidNCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2, UINT count)
362 unsigned int i, len;
364 TRACE("(%p, %p, %d)\n", oid1, oid2, count);
366 if (!oid1 || !oid2) return 0;
368 len = min(count, oid1->idLength);
369 len = min(len, oid2->idLength);
370 for (i = 0; i < len; i++)
372 if (oid1->ids[i] > oid2->ids[i]) return 1;
373 if (oid1->ids[i] < oid2->ids[i]) return -1;
375 if (i == count) return 0;
376 if (oid1->idLength < oid2->idLength) return -1;
377 if (oid1->idLength > oid2->idLength) return 1;
378 return 0;
381 /***********************************************************************
382 * SnmpUtilOidCmp (SNMPAPI.@)
384 INT WINAPI SnmpUtilOidCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2)
386 TRACE("(%p, %p)\n", oid1, oid2);
388 if (oid1->idLength < oid2->idLength) return -1;
389 if (oid1->idLength > oid2->idLength) return 1;
391 return SnmpUtilOidNCmp(oid1, oid2, oid1->idLength);
394 /***********************************************************************
395 * SnmpUtilVarBindCpy (SNMPAPI.@)
397 INT WINAPI SnmpUtilVarBindCpy(SnmpVarBind *dst, SnmpVarBind *src)
399 unsigned int i, size;
401 TRACE("(%p, %p)\n", dst, src);
403 if (!dst) return SNMPAPI_ERROR;
404 if (!src)
406 dst->value.asnType = ASN_NULL;
407 return SNMPAPI_NOERROR;
410 size = src->name.idLength * sizeof(UINT);
411 if (!(dst->name.ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
413 for (i = 0; i < src->name.idLength; i++) dst->name.ids[i] = src->name.ids[i];
414 dst->name.idLength = src->name.idLength;
416 if (!asn_any_copy(&dst->value, &src->value))
418 HeapFree(GetProcessHeap(), 0, dst->name.ids);
419 return SNMPAPI_ERROR;
421 return SNMPAPI_NOERROR;
424 /***********************************************************************
425 * SnmpUtilVarBindFree (SNMPAPI.@)
427 VOID WINAPI SnmpUtilVarBindFree(SnmpVarBind *vb)
429 TRACE("(%p)\n", vb);
431 if (!vb) return;
433 asn_any_free(&vb->value);
434 HeapFree(GetProcessHeap(), 0, vb->name.ids);
435 vb->name.idLength = 0;
436 vb->name.ids = NULL;
439 /***********************************************************************
440 * SnmpUtilVarBindListCpy (SNMPAPI.@)
442 INT WINAPI SnmpUtilVarBindListCpy(SnmpVarBindList *dst, SnmpVarBindList *src)
444 unsigned int i, size;
445 SnmpVarBind *src_entry, *dst_entry;
447 TRACE("(%p, %p)\n", dst, src);
449 if (!src)
451 dst->list = NULL;
452 dst->len = 0;
453 return SNMPAPI_NOERROR;
455 size = src->len * sizeof(SnmpVarBind);
456 if (!(dst->list = HeapAlloc(GetProcessHeap(), 0, size)))
457 return SNMPAPI_ERROR;
459 src_entry = src->list;
460 dst_entry = dst->list;
461 for (i = 0; i < src->len; i++)
463 if (SnmpUtilVarBindCpy(dst_entry, src_entry))
465 src_entry++;
466 dst_entry++;
468 else
470 for (--i; i > 0; i--) SnmpUtilVarBindFree(--dst_entry);
471 HeapFree(GetProcessHeap(), 0, dst->list);
472 return SNMPAPI_ERROR;
475 dst->len = src->len;
476 return SNMPAPI_NOERROR;
479 /***********************************************************************
480 * SnmpUtilVarBindListFree (SNMPAPI.@)
482 VOID WINAPI SnmpUtilVarBindListFree(SnmpVarBindList *vb)
484 unsigned int i;
485 SnmpVarBind *entry;
487 TRACE("(%p)\n", vb);
489 entry = vb->list;
490 for (i = 0; i < vb->len; i++) SnmpUtilVarBindFree(entry++);
491 HeapFree(GetProcessHeap(), 0, vb->list);
492 vb->list = NULL;
493 vb->len = 0;
496 /***********************************************************************
497 * SnmpUtilIdsToA (SNMPAPI.@)
499 LPSTR WINAPI SnmpUtilIdsToA(UINT *ids, UINT length)
501 static char one[10], oid[514], null_oid[] = "<null oid>";
502 unsigned int i, len, left = sizeof(oid) - 1;
504 TRACE("(%p, %d)\n", ids, length);
506 if (!ids || !length) return null_oid;
508 *oid = 0;
509 for (i = 0; i < length; i++)
511 sprintf(one, "%d", ids[i]);
512 len = strlen(one);
513 if (left >= len)
515 strcat(oid, one);
516 left -= len;
518 else return oid;
520 if (i < length - 1)
522 if (left > 0)
524 strcat(oid, ".");
525 left--;
527 else return oid;
530 return oid;
533 /***********************************************************************
534 * SnmpUtilOidToA (SNMPAPI.@)
536 LPSTR WINAPI SnmpUtilOidToA(AsnObjectIdentifier *oid)
538 static char null_oid[] = "<null oid>";
540 TRACE("(%p)\n", oid);
542 if (oid)
543 return SnmpUtilIdsToA(oid->ids, oid->idLength);
544 else
545 return null_oid;
548 /***********************************************************************
549 * SnmpUtilPrintOid (SNMPAPI.@)
551 VOID WINAPI SnmpUtilPrintOid(AsnObjectIdentifier *oid)
553 unsigned int i;
555 TRACE("(%p)\n", oid);
557 if (!oid) return;
559 for (i = 0; i < oid->idLength; i++)
561 TRACE("%u", oid->ids[i]);
562 if (i < oid->idLength - 1) TRACE(".");
564 TRACE("\n");
567 /***********************************************************************
568 * SnmpUtilPrintAsnAny (SNMPAPI.@)
570 VOID WINAPI SnmpUtilPrintAsnAny(AsnAny *any)
572 unsigned int i;
574 TRACE("(%p)\n", any);
576 switch (any->asnType)
578 case ASN_NULL: TRACE("Null value\n"); return;
579 case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return;
580 case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return;
581 case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return;
582 case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return;
583 case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return;
584 case ASN_COUNTER64:
586 TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
587 return;
589 case ASN_OCTETSTRING:
591 TRACE("String ");
592 for (i = 0; i < any->asnValue.string.length; i++)
593 TRACE("%c", any->asnValue.string.stream[i]);
594 TRACE("\n");
595 return;
597 case ASN_IPADDRESS:
599 TRACE("IpAddress ");
600 if (any->asnValue.string.length < 4)
602 TRACE("Invalid\n");
603 return;
605 for (i = 0; i < 4; i++)
607 TRACE("%u", any->asnValue.string.stream[i]);
608 if (i < 3) TRACE(".");
610 TRACE("\n");
611 return;
613 case ASN_BITS:
615 TRACE("Bits ");
616 for (i = 0; i < any->asnValue.string.length; i++)
618 TRACE("0x%02x", any->asnValue.string.stream[i]);
619 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
621 TRACE("\n");
622 return;
624 case ASN_OPAQUE:
626 TRACE("Opaque ");
627 for (i = 0; i < any->asnValue.string.length; i++)
629 TRACE("0x%02x", any->asnValue.string.stream[i]);
630 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
632 TRACE("\n");
633 return;
635 case ASN_OBJECTIDENTIFIER:
637 TRACE("ObjectID ");
638 for (i = 0; i < any->asnValue.object.idLength; i++)
640 TRACE("%u", any->asnValue.object.ids[i]);
641 if (i < any->asnValue.object.idLength - 1) TRACE(".");
643 TRACE("\n");
644 return;
646 default:
648 TRACE("Invalid type %d\n", any->asnType);
649 return;