mirror of
https://github.com/expo/bsdiff.git
synced 2026-11-13 20:11:27 -10:00
Most patch formats include the size of the new file in the header. To help streaming the patch to disk while generating it, this CL passes the size of the new file to the patch writer on initialization. To do this, we also move the Init() call to the patch writer to the DiffEncoder, which makes more sense since the Close() call is also made from the DiffEnconder. Bug: None Test: Updated tests to check for this value. Change-Id: Idfaedbd492d68ab6e6cb2c1cb3883947f068c3aa
68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
// Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef _BSDIFF_DIFF_ENCODER_H_
|
|
#define _BSDIFF_DIFF_ENCODER_H_
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "bsdiff/bz2_compressor.h"
|
|
#include "bsdiff/patch_writer_interface.h"
|
|
|
|
namespace bsdiff {
|
|
|
|
// Helper class to encapsulate the diff and extra stream generation logic
|
|
// derived from the old and new file buffers. Using this class is impossible to
|
|
// produce an invalid or incomplete bsdiff patch, since it has checks in place
|
|
// verifying its correct usage.
|
|
|
|
class DiffEncoder {
|
|
public:
|
|
// Initialize the DiffEncoder with the old and new file buffers, as well as
|
|
// the path writer used. The |patch| will be initialized when calling Init().
|
|
DiffEncoder(PatchWriterInterface* patch,
|
|
const uint8_t* old_buf,
|
|
uint64_t old_size,
|
|
const uint8_t* new_buf,
|
|
uint64_t new_size)
|
|
: patch_(patch),
|
|
old_buf_(old_buf),
|
|
old_size_(old_size),
|
|
new_buf_(new_buf),
|
|
new_size_(new_size) {}
|
|
|
|
// Initialize the diff encoder and the underlying patch.
|
|
bool Init();
|
|
|
|
// Add a new control triplet entry to the patch. The |entry.diff_size| bytes
|
|
// for the diff stream and the |entry.extra_size| bytes for the extra stream
|
|
// will be computed and added to the corresponding streams in the patch.
|
|
// Returns whether the operation succeeded. The operation can fail if either
|
|
// the old or new files are referenced out of bounds.
|
|
bool AddControlEntry(const ControlEntry& entry);
|
|
|
|
// Finalize the patch writing process and close the underlying patch writer.
|
|
bool Close();
|
|
|
|
private:
|
|
// Pointer to the patch we are writing to.
|
|
PatchWriterInterface* patch_;
|
|
|
|
// Old and new file buffers.
|
|
const uint8_t* old_buf_;
|
|
uint64_t old_size_;
|
|
const uint8_t* new_buf_;
|
|
uint64_t new_size_;
|
|
|
|
// Bytes of the new_buf_ already written.
|
|
uint64_t written_output_{0};
|
|
|
|
// The current position in the old buf.
|
|
int64_t old_pos_{0};
|
|
};
|
|
|
|
} // namespace bsdiff
|
|
|
|
#endif // _BSDIFF_DIFF_ENCODER_H_
|