Home | History | Annotate | Download | only in src
      1 // Copyright 2008 Google Inc.
      2 // Author: Lincoln Smith
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 // Definition of an abstract class that describes the interface between the
     17 // encoding engine (which finds the best string matches between the source and
     18 // target data) and the code table writer.  The code table writer is passed a
     19 // series of Add, Copy, and Run instructions and produces an output file in the
     20 // desired format.
     21 
     22 #ifndef OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_
     23 #define OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_
     24 
     25 #include <stddef.h>  // size_t
     26 #include "checksum.h"  // VCDChecksum
     27 #include "google/format_extension_flags.h"  // VCDiffFormatExtensionFlags
     28 
     29 namespace open_vcdiff {
     30 
     31 class OutputStringInterface;
     32 
     33 // The method calls after construction should follow this pattern:
     34 //    {{Add|Copy|Run}* Output}*
     35 //
     36 // Output() will produce an encoding using the given series of Add, Copy,
     37 // and/or Run instructions.  One implementation of the interface
     38 // (VCDiffCodeTableWriter) produces a VCDIFF delta window, but other
     39 // implementations may be used to produce other output formats, or as test
     40 // mocks, or to gather encoding statistics.
     41 //
     42 class CodeTableWriterInterface {
     43  public:
     44   virtual ~CodeTableWriterInterface() { }
     45 
     46   // Initializes the constructed object for use. It will return
     47   // false if there was an error initializing the object, or true if it
     48   // was successful.  After the object has been initialized and used,
     49   // Init() can be called again to restore the initial state of the object.
     50   virtual bool Init(size_t dictionary_size) = 0;
     51 
     52   // Writes the header to the output string.
     53   virtual void WriteHeader(OutputStringInterface* out,
     54                            VCDiffFormatExtensionFlags format_extensions) = 0;
     55 
     56   // Encode an ADD opcode with the "size" bytes starting at data
     57   virtual void Add(const char* data, size_t size) = 0;
     58 
     59   // Encode a COPY opcode with args "offset" (into dictionary) and "size" bytes.
     60   virtual void Copy(int32_t offset, size_t size) = 0;
     61 
     62   // Encode a RUN opcode for "size" copies of the value "byte".
     63   virtual void Run(size_t size, unsigned char byte) = 0;
     64 
     65   // Adds a checksum to the output.
     66   virtual void AddChecksum(VCDChecksum checksum) = 0;
     67 
     68   // Appends the encoded delta window to the output
     69   // string.  The output string is not null-terminated and may contain embedded
     70   // '\0' characters.
     71   virtual void Output(OutputStringInterface* out) = 0;
     72 
     73   // Finishes encoding.
     74   virtual void FinishEncoding(OutputStringInterface* out) = 0;
     75 
     76   // Returns the number of target bytes processed, which is the sum of all the
     77   // size arguments passed to Add(), Copy(), and Run().
     78   virtual size_t target_length() const = 0;
     79 };
     80 
     81 }  // namespace open_vcdiff
     82 
     83 #endif  // OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_
     84