basic_binary_iarchive.ipp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // basic_binary_iarchive.ipp:
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <string>
  9. #include <boost/assert.hpp>
  10. #include <algorithm>
  11. #include <cstring>
  12. #include <boost/config.hpp>
  13. #if defined(BOOST_NO_STDC_NAMESPACE)
  14. namespace std{
  15. using ::memcpy;
  16. using ::strlen;
  17. using ::size_t;
  18. }
  19. #endif
  20. #include <boost/detail/workaround.hpp>
  21. #include <boost/predef/other/endian.h>
  22. #include <boost/archive/basic_binary_iarchive.hpp>
  23. namespace boost {
  24. namespace archive {
  25. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  26. // implementation of binary_binary_archive
  27. template<class Archive>
  28. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  29. basic_binary_iarchive<Archive>::load_override(class_name_type & t){
  30. std::string cn;
  31. cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE);
  32. load_override(cn);
  33. if(cn.size() > (BOOST_SERIALIZATION_MAX_KEY_SIZE - 1))
  34. boost::serialization::throw_exception(
  35. archive_exception(archive_exception::invalid_class_name)
  36. );
  37. std::memcpy(t, cn.data(), cn.size());
  38. // borland tweak
  39. t.t[cn.size()] = '\0';
  40. }
  41. template<class Archive>
  42. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  43. basic_binary_iarchive<Archive>::init() {
  44. // read signature in an archive version independent manner
  45. std::string file_signature;
  46. #if 0 // commented out since it interfers with derivation
  47. BOOST_TRY {
  48. std::size_t l;
  49. this->This()->load(l);
  50. if(l == std::strlen(BOOST_ARCHIVE_SIGNATURE())) {
  51. // borland de-allocator fixup
  52. #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
  53. if(NULL != file_signature.data())
  54. #endif
  55. file_signature.resize(l);
  56. // note breaking a rule here - could be a problem on some platform
  57. if(0 < l)
  58. this->This()->load_binary(&(*file_signature.begin()), l);
  59. }
  60. }
  61. BOOST_CATCH(archive_exception const &) { // catch stream_error archive exceptions
  62. // will cause invalid_signature archive exception to be thrown below
  63. file_signature = "";
  64. }
  65. BOOST_CATCH_END
  66. #else
  67. // https://svn.boost.org/trac/boost/ticket/7301
  68. * this->This() >> file_signature;
  69. #endif
  70. if(file_signature != BOOST_ARCHIVE_SIGNATURE())
  71. boost::serialization::throw_exception(
  72. archive_exception(archive_exception::invalid_signature)
  73. );
  74. // make sure the version of the reading archive library can
  75. // support the format of the archive being read
  76. boost::serialization::library_version_type input_library_version;
  77. //* this->This() >> input_library_version;
  78. {
  79. int v = 0;
  80. v = this->This()->m_sb.sbumpc();
  81. #if BOOST_ENDIAN_LITTLE_BYTE
  82. if(v < 6){
  83. ;
  84. }
  85. else
  86. if(v < 7){
  87. // version 6 - next byte should be zero
  88. this->This()->m_sb.sbumpc();
  89. }
  90. else
  91. if(v < 8){
  92. int x1;
  93. // version 7 = might be followed by zero or some other byte
  94. x1 = this->This()->m_sb.sgetc();
  95. // it's =a zero, push it back
  96. if(0 == x1)
  97. this->This()->m_sb.sbumpc();
  98. }
  99. else{
  100. // version 8+ followed by a zero
  101. this->This()->m_sb.sbumpc();
  102. }
  103. #elif BOOST_ENDIAN_BIG_BYTE
  104. if(v == 0)
  105. v = this->This()->m_sb.sbumpc();
  106. #endif
  107. input_library_version = static_cast<boost::serialization::library_version_type>(v);
  108. }
  109. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
  110. this->set_library_version(input_library_version);
  111. #else
  112. detail::basic_iarchive::set_library_version(input_library_version);
  113. #endif
  114. if(BOOST_ARCHIVE_VERSION() < input_library_version)
  115. boost::serialization::throw_exception(
  116. archive_exception(archive_exception::unsupported_version)
  117. );
  118. }
  119. } // namespace archive
  120. } // namespace boost