JIRA connector - initial import
[public/netxms.git] / include / nms_util.h
CommitLineData
cc022855 1/*
7ac71e60 2** NetXMS - Network Management System
b0608204 3** Copyright (C) 2003-2013 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
e2e66145
VK
69/**
70 * Return codes for IcmpPing()
71 */
66c04cc7
VK
72#define ICMP_SUCCESS 0
73#define ICMP_UNREACHEABLE 1
74#define ICMP_TIMEOUT 2
75#define ICMP_RAW_SOCK_FAILED 3
5418d4bb 76#define ICMP_API_ERROR 4
66c04cc7 77
e2e66145
VK
78/**
79 * Token types for configuration loader
80 */
4826c8a5
VK
81#define CT_LONG 0
82#define CT_STRING 1
83#define CT_STRING_LIST 2
84#define CT_END_OF_LIST 3
85#define CT_BOOLEAN 4
86#define CT_WORD 5
fc935a60 87#define CT_IGNORE 6
dda7c270 88#define CT_MB_STRING 7
4826c8a5 89
605fc935 90/**
e2e66145
VK
91 * Uninitialized value for override indicator
92 */
93#define NXCONFIG_UNINITIALIZED_VALUE (-1)
94
95/**
605fc935
VK
96 * Return codes for NxLoadConfig()
97 */
4826c8a5
VK
98#define NXCFG_ERR_OK 0
99#define NXCFG_ERR_NOFILE 1
100#define NXCFG_ERR_SYNTAX 2
101
605fc935
VK
102/**
103 * nxlog_open() flags
104 */
967893bb
VK
105#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
106#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
107#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 108
605fc935
VK
109/**
110 * nxlog rotation policy
111 */
5945d50a
VK
112#define NXLOG_ROTATION_DISABLED 0
113#define NXLOG_ROTATION_DAILY 1
114#define NXLOG_ROTATION_BY_SIZE 2
115
605fc935
VK
116/**
117 * _tcsdup() replacement
118 */
8ff13508
VK
119#if defined(_WIN32) && defined(USE_WIN32_HEAP)
120#ifdef __cplusplus
121extern "C" {
122#endif
fd72502a 123char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
8ff13508
VK
124WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
125#ifdef __cplusplus
126}
127#endif
128#endif
129
605fc935 130/**
f4d2c9ba
VK
131 * Custom wcsdup
132 */
133#if !HAVE_WCSDUP && !defined(_WIN32)
fd72502a 134
f4d2c9ba
VK
135#ifdef __cplusplus
136extern "C" {
137#endif
138 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
139#ifdef __cplusplus
140}
141#endif
fd72502a
VK
142
143#elif defined(_AIX)
144
145// Some AIX versions have broken wcsdup() so we use internal implementation
146#ifdef __cplusplus
147extern "C" {
148#endif
149 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
150#ifdef __cplusplus
151}
152#endif
153
154#define wcsdup nx_wcsdup
155
f4d2c9ba
VK
156#endif
157
158/**
605fc935
VK
159 * Class for serial communications
160 */
b160350d
VK
161#ifdef __cplusplus
162
163#ifndef _WIN32
164enum
165{
166 NOPARITY,
167 ODDPARITY,
168 EVENPARITY,
169 ONESTOPBIT,
170 TWOSTOPBITS
171};
172
70fed300
AK
173enum
174{
175 FLOW_NONE,
176 FLOW_HARDWARE,
177 FLOW_SOFTWARE
178};
179
b160350d
VK
180#ifndef INVALID_HANDLE_VALUE
181#define INVALID_HANDLE_VALUE (-1)
182#endif
183#endif /* _WIN32 */
184
185class LIBNETXMS_EXPORTABLE Serial
186{
b160350d
VK
187private:
188 TCHAR *m_pszPort;
189 int m_nTimeout;
190 int m_nSpeed;
191 int m_nDataBits;
192 int m_nStopBits;
193 int m_nParity;
70fed300 194 int m_nFlowControl;
b160350d
VK
195
196#ifndef _WIN32
197 int m_hPort;
70fed300 198 struct termios m_originalSettings;
b160350d
VK
199#else
200 HANDLE m_hPort;
201#endif
b160350d 202
826fcc1f
VK
203public:
204 Serial();
205 ~Serial();
206
207 bool open(const TCHAR *pszPort);
208 void close();
209 void setTimeout(int nTimeout);
210 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
211 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 212 int readToMark(char *buff, int size, const char **marks, char **occurence);
826fcc1f
VK
213 bool write(const char *pBuff, int nSize);
214 void flush();
215 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
216 bool restart();
217};
ff214673 218
3fe40613
VK
219/**
220 * Dynamic string class
221 */
8bad8476
VK
222class LIBNETXMS_EXPORTABLE String
223{
224protected:
225 TCHAR *m_pszBuffer;
967893bb 226 UINT32 m_dwBufSize;
8bad8476
VK
227
228public:
c577e360
VK
229 static const int npos;
230
8bad8476 231 String();
f3cdd1c5 232 String(const TCHAR *init);
643c9dcb 233 String(const String &src);
8bad8476
VK
234 ~String();
235
7618e362 236 TCHAR *getBuffer() { return m_pszBuffer; }
ce7565e7 237 void setBuffer(TCHAR *pszBuffer);
8bad8476 238
2391d4fd 239 const String& operator =(const TCHAR *pszStr);
49bf9d8d 240 const String& operator =(const String &src);
1d34c533
VK
241 const String& operator +=(const TCHAR *str);
242 const String& operator +=(const String &str);
ce7565e7 243 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
8bad8476 244
08b214c6
VK
245 char *getUTF8String();
246
967893bb 247 void addString(const TCHAR *pStr, UINT32 dwLen);
ce7565e7 248 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
c577e360 249
e0471fad
VK
250 void addMultiByteString(const char *pStr, UINT32 size, int nCodePage);
251 void addWideCharString(const WCHAR *pStr, UINT32 size);
ff175e91 252
ce7565e7 253 void addFormattedString(const TCHAR *format, ...);
893dfdd7 254 void addFormattedStringV(const TCHAR *format, va_list args);
c577e360 255
967893bb 256 UINT32 getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
ce7565e7 257 BOOL isEmpty() { return m_dwBufSize <= 1; }
c577e360 258
ce7565e7
VK
259 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
260 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
261 int find(const TCHAR *pszStr, int nStart = 0);
1e1450ef 262
6bcafa2d
VK
263 void escapeCharacter(int ch, int esc);
264 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
265 void trim();
266 void shrink(int chars = 1);
c577e360
VK
267};
268
3fe40613
VK
269/**
270 * String maps base class
271 */
272class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
273{
274protected:
967893bb 275 UINT32 m_size;
fb986055 276 TCHAR **m_keys;
3fe40613
VK
277 void **m_values;
278 bool m_objectOwner;
55bdca5a 279 bool m_ignoreCase;
3fe40613 280 void (*m_objectDestructor)(void *);
c577e360 281
967893bb 282 UINT32 find(const TCHAR *key);
3fe40613
VK
283 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
284 void *getObject(const TCHAR *key);
285 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
286
287public:
3fe40613
VK
288 StringMapBase(bool objectOwner);
289 virtual ~StringMapBase();
290
55bdca5a
VK
291 void setOwner(bool owner) { m_objectOwner = owner; }
292 void setIgnoreCase(bool ignore) { m_ignoreCase = ignore; }
293
3fe40613
VK
294 void remove(const TCHAR *key);
295 void clear();
296
967893bb
VK
297 UINT32 getSize() { return m_size; }
298 const TCHAR *getKeyByIndex(UINT32 idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
3fe40613
VK
299};
300
301/**
302 * String map class
303 */
304class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
305{
306public:
307 StringMap() : StringMapBase(true) { }
fb986055 308 StringMap(const StringMap &src);
3fe40613 309 virtual ~StringMap();
c577e360 310
fb986055 311 StringMap& operator =(const StringMap &src);
40b7416c 312
3fe40613
VK
313 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
314 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 315 void set(const TCHAR *key, UINT32 value);
5ad2167d 316
3fe40613 317 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
967893bb 318 UINT32 getULong(const TCHAR *key, UINT32 defaultValue);
6acb8479 319 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 320
967893bb 321 const TCHAR *getValueByIndex(UINT32 idx) { return (idx < m_size) ? CHECK_NULL_EX((TCHAR *)m_values[idx]) : NULL; }
8bad8476
VK
322};
323
3fe40613
VK
324/**
325 * String map template for holding objects as values
326 */
327template <class T> class StringObjectMap : public StringMapBase
328{
329private:
330 static void destructor(void *object) { delete (T*)object; }
6173bea8 331
3fe40613
VK
332public:
333 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
334
335 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
336 T *get(const TCHAR *key) { return (T*)getObject(key); }
967893bb 337 T *getValueByIndex(UINT32 idx) { return (idx < m_size) ? (T *)m_values[idx] : NULL; }
3fe40613 338};
6173bea8 339
3fe40613
VK
340/**
341 * String list class
342 */
6173bea8
VK
343class LIBNETXMS_EXPORTABLE StringList
344{
345private:
346 int m_count;
347 int m_allocated;
348 TCHAR **m_values;
349
350public:
351 StringList();
86c126f5 352 StringList(StringList *src);
22ee6d97 353 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
354 ~StringList();
355
356 void add(const TCHAR *value);
357 void addPreallocated(TCHAR *value);
967893bb
VK
358 void add(INT32 value);
359 void add(UINT32 value);
6173bea8 360 void add(INT64 value);
967893bb 361 void add(UINT64 value);
6173bea8 362 void add(double value);
24dc5346 363 void replace(int index, const TCHAR *value);
6173bea8
VK
364 void clear();
365 int getSize() { return m_count; }
366 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
ed806a1a
VK
367 int getIndex(const TCHAR *value);
368 int getIndexIgnoreCase(const TCHAR *value);
d51f2182 369 void remove(int index);
86c126f5 370 void addAll(const StringList *src);
93dbc1be 371 void merge(const StringList *src, bool matchCase);
7d25a5c5 372 TCHAR *join(const TCHAR *separator);
22ee6d97 373 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
374};
375
3fe40613 376/**
a58501c0
VK
377 * Entry of string set
378 */
379struct StringSetEntry;
380
381/**
f3603b79
VK
382 * NXCP message
383 */
384class CSCPMessage;
385
386/**
a58501c0
VK
387 * String set class
388 */
389class LIBNETXMS_EXPORTABLE StringSet
390{
391private:
392 StringSetEntry *m_data;
393
394public:
395 StringSet();
396 ~StringSet();
397
398 void add(const TCHAR *str);
f3603b79 399 void addPreallocated(TCHAR *str);
a58501c0
VK
400 void remove(const TCHAR *str);
401 void clear();
402
403 int size();
f3603b79 404 bool contains(const TCHAR *str);
5b521bfc 405 bool equals(StringSet *s);
a58501c0 406
fa493140 407 void addAll(StringSet *src);
f3603b79 408 void addAll(TCHAR **strings, int count);
d6c4cdc1 409 void addAllPreallocated(TCHAR **strings, int count);
a58501c0 410 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
f3603b79
VK
411
412 void fillMessage(CSCPMessage *msg, UINT32 baseId, UINT32 countId);
c262bc08
VK
413 void addAllFromMessage(CSCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
414
415 String getAll(const TCHAR *separator);
a58501c0
VK
416};
417
418/**
3fe40613
VK
419 * Dynamic array class
420 */
0d75ea88
VK
421class LIBNETXMS_EXPORTABLE Array
422{
423private:
424 int m_size;
425 int m_allocated;
426 int m_grow;
ac5b1642 427 size_t m_elementSize;
0d75ea88
VK
428 void **m_data;
429 bool m_objectOwner;
430
16d6f798 431 void internalRemove(int index, bool allowDestruction);
cfa7c21f 432 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
16d6f798 433
0d75ea88 434protected:
1c6d61ca 435 bool m_storePointers;
cfa7c21f 436 void (*m_objectDestructor)(void *);
0d75ea88 437
ac5b1642 438 Array(void *data, int initial, int grow, size_t elementSize);
9a68ca24 439 void *__getBuffer() { return m_data; }
8da70db3 440
0d75ea88
VK
441public:
442 Array(int initial = 0, int grow = 16, bool owner = false);
443 virtual ~Array();
444
8da70db3 445 int add(void *element);
1c6d61ca 446 void *get(int index) { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
8da70db3
VK
447 int indexOf(void *element);
448 void set(int index, void *element);
449 void replace(int index, void *element);
16d6f798 450 void remove(int index) { internalRemove(index, true); }
8da70db3 451 void remove(void *element) { internalRemove(indexOf(element), true); }
16d6f798 452 void unlink(int index) { internalRemove(index, false); }
8da70db3 453 void unlink(void *element) { internalRemove(indexOf(element), false); }
0d75ea88 454 void clear();
8b7ae7eb 455 void sort(int (*cb)(const void *, const void *));
0d75ea88
VK
456
457 int size() { return m_size; }
458
459 void setOwner(bool owner) { m_objectOwner = owner; }
460 bool isOwner() { return m_objectOwner; }
461};
462
3fe40613
VK
463/**
464 * Template class for dynamic array which holds objects
465 */
0d75ea88
VK
466template <class T> class ObjectArray : public Array
467{
cfa7c21f
VK
468private:
469 static void destructor(void *object) { delete (T*)object; }
0d75ea88
VK
470
471public:
cfa7c21f 472 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
0d75ea88
VK
473 virtual ~ObjectArray() { }
474
47a89886 475 int add(T *object) { return Array::add((void *)object); }
0d75ea88 476 T *get(int index) { return (T*)Array::get(index); }
74d4ba34 477 int indexOf(T *object) { return Array::indexOf((void *)object); }
47a89886
VK
478 void set(int index, T *object) { Array::set(index, (void *)object); }
479 void replace(int index, T *object) { Array::replace(index, (void *)object); }
74d4ba34
VK
480 void remove(int index) { Array::remove(index); }
481 void remove(T *object) { Array::remove((void *)object); }
482 void unlink(int index) { Array::unlink(index); }
483 void unlink(T *object) { Array::unlink((void *)object); }
0d75ea88
VK
484};
485
25fe6265 486/**
e8e76b4f
VK
487 * Template class for dynamic array which holds scalar values
488 */
489template <class T> class IntegerArray : public Array
490{
491private:
8da70db3 492 static void destructor(void *element) { }
e8e76b4f
VK
493
494public:
1c6d61ca 495 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
e8e76b4f
VK
496 virtual ~IntegerArray() { }
497
1c6d61ca
VK
498 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
499 T get(int index) { return m_storePointers ? CAST_FROM_POINTER(Array::get(index), T) : *((T*)Array::get(index)); }
500 int indexOf(T value) { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
501 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
502 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
9a68ca24
VK
503
504 T *getBuffer() { return (T*)__getBuffer(); }
e8e76b4f
VK
505};
506
507/**
25fe6265
VK
508 * Auxilliary class to hold dynamically allocated array of structures
509 */
8da70db3 510template <class T> class StructArray : public Array
cc8ce218
VK
511{
512private:
8da70db3 513 static void destructor(void *element) { }
cc8ce218
VK
514
515public:
8da70db3
VK
516 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
517 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
518 virtual ~StructArray() { }
0105c251 519
8da70db3
VK
520 int add(T *element) { return Array::add((void *)element); }
521 T *get(int index) { return (T*)Array::get(index); }
522 int indexOf(T *element) { return Array::indexOf((void *)element); }
523 void set(int index, T *element) { Array::set(index, (void *)element); }
524 void replace(int index, T *element) { Array::replace(index, (void *)element); }
525 void remove(int index) { Array::remove(index); }
526 void remove(T *element) { Array::remove((void *)element); }
527 void unlink(int index) { Array::unlink(index); }
528 void unlink(T *element) { Array::unlink((void *)element); }
4c5495d9
VK
529
530 T *getBuffer() { return (T*)__getBuffer(); }
cc8ce218
VK
531};
532
25fe6265
VK
533/**
534 * Auxilliary class for objects which counts references and
535 * destroys itself wheren reference count falls to 0
536 */
7f632dfe
VK
537class LIBNETXMS_EXPORTABLE RefCountObject
538{
539private:
7785322f 540 VolatileCounter m_refCount;
7f632dfe
VK
541
542public:
7785322f
VK
543 RefCountObject()
544 {
545 m_refCount = 1;
546 }
b549a0f8 547 virtual ~RefCountObject();
7785322f
VK
548
549 void incRefCount()
550 {
551 InterlockedIncrement(&m_refCount);
552 }
553
554 void decRefCount()
555 {
556 if (InterlockedDecrement(&m_refCount) == 0)
557 delete this;
558 }
7f632dfe
VK
559};
560
967893bb
VK
561class CSCPMessage;
562
563/**
564 * Table column definition
565 */
566class LIBNETXMS_EXPORTABLE TableColumnDefinition
567{
568private:
569 TCHAR *m_name;
570 TCHAR *m_displayName;
571 INT32 m_dataType;
572 bool m_instanceColumn;
573
574public:
575 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
576 TableColumnDefinition(CSCPMessage *msg, UINT32 baseId);
577 TableColumnDefinition(TableColumnDefinition *src);
578 ~TableColumnDefinition();
579
580 void fillMessage(CSCPMessage *msg, UINT32 baseId);
581
582 const TCHAR *getName() { return m_name; }
583 const TCHAR *getDisplayName() { return m_displayName; }
584 INT32 getDataType() { return m_dataType; }
585 bool isInstanceColumn() { return m_instanceColumn; }
586
587 void setDataType(INT32 type) { m_dataType = type; }
588 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 589 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
590};
591
592/**
d6bbfa4e
VK
593 * Table cell
594 */
595class TableCell
596{
597private:
598 TCHAR *m_value;
599 int m_status;
600
601public:
602 TableCell() { m_value = NULL; m_status = -1; }
603 TableCell(const TCHAR *value) { m_value = (value != NULL) ? _tcsdup(value) : NULL; m_status = -1; }
604 TableCell(const TCHAR *value, int status) { m_value = (value != NULL) ? _tcsdup(value) : NULL; m_status = status; }
605 TableCell(TableCell *src) { m_value = (src->m_value != NULL) ? _tcsdup(src->m_value) : NULL; m_status = src->m_status; }
606 ~TableCell() { safe_free(m_value); }
607
608 void set(const TCHAR *value, int status) { safe_free(m_value); m_value = (value != NULL) ? _tcsdup(value) : NULL; m_status = status; }
609 void setPreallocated(TCHAR *value, int status) { safe_free(m_value); m_value = value; m_status = status; }
610
611 const TCHAR *getValue() { return m_value; }
612 void setValue(const TCHAR *value) { safe_free(m_value); m_value = (value != NULL) ? _tcsdup(value) : NULL; }
613 void setPreallocatedValue(TCHAR *value) { safe_free(m_value); m_value = value; }
614
615 int getStatus() { return m_status; }
616 void setStatus(int status) { m_status = status; }
617};
618
619/**
620 * Table row
621 */
622class TableRow
623{
624private:
625 ObjectArray<TableCell> *m_cells;
626 UINT32 m_objectId;
bac74bf7 627
d6bbfa4e
VK
628public:
629 TableRow(int columnCount);
630 TableRow(TableRow *src);
631 ~TableRow() { delete m_cells; }
632
633 void addColumn() { m_cells->add(new TableCell); }
634 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 635
d6bbfa4e
VK
636 void set(int index, const TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status); }
637 void setPreallocated(int index, TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status); }
638
639 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
640 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); }
641
642 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
643
644 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
645 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
646
647 UINT32 getObjectId() { return m_objectId; }
648 void setObjectId(UINT32 id) { m_objectId = id; }
649};
650
651/**
967893bb
VK
652 * Class for table data storage
653 */
a0ddfb29 654class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
655{
656private:
d6bbfa4e 657 ObjectArray<TableRow> *m_data;
967893bb
VK
658 ObjectArray<TableColumnDefinition> *m_columns;
659 TCHAR *m_title;
660 int m_source;
d6bbfa4e 661 bool m_extendedFormat;
967893bb
VK
662
663 void createFromMessage(CSCPMessage *msg);
664 void destroy();
665
666public:
667 Table();
a0ddfb29 668 Table(Table *src);
967893bb 669 Table(CSCPMessage *msg);
b549a0f8 670 virtual ~Table();
967893bb
VK
671
672 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
673 void updateFromMessage(CSCPMessage *msg);
674
a0ddfb29
VK
675 void addAll(Table *src);
676 void copyRow(Table *src, int row);
967893bb 677
d6bbfa4e
VK
678 int getNumRows() { return m_data->size(); }
679 int getNumColumns() { return m_columns->size(); }
967893bb
VK
680 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
681 int getSource() { return m_source; }
682
d6bbfa4e
VK
683 bool isExtendedFormat() { return m_extendedFormat; }
684 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
685
967893bb
VK
686 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
687 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
688 int getColumnIndex(const TCHAR *name);
689 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
690
691 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
692 void setSource(int source) { m_source = source; }
8dba1eac 693 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
694 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
695 int addRow();
696
25076d53
VK
697 void deleteRow(int row);
698 void deleteColumn(int col);
699
967893bb
VK
700 void setAt(int nRow, int nCol, INT32 nData);
701 void setAt(int nRow, int nCol, UINT32 dwData);
702 void setAt(int nRow, int nCol, double dData);
703 void setAt(int nRow, int nCol, INT64 nData);
704 void setAt(int nRow, int nCol, UINT64 qwData);
705 void setAt(int nRow, int nCol, const TCHAR *pszData);
706 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
707
d6bbfa4e
VK
708 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
709 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
710 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
711 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
712 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
713 void set(int nCol, const TCHAR *pszData) { setAt(getNumRows() - 1, nCol, pszData); }
714 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(getNumRows() - 1, nCol, pszData); }
715
716 void setStatusAt(int row, int col, int status);
717 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb
VK
718
719 const TCHAR *getAsString(int nRow, int nCol);
720 INT32 getAsInt(int nRow, int nCol);
721 UINT32 getAsUInt(int nRow, int nCol);
722 INT64 getAsInt64(int nRow, int nCol);
723 UINT64 getAsUInt64(int nRow, int nCol);
724 double getAsDouble(int nRow, int nCol);
22aaa779 725
d6bbfa4e
VK
726 int getStatus(int nRow, int nCol);
727
22aaa779 728 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 729 int findRowByInstance(const TCHAR *instance);
d6bbfa4e
VK
730
731 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
732 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
967893bb
VK
733};
734
25fe6265
VK
735/**
736 * Network connection
737 */
58ee2f43
VK
738class LIBNETXMS_EXPORTABLE SocketConnection
739{
740protected:
741 SOCKET m_socket;
742 char m_data[4096];
743 int m_dataPos;
744
745public:
746 SocketConnection();
747 virtual ~SocketConnection();
748
967893bb
VK
749 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
750 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
751 void disconnect();
752
967893bb
VK
753 bool canRead(UINT32 timeout);
754 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 755 bool waitForText(const char *text, int timeout);
cc022855 756
58ee2f43 757 int write(const char *pBuff, int nSize);
1671f85d
VK
758 bool writeLine(const char *line);
759
967893bb 760 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
761};
762
030a0779
VK
763/**
764 * Telnet connection - handles all telnet negotiation
765 */
def5b792
AK
766class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
767{
030a0779 768protected:
967893bb
VK
769 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
770 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 771
def5b792 772public:
967893bb 773 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 774
967893bb
VK
775 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
776 bool connect(UINT32 ip, WORD port, UINT32 timeout);
777 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
778 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
779};
780
b160350d
VK
781#endif /* __cplusplus */
782
030a0779
VK
783/**
784 * Configuration item template for configuration loader
785 */
4826c8a5
VK
786typedef struct
787{
e2e66145
VK
788 TCHAR token[64];
789 BYTE type;
790 BYTE separator; // Separator character for lists
791 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
792 UINT32 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
793 UINT32 bufferPos; // Should be set to 0
794 void *buffer;
795 void *overrideIndicator;
4826c8a5 796} NX_CFG_TEMPLATE;
dcb44c9a 797
030a0779
VK
798/**
799 * Code translation structure
800 */
bc858635
VK
801typedef struct __CODE_TO_TEXT
802{
803 int code;
804 const TCHAR *text;
805} CODE_TO_TEXT;
806
e2e66145
VK
807/**
808 * getopt() prototype if needed
809 */
df9812a9 810#if USE_BUNDLED_GETOPT
99e50bb7 811#include <netxms_getopt.h>
3046f9a4
VK
812#endif
813
814
815//
f2fdf1b5
VK
816// Win32 API functions missing under WinCE
817//
818
c32b805a 819#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
820
821inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
822{
823 SYSTEMTIME sysTime;
cc022855 824
f2fdf1b5
VK
825 GetSystemTime(&sysTime);
826 SystemTimeToFileTime(&sysTime, pFt);
827}
828
829#endif // UNDER_CE
830
831
832//
3d1cb7d6
VK
833// Structures for opendir() / readdir() / closedir()
834//
835
836#ifdef _WIN32
837
7f9e0c00 838typedef struct dirent
3d1cb7d6
VK
839{
840 long d_ino; /* inode number (not used by MS-DOS) */
841 int d_namlen; /* Name length */
842 char d_name[257]; /* file name */
843} _DIRECT;
844
845typedef struct _dir_struc
846{
847 char *start; /* Starting position */
848 char *curr; /* Current position */
849 long size; /* Size of string table */
850 long nfiles; /* number if filenames in table */
7f9e0c00 851 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
852} DIR;
853
5ea2db30
VK
854#ifdef UNICODE
855
856typedef struct dirent_w
857{
858 long d_ino; /* inode number (not used by MS-DOS) */
859 int d_namlen; /* Name length */
860 WCHAR d_name[257]; /* file name */
861} _DIRECTW;
862
863typedef struct _dir_struc_w
864{
865 WCHAR *start; /* Starting position */
866 WCHAR *curr; /* Current position */
867 long size; /* Size of string table */
868 long nfiles; /* number if filenames in table */
869 struct dirent_w dirstr; /* Directory structure to return */
870} DIRW;
871
872#define _TDIR DIRW
873#define _TDIRECT _DIRECTW
874#define _tdirent dirent_w
875
876#else
877
878#define _TDIR DIR
879#define _TDIRECT _DIRECT
880#define _tdirent dirent
881
37d62364
AK
882#endif
883
76b1d3d8
VK
884#else /* not _WIN32 */
885
886typedef struct dirent_w
887{
888 long d_ino; /* inode number */
889 WCHAR d_name[257]; /* file name */
890} _DIRECTW;
891
892typedef struct _dir_struc_w
893{
894 DIR *dir; /* Original non-unicode structure */
895 struct dirent_w dirstr; /* Directory structure to return */
896} DIRW;
897
3d1cb7d6
VK
898#endif /* _WIN32 */
899
900
901//
e9246d26
VK
902// Functions
903//
904
babb8280 905#if WORDS_BIGENDIAN
43d92b4c
VK
906#define htonq(x) (x)
907#define ntohq(x) (x)
9f20696e
VK
908#define htond(x) (x)
909#define ntohd(x) (x)
babb8280
VK
910#define SwapWideString(x)
911#else
ca6ad6bb
VK
912#ifdef HAVE_HTONLL
913#define htonq(x) htonll(x)
914#else
babb8280 915#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
916#endif
917#ifdef HAVE_NTOHLL
918#define ntohq(x) ntohll(x)
919#else
babb8280 920#define ntohq(x) __bswap_64(x)
ca6ad6bb 921#endif
babb8280
VK
922#define htond(x) __bswap_double(x)
923#define ntohd(x) __bswap_double(x)
924#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 925#endif
15929b31 926
55b33d4c
VK
927#ifdef UNDER_CE
928#define close(x) CloseHandle((HANDLE)(x))
929#endif
930
b50f1100 931#ifdef __cplusplus
2e590d1a
VK
932inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
933{
912b994d 934#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 935 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 936#else
2e590d1a
VK
937 _tcsncpy(pszDest, pszSrc, nLen - 1);
938 pszDest[nLen - 1] = 0;
912b994d 939#endif
2e590d1a
VK
940 return pszDest;
941}
f3387429
VK
942
943#ifdef UNICODE
2e590d1a
VK
944inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
945{
f3387429 946#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 947 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 948#else
2e590d1a
VK
949 strncpy(pszDest, pszSrc, nLen - 1);
950 pszDest[nLen - 1] = 0;
c81979bc 951#endif
2e590d1a
VK
952 return pszDest;
953}
c81979bc 954#else
f3387429 955#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
956#endif
957
2e590d1a
VK
958inline TCHAR *_tcsdup_ex(const TCHAR *s)
959{
960 return (s != NULL) ? _tcsdup(s) : NULL;
961}
962
967893bb 963int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
964int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
965int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
b0608204 966#endif /* __cplusplus */
c81979bc
VK
967
968#ifdef __cplusplus
e9246d26
VK
969extern "C"
970{
b50f1100 971#endif
c5b523e2 972#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
967893bb 973 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
c5b523e2 974#endif
f52be742 975#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
967893bb 976 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
64584976 977#endif
9f20696e 978 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 979 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 980
076a55c4 981#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
982#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
983 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
cc022855 984#endif
8b86c5dc 985 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 986#endif
cc022855 987
b8a8d064
VK
988 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
989 int (*compare)(const void *, const void *, void *));
990
244c65ef 991 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 992
967893bb
VK
993 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
994 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
995#ifdef UNICODE
996#define FileSize FileSizeW
997#else
998#define FileSize FileSizeA
999#endif
670c7f3d 1000
967893bb
VK
1001 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1002 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1003#ifdef UNICODE
967893bb 1004 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1005#else
1006#define IpToStrA IpToStr
e0f99bf0 1007#endif
36e44abe 1008 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
1009 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
1010
cfe20cce
VK
1011 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1012 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1013#ifdef UNICODE
cfe20cce 1014#define ResolveHostName ResolveHostNameW
08b214c6 1015#else
cfe20cce 1016#define ResolveHostName ResolveHostNameA
08b214c6 1017#endif
a1dc75b0 1018
e0471fad
VK
1019 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1020 void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1021
e0471fad
VK
1022 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1023 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1024#ifdef UNICODE
1025#define BinToStr BinToStrW
1026#else
1027#define BinToStr BinToStrA
1028#endif
1029
ac14e3e6
VK
1030 size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1031 size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1032#ifdef UNICODE
1033#define StrToBin StrToBinW
1034#else
1035#define StrToBin StrToBinA
1036#endif
cc022855 1037
e0471fad 1038 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1039
b07c50cc
VK
1040 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1041 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
1042#ifdef UNICODE
1043#define StrStrip StrStripW
1044#else
1045#define StrStrip StrStripA
1046#endif
1047
ada97259 1048 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
cafe592d 1049 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1050#ifdef UNICODE
1051#define ExtractWord ExtractWordW
1052#else
1053#define ExtractWord ExtractWordA
1054#endif
1055
3583a400
VK
1056 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1057 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
1058#ifdef UNICODE
1059#define NumChars NumCharsW
1060#else
1061#define NumChars NumCharsA
1062#endif
ada97259 1063
35f836fe
VK
1064 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1065 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
1066#ifdef UNICODE
1067#define RemoveTrailingCRLF RemoveTrailingCRLFW
1068#else
1069#define RemoveTrailingCRLF RemoveTrailingCRLFA
1070#endif
1071
cafe592d
VK
1072 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
1073 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
1074#ifdef UNICODE
1075#define RegexpMatch RegexpMatchW
1076#else
1077#define RegexpMatch RegexpMatchA
1078#endif
1079
bac74bf7 1080 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
ada97259 1081 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
240816b0 1082 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
ada97259
VK
1083 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
1084
f7794b68
VK
1085#ifdef __cplusplus
1086 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
1087#endif
fa2b47a7 1088 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 1089 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 1090 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 1091 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
967893bb 1092 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1093#ifdef UNICODE
967893bb 1094 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1095#else
1096#define LoadFileA LoadFile
1097#endif
1098
e0471fad 1099 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
a3c76aba 1100 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 1101 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 1102 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 1103 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
1104 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1105 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
967893bb 1106 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1107
4e2debcc
VK
1108 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1109 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1110
6b1282e7
VK
1111 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
1112
967893bb
VK
1113 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(UINT32 dwAddr, int iNumRetries, UINT32 dwTimeout,
1114 UINT32 *pdwRTT, UINT32 dwPacketSize);
4826c8a5 1115
3aa0061c 1116 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1117
4560d8bd 1118 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 1119 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
dda7c270 1120 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1121
289d126b
VK
1122 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1123 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1124 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
1125
1126 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1127 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1128 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
1129
1130#ifdef UNICODE
1131#define ExtractNamedOptionValue ExtractNamedOptionValueW
1132#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1133#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1134#else
1135#define ExtractNamedOptionValue ExtractNamedOptionValueA
1136#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1137#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1138#endif
ae9cddaf 1139
bc858635
VK
1140#ifdef __cplusplus
1141 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
1142#else
1143 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
1144#endif
1145
aa13cfd9 1146#ifdef _WIN32
967893bb 1147 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 1148 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 1149 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1150#endif
696fc54f
VK
1151
1152#if !(HAVE_DAEMON)
b1a57444 1153 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 1154#endif
4332e9d8 1155
967893bb 1156 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1157
4332e9d8 1158#ifndef _WIN32
664c7c49 1159 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
cc022855 1160 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1161 int cchWideChar, char *pByteStr, int cchByteChar,
4332e9d8 1162 char *pDefaultChar, BOOL *pbUsedDefChar);
cc022855 1163 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1164 int cchByteChar, WCHAR *pWideCharStr,
4332e9d8 1165 int cchWideChar);
04dbaf62 1166
6b93c954 1167#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 1168 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1169#endif
1170#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 1171 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1172#endif
1173#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 1174 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 1175#endif
8b86c5dc 1176
53c96e2d
VK
1177 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1178 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
1179 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1180 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1181
6e17f325
VK
1182 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1183 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1184 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1185 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1186 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1187 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1188
25575d38
VK
1189 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1190 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
7ff399a5 1191 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
25575d38
VK
1192 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1193 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
7ff399a5 1194 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1195
3e39fcde 1196#endif /* _WIN32 */
8b86c5dc 1197
3aa0061c 1198 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 1199 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
1200 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
1201 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
cc022855 1202
3d1058ed
VK
1203#ifdef _WITH_ENCRYPTION
1204 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1205#endif
1206
8b86c5dc 1207#ifdef UNICODE_UCS4
53c96e2d
VK
1208 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1209 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1210 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
1211 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1212 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1213#endif
8b86c5dc 1214
842c22f4
AK
1215#if !defined(_WIN32) && !HAVE_WSTAT
1216 int wstat(const WCHAR *_path, struct stat *_sbuf);
1217#endif
1218
ef856d02 1219#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1220
f3387429
VK
1221#if !HAVE_WPOPEN
1222 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1223#endif
8b86c5dc
VK
1224#if !HAVE_WFOPEN
1225 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
1226#endif
8c2489aa
VK
1227#if HAVE_FOPEN64 && !HAVE_WFOPEN64
1228 FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
1229#endif
8b86c5dc
VK
1230#if !HAVE_WOPEN
1231 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1232#endif
f3387429
VK
1233#if !HAVE_WCHMOD
1234 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
1235#endif
b07c50cc
VK
1236#if !HAVE_WCHDIR
1237 int wchdir(const WCHAR *_path);
1238#endif
1239#if !HAVE_WMKDIR
1240 int wmkdir(const WCHAR *_path, int mode);
1241#endif
1242#if !HAVE_WRMDIR
1243 int wrmdir(const WCHAR *_path);
1244#endif
ccc34207
VK
1245#if !HAVE_WRENAME
1246 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
1247#endif
1248#if !HAVE_WUNLINK
1249 int wunlink(const WCHAR *_path);
1250#endif
b07c50cc
VK
1251#if !HAVE_WREMOVE
1252 int wremove(const WCHAR *_path);
1253#endif
1254#if !HAVE_WSYSTEM
1255 int wsystem(const WCHAR *_cmd);
1256#endif
dda7c270 1257#if !HAVE_WMKSTEMP
0006ecfe 1258 int wmkstemp(WCHAR *_template);
dda7c270 1259#endif
b07c50cc
VK
1260#if !HAVE_WACCESS
1261 int waccess(const WCHAR *_path, int mode);
1262#endif
273da39a
VK
1263#if !HAVE_WGETENV
1264 WCHAR *wgetenv(const WCHAR *_string);
1265#endif
f3387429
VK
1266#if !HAVE_WCTIME
1267 WCHAR *wctime(const time_t *timep);
1268#endif
0186b5da
VK
1269#if !HAVE_PUTWS
1270 int putws(const WCHAR *s);
1271#endif
d49d7500
VK
1272#if !HAVE_WCSERROR && HAVE_STRERROR
1273 WCHAR *wcserror(int errnum);
1274#endif
1275#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
1276#if HAVE_POSIX_STRERROR_R
1277 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1278#else
d49d7500
VK
1279 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1280#endif
b0823d06 1281#endif
e05f8aaf 1282
ef856d02 1283#endif /* UNICODE && !_WIN32*/
e05f8aaf 1284
8b86c5dc
VK
1285#if !HAVE_STRTOLL
1286 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1287#endif
1288#if !HAVE_STRTOULL
967893bb 1289 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1290#endif
e05f8aaf
VK
1291
1292#if !HAVE_WCSTOLL
1293 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1294#endif
1295#if !HAVE_WCSTOULL
967893bb 1296 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1297#endif
b50f1100 1298
4d978b4a
VK
1299#if !HAVE_WCSCASECMP && !defined(_WIN32)
1300 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
1301#endif
1302
f4d2c9ba
VK
1303#if !HAVE_WCSNCASECMP && !defined(_WIN32)
1304 int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
1305#endif
1306
3d1cb7d6 1307#ifdef _WIN32
5ea2db30
VK
1308#ifdef UNICODE
1309 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1310 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1311 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1312
1313#define _topendir wopendir
1314#define _treaddir wreaddir
1315#define _tclosedir wclosedir
1316#else
1317#define _topendir opendir
1318#define _treaddir readdir
1319#define _tclosedir closedir
1320#endif
1321
3d1cb7d6 1322 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 1323 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 1324 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1325
76b1d3d8
VK
1326#else /* not _WIN32 */
1327
1328 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1329 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1330 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1331
37d62364 1332#endif
3d1cb7d6 1333
7f9e0c00
VK
1334#if defined(_WIN32) || !(HAVE_SCANDIR)
1335 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1336 int (*select)(const struct dirent *),
1337 int (*compar)(const struct dirent **, const struct dirent **));
1338 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1339#endif
1340
3bf0df9f
VK
1341TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1342
55b33d4c 1343#ifdef UNDER_CE
3bf0df9f
VK
1344int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1345int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1346int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
55b33d4c
VK
1347#endif
1348
967893bb 1349BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1350 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1351void LIBNETXMS_EXPORTABLE nxlog_close(void);
1352void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1353BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1354BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1355
1356typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1357void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1358
1359void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1360void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1361#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1362 __attribute__ ((format(printf, 1, 2)))
1363#endif
1364;
9f2902ac 1365
37a7a862
VK
1366#ifdef _WIN32
1367int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1368int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1369#ifdef UNICODE
1370#define _tmkstemp wmkstemp
1371#else
1372#define _tmkstemp mkstemp
1373#endif
1374#endif
1375
9b13553b
VK
1376#ifndef _WIN32
1377int strcat_s(char *dst, size_t dstSize, const char *src);
1378int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1379#endif
1380
c57d020a
VK
1381#if !HAVE_STRPTIME
1382char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1383#endif
1384
1385#if !HAVE_TIMEGM
1386time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1387#endif
1388
b50f1100 1389#ifdef __cplusplus
e9246d26 1390}
b50f1100 1391#endif
e9246d26 1392
a7ff20a5
VK
1393
1394//
1395// C++ only finctions
1396//
1397
1398#ifdef __cplusplus
1399
240816b0
VK
1400TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1401String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1402const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1403int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1404UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1405bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1406
1407#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1408void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1409#endif
1410
a7ff20a5
VK
1411#endif
1412
e9246d26 1413#endif /* _nms_util_h_ */