1 // Copyright 2009 Google Inc. 2 // Author: James deBoer 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 // A class for a code table writer which outputs JSON. 17 18 #ifndef OPEN_VCDIFF_JSONWRITER_H_ 19 #define OPEN_VCDIFF_JSONWRITER_H_ 20 21 #include <config.h> 22 #include <string> 23 #include "addrcache.h" 24 #include "checksum.h" 25 #include "codetable.h" 26 #include "codetablewriter_interface.h" 27 28 namespace open_vcdiff { 29 30 // class JSONCodeTableWriter: 31 // 32 // A code table writer which outputs a JSON representation of the diff. 33 // The output is a JSON array of commands. 34 // * Each ADD is represented by a single JSON string containing 35 // the data to add. 36 // * Each COPY is represented by two numbers. The first is an offset into 37 // the dictionary. The second is a length. 38 // * Each RUN is represented by a JSON string containing the data to add, 39 // similar to the ADD command. 40 // 41 class JSONCodeTableWriter : public CodeTableWriterInterface { 42 public: 43 JSONCodeTableWriter(); 44 ~JSONCodeTableWriter(); 45 46 // Initializes the writer. 47 virtual bool Init(size_t dictionary_size); 48 49 // Encode an ADD opcode with the "size" bytes starting at data 50 virtual void Add(const char* data, size_t size); 51 52 // Encode a COPY opcode with args "offset" (into dictionary) and "size" bytes. 53 virtual void Copy(int32_t offset, size_t size); 54 55 // Encode a RUN opcode for "size" copies of the value "byte". 56 virtual void Run(size_t size, unsigned char byte); 57 58 // Writes the header to the output string. 59 virtual void WriteHeader(OutputStringInterface* out, 60 VCDiffFormatExtensionFlags format_extensions); 61 62 virtual void AddChecksum(VCDChecksum) { } 63 64 // Appends the encoded delta window to the output 65 // string. The output string is not null-terminated. 66 virtual void Output(OutputStringInterface* out); 67 68 // Finishes the encoding. 69 virtual void FinishEncoding(OutputStringInterface *out); 70 71 // Returns the number of target bytes processed, which is the sum of all the 72 // size arguments passed to Add(), Copy(), and Run(). 73 // TODO(ajenjo): Eliminate the need for this method. 74 virtual size_t target_length() const; 75 private: 76 typedef std::string string; 77 78 // Escape the input data to conform with the JSON string spec 79 // and add it to the 'out' string. 80 void JSONEscape(const char* data, size_t size, string* out); 81 82 // Stores the JSON data before it is sent to the OutputString. 83 string output_; 84 85 // The sum of all the size arguments passed to Add(), Copy() and Run(). 86 size_t target_length_; 87 88 // Set if some data has been output. 89 bool output_called_; 90 }; 91 92 } // namespace open_vcdiff 93 #endif // OPEN_VCDIFF_JSONWRITER_H_ 94