svn:ignore changes
[public/netxms.git] / include / nms_util.h
CommitLineData
e9246d26 1/*
7ac71e60 2** NetXMS - Network Management System
36e44abe 3** Copyright (C) 2003-2011 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
6d8aee95
VK
48
49//
92502d5f 50// Serial communications
6d8aee95
VK
51//
52
53#ifdef _WIN32
54
55#define FLOW_NONE 0
56#define FLOW_SOFTWARE 1
57#define FLOW_HARDWARE 2
58
59#else /* _WIN32 */
60
70fed300
AK
61#ifdef HAVE_TERMIOS_H
62# include <termios.h>
63#else
64# error termios.h not found
65#endif
66
6d8aee95
VK
67#endif /* _WIN32 */
68
69
70//
66c04cc7
VK
71// Return codes for IcmpPing()
72//
73
74#define ICMP_SUCCESS 0
75#define ICMP_UNREACHEABLE 1
76#define ICMP_TIMEOUT 2
77#define ICMP_RAW_SOCK_FAILED 3
78
79
80//
4826c8a5 81// Token types for configuration loader
dcb44c9a
VK
82//
83
4826c8a5
VK
84#define CT_LONG 0
85#define CT_STRING 1
86#define CT_STRING_LIST 2
87#define CT_END_OF_LIST 3
88#define CT_BOOLEAN 4
89#define CT_WORD 5
fc935a60 90#define CT_IGNORE 6
4826c8a5
VK
91
92
93//
94// Return codes for NxLoadConfig()
95//
96
97#define NXCFG_ERR_OK 0
98#define NXCFG_ERR_NOFILE 1
99#define NXCFG_ERR_SYNTAX 2
100
101
102//
9f2902ac
VK
103// nxlog_open() flags
104//
105
106#define NXLOG_USE_SYSLOG ((DWORD)0x00000001)
107#define NXLOG_PRINT_TO_STDOUT ((DWORD)0x00000002)
108#define NXLOG_IS_OPEN ((DWORD)0x80000000)
109
110
111//
35f836fe 112// _tcsdup() replacement
8ff13508
VK
113//
114
115#if defined(_WIN32) && defined(USE_WIN32_HEAP)
116#ifdef __cplusplus
117extern "C" {
118#endif
35f836fe 119char LIBNETXMS_EXPORTABLE *nx__tcsdup(const char *src);
8ff13508
VK
120WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
121#ifdef __cplusplus
122}
123#endif
124#endif
125
126
127//
b160350d
VK
128// Class for serial communications
129//
130
131#ifdef __cplusplus
132
133#ifndef _WIN32
134enum
135{
136 NOPARITY,
137 ODDPARITY,
138 EVENPARITY,
139 ONESTOPBIT,
140 TWOSTOPBITS
141};
142
70fed300
AK
143enum
144{
145 FLOW_NONE,
146 FLOW_HARDWARE,
147 FLOW_SOFTWARE
148};
149
b160350d
VK
150#ifndef INVALID_HANDLE_VALUE
151#define INVALID_HANDLE_VALUE (-1)
152#endif
153#endif /* _WIN32 */
154
155class LIBNETXMS_EXPORTABLE Serial
156{
157public:
158 Serial(void);
159 ~Serial(void);
160
e2d68ddd 161 bool Open(const TCHAR *pszPort);
b160350d
VK
162 void Close(void);
163 void SetTimeout(int nTimeout);
f3a581d0
AK
164 int Read(char *pBuff, int nSize); /* waits up to timeout and do single read */
165 int ReadAll(char *pBuff, int nSize); /* read until timeout or out of space */
e2d68ddd 166 bool Write(const char *pBuff, int nSize);
b160350d
VK
167 void Flush(void);
168 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits);
70fed300 169 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl);
b160350d
VK
170 bool Restart(void);
171
172private:
173 TCHAR *m_pszPort;
174 int m_nTimeout;
175 int m_nSpeed;
176 int m_nDataBits;
177 int m_nStopBits;
178 int m_nParity;
70fed300 179 int m_nFlowControl;
b160350d
VK
180
181#ifndef _WIN32
182 int m_hPort;
70fed300 183 struct termios m_originalSettings;
b160350d
VK
184#else
185 HANDLE m_hPort;
186#endif
187};
188
ff214673
VK
189
190//
191// Class for table data storage
192//
193
ccc34207
VK
194class CSCPMessage;
195
ff214673
VK
196class LIBNETXMS_EXPORTABLE Table
197{
198private:
199 int m_nNumRows;
200 int m_nNumCols;
201 TCHAR **m_ppData;
202 TCHAR **m_ppColNames;
e31c8e60
VK
203 LONG *m_colFormats;
204 TCHAR *m_title;
ff214673
VK
205
206public:
207 Table();
ccc34207 208 Table(CSCPMessage *msg);
ff214673
VK
209 ~Table();
210
3d5610eb 211 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
9e126e57 212
e31c8e60
VK
213 int getNumRows() { return m_nNumRows; }
214 int getNumColumns() { return m_nNumCols; }
215 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
9e126e57 216
e31c8e60
VK
217 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_ppColNames[col] : NULL; }
218 LONG getColumnFormat(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_colFormats[col] : 0; }
219
220 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
221 int addColumn(const TCHAR *name, LONG format = 0);
222 int addRow();
9e126e57
VK
223
224 void setAt(int nRow, int nCol, LONG nData);
225 void setAt(int nRow, int nCol, DWORD dwData);
226 void setAt(int nRow, int nCol, double dData);
227 void setAt(int nRow, int nCol, INT64 nData);
228 void setAt(int nRow, int nCol, QWORD qwData);
229 void setAt(int nRow, int nCol, const TCHAR *pszData);
230 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
231
232 void set(int nCol, LONG nData) { setAt(m_nNumRows - 1, nCol, nData); }
233 void set(int nCol, DWORD dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
234 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
235 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
236 void set(int nCol, QWORD qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
237 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
238 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
239
240 const TCHAR *getAsString(int nRow, int nCol);
241 LONG getAsInt(int nRow, int nCol);
242 DWORD getAsUInt(int nRow, int nCol);
243 INT64 getAsInt64(int nRow, int nCol);
244 QWORD getAsUInt64(int nRow, int nCol);
245 double getAsDouble(int nRow, int nCol);
ff214673
VK
246};
247
8bad8476
VK
248
249//
250// Dynamic string class
251//
252
253class LIBNETXMS_EXPORTABLE String
254{
255protected:
256 TCHAR *m_pszBuffer;
257 DWORD m_dwBufSize;
258
259public:
c577e360
VK
260 static const int npos;
261
8bad8476 262 String();
f3cdd1c5 263 String(const TCHAR *init);
643c9dcb 264 String(const String &src);
8bad8476
VK
265 ~String();
266
7618e362 267 TCHAR *getBuffer() { return m_pszBuffer; }
ce7565e7 268 void setBuffer(TCHAR *pszBuffer);
8bad8476 269
2391d4fd 270 const String& operator =(const TCHAR *pszStr);
49bf9d8d 271 const String& operator =(const String &src);
2391d4fd 272 const String& operator +=(const TCHAR *pszStr);
ce7565e7 273 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
8bad8476 274
08b214c6
VK
275 char *getUTF8String();
276
ce7565e7
VK
277 void addString(const TCHAR *pStr, DWORD dwLen);
278 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
c577e360 279
ce7565e7
VK
280 void addMultiByteString(const char *pStr, DWORD dwSize, int nCodePage);
281 void addWideCharString(const WCHAR *pStr, DWORD dwSize);
ff175e91 282
ce7565e7 283 void addFormattedString(const TCHAR *format, ...);
893dfdd7 284 void addFormattedStringV(const TCHAR *format, va_list args);
ce7565e7
VK
285 void escapeCharacter(int ch, int esc);
286 void translate(const TCHAR *pszSrc, const TCHAR *pszDst);
c577e360 287
ce7565e7
VK
288 DWORD getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
289 BOOL isEmpty() { return m_dwBufSize <= 1; }
c577e360 290
ce7565e7
VK
291 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
292 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
293 int find(const TCHAR *pszStr, int nStart = 0);
1e1450ef 294
0bbab9d3
VK
295 void trim();
296 void shrink(int chars = 1);
c577e360
VK
297};
298
299
300//
301// String map class
302//
303
304class LIBNETXMS_EXPORTABLE StringMap
305{
306protected:
fb986055
VK
307 DWORD m_size;
308 TCHAR **m_keys;
309 TCHAR **m_values;
c577e360 310
fb986055 311 DWORD find(const TCHAR *key);
c577e360
VK
312
313public:
314 StringMap();
fb986055 315 StringMap(const StringMap &src);
c577e360
VK
316 ~StringMap();
317
fb986055 318 StringMap& operator =(const StringMap &src);
40b7416c 319
fb986055
VK
320 void set(const TCHAR *key, const TCHAR *value);
321 void setPreallocated(TCHAR *key, TCHAR *value);
5ad2167d
VK
322 void set(const TCHAR *key, DWORD value);
323
fb986055 324 const TCHAR *get(const TCHAR *key);
5ad2167d 325 DWORD getULong(const TCHAR *key, DWORD defaultValue);
6acb8479 326 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 327
fb986055
VK
328 void remove(const TCHAR *key);
329 void clear();
c577e360 330
fb986055
VK
331 DWORD getSize() { return m_size; }
332 const TCHAR *getKeyByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
333 const TCHAR *getValueByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_values[idx]) : NULL; }
8bad8476
VK
334};
335
6173bea8
VK
336
337//
338// String list class
339//
340
341class LIBNETXMS_EXPORTABLE StringList
342{
343private:
344 int m_count;
345 int m_allocated;
346 TCHAR **m_values;
347
348public:
349 StringList();
350 ~StringList();
351
352 void add(const TCHAR *value);
353 void addPreallocated(TCHAR *value);
354 void add(LONG value);
355 void add(DWORD value);
356 void add(INT64 value);
357 void add(QWORD value);
358 void add(double value);
24dc5346 359 void replace(int index, const TCHAR *value);
6173bea8
VK
360 void clear();
361 int getSize() { return m_count; }
362 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
ed806a1a
VK
363 int getIndex(const TCHAR *value);
364 int getIndexIgnoreCase(const TCHAR *value);
6173bea8
VK
365};
366
0d75ea88
VK
367
368//
369// Dynamic array class
370//
371
372class LIBNETXMS_EXPORTABLE Array
373{
374private:
375 int m_size;
376 int m_allocated;
377 int m_grow;
378 void **m_data;
379 bool m_objectOwner;
380
381protected:
382 virtual void destroyObject(void *object);
383
384public:
385 Array(int initial = 0, int grow = 16, bool owner = false);
386 virtual ~Array();
387
388 int add(void *object);
389 void *get(int index) { return ((index >= 0) && (index < m_size)) ? m_data[index] : NULL; }
24dc5346 390 void replace(int index, void *object);
0d75ea88
VK
391 void remove(int index);
392 void clear();
393
394 int size() { return m_size; }
395
396 void setOwner(bool owner) { m_objectOwner = owner; }
397 bool isOwner() { return m_objectOwner; }
398};
399
400template <class T> class ObjectArray : public Array
401{
402protected:
403 virtual void destroyObject(void *object) { delete (T*)object; }
404
405public:
926e8ce7 406 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { }
0d75ea88
VK
407 virtual ~ObjectArray() { }
408
409 int add(T *object) { return Array::add(object); }
410 T *get(int index) { return (T*)Array::get(index); }
411};
412
7f632dfe
VK
413
414//
415// Auxilliary class for objects which counts references and
416// destroys itself wheren reference count falls to 0
417//
418
419class LIBNETXMS_EXPORTABLE RefCountObject
420{
421private:
422 int m_refCount;
423 MUTEX m_mutex;
424
425public:
426 RefCountObject();
427 virtual ~RefCountObject();
428
429 void incRefCount();
430 void decRefCount();
431};
432
58ee2f43
VK
433
434//
435// Network connection
436//
437
438class LIBNETXMS_EXPORTABLE SocketConnection
439{
440protected:
441 SOCKET m_socket;
442 char m_data[4096];
443 int m_dataPos;
444
445public:
446 SocketConnection();
447 virtual ~SocketConnection();
448
449 bool connectTCP(const TCHAR *hostName, WORD port, DWORD timeout);
450 void disconnect();
451
452 bool canRead(DWORD timeout);
453 int read(char *pBuff, int nSize);
454 bool waitForText(const char *text, int timeout);
455
456 int write(const char *pBuff, int nSize);
1671f85d
VK
457 bool writeLine(const char *line);
458
459 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, DWORD timeout);
58ee2f43
VK
460};
461
b160350d
VK
462#endif /* __cplusplus */
463
464
465//
4826c8a5
VK
466// Configuration item template for configuration loader
467//
468
469typedef struct
470{
018fda4d 471 TCHAR szToken[64];
4826c8a5
VK
472 BYTE iType;
473 BYTE cSeparator; // Separator character for lists
474 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
475 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
476 DWORD dwBufferPos; // Should be set to 0
477 void *pBuffer;
478} NX_CFG_TEMPLATE;
dcb44c9a
VK
479
480
481//
bc858635
VK
482// Code translation structure
483//
484
485typedef struct __CODE_TO_TEXT
486{
487 int code;
488 const TCHAR *text;
489} CODE_TO_TEXT;
490
491
492//
3046f9a4
VK
493// getopt() prototype if needed
494//
495
496#ifdef _WIN32
99e50bb7 497#include <netxms_getopt.h>
3046f9a4
VK
498#endif
499
500
501//
f2fdf1b5
VK
502// Win32 API functions missing under WinCE
503//
504
c32b805a 505#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
506
507inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
508{
509 SYSTEMTIME sysTime;
510
511 GetSystemTime(&sysTime);
512 SystemTimeToFileTime(&sysTime, pFt);
513}
514
515#endif // UNDER_CE
516
517
518//
3d1cb7d6
VK
519// Structures for opendir() / readdir() / closedir()
520//
521
522#ifdef _WIN32
523
7f9e0c00 524typedef struct dirent
3d1cb7d6
VK
525{
526 long d_ino; /* inode number (not used by MS-DOS) */
527 int d_namlen; /* Name length */
528 char d_name[257]; /* file name */
529} _DIRECT;
530
531typedef struct _dir_struc
532{
533 char *start; /* Starting position */
534 char *curr; /* Current position */
535 long size; /* Size of string table */
536 long nfiles; /* number if filenames in table */
7f9e0c00 537 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
538} DIR;
539
5ea2db30
VK
540#ifdef UNICODE
541
542typedef struct dirent_w
543{
544 long d_ino; /* inode number (not used by MS-DOS) */
545 int d_namlen; /* Name length */
546 WCHAR d_name[257]; /* file name */
547} _DIRECTW;
548
549typedef struct _dir_struc_w
550{
551 WCHAR *start; /* Starting position */
552 WCHAR *curr; /* Current position */
553 long size; /* Size of string table */
554 long nfiles; /* number if filenames in table */
555 struct dirent_w dirstr; /* Directory structure to return */
556} DIRW;
557
558#define _TDIR DIRW
559#define _TDIRECT _DIRECTW
560#define _tdirent dirent_w
561
562#else
563
564#define _TDIR DIR
565#define _TDIRECT _DIRECT
566#define _tdirent dirent
567
37d62364
AK
568#endif
569
3d1cb7d6
VK
570#endif /* _WIN32 */
571
572
573//
e9246d26
VK
574// Functions
575//
576
43d92b4c 577
babb8280 578#if WORDS_BIGENDIAN
43d92b4c
VK
579#define htonq(x) (x)
580#define ntohq(x) (x)
9f20696e
VK
581#define htond(x) (x)
582#define ntohd(x) (x)
babb8280
VK
583#define SwapWideString(x)
584#else
ca6ad6bb
VK
585#ifdef HAVE_HTONLL
586#define htonq(x) htonll(x)
587#else
babb8280 588#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
589#endif
590#ifdef HAVE_NTOHLL
591#define ntohq(x) ntohll(x)
592#else
babb8280 593#define ntohq(x) __bswap_64(x)
ca6ad6bb 594#endif
babb8280
VK
595#define htond(x) __bswap_double(x)
596#define ntohd(x) __bswap_double(x)
597#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 598#endif
15929b31 599
55b33d4c
VK
600#ifdef UNDER_CE
601#define close(x) CloseHandle((HANDLE)(x))
602#endif
603
b50f1100 604#ifdef __cplusplus
c81979bc 605#ifndef LIBNETXMS_INLINE
4299401e 606 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 607 {
912b994d
VK
608#if defined(_WIN32) && (_MSC_VER >= 1400)
609 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
610#else
c81979bc
VK
611 _tcsncpy(pszDest, pszSrc, nLen - 1);
612 pszDest[nLen - 1] = 0;
912b994d 613#endif
c81979bc
VK
614 return pszDest;
615 }
616#endif
617#else
7b8b337e
VK
618TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen);
619#endif
620
621#ifdef __cplusplus
622int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, DWORD timeout);
623int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
624int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
625 size_t nSize, int nFlags, DWORD dwTimeout);
c81979bc
VK
626#endif
627
628#ifdef __cplusplus
e9246d26
VK
629extern "C"
630{
b50f1100 631#endif
c5b523e2
VK
632#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
633 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
634#endif
f52be742 635#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
636 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
637#endif
9f20696e 638 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 639 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 640
076a55c4 641#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
642#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
643 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
644#endif
645 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 646#endif
ab588c84 647
b8a8d064
VK
648 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
649 int (*compare)(const void *, const void *, void *));
650
670c7f3d 651 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
3aa0061c 652 QWORD LIBNETXMS_EXPORTABLE FileSize(const TCHAR *pszFileName);
670c7f3d 653
64584976 654 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 655 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
e0f99bf0
VK
656#ifdef UNICODE
657 char LIBNETXMS_EXPORTABLE *IpToStrA(DWORD dwAddr, char *szBuffer);
17b35ccc
VK
658#else
659#define IpToStrA IpToStr
e0f99bf0 660#endif
36e44abe 661 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
662 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
663
3aa0061c 664 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6
VK
665#ifdef UNICODE
666 DWORD LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
667#else
668#define ResolveHostNameA ResolveHostName
669#endif
a1dc75b0 670
64584976 671 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 672 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 673
f42b8099 674 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(const BYTE *pData, DWORD dwSize, TCHAR *pStr);
3aa0061c 675 DWORD LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, DWORD dwSize);
f42b8099 676 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 677
018fda4d 678 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
0bbab9d3 679 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
018fda4d 680 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
9eea20df 681 BOOL LIBNETXMS_EXPORTABLE RegexpMatch(const TCHAR *pszStr, const TCHAR *pszExpr, BOOL bMatchCase);
35f836fe 682 const TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(const TCHAR *line, TCHAR *buffer);
f3cdd1c5 683 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
3583a400
VK
684 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
685 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
686#ifdef UNICODE
687#define NumChars NumCharsW
688#else
689#define NumChars NumCharsA
690#endif
35f836fe
VK
691 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
692 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
693#ifdef UNICODE
694#define RemoveTrailingCRLF RemoveTrailingCRLFW
695#else
696#define RemoveTrailingCRLF RemoveTrailingCRLFA
697#endif
698
f7794b68
VK
699#ifdef __cplusplus
700 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
701#endif
fa2b47a7 702 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 703 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 704 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 705 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
3aa0061c 706 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, DWORD *pdwFileSize);
08b214c6
VK
707#ifdef UNICODE
708 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, DWORD *pdwFileSize);
709#else
710#define LoadFileA LoadFile
711#endif
712
6120112b 713 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba 714 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 715 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 716 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 717 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
718 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
719 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
720 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, DWORD *pResult);
66c04cc7 721
4e2debcc
VK
722 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
723 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
724
6b1282e7
VK
725 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
726
46ef501a
VK
727 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
728 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 729
e6c91aac
VK
730 /* deprecated:
731 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(const TCHAR *pszFileName, const TCHAR *pszSection,
732 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);*/
3aa0061c 733 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 734
4560d8bd 735 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 736 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
4560d8bd 737 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const TCHAR *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 738
2e06d08b
VK
739 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValue(const TCHAR *optString, const TCHAR *option, TCHAR *buffer, int bufSize);
740 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBool(const TCHAR *optString, const TCHAR *option, BOOL defVal);
741 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsInt(const TCHAR *optString, const TCHAR *option, long defVal);
ae9cddaf 742
bc858635
VK
743#ifdef __cplusplus
744 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
745#else
746 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
747#endif
748
aa13cfd9 749#ifdef _WIN32
4299401e 750 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 751 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 752 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 753#endif
696fc54f
VK
754
755#if !(HAVE_DAEMON)
b1a57444 756 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 757#endif
4332e9d8 758
3aa0061c 759 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 760
4332e9d8 761#ifndef _WIN32
664c7c49 762 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
3aa0061c 763 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
764 int cchWideChar, char *pByteStr, int cchByteChar,
765 char *pDefaultChar, BOOL *pbUsedDefChar);
3aa0061c 766 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, const char *pByteStr,
4332e9d8
VK
767 int cchByteChar, WCHAR *pWideCharStr,
768 int cchWideChar);
04dbaf62 769
6b93c954 770#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 771 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
772#endif
773#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 774 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
775#endif
776#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 777 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 778#endif
8b86c5dc
VK
779
780#ifndef UNICODE
53c96e2d
VK
781 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
782 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
783 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
784 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc
VK
785#endif
786
04dbaf62 787#ifdef UNICODE
6e17f325
VK
788 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
789 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
790 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
791 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
792 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
793 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62
VK
794#endif
795
3e39fcde 796#endif /* _WIN32 */
8b86c5dc 797
3aa0061c 798 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 799 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
800 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
801 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 802
3d1058ed
VK
803#ifdef _WITH_ENCRYPTION
804 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
805#endif
806
8b86c5dc 807#ifdef UNICODE_UCS4
53c96e2d
VK
808 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
809 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
810 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
811 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
812 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 813#endif
8b86c5dc 814
d9b66a56
VK
815#ifdef UNICODE
816
8b86c5dc
VK
817#if !HAVE_WFOPEN
818 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
819#endif
820#if !HAVE_WOPEN
821 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 822#endif
8b86c5dc
VK
823#if !HAVE_WSTAT
824 int wstat(const WCHAR *_path, struct stat *_sbuf);
dde96745 825#endif
ccc34207
VK
826#if !HAVE_WRENAME
827 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
828#endif
829#if !HAVE_WUNLINK
830 int wunlink(const WCHAR *_path);
831#endif
273da39a
VK
832#if !HAVE_WGETENV
833 WCHAR *wgetenv(const WCHAR *_string);
834#endif
d49d7500
VK
835#if !HAVE_WCSERROR && HAVE_STRERROR
836 WCHAR *wcserror(int errnum);
837#endif
838#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
839#if HAVE_POSIX_STRERROR_R
840 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
841#else
d49d7500
VK
842 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
843#endif
b0823d06 844#endif
e05f8aaf
VK
845
846#endif /* UNICODE */
847
8b86c5dc
VK
848#if !HAVE_STRTOLL
849 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
850#endif
851#if !HAVE_STRTOULL
852 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
853#endif
e05f8aaf
VK
854
855#if !HAVE_WCSTOLL
856 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
857#endif
858#if !HAVE_WCSTOULL
859 QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
860#endif
b50f1100 861
3d1cb7d6 862#ifdef _WIN32
5ea2db30
VK
863#ifdef UNICODE
864 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
865 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
866 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
867
868#define _topendir wopendir
869#define _treaddir wreaddir
870#define _tclosedir wclosedir
871#else
872#define _topendir opendir
873#define _treaddir readdir
874#define _tclosedir closedir
875#endif
876
3d1cb7d6 877 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 878 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 879 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 880
37d62364 881#endif
3d1cb7d6 882
7f9e0c00
VK
883#if defined(_WIN32) || !(HAVE_SCANDIR)
884 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
885 int (*select)(const struct dirent *),
886 int (*compar)(const struct dirent **, const struct dirent **));
887 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
888#endif
889
55b33d4c
VK
890#ifdef UNDER_CE
891 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
892 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
893 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
894#endif
895
9f2902ac 896BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, DWORD flags, const TCHAR *msgModule,
d49d7500 897 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
898void LIBNETXMS_EXPORTABLE nxlog_close(void);
899void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
4addc3a3
VK
900BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int maxLogSize, int historySize);
901BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
f669df41
VK
902void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(void (*writer)(const TCHAR *, ...));
903
904void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
905void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 906
b50f1100 907#ifdef __cplusplus
e9246d26 908}
b50f1100 909#endif
e9246d26 910
a7ff20a5
VK
911
912//
913// C++ only finctions
914//
915
916#ifdef __cplusplus
917
53c96e2d 918TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *string, int length);
a7ff20a5 919String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *string, int length = -1);
53c96e2d
VK
920const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
921int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
922DWORD LIBNETXMS_EXPORTABLE XMLGetAttrDWORD(const char **attrs, const char *name, DWORD defVal);
923bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
924
925#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
926void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
927 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
928#endif
929
930void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
931 void (* sendTrap1)(DWORD, const char *, va_list),
932 void (* sendTrap2)(DWORD, int, TCHAR **),
933 BOOL (* sendFile)(void *, DWORD, const TCHAR *, long),
934 BOOL (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
935
936#endif
937
e9246d26 938#endif /* _nms_util_h_ */