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