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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2019-2022 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. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rdr/HexInStream.h>
  23. #include <rdr/Exception.h>
  24. #include <rfb/util.h>
  25. using namespace rdr;
  26. static inline int min(int a, int b) {return a<b ? a : b;}
  27. HexInStream::HexInStream(InStream& is)
  28. : in_stream(is)
  29. {
  30. }
  31. HexInStream::~HexInStream() {
  32. }
  33. bool HexInStream::fillBuffer() {
  34. if (!in_stream.hasData(2))
  35. return false;
  36. size_t length = min(in_stream.avail()/2, availSpace());
  37. const uint8_t* iptr = in_stream.getptr(length*2);
  38. uint8_t* optr = (uint8_t*) end;
  39. for (size_t i=0; i<length; i++) {
  40. if (!rfb::hexToBin((const char*)&iptr[i*2], 2, &optr[i], 1))
  41. throw Exception("HexInStream: Invalid input data");
  42. }
  43. in_stream.setptr(length*2);
  44. end += length;
  45. return true;
  46. }