virtual_binary.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2001 Olaf van der Spek <[email protected]>
  4. This program 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 3 of the License, or
  7. (at your option) any later version.
  8. This program 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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <cassert>
  17. #include <memory>
  18. #include <string>
  19. #include <vartypes.h>
  20. #include <xcc/data_ref.h>
  21. using namespace std;
  22. class Cvirtual_binary_source
  23. {
  24. public:
  25. Cvirtual_binary_source(const void* d, size_t cb_d, const std::shared_ptr<const void>& source = NULL);
  26. Cvirtual_binary_source* attach();
  27. void detach();
  28. Cvirtual_binary_source* pre_edit();
  29. const byte* data() const
  30. {
  31. return m_data;
  32. }
  33. const byte* data_end() const
  34. {
  35. return data() + size();
  36. }
  37. byte* data_edit()
  38. {
  39. assert(mc_references == 1 && !m_source);
  40. return m_data;
  41. }
  42. size_t size() const
  43. {
  44. return m_size;
  45. }
  46. void size(size_t v)
  47. {
  48. assert(mc_references == 1 && !m_source && v <= m_size);
  49. m_size = v;
  50. }
  51. bool unique() const
  52. {
  53. return mc_references == 1;
  54. }
  55. private:
  56. byte* m_data;
  57. size_t m_size;
  58. int mc_references;
  59. std::shared_ptr<const void> m_source;
  60. };
  61. class Cvirtual_binary
  62. {
  63. public:
  64. Cvirtual_binary sub_bin(size_t offset, size_t size) const;
  65. int save(const string& fname) const;
  66. int load(const string& fname, bool use_mm = true);
  67. void clear();
  68. void memset(int v);
  69. size_t read(void* d) const;
  70. byte* write_start(size_t cb_d);
  71. void write(const void* d, size_t cb_d);
  72. const Cvirtual_binary& operator=(const Cvirtual_binary& v);
  73. Cvirtual_binary();
  74. Cvirtual_binary(const Cvirtual_binary&);
  75. Cvirtual_binary(data_ref);
  76. Cvirtual_binary(const void*, size_t);
  77. Cvirtual_binary(const void*, size_t, const std::shared_ptr<void>& source);
  78. explicit Cvirtual_binary(const string& fname, bool use_mm = true);
  79. ~Cvirtual_binary();
  80. const byte* data() const
  81. {
  82. return m_source ? m_source->data() : NULL;
  83. }
  84. const byte* data_end() const
  85. {
  86. return m_source ? m_source->data_end() : NULL;
  87. }
  88. const byte* begin() const
  89. {
  90. return data();
  91. }
  92. const byte* end() const
  93. {
  94. return data_end();
  95. }
  96. byte* data_edit()
  97. {
  98. assert(m_source);
  99. m_source = m_source->pre_edit();
  100. return m_source->data_edit();
  101. }
  102. byte* mutable_end()
  103. {
  104. return data_edit() + size();
  105. }
  106. size_t size() const
  107. {
  108. return m_source ? m_source->size() : 0;
  109. }
  110. void set_size(size_t v)
  111. {
  112. assert(m_source);
  113. m_source = m_source->pre_edit();
  114. m_source->size(v);
  115. }
  116. private:
  117. Cvirtual_binary_source* m_source;
  118. };