datasource.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright 2019 Guido Vranken
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining
  4. * a copy of this software and associated documentation files (the
  5. * "Software"), to deal in the Software without restriction, including
  6. * without limitation the rights to use, copy, modify, merge, publish,
  7. * distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject
  9. * to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #pragma once
  24. #include <fuzzing/exception.hpp>
  25. #include <fuzzing/types.hpp>
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <string>
  31. #include <vector>
  32. namespace fuzzing {
  33. namespace datasource {
  34. class Base
  35. {
  36. protected:
  37. virtual std::vector<uint8_t> get(const size_t min, const size_t max, const uint64_t id = 0) = 0;
  38. public:
  39. Base(void) = default;
  40. virtual ~Base(void) = default;
  41. template<class T> T Get(const uint64_t id = 0);
  42. uint16_t GetChoice(const uint64_t id = 0);
  43. std::vector<uint8_t> GetData(const uint64_t id, const size_t min = 0, const size_t max = 0);
  44. template <class T> std::vector<T> GetVector(const uint64_t id = 0);
  45. class OutOfData : public fuzzing::exception::FlowException {
  46. public:
  47. OutOfData() = default;
  48. };
  49. class DeserializationFailure : public fuzzing::exception::FlowException {
  50. public:
  51. DeserializationFailure() = default;
  52. };
  53. };
  54. #ifndef FUZZING_HEADERS_NO_IMPL
  55. template<class T> T Base::Get(const uint64_t id)
  56. {
  57. T ret;
  58. const auto v = get(sizeof(ret), sizeof(ret), id);
  59. memcpy(&ret, v.data(), sizeof(ret));
  60. return ret;
  61. }
  62. template <> bool Base::Get<bool>(const uint64_t id)
  63. {
  64. uint8_t ret;
  65. const auto v = get(sizeof(ret), sizeof(ret), id);
  66. memcpy(&ret, v.data(), sizeof(ret));
  67. return (ret % 2) ? true : false;
  68. }
  69. template <> std::string Base::Get<std::string>(const uint64_t id)
  70. {
  71. auto data = GetData(id);
  72. return std::string(data.data(), data.data() + data.size());
  73. }
  74. template <> std::vector<std::string> Base::Get<std::vector<std::string>>(const uint64_t id)
  75. {
  76. std::vector<std::string> ret;
  77. while ( true ) {
  78. auto data = GetData(id);
  79. ret.push_back( std::string(data.data(), data.data() + data.size()) );
  80. if ( Get<bool>(id) == false ) {
  81. break;
  82. }
  83. }
  84. return ret;
  85. }
  86. uint16_t Base::GetChoice(const uint64_t id)
  87. {
  88. return Get<uint16_t>(id);
  89. }
  90. std::vector<uint8_t> Base::GetData(const uint64_t id, const size_t min, const size_t max)
  91. {
  92. return get(min, max, id);
  93. }
  94. template <> types::String<> Base::Get<types::String<>>(const uint64_t id) {
  95. const auto data = GetData(id);
  96. types::String<> ret(data.data(), data.size());
  97. return ret;
  98. }
  99. template <> types::Data<> Base::Get<types::Data<>>(const uint64_t id) {
  100. const auto data = GetData(id);
  101. types::Data<> ret(data.data(), data.size());
  102. return ret;
  103. }
  104. template <class T>
  105. std::vector<T> Base::GetVector(const uint64_t id) {
  106. std::vector<T> ret;
  107. while ( Get<bool>(id) == true ) {
  108. ret.push_back( Get<T>(id) );
  109. }
  110. return ret;
  111. }
  112. #endif
  113. class Datasource : public Base
  114. {
  115. private:
  116. const uint8_t* data;
  117. const size_t size;
  118. size_t idx;
  119. size_t left;
  120. std::vector<uint8_t> get(const size_t min, const size_t max, const uint64_t id = 0) override;
  121. // Make copy constructor and assignment operator private.
  122. Datasource(const Datasource &) : data(0), size(0), idx(0), left(0) {}
  123. Datasource& operator=(const Datasource &) { return *this; }
  124. public:
  125. Datasource(const uint8_t* _data, const size_t _size);
  126. };
  127. #ifndef FUZZING_HEADERS_NO_IMPL
  128. Datasource::Datasource(const uint8_t* _data, const size_t _size) :
  129. Base(), data(_data), size(_size), idx(0), left(size)
  130. {
  131. }
  132. std::vector<uint8_t> Datasource::get(const size_t min, const size_t max, const uint64_t id) {
  133. (void)id;
  134. uint32_t getSize;
  135. if ( left < sizeof(getSize) ) {
  136. throw OutOfData();
  137. }
  138. memcpy(&getSize, data + idx, sizeof(getSize));
  139. idx += sizeof(getSize);
  140. left -= sizeof(getSize);
  141. if ( getSize < min ) {
  142. getSize = min;
  143. }
  144. if ( max && getSize > max ) {
  145. getSize = max;
  146. }
  147. if ( left < getSize ) {
  148. throw OutOfData();
  149. }
  150. std::vector<uint8_t> ret(getSize);
  151. if ( getSize > 0 ) {
  152. memcpy(ret.data(), data + idx, getSize);
  153. }
  154. idx += getSize;
  155. left -= getSize;
  156. return ret;
  157. }
  158. #endif
  159. } /* namespace datasource */
  160. } /* namespace fuzzing */