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.

HexOutStream.cxx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <rdr/HexOutStream.h>
  22. #include <rdr/Exception.h>
  23. using namespace rdr;
  24. const int DEFAULT_BUF_LEN = 16384;
  25. static inline size_t min(size_t a, size_t b) {return a<b ? a : b;}
  26. HexOutStream::HexOutStream(OutStream& os)
  27. : out_stream(os), offset(0), bufSize(DEFAULT_BUF_LEN)
  28. {
  29. if (bufSize % 2)
  30. bufSize--;
  31. ptr = start = new U8[bufSize];
  32. end = start + bufSize;
  33. }
  34. HexOutStream::~HexOutStream() {
  35. delete [] start;
  36. }
  37. char HexOutStream::intToHex(int i) {
  38. if ((i>=0) && (i<=9))
  39. return '0'+i;
  40. else if ((i>=10) && (i<=15))
  41. return 'a'+(i-10);
  42. else
  43. throw rdr::Exception("intToHex failed");
  44. }
  45. char* HexOutStream::binToHexStr(const char* data, size_t length) {
  46. char* buffer = new char[length*2+1];
  47. for (size_t i=0; i<length; i++) {
  48. buffer[i*2] = intToHex((data[i] >> 4) & 15);
  49. buffer[i*2+1] = intToHex((data[i] & 15));
  50. if (!buffer[i*2] || !buffer[i*2+1]) {
  51. delete [] buffer;
  52. return 0;
  53. }
  54. }
  55. buffer[length*2] = 0;
  56. return buffer;
  57. }
  58. void
  59. HexOutStream::writeBuffer() {
  60. U8* pos = start;
  61. while (pos != ptr) {
  62. U8* optr = out_stream.getptr(2);
  63. size_t length = min(ptr-pos, out_stream.avail()/2);
  64. for (size_t i=0; i<length; i++) {
  65. optr[i*2] = intToHex((pos[i] >> 4) & 0xf);
  66. optr[i*2+1] = intToHex(pos[i] & 0xf);
  67. }
  68. out_stream.setptr(length*2);
  69. pos += length;
  70. }
  71. offset += ptr - start;
  72. ptr = start;
  73. }
  74. size_t HexOutStream::length()
  75. {
  76. return offset + ptr - start;
  77. }
  78. void
  79. HexOutStream::flush() {
  80. writeBuffer();
  81. out_stream.flush();
  82. }
  83. void HexOutStream::cork(bool enable)
  84. {
  85. OutStream::cork(enable);
  86. out_stream.cork(enable);
  87. }
  88. void HexOutStream::overrun(size_t needed) {
  89. if (needed > bufSize)
  90. throw Exception("HexOutStream overrun: buffer size exceeded");
  91. writeBuffer();
  92. }