Tar.cpp 917 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Tar.h"
  8. namespace Archive {
  9. unsigned TarFileHeader::expected_checksum() const
  10. {
  11. auto checksum = 0u;
  12. const u8* u8_this = reinterpret_cast<const u8*>(this);
  13. const u8* u8_m_checksum = reinterpret_cast<const u8*>(&m_checksum);
  14. for (auto i = 0u; i < sizeof(TarFileHeader); ++i) {
  15. if (u8_this + i >= u8_m_checksum && u8_this + i < u8_m_checksum + sizeof(m_checksum)) {
  16. checksum += ' ';
  17. } else {
  18. checksum += u8_this[i];
  19. }
  20. }
  21. return checksum;
  22. }
  23. void TarFileHeader::calculate_checksum()
  24. {
  25. memset(m_checksum, ' ', sizeof(m_checksum));
  26. VERIFY(String::formatted("{:06o}", expected_checksum()).copy_characters_to_buffer(m_checksum, sizeof(m_checksum)));
  27. }
  28. }