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.

HexInStream.cxx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2002-2004 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/HexInStream.h>
  19. #include <rdr/Exception.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. using namespace rdr;
  23. const int DEFAULT_BUF_LEN = 16384;
  24. static inline int min(int a, int b) {return a<b ? a : b;}
  25. HexInStream::HexInStream(InStream& is, int bufSize_)
  26. : bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_LEN), offset(0), in_stream(is)
  27. {
  28. ptr = end = start = new U8[bufSize];
  29. }
  30. HexInStream::~HexInStream() {
  31. delete [] start;
  32. }
  33. bool HexInStream::readHexAndShift(char c, int* v) {
  34. c=tolower(c);
  35. if ((c >= '0') && (c <= '9'))
  36. *v = (*v << 4) + (c - '0');
  37. else if ((c >= 'a') && (c <= 'f'))
  38. *v = (*v << 4) + (c - 'a' + 10);
  39. else
  40. return false;
  41. return true;
  42. }
  43. bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
  44. int l=strlen(s);
  45. if ((l % 2) == 0) {
  46. delete [] *data;
  47. *data = 0; *length = 0;
  48. if (l == 0)
  49. return true;
  50. *data = new char[l/2];
  51. *length = l/2;
  52. for(int i=0;i<l;i+=2) {
  53. int byte = 0;
  54. if (!readHexAndShift(s[i], &byte) ||
  55. !readHexAndShift(s[i+1], &byte))
  56. goto decodeError;
  57. (*data)[i/2] = byte;
  58. }
  59. return true;
  60. }
  61. decodeError:
  62. delete [] *data;
  63. *data = 0;
  64. *length = 0;
  65. return false;
  66. }
  67. int HexInStream::pos() {
  68. return offset + ptr - start;
  69. }
  70. int HexInStream::overrun(int itemSize, int nItems, bool wait) {
  71. if (itemSize > bufSize)
  72. throw Exception("HexInStream overrun: max itemSize exceeded");
  73. if (end - ptr != 0)
  74. memmove(start, ptr, end - ptr);
  75. end -= ptr - start;
  76. offset += ptr - start;
  77. ptr = start;
  78. while (end < ptr + itemSize) {
  79. int n = in_stream.check(2, 1, wait);
  80. if (n == 0) return 0;
  81. const U8* iptr = in_stream.getptr();
  82. const U8* eptr = in_stream.getend();
  83. int length = min((eptr - iptr)/2, start + bufSize - end);
  84. U8* optr = (U8*) end;
  85. for (int i=0; i<length; i++) {
  86. int v = 0;
  87. readHexAndShift(iptr[i*2], &v);
  88. readHexAndShift(iptr[i*2+1], &v);
  89. optr[i] = v;
  90. }
  91. in_stream.setptr(iptr + length*2);
  92. end += length;
  93. }
  94. if (itemSize * nItems > end - ptr)
  95. nItems = (end - ptr) / itemSize;
  96. return nItems;
  97. }