aboutsummaryrefslogtreecommitdiff
path: root/src/codetablewriter_interface.h
blob: 7d1051697dafde2f17848a4cbc94c03d0e2adb48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2008 Google Inc. All Rights Reserved.
// Author: ajenjo@google.com (Lincoln Smith)
//
// Definition of an abstract class that describes the interface between the
// encoding engine (which finds the best string matches between the source and
// target data) and the code table writer.  The code table writer is passed a
// series of Add, Copy, and Run instructions and produces an output file in the
// desired format.

#ifndef OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_
#define OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_

#include <stddef.h>  // size_t
#include "checksum.h"  // VCDChecksum
#include "google/format_extension_flags.h"  // VCDiffFormatExtensionFlags

namespace open_vcdiff {

class OutputStringInterface;

// The method calls after construction should follow this pattern:
//    {{Add|Copy|Run}* Output}*
//
// Output() will produce an encoding using the given series of Add, Copy,
// and/or Run instructions.  One implementation of the interface
// (VCDiffCodeTableWriter) produces a VCDIFF delta window, but other
// implementations may be used to produce other output formats, or as test
// mocks, or to gather encoding statistics.
//
class CodeTableWriterInterface {
 public:
  virtual ~CodeTableWriterInterface() { }

  // Initializes the constructed object for use. It will return
  // false if there was an error initializing the object, or true if it
  // was successful.  After the object has been initialized and used,
  // Init() can be called again to restore the initial state of the object.
  virtual bool Init(size_t dictionary_size) = 0;

  // Writes the header to the output string.
  virtual void WriteHeader(OutputStringInterface* out,
                           VCDiffFormatExtensionFlags format_extensions) = 0;

  // Encode an ADD opcode with the "size" bytes starting at data
  virtual void Add(const char* data, size_t size) = 0;

  // Encode a COPY opcode with args "offset" (into dictionary) and "size" bytes.
  virtual void Copy(int32_t offset, size_t size) = 0;

  // Encode a RUN opcode for "size" copies of the value "byte".
  virtual void Run(size_t size, unsigned char byte) = 0;

  // Adds a checksum to the output.
  virtual void AddChecksum(VCDChecksum checksum) = 0;

  // Appends the encoded delta window to the output
  // string.  The output string is not null-terminated and may contain embedded
  // '\0' characters.
  virtual void Output(OutputStringInterface* out) = 0;

  // Finishes encoding.
  virtual void FinishEncoding(OutputStringInterface* out) = 0;

  // Returns the number of target bytes processed, which is the sum of all the
  // size arguments passed to Add(), Copy(), and Run().
  virtual size_t target_length() const = 0;
};

}  // namespace open_vcdiff

#endif  // OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_