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. #include <rdr/HexOutStream.h>
  19. #include <rdr/Exception.h>
  20. using namespace rdr;
  21. const int DEFAULT_BUF_LEN = 16384;
  22. static inline size_t min(size_t a, size_t b) {return a<b ? a : b;}
  23. HexOutStream::HexOutStream(OutStream& os, size_t buflen)
  24. : out_stream(os), offset(0), bufSize(buflen ? buflen : DEFAULT_BUF_LEN)
  25. {
  26. if (bufSize % 2)
  27. bufSize--;
  28. ptr = start = new U8[bufSize];
  29. end = start + bufSize;
  30. }
  31. HexOutStream::~HexOutStream() {
  32. delete [] start;
  33. }
  34. char HexOutStream::intToHex(int i) {
  35. if ((i>=0) && (i<=9))
  36. return '0'+i;
  37. else if ((i>=10) && (i<=15))
  38. return 'a'+(i-10);
  39. else
  40. throw rdr::Exception("intToHex failed");
  41. }
  42. char* HexOutStream::binToHexStr(const char* data, size_t length) {
  43. char* buffer = new char[length*2+1];
  44. for (size_t i=0; i<length; i++) {
  45. buffer[i*2] = intToHex((data[i] >> 4) & 15);
  46. buffer[i*2+1] = intToHex((data[i] & 15));
  47. if (!buffer[i*2] || !buffer[i*2+1]) {
  48. delete [] buffer;
  49. return 0;
  50. }
  51. }
  52. buffer[length*2] = 0;
  53. return buffer;
  54. }
  55. void
  56. HexOutStream::writeBuffer() {
  57. U8* pos = start;
  58. while (pos != ptr) {
  59. out_stream.check(2);
  60. U8* optr = out_stream.getptr();
  61. U8* oend = out_stream.getend();
  62. size_t length = min(ptr-pos, (oend-optr)/2);
  63. for (size_t i=0; i<length; i++) {
  64. optr[i*2] = intToHex((pos[i] >> 4) & 0xf);
  65. optr[i*2+1] = intToHex(pos[i] & 0xf);
  66. }
  67. out_stream.setptr(optr + length*2);
  68. pos += length;
  69. }
  70. offset += ptr - start;
  71. ptr = start;
  72. }
  73. size_t HexOutStream::length()
  74. {
  75. return offset + ptr - start;
  76. }
  77. void
  78. HexOutStream::flush() {
  79. writeBuffer();
  80. out_stream.flush();
  81. }
  82. size_t
  83. HexOutStream::overrun(size_t itemSize, size_t nItems) {
  84. if (itemSize > bufSize)
  85. throw Exception("HexOutStream overrun: max itemSize exceeded");
  86. writeBuffer();
  87. size_t nAvail;
  88. nAvail = (end - ptr) / itemSize;
  89. if (nAvail < nItems)
  90. return nAvail;
  91. return nItems;
  92. }