compound_document.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* POLE - Portable C++ library to access OLE Storage
  2. Copyright (C) 2002-2007 Ariya Hidayat (ariya@kde.org).
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  12. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  13. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  14. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  15. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  16. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #pragma once
  23. #include <array>
  24. #include <memory>
  25. #include <string>
  26. #include <unordered_map>
  27. #include <detail/binary.hpp>
  28. #include <detail/unicode.hpp>
  29. namespace xlnt {
  30. namespace detail {
  31. using directory_id = std::int32_t;
  32. using sector_id = std::int32_t;
  33. using sector_chain = std::vector<sector_id>;
  34. struct compound_document_header
  35. {
  36. enum class byte_order_type : uint16_t
  37. {
  38. big_endian = 0xFFFE,
  39. little_endian = 0xFEFF
  40. };
  41. std::uint64_t file_id = 0xE11AB1A1E011CFD0;
  42. std::array<std::uint8_t, 16> ignore1 = { { 0 } };
  43. std::uint16_t revision = 0x003E;
  44. std::uint16_t version = 0x0003;
  45. byte_order_type byte_order = byte_order_type::little_endian;
  46. std::uint16_t sector_size_power = 9;
  47. std::uint16_t short_sector_size_power = 6;
  48. std::array<std::uint8_t, 10> ignore2 = { { 0 } };
  49. std::uint32_t num_msat_sectors = 0;
  50. sector_id directory_start = -1;
  51. std::array<std::uint8_t, 4> ignore3 = { { 0 } };
  52. std::uint32_t threshold = 4096;
  53. sector_id ssat_start = -2;
  54. std::uint32_t num_short_sectors = 0;
  55. sector_id extra_msat_start = -2;
  56. std::uint32_t num_extra_msat_sectors = 0;
  57. std::array<sector_id, 109> msat = {{0}};
  58. };
  59. struct compound_document_entry
  60. {
  61. void name(const std::string &new_name)
  62. {
  63. auto u16_name = utf8_to_utf16(new_name);
  64. name_length = std::min(static_cast<std::uint16_t>(u16_name.size()), std::uint16_t(31));
  65. std::copy(u16_name.begin(), u16_name.begin() + name_length, name_array.begin());
  66. name_array[name_length] = 0;
  67. name_length = (name_length + 1) * 2;
  68. }
  69. std::string name() const
  70. {
  71. return utf16_to_utf8(std::u16string(name_array.begin(),
  72. name_array.begin() + (name_length - 1) / 2));
  73. }
  74. enum class entry_type : std::uint8_t
  75. {
  76. Empty = 0,
  77. UserStorage = 1,
  78. UserStream = 2,
  79. LockBytes = 3,
  80. Property = 4,
  81. RootStorage = 5
  82. };
  83. enum class entry_color : std::uint8_t
  84. {
  85. Red = 0,
  86. Black = 1
  87. };
  88. std::array<char16_t, 32> name_array = { { 0 } };
  89. std::uint16_t name_length = 2;
  90. entry_type type = entry_type::Empty;
  91. entry_color color = entry_color::Black;
  92. directory_id prev = -1;
  93. directory_id next = -1;
  94. directory_id child = -1;
  95. std::array<std::uint8_t, 36> ignore;
  96. sector_id start = -2;
  97. std::uint32_t size = 0;
  98. std::uint32_t ignore2;
  99. };
  100. class compound_document_istreambuf;
  101. class compound_document_ostreambuf;
  102. class compound_document
  103. {
  104. public:
  105. compound_document(std::istream &in);
  106. compound_document(std::ostream &out);
  107. ~compound_document();
  108. void close();
  109. std::istream &open_read_stream(const std::string &filename);
  110. std::ostream &open_write_stream(const std::string &filename);
  111. private:
  112. friend class compound_document_istreambuf;
  113. friend class compound_document_ostreambuf;
  114. template<typename T>
  115. void read_sector(sector_id id, binary_writer<T> &writer);
  116. template<typename T>
  117. void read_sector_chain(sector_id id, binary_writer<T> &writer);
  118. template<typename T>
  119. void read_sector_chain(sector_id start, binary_writer<T> &writer, sector_id offset, std::size_t count);
  120. template<typename T>
  121. void read_short_sector(sector_id id, binary_writer<T> &writer);
  122. template<typename T>
  123. void read_short_sector_chain(sector_id start, binary_writer<T> &writer);
  124. template<typename T>
  125. void read_short_sector_chain(sector_id start, binary_writer<T> &writer, sector_id offset, std::size_t count);
  126. sector_chain follow_chain(sector_id start, const sector_chain &table);
  127. template<typename T>
  128. void write_sector(binary_reader<T> &reader, sector_id id);
  129. template<typename T>
  130. void write_short_sector(binary_reader<T> &reader, sector_id id);
  131. void read_header();
  132. void read_msat();
  133. void read_sat();
  134. void read_ssat();
  135. void read_entry(directory_id id);
  136. void read_directory();
  137. void write_header();
  138. void write_msat();
  139. void write_sat();
  140. void write_ssat();
  141. void write_entry(directory_id id);
  142. void write_directory();
  143. std::size_t sector_size();
  144. std::size_t short_sector_size();
  145. std::size_t sector_data_start();
  146. void print_directory();
  147. sector_id allocate_msat_sector();
  148. sector_id allocate_sat_sector();
  149. sector_id allocate_ssat_sector();
  150. sector_id allocate_sector();
  151. sector_chain allocate_sectors(std::size_t sectors);
  152. sector_id allocate_short_sector();
  153. sector_chain allocate_short_sectors(std::size_t sectors);
  154. bool contains_entry(const std::string &path,
  155. compound_document_entry::entry_type type);
  156. directory_id find_entry(const std::string &path,
  157. compound_document_entry::entry_type type);
  158. directory_id next_empty_entry();
  159. directory_id insert_entry(const std::string &path,
  160. compound_document_entry::entry_type type);
  161. // Red black tree helper functions
  162. void tree_insert(directory_id new_id, directory_id storage_id);
  163. void tree_insert_fixup(directory_id x);
  164. std::string tree_path(directory_id id);
  165. void tree_rotate_left(directory_id x);
  166. void tree_rotate_right(directory_id y);
  167. directory_id &tree_left(directory_id id);
  168. directory_id &tree_right(directory_id id);
  169. directory_id &tree_parent(directory_id id);
  170. directory_id &tree_root(directory_id id);
  171. directory_id &tree_child(directory_id id);
  172. std::string tree_key(directory_id id);
  173. compound_document_entry::entry_color &tree_color(directory_id id);
  174. compound_document_header header_;
  175. sector_chain msat_;
  176. sector_chain sat_;
  177. sector_chain ssat_;
  178. std::vector<compound_document_entry> entries_;
  179. std::unordered_map<directory_id, directory_id> parent_storage_;
  180. std::unordered_map<directory_id, directory_id> parent_;
  181. std::istream *in_;
  182. std::ostream *out_;
  183. std::unique_ptr<compound_document_istreambuf> stream_in_buffer_;
  184. std::istream stream_in_;
  185. std::unique_ptr<compound_document_ostreambuf> stream_out_buffer_;
  186. std::ostream stream_out_;
  187. };
  188. } // namespace detail
  189. } // namespace xlnt