You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2022 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. //
  20. // util.h - miscellaneous useful bits
  21. //
  22. #ifndef __RFB_UTIL_H__
  23. #define __RFB_UTIL_H__
  24. #include <limits.h>
  25. #include <string.h>
  26. #include <stdint.h>
  27. #include <string>
  28. #include <vector>
  29. struct timeval;
  30. namespace rfb {
  31. // -=- Class to handle cleanup of arrays of characters
  32. class CharArray {
  33. public:
  34. CharArray() : buf(0) {}
  35. CharArray(char* str) : buf(str) {} // note: assumes ownership
  36. CharArray(size_t len) {
  37. buf = new char[len]();
  38. memset(buf, 0, len);
  39. }
  40. ~CharArray() {
  41. delete [] buf;
  42. }
  43. void format(const char *fmt, ...)
  44. __attribute__((__format__ (__printf__, 2, 3)));
  45. // Get the buffer pointer & clear it (i.e. caller takes ownership)
  46. char* takeBuf() {char* tmp = buf; buf = 0; return tmp;}
  47. void replaceBuf(char* b) {delete [] buf; buf = b;}
  48. char* buf;
  49. private:
  50. CharArray(const CharArray&);
  51. CharArray& operator=(const CharArray&);
  52. };
  53. char* strDup(const char* s);
  54. void strFree(char* s);
  55. // Returns true if split successful. Returns false otherwise.
  56. // ALWAYS *copies* first part of string to out1 buffer.
  57. // If limiter not found, leaves out2 alone (null) and just copies to out1.
  58. // If out1 or out2 non-zero, calls strFree and zeroes them.
  59. // If fromEnd is true, splits at end of string rather than beginning.
  60. // Either out1 or out2 may be null, in which case the split will not return
  61. // that part of the string. Obviously, setting both to 0 is not useful...
  62. bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false);
  63. // Returns true if src contains c
  64. bool strContains(const char* src, char c);
  65. // Copies src to dest, up to specified length-1, and guarantees termination
  66. void strCopy(char* dest, const char* src, int destlen);
  67. // Conversion to and from a hex string
  68. void binToHex(const uint8_t* in, size_t inlen, char* out, size_t outlen);
  69. std::string binToHex(const uint8_t* in, size_t inlen);
  70. bool hexToBin(const char* in, size_t inlen, uint8_t* out, size_t outlen);
  71. std::vector<uint8_t> hexToBin(const char* in, size_t inlen);
  72. // Makes sure line endings are in a certain format
  73. std::string convertLF(const char* src, size_t bytes = (size_t)-1);
  74. std::string convertCRLF(const char* src, size_t bytes = (size_t)-1);
  75. // Convertions between various Unicode formats
  76. size_t ucs4ToUTF8(unsigned src, char dst[5]);
  77. size_t utf8ToUCS4(const char* src, size_t max, unsigned* dst);
  78. size_t ucs4ToUTF16(unsigned src, wchar_t dst[3]);
  79. size_t utf16ToUCS4(const wchar_t* src, size_t max, unsigned* dst);
  80. std::string latin1ToUTF8(const char* src, size_t bytes = (size_t)-1);
  81. std::string utf8ToLatin1(const char* src, size_t bytes = (size_t)-1);
  82. std::string utf16ToUTF8(const wchar_t* src, size_t units = (size_t)-1);
  83. std::wstring utf8ToUTF16(const char* src, size_t bytes = (size_t)-1);
  84. // HELPER functions for timeout handling
  85. // soonestTimeout() is a function to help work out the soonest of several
  86. // timeouts.
  87. inline void soonestTimeout(int* timeout, int newTimeout) {
  88. if (newTimeout && (!*timeout || newTimeout < *timeout))
  89. *timeout = newTimeout;
  90. }
  91. // secsToMillis() turns seconds into milliseconds, capping the value so it
  92. // can't wrap round and become -ve
  93. inline int secsToMillis(int secs) {
  94. return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000);
  95. }
  96. // Returns time elapsed between two moments in milliseconds.
  97. unsigned msBetween(const struct timeval *first,
  98. const struct timeval *second);
  99. // Returns time elapsed since given moment in milliseconds.
  100. unsigned msSince(const struct timeval *then);
  101. // Returns true if first happened before seconds
  102. bool isBefore(const struct timeval *first,
  103. const struct timeval *second);
  104. std::string siPrefix(long long value, const char *unit,
  105. int precision=6);
  106. std::string iecPrefix(long long value, const char *unit,
  107. int precision=6);
  108. }
  109. // Some platforms (e.g. Windows) include max() and min() macros in their
  110. // standard headers, but they are also standard C++ template functions, so some
  111. // C++ headers will undefine them. So we steer clear of the names min and max
  112. // and define __rfbmin and __rfbmax instead.
  113. #ifndef __rfbmax
  114. #define __rfbmax(a,b) (((a) > (b)) ? (a) : (b))
  115. #endif
  116. #ifndef __rfbmin
  117. #define __rfbmin(a,b) (((a) < (b)) ? (a) : (b))
  118. #endif
  119. #endif