libsnapshot: update offset functions

Since these functions are used across both parser and writer, updating
it as inline functions in cow_format.

Test: cow_api_test
Change-Id: I9824684e3b9b48947accce935335d4019d745ae0
diff --git a/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h b/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h
index 2079609..75467cb 100644
--- a/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h
+++ b/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h
@@ -232,6 +232,19 @@
     return op.source_info & kCowOpSourceInfoDataMask;
 }
 
+static constexpr off_t GetOpOffset(uint32_t op_index, const CowHeaderV3 header) {
+    return header.prefix.header_size + header.buffer_size +
+           (header.resume_point_max * sizeof(ResumePoint)) + (op_index * sizeof(CowOperationV3));
+}
+static constexpr off_t GetDataOffset(const CowHeaderV3 header) {
+    return header.prefix.header_size + header.buffer_size +
+           (header.resume_point_max * sizeof(ResumePoint)) +
+           header.op_count_max * sizeof(CowOperation);
+}
+static constexpr off_t GetResumeOffset(const CowHeaderV3 header) {
+    return header.prefix.header_size + header.buffer_size;
+}
+
 struct CowFooter {
     CowFooterOperation op;
     uint8_t unused[64];
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp
index 778ba62..52c6348 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp
@@ -94,18 +94,12 @@
     return std::nullopt;
 }
 
-off_t CowParserV3::GetDataOffset() const {
-    return sizeof(CowHeaderV3) + header_.buffer_size +
-           header_.resume_point_max * sizeof(ResumePoint) +
-           header_.op_count_max * sizeof(CowOperation);
-}
-
 bool CowParserV3::ParseOps(borrowed_fd fd, const uint32_t op_index) {
     ops_ = std::make_shared<std::vector<CowOperationV3>>();
     ops_->resize(op_index);
 
-    const off_t offset = header_.prefix.header_size + header_.buffer_size +
-                         header_.resume_point_max * sizeof(ResumePoint);
+    // read beginning of operation buffer -> so op_index = 0
+    const off_t offset = GetOpOffset(0, header_);
     if (!android::base::ReadFullyAtOffset(fd, ops_->data(), ops_->size() * sizeof(CowOperationV3),
                                           offset)) {
         PLOG(ERROR) << "read ops failed";
@@ -113,7 +107,7 @@
     }
 
     // fill out mapping of XOR op data location
-    uint64_t data_pos = GetDataOffset();
+    uint64_t data_pos = GetDataOffset(header_);
 
     xor_data_loc_ = std::make_shared<std::unordered_map<uint64_t, uint64_t>>();
 
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h
index fe3a2fb..afc01af 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h
@@ -50,7 +50,6 @@
   private:
     bool ParseOps(android::base::borrowed_fd fd, const uint32_t op_index);
     std::optional<uint32_t> FindResumeOp(const uint64_t label);
-    off_t GetDataOffset() const;
     CowHeaderV3 header_ = {};
     std::shared_ptr<std::vector<CowOperationV3>> ops_;
     bool ReadResumeBuffer(android::base::borrowed_fd fd);
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp
index e8dad92..6883c5e 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp
@@ -170,7 +170,7 @@
         LOG(ERROR) << "Header sync failed";
         return false;
     }
-    next_data_pos_ = GetDataOffset();
+    next_data_pos_ = GetDataOffset(header_);
     return true;
 }
 
@@ -192,7 +192,7 @@
 
     resume_points_ = parser.resume_points();
     options_.block_size = header_.block_size;
-    next_data_pos_ = GetDataOffset();
+    next_data_pos_ = GetDataOffset(header_);
 
     TranslatedCowOps ops;
     parser.Translate(&ops);
@@ -307,7 +307,7 @@
 
     if (!android::base::WriteFullyAtOffset(fd_, resume_points_->data(),
                                            resume_points_->size() * sizeof(ResumePoint),
-                                           GetResumeOffset())) {
+                                           GetResumeOffset(header_))) {
         PLOG(ERROR) << "writing resume buffer failed";
         return false;
     }
@@ -333,7 +333,7 @@
         return false;
     }
 
-    const off_t offset = GetOpOffset(header_.op_count);
+    const off_t offset = GetOpOffset(header_.op_count, header_);
     if (!android::base::WriteFullyAtOffset(fd_, &op, sizeof(op), offset)) {
         PLOG(ERROR) << "write failed for " << op << " at " << offset;
         return false;
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h
index e928d35..3dfc33c 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h
@@ -49,19 +49,6 @@
                     uint16_t offset, uint8_t type);
     bool CompressBlocks(size_t num_blocks, const void* data);
 
-    off_t GetOpOffset(uint32_t op_index) const {
-        CHECK_LT(op_index, header_.op_count_max);
-        return header_.prefix.header_size + header_.buffer_size +
-               (header_.resume_point_max * sizeof(ResumePoint)) +
-               (op_index * sizeof(CowOperationV3));
-    }
-    off_t GetDataOffset() const {
-        return sizeof(CowHeaderV3) + header_.buffer_size +
-               (header_.resume_point_max * sizeof(ResumePoint)) +
-               header_.op_count_max * sizeof(CowOperation);
-    }
-    off_t GetResumeOffset() const { return sizeof(CowHeaderV3) + header_.buffer_size; }
-
   private:
     CowHeaderV3 header_{};
     CowCompression compression_;