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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2023 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 <stdint.h>
  26. #include <string>
  27. #include <vector>
  28. struct timeval;
  29. namespace rfb {
  30. // Formats according to printf(), with a dynamic allocation
  31. std::string format(const char *fmt, ...)
  32. __attribute__((__format__ (__printf__, 1, 2)));
  33. // Splits a string with the specified delimiter
  34. std::vector<std::string> split(const char* src,
  35. const char delimiter);
  36. // Conversion to and from a hex string
  37. void binToHex(const uint8_t* in, size_t inlen, char* out, size_t outlen);
  38. std::string binToHex(const uint8_t* in, size_t inlen);
  39. bool hexToBin(const char* in, size_t inlen, uint8_t* out, size_t outlen);
  40. std::vector<uint8_t> hexToBin(const char* in, size_t inlen);
  41. // Makes sure line endings are in a certain format
  42. std::string convertLF(const char* src, size_t bytes = (size_t)-1);
  43. std::string convertCRLF(const char* src, size_t bytes = (size_t)-1);
  44. // Convertions between various Unicode formats
  45. size_t ucs4ToUTF8(unsigned src, char dst[5]);
  46. size_t utf8ToUCS4(const char* src, size_t max, unsigned* dst);
  47. size_t ucs4ToUTF16(unsigned src, wchar_t dst[3]);
  48. size_t utf16ToUCS4(const wchar_t* src, size_t max, unsigned* dst);
  49. std::string latin1ToUTF8(const char* src, size_t bytes = (size_t)-1);
  50. std::string utf8ToLatin1(const char* src, size_t bytes = (size_t)-1);
  51. std::string utf16ToUTF8(const wchar_t* src, size_t units = (size_t)-1);
  52. std::wstring utf8ToUTF16(const char* src, size_t bytes = (size_t)-1);
  53. bool isValidUTF8(const char* str, size_t bytes = (size_t)-1);
  54. bool isValidUTF16(const wchar_t* wstr, size_t units = (size_t)-1);
  55. // HELPER functions for timeout handling
  56. // soonestTimeout() is a function to help work out the soonest of several
  57. // timeouts.
  58. inline void soonestTimeout(int* timeout, int newTimeout) {
  59. if (newTimeout && (!*timeout || newTimeout < *timeout))
  60. *timeout = newTimeout;
  61. }
  62. // secsToMillis() turns seconds into milliseconds, capping the value so it
  63. // can't wrap round and become -ve
  64. inline int secsToMillis(int secs) {
  65. return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000);
  66. }
  67. // Returns time elapsed between two moments in milliseconds.
  68. unsigned msBetween(const struct timeval *first,
  69. const struct timeval *second);
  70. // Returns time elapsed since given moment in milliseconds.
  71. unsigned msSince(const struct timeval *then);
  72. // Returns true if first happened before seconds
  73. bool isBefore(const struct timeval *first,
  74. const struct timeval *second);
  75. std::string siPrefix(long long value, const char *unit,
  76. int precision=6);
  77. std::string iecPrefix(long long value, const char *unit,
  78. int precision=6);
  79. }
  80. // Some platforms (e.g. Windows) include max() and min() macros in their
  81. // standard headers, but they are also standard C++ template functions, so some
  82. // C++ headers will undefine them. So we steer clear of the names min and max
  83. // and define __rfbmin and __rfbmax instead.
  84. #ifndef __rfbmax
  85. #define __rfbmax(a,b) (((a) > (b)) ? (a) : (b))
  86. #endif
  87. #ifndef __rfbmin
  88. #define __rfbmin(a,b) (((a) < (b)) ? (a) : (b))
  89. #endif
  90. #endif