10a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Copyright 2009 Google Inc.
20a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Author: James deBoer
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// A class for a code table writer which outputs JSON.
170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef OPEN_VCDIFF_JSONWRITER_H_
190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define OPEN_VCDIFF_JSONWRITER_H_
200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <config.h>
220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <string>
230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "addrcache.h"
240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "checksum.h"
250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "codetable.h"
260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "codetablewriter_interface.h"
270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnamespace open_vcdiff {
290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// class JSONCodeTableWriter:
310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A code table writer which outputs a JSON representation of the diff.
330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The output is a JSON array of commands.
340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// * Each ADD is represented by a single JSON string containing
350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//   the data to add.
360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// * Each COPY is represented by two numbers. The first is an offset into
370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//   the dictionary.  The second is a length.
380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// * Each RUN is represented by a JSON string containing the data to add,
390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//   similar to the ADD command.
400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass JSONCodeTableWriter : public CodeTableWriterInterface {
420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  JSONCodeTableWriter();
440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  ~JSONCodeTableWriter();
450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Initializes the writer.
470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual bool Init(size_t dictionary_size);
480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Encode an ADD opcode with the "size" bytes starting at data
500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void Add(const char* data, size_t size);
510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Encode a COPY opcode with args "offset" (into dictionary) and "size" bytes.
530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void Copy(int32_t offset, size_t size);
540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Encode a RUN opcode for "size" copies of the value "byte".
560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void Run(size_t size, unsigned char byte);
570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Writes the header to the output string.
590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void WriteHeader(OutputStringInterface* out,
600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                           VCDiffFormatExtensionFlags format_extensions);
610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void AddChecksum(VCDChecksum) { }
630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Appends the encoded delta window to the output
650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // string.  The output string is not null-terminated.
660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void Output(OutputStringInterface* out);
670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Finishes the encoding.
690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void FinishEncoding(OutputStringInterface *out);
700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the number of target bytes processed, which is the sum of all the
720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // size arguments passed to Add(), Copy(), and Run().
730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // TODO(ajenjo): Eliminate the need for this method.
740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual size_t target_length() const;
750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  typedef std::string string;
770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Escape the input data to conform with the JSON string spec
790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // and add it to the 'out' string.
800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void JSONEscape(const char* data, size_t size, string* out);
810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Stores the JSON data before it is sent to the OutputString.
830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  string output_;
840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The sum of all the size arguments passed to Add(), Copy() and Run().
860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  size_t target_length_;
870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Set if some data has been output.
890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool output_called_;
900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}  // namespace open_vcdiff
930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // OPEN_VCDIFF_JSONWRITER_H_
94