10a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Copyright 2008 Google Inc. 20a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Author: Lincoln Smith 30a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 40a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Licensed under the Apache License, Version 2.0 (the "License"); 50a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// you may not use this file except in compliance with the License. 60a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// You may obtain a copy of the License at 70a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 80a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// http://www.apache.org/licenses/LICENSE-2.0 90a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Unless required by applicable law or agreed to in writing, software 110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// distributed under the License is distributed on an "AS IS" BASIS, 120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// See the License for the specific language governing permissions and 140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// limitations under the License. 150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Definition of an abstract class that describes the interface between the 170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// encoding engine (which finds the best string matches between the source and 180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// target data) and the code table writer. The code table writer is passed a 190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// series of Add, Copy, and Run instructions and produces an output file in the 200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// desired format. 210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_ 230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_ 240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <stddef.h> // size_t 260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "checksum.h" // VCDChecksum 270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "google/format_extension_flags.h" // VCDiffFormatExtensionFlags 280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnamespace open_vcdiff { 300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass OutputStringInterface; 320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The method calls after construction should follow this pattern: 340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// {{Add|Copy|Run}* Output}* 350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Output() will produce an encoding using the given series of Add, Copy, 370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// and/or Run instructions. One implementation of the interface 380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (VCDiffCodeTableWriter) produces a VCDIFF delta window, but other 390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// implementations may be used to produce other output formats, or as test 400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// mocks, or to gather encoding statistics. 410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass CodeTableWriterInterface { 430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public: 440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual ~CodeTableWriterInterface() { } 450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Initializes the constructed object for use. It will return 470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // false if there was an error initializing the object, or true if it 480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // was successful. After the object has been initialized and used, 490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Init() can be called again to restore the initial state of the object. 500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual bool Init(size_t dictionary_size) = 0; 510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Writes the header to the output string. 530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void WriteHeader(OutputStringInterface* out, 540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath VCDiffFormatExtensionFlags format_extensions) = 0; 550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Encode an ADD opcode with the "size" bytes starting at data 570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void Add(const char* data, size_t size) = 0; 580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Encode a COPY opcode with args "offset" (into dictionary) and "size" bytes. 600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void Copy(int32_t offset, size_t size) = 0; 610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Encode a RUN opcode for "size" copies of the value "byte". 630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void Run(size_t size, unsigned char byte) = 0; 640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Adds a checksum to the output. 660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void AddChecksum(VCDChecksum checksum) = 0; 670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Appends the encoded delta window to the output 690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // string. The output string is not null-terminated and may contain embedded 700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // '\0' characters. 710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void Output(OutputStringInterface* out) = 0; 720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Finishes encoding. 740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual void FinishEncoding(OutputStringInterface* out) = 0; 750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // Returns the number of target bytes processed, which is the sum of all the 770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // size arguments passed to Add(), Copy(), and Run(). 780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath virtual size_t target_length() const = 0; 790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}; 800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath} // namespace open_vcdiff 820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif // OPEN_VCDIFF_CODETABLEWRITER_INTERFACE_H_ 84