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