added quote character escape in function AgentGetParameterArg
[public/netxms.git] / include / nms_util.h
CommitLineData
cc022855 1/*
7ac71e60 2** NetXMS - Network Management System
6f95ef89 3** Copyright (C) 2003-2015 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
4b47d7d7
VK
48/*** Byte swapping ***/
49#if WORDS_BIGENDIAN
50#define htonq(x) (x)
51#define ntohq(x) (x)
52#define htond(x) (x)
53#define ntohd(x) (x)
54#define SwapWideString(x)
55#else
56#ifdef HAVE_HTONLL
57#define htonq(x) htonll(x)
58#else
59#define htonq(x) __bswap_64(x)
60#endif
61#ifdef HAVE_NTOHLL
62#define ntohq(x) ntohll(x)
63#else
64#define ntohq(x) __bswap_64(x)
65#endif
66#define htond(x) __bswap_double(x)
67#define ntohd(x) __bswap_double(x)
68#define SwapWideString(x) __bswap_wstr(x)
69#endif
6d8aee95 70
4b47d7d7
VK
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
76UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
77#endif
78#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
79UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
80#endif
81double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
82void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
6d8aee95 83
4b47d7d7
VK
84#ifdef __cplusplus
85}
86#endif
87
88
89/*** Serial communications ***/
6d8aee95
VK
90#ifdef _WIN32
91
92#define FLOW_NONE 0
93#define FLOW_SOFTWARE 1
94#define FLOW_HARDWARE 2
95
96#else /* _WIN32 */
97
70fed300
AK
98#ifdef HAVE_TERMIOS_H
99# include <termios.h>
100#else
101# error termios.h not found
102#endif
103
6d8aee95
VK
104#endif /* _WIN32 */
105
e2e66145
VK
106/**
107 * Return codes for IcmpPing()
108 */
66c04cc7
VK
109#define ICMP_SUCCESS 0
110#define ICMP_UNREACHEABLE 1
111#define ICMP_TIMEOUT 2
112#define ICMP_RAW_SOCK_FAILED 3
5418d4bb 113#define ICMP_API_ERROR 4
66c04cc7 114
e2e66145
VK
115/**
116 * Token types for configuration loader
117 */
4826c8a5
VK
118#define CT_LONG 0
119#define CT_STRING 1
120#define CT_STRING_LIST 2
121#define CT_END_OF_LIST 3
122#define CT_BOOLEAN 4
123#define CT_WORD 5
fc935a60 124#define CT_IGNORE 6
dda7c270 125#define CT_MB_STRING 7
e95680e5 126#define CT_BOOLEAN64 8
4826c8a5 127
e2e66145
VK
128/**
129 * Uninitialized value for override indicator
130 */
131#define NXCONFIG_UNINITIALIZED_VALUE (-1)
132
605fc935
VK
133/**
134 * Return codes for NxLoadConfig()
135 */
4826c8a5
VK
136#define NXCFG_ERR_OK 0
137#define NXCFG_ERR_NOFILE 1
138#define NXCFG_ERR_SYNTAX 2
139
605fc935
VK
140/**
141 * nxlog_open() flags
142 */
4e0e77e6
VK
143#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
144#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
145#define NXLOG_BACKGROUND_WRITER ((UINT32)0x00000004)
70be5423 146#define NXLOG_DEBUG_MODE ((UINT32)0x00000008)
4e0e77e6 147#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 148
605fc935
VK
149/**
150 * nxlog rotation policy
151 */
5945d50a
VK
152#define NXLOG_ROTATION_DISABLED 0
153#define NXLOG_ROTATION_DAILY 1
154#define NXLOG_ROTATION_BY_SIZE 2
155
605fc935
VK
156/**
157 * _tcsdup() replacement
158 */
8ff13508
VK
159#if defined(_WIN32) && defined(USE_WIN32_HEAP)
160#ifdef __cplusplus
161extern "C" {
162#endif
fd72502a 163char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
8ff13508
VK
164WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
165#ifdef __cplusplus
166}
167#endif
168#endif
169
f4d2c9ba
VK
170/**
171 * Custom wcsdup
172 */
173#if !HAVE_WCSDUP && !defined(_WIN32)
fd72502a 174
f4d2c9ba
VK
175#ifdef __cplusplus
176extern "C" {
177#endif
178 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
179#ifdef __cplusplus
180}
181#endif
fd72502a
VK
182
183#elif defined(_AIX)
184
185// Some AIX versions have broken wcsdup() so we use internal implementation
186#ifdef __cplusplus
187extern "C" {
188#endif
189 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
190#ifdef __cplusplus
191}
192#endif
193
194#define wcsdup nx_wcsdup
195
f4d2c9ba
VK
196#endif
197
c012b7b7
VK
198#ifdef __cplusplus
199
c83f6449
VK
200/**
201 * Extended tcsdup: returns NULL if NULL pointer passed
202 */
203inline TCHAR *_tcsdup_ex(const TCHAR *s)
204{
205 return (s != NULL) ? _tcsdup(s) : NULL;
206}
207
c012b7b7
VK
208#endif
209
d65c16a8
VK
210/**
211 * String conversion helpers
212 */
213#ifdef __cplusplus
214extern "C" {
215#endif
216WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
fe8ea784 217WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBStringSysLocale(const char *pszString);
d65c16a8
VK
218WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
219char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
fe8ea784 220char LIBNETXMS_EXPORTABLE *MBStringFromWideStringSysLocale(const WCHAR *pwszString);
d65c16a8
VK
221char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
222#ifdef __cplusplus
223}
224#endif
225
f38a7636 226#ifdef UNICODE
7fcdcb0c 227#define UTF8StringFromTString(s) UTF8StringFromWideString(s)
f38a7636 228#else
7fcdcb0c 229#define UTF8StringFromTString(s) UTF8StringFromMBString(s)
f38a7636
AK
230#endif
231
605fc935
VK
232/**
233 * Class for serial communications
234 */
b160350d
VK
235#ifdef __cplusplus
236
237#ifndef _WIN32
238enum
239{
240 NOPARITY,
241 ODDPARITY,
242 EVENPARITY,
243 ONESTOPBIT,
244 TWOSTOPBITS
245};
246
70fed300
AK
247enum
248{
249 FLOW_NONE,
250 FLOW_HARDWARE,
251 FLOW_SOFTWARE
252};
253
b160350d
VK
254#ifndef INVALID_HANDLE_VALUE
255#define INVALID_HANDLE_VALUE (-1)
256#endif
257#endif /* _WIN32 */
258
259class LIBNETXMS_EXPORTABLE Serial
260{
b160350d
VK
261private:
262 TCHAR *m_pszPort;
263 int m_nTimeout;
264 int m_nSpeed;
265 int m_nDataBits;
266 int m_nStopBits;
267 int m_nParity;
70fed300 268 int m_nFlowControl;
b160350d
VK
269
270#ifndef _WIN32
271 int m_hPort;
70fed300 272 struct termios m_originalSettings;
b160350d
VK
273#else
274 HANDLE m_hPort;
275#endif
b160350d 276
826fcc1f
VK
277public:
278 Serial();
279 ~Serial();
280
281 bool open(const TCHAR *pszPort);
282 void close();
283 void setTimeout(int nTimeout);
284 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
285 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 286 int readToMark(char *buff, int size, const char **marks, char **occurence);
826fcc1f
VK
287 bool write(const char *pBuff, int nSize);
288 void flush();
289 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
290 bool restart();
291};
ff214673 292
3fe40613
VK
293/**
294 * Dynamic string class
295 */
8bad8476
VK
296class LIBNETXMS_EXPORTABLE String
297{
298protected:
4e0e77e6
VK
299 TCHAR *m_buffer;
300 size_t m_length;
301 size_t m_allocated;
dcc7a2b0 302 size_t m_allocationStep;
8bad8476
VK
303
304public:
c577e360
VK
305 static const int npos;
306
8bad8476 307 String();
f3cdd1c5 308 String(const TCHAR *init);
643c9dcb 309 String(const String &src);
8bad8476
VK
310 ~String();
311
4e0e77e6
VK
312 TCHAR *getBuffer() { return m_buffer; }
313 void setBuffer(TCHAR *buffer);
8bad8476 314
dcc7a2b0
VK
315 size_t getAllocationStep() { return m_allocationStep; }
316 void setAllocationStep(size_t step) { m_allocationStep = step; }
4e0e77e6
VK
317
318 const String& operator =(const TCHAR *str);
49bf9d8d 319 const String& operator =(const String &src);
1d34c533
VK
320 const String& operator +=(const TCHAR *str);
321 const String& operator +=(const String &str);
5508ef9f 322 operator const TCHAR*() const { return CHECK_NULL_EX(m_buffer); }
8bad8476 323
08b214c6
VK
324 char *getUTF8String();
325
4e0e77e6
VK
326 void append(const TCHAR *str) { if (str != NULL) append(str, _tcslen(str)); }
327 void append(const TCHAR *str, size_t len);
f98a8402 328 void append(const TCHAR c) { append(&c, 1); }
5f648670
VK
329 void append(INT32 n);
330 void append(UINT32 n);
331 void append(INT64 n);
332 void append(UINT64 n);
4e0e77e6
VK
333
334 void appendPreallocated(TCHAR *str) { if (str != NULL) { append(str); free(str); } }
335
336 void appendMBString(const char *str, size_t len, int nCodePage);
337 void appendWideString(const WCHAR *str, size_t len);
c577e360 338
4e0e77e6
VK
339 void appendFormattedString(const TCHAR *format, ...);
340 void appendFormattedStringV(const TCHAR *format, va_list args);
ff175e91 341
4e0e77e6 342 void clear();
c577e360 343
4e0e77e6
VK
344 size_t length() const { return m_length; }
345 bool isEmpty() const { return m_length == 0; }
c577e360 346
7ac965a4 347 wchar_t charAt(size_t pos) const { return (pos < m_length) ? m_buffer[pos] : 0; }
807a4c40 348
4e0e77e6
VK
349 TCHAR *substring(int nStart, int nLen, TCHAR *pszBuffer = NULL);
350 int find(const TCHAR *str, int nStart = 0);
1e1450ef 351
6bcafa2d
VK
352 void escapeCharacter(int ch, int esc);
353 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
354 void trim();
355 void shrink(int chars = 1);
c577e360
VK
356};
357
df26f039
VK
358/**
359 * Abstract iterator class (to be implemented by actual iterable class)
360 */
361class LIBNETXMS_EXPORTABLE AbstractIterator
362{
363public:
c5e1f0bb
VK
364 virtual ~AbstractIterator();
365
df26f039
VK
366 virtual bool hasNext() = 0;
367 virtual void *next() = 0;
e0f7dbec 368 virtual void remove() = 0;
df26f039
VK
369};
370
371/**
372 * Iterator class (public interface for iteration)
373 */
374template <class T> class Iterator
375{
376private:
377 AbstractIterator *m_worker;
378
379public:
380 Iterator(AbstractIterator *worker) { m_worker = worker; }
381 ~Iterator() { delete m_worker; }
382
383 bool hasNext() { return m_worker->hasNext(); }
384 T *next() { return (T *)m_worker->next(); }
e0f7dbec 385 void remove() { m_worker->remove(); }
df26f039
VK
386};
387
400e55c4 388/**
f9861ff9
VK
389 * Dynamic array class
390 */
391class LIBNETXMS_EXPORTABLE Array
392{
393private:
394 int m_size;
395 int m_allocated;
396 int m_grow;
397 size_t m_elementSize;
398 void **m_data;
399 bool m_objectOwner;
400
401 void internalRemove(int index, bool allowDestruction);
402 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
403
404protected:
405 bool m_storePointers;
406 void (*m_objectDestructor)(void *);
407
408 Array(void *data, int initial, int grow, size_t elementSize);
dcc7a2b0 409 void *__getBuffer() const { return m_data; }
f9861ff9
VK
410
411public:
412 Array(int initial = 0, int grow = 16, bool owner = false);
413 virtual ~Array();
414
415 int add(void *element);
dcc7a2b0
VK
416 void *get(int index) const { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
417 int indexOf(void *element) const;
f9861ff9
VK
418 void set(int index, void *element);
419 void replace(int index, void *element);
420 void remove(int index) { internalRemove(index, true); }
421 void remove(void *element) { internalRemove(indexOf(element), true); }
422 void unlink(int index) { internalRemove(index, false); }
423 void unlink(void *element) { internalRemove(indexOf(element), false); }
424 void clear();
425 void sort(int (*cb)(const void *, const void *));
dcc7a2b0 426 void *find(const void *key, int (*cb)(const void *, const void *)) const;
f9861ff9 427
dcc7a2b0 428 int size() const { return m_size; }
f9861ff9
VK
429
430 void setOwner(bool owner) { m_objectOwner = owner; }
dcc7a2b0 431 bool isOwner() const { return m_objectOwner; }
f9861ff9
VK
432};
433
df26f039
VK
434/**
435 * Array iterator class
436 */
437class LIBNETXMS_EXPORTABLE ArrayIterator : public AbstractIterator
438{
439private:
e0f7dbec 440 Array *m_array;
df26f039
VK
441 int m_pos;
442
443public:
e0f7dbec 444 ArrayIterator(Array *array);
df26f039
VK
445
446 virtual bool hasNext();
447 virtual void *next();
e0f7dbec 448 virtual void remove();
df26f039
VK
449};
450
f9861ff9
VK
451/**
452 * Template class for dynamic array which holds objects
453 */
454template <class T> class ObjectArray : public Array
455{
456private:
457 static void destructor(void *object) { delete (T*)object; }
458
459public:
460 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
461 virtual ~ObjectArray() { }
462
463 int add(T *object) { return Array::add((void *)object); }
dcc7a2b0
VK
464 T *get(int index) const { return (T*)Array::get(index); }
465 int indexOf(T *object) const { return Array::indexOf((void *)object); }
dfed892b 466 bool contains(T *object) const { return indexOf(object) >= 0; }
f9861ff9
VK
467 void set(int index, T *object) { Array::set(index, (void *)object); }
468 void replace(int index, T *object) { Array::replace(index, (void *)object); }
469 void remove(int index) { Array::remove(index); }
470 void remove(T *object) { Array::remove((void *)object); }
471 void unlink(int index) { Array::unlink(index); }
472 void unlink(T *object) { Array::unlink((void *)object); }
df26f039 473
e0f7dbec 474 Iterator<T> *iterator() { return new Iterator<T>(new ArrayIterator(this)); }
f9861ff9
VK
475};
476
477/**
478 * Template class for dynamic array which holds scalar values
479 */
480template <class T> class IntegerArray : public Array
481{
482private:
483 static void destructor(void *element) { }
484
485public:
486 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
487 virtual ~IntegerArray() { }
488
489 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
e5390fb5 490 T get(int index) const { if (m_storePointers) return CAST_FROM_POINTER(Array::get(index), T); T *p = (T*)Array::get(index); return (p != NULL) ? *p : 0; }
dcc7a2b0 491 int indexOf(T value) const { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
dfed892b 492 bool contains(T value) const { return indexOf(value) >= 0; }
f9861ff9
VK
493 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
494 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
495
dcc7a2b0 496 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
497};
498
499/**
500 * Auxilliary class to hold dynamically allocated array of structures
501 */
502template <class T> class StructArray : public Array
503{
504private:
505 static void destructor(void *element) { }
506
507public:
508 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
509 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
510 virtual ~StructArray() { }
511
512 int add(T *element) { return Array::add((void *)element); }
dcc7a2b0
VK
513 T *get(int index) const { return (T*)Array::get(index); }
514 int indexOf(T *element) const { return Array::indexOf((void *)element); }
dfed892b 515 bool contains(T *element) const { return indexOf(element) >= 0; }
f9861ff9
VK
516 void set(int index, T *element) { Array::set(index, (void *)element); }
517 void replace(int index, T *element) { Array::replace(index, (void *)element); }
518 void remove(int index) { Array::remove(index); }
519 void remove(T *element) { Array::remove((void *)element); }
520 void unlink(int index) { Array::unlink(index); }
521 void unlink(T *element) { Array::unlink((void *)element); }
522
dcc7a2b0 523 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
524};
525
526/**
527 * Entry of string map (internal)
400e55c4
VK
528 */
529struct StringMapEntry;
530
f9861ff9
VK
531/**
532 * Key/value pair
533 */
534struct KeyValuePair
535{
536 const TCHAR *key;
537 const void *value;
538};
539
3fe40613
VK
540/**
541 * String maps base class
542 */
543class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
544{
545protected:
400e55c4 546 StringMapEntry *m_data;
3fe40613 547 bool m_objectOwner;
55bdca5a 548 bool m_ignoreCase;
3fe40613 549 void (*m_objectDestructor)(void *);
c577e360 550
6e14de7c 551 StringMapEntry *find(const TCHAR *key) const;
3fe40613 552 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
6e14de7c 553 void *getObject(const TCHAR *key) const;
3fe40613 554 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
555
556public:
3fe40613
VK
557 StringMapBase(bool objectOwner);
558 virtual ~StringMapBase();
559
55bdca5a 560 void setOwner(bool owner) { m_objectOwner = owner; }
91a581c5 561 void setIgnoreCase(bool ignore);
55bdca5a 562
3fe40613
VK
563 void remove(const TCHAR *key);
564 void clear();
c07cf3be 565 void filterElements(bool (*filter)(const TCHAR *, const void *, void *), void *userData);
3fe40613 566
6e14de7c
VK
567 int size() const;
568 bool contains(const TCHAR *key) const { return find(key) != NULL; }
400e55c4 569
6e14de7c
VK
570 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const TCHAR *, const void *, void *), void *userData) const;
571 const void *findElement(bool (*comparator)(const TCHAR *, const void *, void *), void *userData) const;
f9861ff9 572
6e14de7c
VK
573 StructArray<KeyValuePair> *toArray() const;
574 StringList *keys() const;
3fe40613
VK
575};
576
400e55c4
VK
577/**
578 * NXCP message class
579 */
b368969c 580class NXCPMessage;
400e55c4 581
3fe40613
VK
582/**
583 * String map class
584 */
585class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
586{
587public:
588 StringMap() : StringMapBase(true) { }
fb986055 589 StringMap(const StringMap &src);
3fe40613 590 virtual ~StringMap();
c577e360 591
fb986055 592 StringMap& operator =(const StringMap &src);
40b7416c 593
3fe40613
VK
594 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
595 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 596 void set(const TCHAR *key, UINT32 value);
5ad2167d 597
a6312bd6
VK
598 void addAll(StringMap *src);
599
6e14de7c 600 const TCHAR *get(const TCHAR *key) const { return (const TCHAR *)getObject(key); }
7ec22f81
VK
601 INT32 getInt32(const TCHAR *key, INT32 defaultValue) const;
602 UINT32 getUInt32(const TCHAR *key, UINT32 defaultValue) const;
603 INT64 getInt64(const TCHAR *key, INT64 defaultValue) const;
604 UINT64 getUInt64(const TCHAR *key, UINT64 defaultValue) const;
605 double getDouble(const TCHAR *key, double defaultValue) const;
6e14de7c 606 bool getBoolean(const TCHAR *key, bool defaultValue) const;
5ad2167d 607
6e14de7c 608 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
ca0a165b 609 void loadMessage(const NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId);
8bad8476
VK
610};
611
3fe40613
VK
612/**
613 * String map template for holding objects as values
614 */
615template <class T> class StringObjectMap : public StringMapBase
616{
617private:
618 static void destructor(void *object) { delete (T*)object; }
6173bea8 619
3fe40613
VK
620public:
621 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
622
623 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
b7c8729b 624 void setPreallocated(TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, true); }
6e14de7c 625 T *get(const TCHAR *key) const { return (T*)getObject(key); }
3fe40613 626};
6173bea8 627
3fe40613
VK
628/**
629 * String list class
630 */
6173bea8
VK
631class LIBNETXMS_EXPORTABLE StringList
632{
633private:
634 int m_count;
635 int m_allocated;
636 TCHAR **m_values;
637
638public:
639 StringList();
01116597 640 StringList(const StringList *src);
22ee6d97 641 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
642 ~StringList();
643
644 void add(const TCHAR *value);
645 void addPreallocated(TCHAR *value);
967893bb
VK
646 void add(INT32 value);
647 void add(UINT32 value);
6173bea8 648 void add(INT64 value);
967893bb 649 void add(UINT64 value);
6173bea8 650 void add(double value);
24dc5346 651 void replace(int index, const TCHAR *value);
b0353a33
VK
652 void addOrReplace(int index, const TCHAR *value);
653 void addOrReplacePreallocated(int index, TCHAR *value);
e2dc0372
VK
654#ifdef UNICODE
655 void addMBString(const char *value) { addPreallocated(WideStringFromMBString(value)); }
656#else
657 void addMBString(const char *value) { add(value); }
658#endif
6173bea8 659 void clear();
01116597
VK
660 int size() const { return m_count; }
661 const TCHAR *get(int index) const { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
662 int indexOf(const TCHAR *value) const;
663 int indexOfIgnoreCase(const TCHAR *value) const;
d51f2182 664 void remove(int index);
86c126f5 665 void addAll(const StringList *src);
93dbc1be 666 void merge(const StringList *src, bool matchCase);
7d25a5c5 667 TCHAR *join(const TCHAR *separator);
22ee6d97 668 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
7d37e727 669 void sort(bool ascending = true, bool caseSensitive = false);
6fbaa926 670 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
6173bea8
VK
671};
672
a58501c0
VK
673/**
674 * Entry of string set
675 */
676struct StringSetEntry;
677
f3603b79
VK
678/**
679 * NXCP message
680 */
b368969c 681class NXCPMessage;
f3603b79 682
8100b554
VK
683/**
684 * String set
685 */
686class StringSet;
687
688/**
689 * String set iterator
690 */
691class LIBNETXMS_EXPORTABLE StringSetIterator : public AbstractIterator
692{
693private:
694 StringSet *m_stringSet;
695 StringSetEntry *m_curr;
696 StringSetEntry *m_next;
697
698public:
699 StringSetIterator(StringSet *stringSet);
700
701 virtual bool hasNext();
702 virtual void *next();
703 virtual void remove();
704};
705
a58501c0
VK
706/**
707 * String set class
708 */
709class LIBNETXMS_EXPORTABLE StringSet
710{
8100b554
VK
711 friend class StringSetIterator;
712
a58501c0
VK
713private:
714 StringSetEntry *m_data;
715
716public:
717 StringSet();
718 ~StringSet();
719
720 void add(const TCHAR *str);
f3603b79 721 void addPreallocated(TCHAR *str);
a58501c0
VK
722 void remove(const TCHAR *str);
723 void clear();
724
8100b554
VK
725 int size() const;
726 bool contains(const TCHAR *str) const;
727 bool equals(const StringSet *s) const;
a58501c0 728
fa493140 729 void addAll(StringSet *src);
f3603b79 730 void addAll(TCHAR **strings, int count);
d45f9179 731 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
d6c4cdc1 732 void addAllPreallocated(TCHAR **strings, int count);
f3603b79 733
8100b554
VK
734 void forEach(bool (*cb)(const TCHAR *, void *), void *userData) const;
735
736 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId) const;
737 void addAllFromMessage(const NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
c262bc08 738
d45f9179 739 String join(const TCHAR *separator);
8100b554
VK
740
741 Iterator<const TCHAR> *iterator() { return new Iterator<const TCHAR>(new StringSetIterator(this)); }
a58501c0
VK
742};
743
296ae03d
VK
744/**
745 * Opaque hash map entry structure
746 */
747struct HashMapEntry;
748
749/**
750 * Hash map base class (for fixed size non-pointer keys)
751 */
752class LIBNETXMS_EXPORTABLE HashMapBase
753{
df26f039
VK
754 friend class HashMapIterator;
755
296ae03d
VK
756private:
757 HashMapEntry *m_data;
758 bool m_objectOwner;
759 unsigned int m_keylen;
760
df26f039 761 HashMapEntry *find(const void *key) const;
296ae03d
VK
762 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
763
764protected:
765 void (*m_objectDestructor)(void *);
766
767 HashMapBase(bool objectOwner, unsigned int keylen);
768
df26f039 769 void *_get(const void *key) const;
296ae03d
VK
770 void _set(const void *key, void *value);
771 void _remove(const void *key);
772
df26f039 773 bool _contains(const void *key) const { return find(key) != NULL; }
296ae03d
VK
774
775public:
776 virtual ~HashMapBase();
777
778 void setOwner(bool owner) { m_objectOwner = owner; }
779 void clear();
780
df26f039
VK
781 int size() const;
782
783 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const void *, const void *, void *), void *userData) const;
784 const void *findElement(bool (*comparator)(const void *, const void *, void *), void *userData) const;
785};
296ae03d 786
df26f039
VK
787/**
788 * Hash map iterator
789 */
790class LIBNETXMS_EXPORTABLE HashMapIterator : public AbstractIterator
791{
792private:
793 HashMapBase *m_hashMap;
794 HashMapEntry *m_curr;
795 HashMapEntry *m_next;
796
797public:
798 HashMapIterator(HashMapBase *hashMap);
799
800 virtual bool hasNext();
801 virtual void *next();
e0f7dbec 802 virtual void remove();
296ae03d
VK
803};
804
805/**
806 * Hash map template for holding objects as values
807 */
808template <class K, class V> class HashMap : public HashMapBase
809{
810private:
811 static void destructor(void *object) { delete (V*)object; }
812
813public:
814 HashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
815
816 V *get(const K& key) { return (V*)_get(&key); }
817 void set(const K& key, V *value) { _set(&key, (void *)value); }
818 void remove(const K& key) { _remove(&key); }
819 bool contains(const K& key) { return _contains(&key); }
df26f039
VK
820
821 Iterator<V> *iterator() { return new Iterator<V>(new HashMapIterator(this)); }
296ae03d
VK
822};
823
50963ced
VK
824/**
825 * Hash map template for holding reference counting objects as values
826 */
827template <class K, class V> class RefCountHashMap : public HashMapBase
828{
829private:
87c91211 830 static void destructor(void *object) { if (object != NULL) ((V*)object)->decRefCount(); }
50963ced
VK
831
832public:
833 RefCountHashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
834
87c91211
VK
835 V *get(const K& key) { V *v = (V*)_get(&key); if (v != NULL) v->incRefCount(); return v; }
836 void set(const K& key, V *value) { if (value != NULL) value->incRefCount(); _set(&key, (void *)value); }
50963ced
VK
837 void remove(const K& key) { _remove(&key); }
838 bool contains(const K& key) { return _contains(&key); }
839
840 Iterator<V> *iterator() { return new Iterator<V>(new HashMapIterator(this)); }
841};
842
57f34c0a
VK
843/**
844 * Byte stream
845 */
846class LIBNETXMS_EXPORTABLE ByteStream
847{
848private:
849 BYTE *m_data;
850 size_t m_size;
851 size_t m_allocated;
852 size_t m_pos;
3cf3dedd 853 size_t m_allocationStep;
57f34c0a
VK
854
855public:
856 ByteStream(size_t initial = 8192);
857 ByteStream(const void *data, size_t size);
858 virtual ~ByteStream();
859
4b47d7d7
VK
860 static ByteStream *load(const TCHAR *file);
861
862 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
57f34c0a
VK
863 size_t pos() { return m_pos; }
864 size_t size() { return m_size; }
865 bool eos() { return m_pos == m_size; }
866
3cf3dedd
VK
867 void setAllocationStep(size_t s) { m_allocationStep = s; }
868
869 const BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
57f34c0a 870
4b47d7d7 871 void write(const void *data, size_t size);
57f34c0a
VK
872 void write(char c) { write(&c, 1); }
873 void write(BYTE b) { write(&b, 1); }
874 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
875 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
876 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
877 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
4b47d7d7
VK
878 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
879 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
880 void write(double n) { double x = htond(n); write(&x, 8); }
881 void writeString(const TCHAR *s);
57f34c0a 882
4b47d7d7 883 size_t read(void *buffer, size_t count);
57f34c0a
VK
884 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
885 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
886 INT16 readInt16();
887 UINT16 readUInt16();
888 INT32 readInt32();
889 UINT32 readUInt32();
4b47d7d7
VK
890 INT64 readInt64();
891 UINT64 readUInt64();
892 double readDouble();
893 TCHAR *readString();
894
895 bool save(int f);
57f34c0a
VK
896};
897
25fe6265
VK
898/**
899 * Auxilliary class for objects which counts references and
900 * destroys itself wheren reference count falls to 0
901 */
7f632dfe
VK
902class LIBNETXMS_EXPORTABLE RefCountObject
903{
904private:
7785322f 905 VolatileCounter m_refCount;
7f632dfe 906
7dd6369a
VK
907protected:
908 virtual ~RefCountObject();
909
7f632dfe 910public:
7785322f
VK
911 RefCountObject()
912 {
913 m_refCount = 1;
914 }
915
916 void incRefCount()
917 {
918 InterlockedIncrement(&m_refCount);
919 }
920
921 void decRefCount()
922 {
923 if (InterlockedDecrement(&m_refCount) == 0)
924 delete this;
925 }
7f632dfe
VK
926};
927
b368969c 928class NXCPMessage;
967893bb
VK
929
930/**
931 * Table column definition
932 */
933class LIBNETXMS_EXPORTABLE TableColumnDefinition
934{
935private:
936 TCHAR *m_name;
937 TCHAR *m_displayName;
938 INT32 m_dataType;
939 bool m_instanceColumn;
940
941public:
942 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
b368969c 943 TableColumnDefinition(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
944 TableColumnDefinition(TableColumnDefinition *src);
945 ~TableColumnDefinition();
946
b368969c 947 void fillMessage(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
948
949 const TCHAR *getName() { return m_name; }
950 const TCHAR *getDisplayName() { return m_displayName; }
951 INT32 getDataType() { return m_dataType; }
952 bool isInstanceColumn() { return m_instanceColumn; }
953
954 void setDataType(INT32 type) { m_dataType = type; }
955 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 956 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
957};
958
d6bbfa4e
VK
959/**
960 * Table cell
961 */
962class TableCell
963{
964private:
965 TCHAR *m_value;
966 int m_status;
43666be9 967 UINT32 m_objectId;
d6bbfa4e
VK
968
969public:
43666be9
VK
970 TableCell() { m_value = NULL; m_status = -1; m_objectId = 0; }
971 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; m_objectId = 0; }
972 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; m_objectId = 0; }
973 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; m_objectId = src->m_objectId; }
d6bbfa4e
VK
974 ~TableCell() { safe_free(m_value); }
975
43666be9
VK
976 void set(const TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; m_objectId = objectId; }
977 void setPreallocated(TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = value; m_status = status; m_objectId = objectId; }
d6bbfa4e
VK
978
979 const TCHAR *getValue() { return m_value; }
c83f6449 980 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
e40e0fdc 981 void setPreallocatedValue(TCHAR *value) { free(m_value); m_value = value; }
d6bbfa4e
VK
982
983 int getStatus() { return m_status; }
984 void setStatus(int status) { m_status = status; }
43666be9
VK
985
986 int getObjectId() { return m_objectId; }
987 void setObjectId(UINT32 id) { m_objectId = id; }
d6bbfa4e
VK
988};
989
990/**
991 * Table row
992 */
993class TableRow
994{
995private:
996 ObjectArray<TableCell> *m_cells;
997 UINT32 m_objectId;
bac74bf7 998
d6bbfa4e
VK
999public:
1000 TableRow(int columnCount);
1001 TableRow(TableRow *src);
1002 ~TableRow() { delete m_cells; }
1003
1004 void addColumn() { m_cells->add(new TableCell); }
1005 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 1006
43666be9
VK
1007 void set(int index, const TCHAR *value, int status, UINT32 objectId) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status, objectId); }
1008 void setPreallocated(int index, TCHAR *value, int status, UINT32 objectId) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status, objectId); }
d6bbfa4e
VK
1009
1010 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
e40e0fdc 1011 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); else free(value); }
d6bbfa4e
VK
1012
1013 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
1014
1015 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
1016 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
1017
1018 UINT32 getObjectId() { return m_objectId; }
1019 void setObjectId(UINT32 id) { m_objectId = id; }
43666be9
VK
1020
1021 UINT32 getCellObjectId(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getObjectId() : 0; }
1022 void setCellObjectId(int index, UINT32 id) { TableCell *c = m_cells->get(index); if (c != NULL) c->setObjectId(id); }
d6bbfa4e
VK
1023};
1024
967893bb
VK
1025/**
1026 * Class for table data storage
1027 */
a0ddfb29 1028class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
1029{
1030private:
d6bbfa4e 1031 ObjectArray<TableRow> *m_data;
967893bb
VK
1032 ObjectArray<TableColumnDefinition> *m_columns;
1033 TCHAR *m_title;
1034 int m_source;
d6bbfa4e 1035 bool m_extendedFormat;
967893bb 1036
b368969c 1037 void createFromMessage(NXCPMessage *msg);
967893bb 1038 void destroy();
63ff3c9d 1039 bool parseXML(const char *xml);
967893bb
VK
1040
1041public:
1042 Table();
a0ddfb29 1043 Table(Table *src);
b368969c 1044 Table(NXCPMessage *msg);
b549a0f8 1045 virtual ~Table();
967893bb 1046
b368969c
VK
1047 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
1048 void updateFromMessage(NXCPMessage *msg);
967893bb 1049
a0ddfb29
VK
1050 void addAll(Table *src);
1051 void copyRow(Table *src, int row);
967893bb 1052
d6bbfa4e
VK
1053 int getNumRows() { return m_data->size(); }
1054 int getNumColumns() { return m_columns->size(); }
967893bb
VK
1055 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
1056 int getSource() { return m_source; }
1057
d6bbfa4e
VK
1058 bool isExtendedFormat() { return m_extendedFormat; }
1059 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
1060
967893bb
VK
1061 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
1062 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
1063 int getColumnIndex(const TCHAR *name);
1064 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
1065
c83f6449 1066 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
967893bb 1067 void setSource(int source) { m_source = source; }
8dba1eac 1068 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
1069 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
1070 int addRow();
1071
25076d53
VK
1072 void deleteRow(int row);
1073 void deleteColumn(int col);
1074
967893bb
VK
1075 void setAt(int nRow, int nCol, INT32 nData);
1076 void setAt(int nRow, int nCol, UINT32 dwData);
1077 void setAt(int nRow, int nCol, double dData);
1078 void setAt(int nRow, int nCol, INT64 nData);
1079 void setAt(int nRow, int nCol, UINT64 qwData);
1080 void setAt(int nRow, int nCol, const TCHAR *pszData);
1081 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
1082
d6bbfa4e
VK
1083 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
1084 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
1085 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
1086 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
1087 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
d65c16a8
VK
1088 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
1089#ifdef UNICODE
1090 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
1091#else
1092 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
1093#endif
1094 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
d6bbfa4e
VK
1095
1096 void setStatusAt(int row, int col, int status);
1097 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb
VK
1098
1099 const TCHAR *getAsString(int nRow, int nCol);
1100 INT32 getAsInt(int nRow, int nCol);
1101 UINT32 getAsUInt(int nRow, int nCol);
1102 INT64 getAsInt64(int nRow, int nCol);
1103 UINT64 getAsUInt64(int nRow, int nCol);
1104 double getAsDouble(int nRow, int nCol);
22aaa779 1105
d6bbfa4e
VK
1106 int getStatus(int nRow, int nCol);
1107
22aaa779 1108 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 1109 int findRowByInstance(const TCHAR *instance);
d6bbfa4e
VK
1110
1111 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
1112 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
a3d3f9d5 1113
43666be9
VK
1114 void setCellObjectIdAt(int row, int col, UINT32 objectId);
1115 void setCellObjectId(int col, UINT32 objectId) { setCellObjectIdAt(getNumRows() - 1, col, objectId); }
1116 UINT32 getCellObjectId(int row, int col) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getCellObjectId(col) : 0; }
1117
63ff3c9d
VK
1118 static Table *createFromXML(const char *xml);
1119 TCHAR *createXML();
967893bb
VK
1120};
1121
c11eee9b
VK
1122/**
1123 * sockaddr buffer
1124 */
1125union SockAddrBuffer
1126{
1127 struct sockaddr_in sa4;
1128#ifdef WITH_IPV6
1129 struct sockaddr_in6 sa6;
1130#endif
1131};
1132
1133/**
1134 * sockaddr length calculation
1135 */
1136#ifdef WITH_IPV6
1137#define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
1138#else
1139#define SA_LEN(sa) sizeof(struct sockaddr_in)
1140#endif
1141
5f6e8d09
VK
1142/**
1143 * Get port number from SockAddrBuffer
1144 */
1145#ifdef WITH_IPV6
1146#define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
1147#else
1148#define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
1149#endif
1150
30592fa6
VK
1151/**
1152 * Compare addresses in sockaddr
1153 */
1154inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
1155{
1156 if (a1->sa_family != a2->sa_family)
1157 return false;
1158 if (a1->sa_family == AF_INET)
1159 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
1160#ifdef WITH_IPV6
1161 if (a1->sa_family == AF_INET6)
1162 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
1163#endif
1164 return false;
1165}
1166
9319c166
VK
1167/**
1168 * IP address
1169 */
1170class LIBNETXMS_EXPORTABLE InetAddress
1171{
9319c166 1172private:
8c75ad41
VK
1173 short m_maskBits;
1174 short m_family;
1175 union
1176 {
1177 UINT32 v4;
1178 BYTE v6[16];
1179 } m_addr;
9319c166
VK
1180
1181public:
8c75ad41
VK
1182 InetAddress();
1183 InetAddress(UINT32 addr);
c75e9ee4 1184 InetAddress(UINT32 addr, UINT32 mask);
4203dcb5 1185 InetAddress(const BYTE *addr, int maskBits = 128);
9319c166 1186
8c75ad41
VK
1187 bool isAnyLocal() const;
1188 bool isLoopback() const;
1189 bool isMulticast() const;
1190 bool isBroadcast() const;
4203dcb5 1191 bool isLinkLocal() const;
8c75ad41 1192 bool isValid() const { return m_family != AF_UNSPEC; }
4203dcb5 1193 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
9319c166 1194
8c75ad41 1195 int getFamily() const { return m_family; }
c7853753
VK
1196 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
1197 const BYTE *getAddressV6() const { return (m_family == AF_INET6) ? m_addr.v6 : (const BYTE *)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; }
9319c166 1198
8c75ad41 1199 bool contain(const InetAddress &a) const;
43709a0c 1200 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
1201 bool equals(const InetAddress &a) const;
1202 int compareTo(const InetAddress &a) const;
9319c166 1203
8c75ad41
VK
1204 void setMaskBits(int m) { m_maskBits = m; }
1205 int getMaskBits() const { return m_maskBits; }
c75e9ee4 1206 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
9319c166 1207
463b1d34 1208 InetAddress getSubnetAddress() const;
ba889094 1209 InetAddress getSubnetBroadcast() const;
463b1d34 1210 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 1211
8c75ad41
VK
1212 String toString() const;
1213 TCHAR *toString(TCHAR *buffer) const;
af4a09df
VK
1214#ifdef UNICODE
1215 char *toStringA(char *buffer) const;
1216#else
1217 char *toStringA(char *buffer) const { return toString(buffer); }
1218#endif
9319c166 1219
43709a0c 1220 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 1221
43709a0c
VK
1222 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
1223
1224 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 1225
c7853753
VK
1226 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
1227 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
1228 static InetAddress parse(const WCHAR *str);
1229 static InetAddress parse(const char *str);
8c75ad41 1230 static InetAddress createFromSockaddr(struct sockaddr *s);
dcc7a2b0
VK
1231
1232 static const InetAddress INVALID;
48313f37 1233 static const InetAddress LOOPBACK;
dcc7a2b0
VK
1234};
1235
1236/**
1237 * IP address list
1238 */
1239class LIBNETXMS_EXPORTABLE InetAddressList
1240{
1241private:
1242 ObjectArray<InetAddress> *m_list;
1243
1244 int indexOf(const InetAddress& addr) const;
1245
1246public:
1247 InetAddressList();
1248 ~InetAddressList();
1249
1250 void add(const InetAddress& addr);
c30c0c0f
VK
1251 void add(const InetAddressList& addrList);
1252 void replace(const InetAddress& addr);
dcc7a2b0 1253 void remove(const InetAddress& addr);
c30c0c0f 1254 void clear() { m_list->clear(); }
4203dcb5 1255 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
dcc7a2b0
VK
1256
1257 int size() const { return m_list->size(); }
1258 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
4203dcb5 1259 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
c34b611c 1260 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
dcc7a2b0 1261 const InetAddress& getFirstUnicastAddress() const;
c34b611c
VK
1262 const InetAddress& getFirstUnicastAddressV4() const;
1263 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
c30c0c0f 1264 bool isLoopbackOnly() const;
dcc7a2b0
VK
1265
1266 const ObjectArray<InetAddress> *getList() const { return m_list; }
40c8f341
VK
1267
1268 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
9319c166
VK
1269};
1270
25fe6265
VK
1271/**
1272 * Network connection
1273 */
58ee2f43
VK
1274class LIBNETXMS_EXPORTABLE SocketConnection
1275{
1276protected:
1277 SOCKET m_socket;
1278 char m_data[4096];
1279 int m_dataPos;
1280
1281public:
1282 SocketConnection();
1283 virtual ~SocketConnection();
1284
967893bb
VK
1285 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1286 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
1287 void disconnect();
1288
967893bb
VK
1289 bool canRead(UINT32 timeout);
1290 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 1291 bool waitForText(const char *text, int timeout);
cc022855 1292
58ee2f43 1293 int write(const char *pBuff, int nSize);
1671f85d
VK
1294 bool writeLine(const char *line);
1295
967893bb 1296 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
1297};
1298
030a0779
VK
1299/**
1300 * Telnet connection - handles all telnet negotiation
1301 */
def5b792
AK
1302class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1303{
030a0779 1304protected:
967893bb
VK
1305 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1306 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 1307
def5b792 1308public:
967893bb 1309 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 1310
967893bb
VK
1311 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1312 bool connect(UINT32 ip, WORD port, UINT32 timeout);
1313 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1314 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
1315};
1316
56fa1092
VK
1317/**
1318 * Postal address representation
1319 */
1320class LIBNETXMS_EXPORTABLE PostalAddress
1321{
1322private:
1323 TCHAR *m_country;
1324 TCHAR *m_city;
1325 TCHAR *m_streetAddress;
1326 TCHAR *m_postcode;
1327
1328public:
1329 PostalAddress();
1330 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1331 ~PostalAddress();
1332
63e99e56
VK
1333 const TCHAR *getCountry() const { return CHECK_NULL_EX(m_country); }
1334 const TCHAR *getCity() const { return CHECK_NULL_EX(m_city); }
1335 const TCHAR *getStreetAddress() const { return CHECK_NULL_EX(m_streetAddress); }
1336 const TCHAR *getPostCode() const { return CHECK_NULL_EX(m_postcode); }
56fa1092
VK
1337
1338 void setCountry(const TCHAR *country) { safe_free(m_country); m_country = _tcsdup_ex(country); }
1339 void setCity(const TCHAR *city) { safe_free(m_city); m_city = _tcsdup_ex(city); }
1340 void setStreetAddress(const TCHAR *streetAddress) { safe_free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1341 void setPostCode(const TCHAR *postcode) { safe_free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1342};
1343
b160350d
VK
1344#endif /* __cplusplus */
1345
030a0779
VK
1346/**
1347 * Configuration item template for configuration loader
1348 */
4826c8a5
VK
1349typedef struct
1350{
e2e66145
VK
1351 TCHAR token[64];
1352 BYTE type;
1353 BYTE separator; // Separator character for lists
1354 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1355 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1356 UINT32 bufferPos; // Should be set to 0
1357 void *buffer;
1358 void *overrideIndicator;
4826c8a5 1359} NX_CFG_TEMPLATE;
dcb44c9a 1360
030a0779
VK
1361/**
1362 * Code translation structure
1363 */
1b56f3c5 1364typedef struct __CODE_TO_TEXT
bc858635
VK
1365{
1366 int code;
1367 const TCHAR *text;
1368} CODE_TO_TEXT;
1369
e2e66145
VK
1370/**
1371 * getopt() prototype if needed
1372 */
df9812a9 1373#if USE_BUNDLED_GETOPT
99e50bb7 1374#include <netxms_getopt.h>
3046f9a4
VK
1375#endif
1376
3d1cb7d6
VK
1377//
1378// Structures for opendir() / readdir() / closedir()
1379//
1380
1381#ifdef _WIN32
1382
7f9e0c00 1383typedef struct dirent
3d1cb7d6
VK
1384{
1385 long d_ino; /* inode number (not used by MS-DOS) */
1386 int d_namlen; /* Name length */
1387 char d_name[257]; /* file name */
1388} _DIRECT;
1389
1390typedef struct _dir_struc
1391{
1392 char *start; /* Starting position */
1393 char *curr; /* Current position */
1394 long size; /* Size of string table */
1395 long nfiles; /* number if filenames in table */
7f9e0c00 1396 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1397} DIR;
1398
5ea2db30
VK
1399#ifdef UNICODE
1400
1401typedef struct dirent_w
1402{
1403 long d_ino; /* inode number (not used by MS-DOS) */
1404 int d_namlen; /* Name length */
1405 WCHAR d_name[257]; /* file name */
1406} _DIRECTW;
1407
1408typedef struct _dir_struc_w
1409{
1410 WCHAR *start; /* Starting position */
1411 WCHAR *curr; /* Current position */
1412 long size; /* Size of string table */
1413 long nfiles; /* number if filenames in table */
1414 struct dirent_w dirstr; /* Directory structure to return */
1415} DIRW;
1416
1417#define _TDIR DIRW
1418#define _TDIRECT _DIRECTW
1419#define _tdirent dirent_w
1420
1421#else
1422
1423#define _TDIR DIR
1424#define _TDIRECT _DIRECT
1425#define _tdirent dirent
1426
37d62364
AK
1427#endif
1428
76b1d3d8
VK
1429#else /* not _WIN32 */
1430
1431typedef struct dirent_w
1432{
1433 long d_ino; /* inode number */
1434 WCHAR d_name[257]; /* file name */
1435} _DIRECTW;
1436
1437typedef struct _dir_struc_w
1438{
1439 DIR *dir; /* Original non-unicode structure */
1440 struct dirent_w dirstr; /* Directory structure to return */
1441} DIRW;
1442
3d1cb7d6
VK
1443#endif /* _WIN32 */
1444
1445
e9246d26
VK
1446//
1447// Functions
1448//
1449
c81979bc 1450#ifdef __cplusplus
18c575eb 1451
2e590d1a
VK
1452inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1453{
912b994d 1454#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1455 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 1456#else
2e590d1a
VK
1457 _tcsncpy(pszDest, pszSrc, nLen - 1);
1458 pszDest[nLen - 1] = 0;
912b994d 1459#endif
2e590d1a
VK
1460 return pszDest;
1461}
f3387429
VK
1462
1463#ifdef UNICODE
2e590d1a
VK
1464inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1465{
f3387429 1466#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1467 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 1468#else
2e590d1a
VK
1469 strncpy(pszDest, pszSrc, nLen - 1);
1470 pszDest[nLen - 1] = 0;
c81979bc 1471#endif
2e590d1a
VK
1472 return pszDest;
1473}
c81979bc 1474#else
f3387429 1475#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
1476#endif
1477
967893bb 1478int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
1479int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1480int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
18c575eb
VK
1481bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1482
b42c9187
VK
1483SOCKET LIBNETXMS_EXPORTABLE ConnectToHost(const InetAddress& addr, UINT16 port, UINT32 timeout);
1484
b0608204 1485#endif /* __cplusplus */
c81979bc 1486
b50f1100 1487#ifdef __cplusplus
e9246d26
VK
1488extern "C"
1489{
b50f1100 1490#endif
735bac5c 1491
fe8ea784
VK
1492void LIBNETXMS_EXPORTABLE InitNetXMSProcess();
1493
076a55c4 1494#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc 1495#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
1496void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1497#define wcsupr __wcsupr
cc022855 1498#endif
735bac5c
VK
1499void LIBNETXMS_EXPORTABLE __strupr(char *in);
1500#define strupr __strupr
7e679c4b 1501#endif
cc022855 1502
735bac5c 1503void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
1504 int (*compare)(const void *, const void *, void *));
1505
735bac5c 1506INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 1507
735bac5c
VK
1508UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1509UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
1510#ifdef UNICODE
1511#define FileSize FileSizeW
1512#else
1513#define FileSize FileSizeA
1514#endif
670c7f3d 1515
735bac5c
VK
1516int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1517TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1518#ifdef UNICODE
735bac5c 1519char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1520#else
1521#define IpToStrA IpToStr
e0f99bf0 1522#endif
8c75ad41 1523TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
af4a09df
VK
1524#ifdef UNICODE
1525char LIBNETXMS_EXPORTABLE *Ip6ToStrA(const BYTE *addr, char *buffer);
1526#else
1527#define Ip6ToStrA Ip6ToStr
1528#endif
735bac5c 1529TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 1530
735bac5c
VK
1531UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1532UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1533#ifdef UNICODE
cfe20cce 1534#define ResolveHostName ResolveHostNameW
08b214c6 1535#else
cfe20cce 1536#define ResolveHostName ResolveHostNameA
08b214c6 1537#endif
a1dc75b0 1538
735bac5c
VK
1539void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1540void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1541
735bac5c
VK
1542WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1543char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1544#ifdef UNICODE
1545#define BinToStr BinToStrW
1546#else
1547#define BinToStr BinToStrA
1548#endif
1549
735bac5c
VK
1550size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1551size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1552#ifdef UNICODE
1553#define StrToBin StrToBinW
1554#else
1555#define StrToBin StrToBinA
1556#endif
cc022855 1557
735bac5c 1558TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1559
735bac5c
VK
1560void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1561void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
1562#ifdef UNICODE
1563#define StrStrip StrStripW
1564#else
1565#define StrStrip StrStripA
1566#endif
1567
735bac5c
VK
1568const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1569const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1570#ifdef UNICODE
1571#define ExtractWord ExtractWordW
1572#else
1573#define ExtractWord ExtractWordA
1574#endif
1575
735bac5c
VK
1576int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1577int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
1578#ifdef UNICODE
1579#define NumChars NumCharsW
1580#else
1581#define NumChars NumCharsA
1582#endif
ada97259 1583
735bac5c
VK
1584void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1585void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
1586#ifdef UNICODE
1587#define RemoveTrailingCRLF RemoveTrailingCRLFW
1588#else
1589#define RemoveTrailingCRLF RemoveTrailingCRLFA
cafe592d
VK
1590#endif
1591
4893b4aa
VK
1592BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1593BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
1594#ifdef UNICODE
1595#define RegexpMatch RegexpMatchW
1596#else
1597#define RegexpMatch RegexpMatchA
35f836fe
VK
1598#endif
1599
735bac5c 1600const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
d77c193c 1601BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
818f8c94 1602TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
1603bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1604TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
0a145c10 1605int LIBNETXMS_EXPORTABLE GetLastMonthDay(struct tm *currTime);
1606#ifdef __cplusplus
1607bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime = 0);
1608#else
1609bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime);
1610#endif
1611
ada97259 1612
f7794b68 1613#ifdef __cplusplus
735bac5c 1614BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 1615#endif
735bac5c
VK
1616BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1617/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1618const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1619void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1620BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1621#ifdef UNICODE
735bac5c 1622BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1623#else
1624#define LoadFileA LoadFile
1625#endif
1626
735bac5c
VK
1627UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1628void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1629void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1630void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1631void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1718cc2a 1632void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const unsigned char *data, size_t len, unsigned char *hash);
735bac5c
VK
1633BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1634BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1635BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1636
981d246a
VK
1637void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
1638
735bac5c
VK
1639void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1640void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 1641
f2436b76 1642bool LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd, size_t bufferLenght);
6b1282e7 1643
735bac5c 1644int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1645
735bac5c
VK
1646HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1647void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1648void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1649
735bac5c
VK
1650bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1651bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1652long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 1653
735bac5c
VK
1654bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1655bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1656long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
1657
1658#ifdef UNICODE
1659#define ExtractNamedOptionValue ExtractNamedOptionValueW
1660#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1661#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1662#else
1663#define ExtractNamedOptionValue ExtractNamedOptionValueA
1664#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1665#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1666#endif
ae9cddaf 1667
bc858635 1668#ifdef __cplusplus
735bac5c 1669const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
bc858635 1670#else
735bac5c 1671const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
bc858635
VK
1672#endif
1673
aa13cfd9 1674#ifdef _WIN32
735bac5c
VK
1675TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1676BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1677INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1678#endif
696fc54f 1679
de4c0511
VK
1680#if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1681int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
1682#define daemon __daemon
696fc54f 1683#endif
4332e9d8 1684
735bac5c 1685UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1686
4332e9d8 1687#ifndef _WIN32
735bac5c
VK
1688BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1689int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1690 int cchWideChar, char *pByteStr, int cchByteChar,
1691 char *pDefaultChar, BOOL *pbUsedDefChar);
1692int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1693 int cchByteChar, WCHAR *pWideCharStr,
1694 int cchWideChar);
04dbaf62 1695
6b93c954 1696#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
735bac5c 1697int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1698#endif
1699#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
735bac5c 1700UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1701#endif
1702#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
b6ec31bb 1703UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
4332e9d8 1704#endif
8b86c5dc 1705
735bac5c
VK
1706size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1707size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1708UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1709char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1710
735bac5c
VK
1711int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1712int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1713int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1714int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1715int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1716int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1717
735bac5c
VK
1718int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1719int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1720int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1721int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1722int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1723int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1724
3e39fcde 1725#endif /* _WIN32 */
8b86c5dc 1726
3d1058ed 1727#ifdef _WITH_ENCRYPTION
735bac5c 1728WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
1729#endif
1730
8b86c5dc 1731#ifdef UNICODE_UCS4
735bac5c
VK
1732size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1733size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1734size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1735UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1736WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1737#endif
8b86c5dc 1738
ebe98224
VK
1739size_t LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
1740size_t LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
1741char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *s);
1742char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *s);
1743
842c22f4 1744#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 1745int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
1746#endif
1747
ef856d02 1748#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1749
f3387429 1750#if !HAVE_WPOPEN
735bac5c 1751FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 1752#endif
8b86c5dc 1753#if !HAVE_WFOPEN
735bac5c 1754FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 1755#endif
8c2489aa 1756#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 1757FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 1758#endif
8b86c5dc 1759#if !HAVE_WOPEN
735bac5c 1760int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1761#endif
f3387429 1762#if !HAVE_WCHMOD
735bac5c 1763int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 1764#endif
b07c50cc 1765#if !HAVE_WCHDIR
735bac5c 1766int wchdir(const WCHAR *_path);
b07c50cc
VK
1767#endif
1768#if !HAVE_WMKDIR
735bac5c 1769int wmkdir(const WCHAR *_path, int mode);
b07c50cc
VK
1770#endif
1771#if !HAVE_WRMDIR
735bac5c 1772int wrmdir(const WCHAR *_path);
b07c50cc 1773#endif
ccc34207 1774#if !HAVE_WRENAME
735bac5c 1775int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
1776#endif
1777#if !HAVE_WUNLINK
735bac5c 1778int wunlink(const WCHAR *_path);
ccc34207 1779#endif
b07c50cc 1780#if !HAVE_WREMOVE
735bac5c 1781int wremove(const WCHAR *_path);
b07c50cc
VK
1782#endif
1783#if !HAVE_WSYSTEM
735bac5c 1784int wsystem(const WCHAR *_cmd);
b07c50cc 1785#endif
dda7c270 1786#if !HAVE_WMKSTEMP
735bac5c 1787int wmkstemp(WCHAR *_template);
dda7c270 1788#endif
b07c50cc 1789#if !HAVE_WACCESS
735bac5c 1790int waccess(const WCHAR *_path, int mode);
b07c50cc 1791#endif
273da39a 1792#if !HAVE_WGETENV
735bac5c 1793WCHAR *wgetenv(const WCHAR *_string);
273da39a 1794#endif
f3387429 1795#if !HAVE_WCTIME
735bac5c 1796WCHAR *wctime(const time_t *timep);
f3387429 1797#endif
0186b5da 1798#if !HAVE_PUTWS
735bac5c 1799int putws(const WCHAR *s);
0186b5da 1800#endif
9757ca46 1801#if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
735bac5c 1802WCHAR *wcserror(int errnum);
d49d7500
VK
1803#endif
1804#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 1805#if HAVE_POSIX_STRERROR_R
735bac5c 1806int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 1807#else
735bac5c 1808WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 1809#endif
b0823d06 1810#endif
e05f8aaf 1811
ef856d02 1812#endif /* UNICODE && !_WIN32*/
e05f8aaf 1813
8b86c5dc 1814#if !HAVE_STRTOLL
735bac5c 1815INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
1816#endif
1817#if !HAVE_STRTOULL
735bac5c 1818UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1819#endif
e05f8aaf
VK
1820
1821#if !HAVE_WCSTOLL
735bac5c 1822INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
1823#endif
1824#if !HAVE_WCSTOULL
735bac5c 1825UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1826#endif
b50f1100 1827
2f56be29 1828#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 1829WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
1830#endif
1831
4d978b4a 1832#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 1833int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
1834#endif
1835
f4d2c9ba 1836#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 1837int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
1838#endif
1839
75b4d48e
VK
1840#if !defined(_WIN32) && (!HAVE_WCSFTIME || !WORKING_WCSFTIME)
1841size_t LIBNETXMS_EXPORTABLE nx_wcsftime(WCHAR *buffer, size_t bufsize, const WCHAR *format, const struct tm *t);
1842#undef wcsftime
1843#define wcsftime nx_wcsftime
1844#endif
1845
9138dfe2
VK
1846#ifndef _WIN32
1847
1848#if HAVE_ITOA && !HAVE__ITOA
1849#define _itoa itoa
1850#undef HAVE__ITOA
1851#define HAVE__ITOA 1
1852#endif
1853#if !HAVE__ITOA && !defined(_WIN32)
1854char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
1855#endif
1856
1857#if HAVE_ITOW && !HAVE__ITOW
1858#define _itow itow
1859#undef HAVE__ITOW
1860#define HAVE__ITOW 1
1861#endif
1862#if !HAVE__ITOW && !defined(_WIN32)
1863WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
1864#endif
1865
1866#endif /* _WIN32 */
1867
3d1cb7d6 1868#ifdef _WIN32
5ea2db30 1869#ifdef UNICODE
735bac5c
VK
1870DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1871struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1872int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
5ea2db30
VK
1873
1874#define _topendir wopendir
1875#define _treaddir wreaddir
1876#define _tclosedir wclosedir
1877#else
1878#define _topendir opendir
1879#define _treaddir readdir
1880#define _tclosedir closedir
1881#endif
1882
735bac5c
VK
1883DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1884struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1885int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1886
76b1d3d8
VK
1887#else /* not _WIN32 */
1888
735bac5c
VK
1889DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1890struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1891int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 1892
37d62364 1893#endif
3d1cb7d6 1894
7f9e0c00 1895#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
1896int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1897 int (*select)(const struct dirent *),
1898 int (*compar)(const struct dirent **, const struct dirent **));
1899int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
1900#endif
1901
3bf0df9f
VK
1902TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1903
2df047f4
VK
1904bool LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
1905 unsigned int msgCount, const TCHAR **messages, DWORD debugMsg);
9f2902ac
VK
1906void LIBNETXMS_EXPORTABLE nxlog_close(void);
1907void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
2df047f4
VK
1908void LIBNETXMS_EXPORTABLE nxlog_debug(int level, const TCHAR *format, ...);
1909void LIBNETXMS_EXPORTABLE nxlog_debug2(int level, const TCHAR *format, va_list args);
1910bool LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
1911bool LIBNETXMS_EXPORTABLE nxlog_rotate();
1912void LIBNETXMS_EXPORTABLE nxlog_set_debug_level(int level);
1913int LIBNETXMS_EXPORTABLE nxlog_get_debug_level();
7c587518
VK
1914
1915typedef void (*NxLogDebugWriter)(const TCHAR *);
1916void LIBNETXMS_EXPORTABLE nxlog_set_debug_writer(NxLogDebugWriter writer);
9113e749
VK
1917
1918typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1919void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1920
1921void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1922void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1923#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1924 __attribute__ ((format(printf, 1, 2)))
1925#endif
1926;
9f2902ac 1927
37a7a862
VK
1928#ifdef _WIN32
1929int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1930int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1931#ifdef UNICODE
1932#define _tmkstemp wmkstemp
1933#else
1934#define _tmkstemp mkstemp
1935#endif
1936#endif
1937
9b13553b
VK
1938#ifndef _WIN32
1939int strcat_s(char *dst, size_t dstSize, const char *src);
1940int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1941#endif
1942
c57d020a
VK
1943#if !HAVE_STRPTIME
1944char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1945#endif
1946
1947#if !HAVE_TIMEGM
1948time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1949#endif
1950
6f95ef89
VK
1951#if !HAVE_INET_PTON
1952int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
1953#define inet_pton nx_inet_pton
1954#endif
1955
b06ae508 1956int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
b60584cf
VK
1957time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
1958time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
1959
1960#ifdef UNICODE
1961#define ParseDateTime ParseDateTimeW
1962#else
1963#define ParseDateTime ParseDateTimeA
1964#endif
b06ae508 1965
b50f1100 1966#ifdef __cplusplus
e9246d26 1967}
b50f1100 1968#endif
e9246d26 1969
a7ff20a5
VK
1970
1971//
b06ae508 1972// C++ only functions
a7ff20a5
VK
1973//
1974
1975#ifdef __cplusplus
1976
a97f02e1
VK
1977enum nxDirectoryType
1978{
1979 nxDirBin = 0,
1980 nxDirData = 1,
1981 nxDirEtc = 2,
1039d7ee
VK
1982 nxDirLib = 3,
1983 nxDirShare = 4
a97f02e1
VK
1984};
1985
5962498b
VK
1986TCHAR LIBNETXMS_EXPORTABLE *GetHeapInfo();
1987
a97f02e1
VK
1988void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
1989
43709a0c
VK
1990UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1991
240816b0
VK
1992TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1993String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1994const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1995int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1996UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1997bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1998
0356b995 1999String LIBNETXMS_EXPORTABLE EscapeStringForJSON(const TCHAR *s);
1d919454 2000String LIBNETXMS_EXPORTABLE EscapeStringForAgent(const TCHAR *s);
0356b995 2001
53c96e2d 2002#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 2003void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
2004#endif
2005
a7ff20a5
VK
2006#endif
2007
e9246d26 2008#endif /* _nms_util_h_ */