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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. static inline size_t min(size_t a, size_t b) {return a<b ? a : b;}
  25. HexOutStream::HexOutStream(OutStream& os)
  26. : out_stream(os)
  27. {
  28. }
  29. HexOutStream::~HexOutStream()
  30. {
  31. }
  32. char HexOutStream::intToHex(int i) {
  33. if ((i>=0) && (i<=9))
  34. return '0'+i;
  35. else if ((i>=10) && (i<=15))
  36. return 'a'+(i-10);
  37. else
  38. throw rdr::Exception("intToHex failed");
  39. }
  40. char* HexOutStream::binToHexStr(const char* data, size_t length) {
  41. char* buffer = new char[length*2+1];
  42. for (size_t i=0; i<length; i++) {
  43. buffer[i*2] = intToHex((data[i] >> 4) & 15);
  44. buffer[i*2+1] = intToHex((data[i] & 15));
  45. if (!buffer[i*2] || !buffer[i*2+1]) {
  46. delete [] buffer;
  47. return 0;
  48. }
  49. }
  50. buffer[length*2] = 0;
  51. return buffer;
  52. }
  53. bool HexOutStream::flushBuffer()
  54. {
  55. while (sentUpTo != ptr) {
  56. U8* optr = out_stream.getptr(2);
  57. size_t length = min(ptr-sentUpTo, out_stream.avail()/2);
  58. for (size_t i=0; i<length; i++) {
  59. optr[i*2] = intToHex((sentUpTo[i] >> 4) & 0xf);
  60. optr[i*2+1] = intToHex(sentUpTo[i] & 0xf);
  61. }
  62. out_stream.setptr(length*2);
  63. sentUpTo += length;
  64. }
  65. return true;
  66. }
  67. void
  68. HexOutStream::flush() {
  69. BufferedOutStream::flush();
  70. out_stream.flush();
  71. }
  72. void HexOutStream::cork(bool enable)
  73. {
  74. BufferedOutStream::cork(enable);
  75. out_stream.cork(enable);
  76. }