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
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
/* -*- c -*- 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "stralloc.h" 
#include "array.h" 
#include "pike_macros.h" 
#include "program.h" 
#include "object.h" 
#include "pike_types.h" 
#include "threads.h" 
#include "module_support.h" 
#include "builtin_functions.h" 
 
#ifdef HAVE_VCDIFF 
 
#include "vcdiff_wrapper.h" 
 
DECLARATIONS 
 
/* NB: Usually pulled in from crtbeginS.o, but we compile with -nostartfiles... 
 * 
 *     cf http://wiki.osdev.org/C%2B%2B#GCC 
 *     and http://www.linuxquestions.org/questions/programming-9/fyi-shared-libs-and-iostream-c-331113/#post1681605 
 * 
 *     It is needed due to vcdiff_wrapper.cc pulling in iostream. 
 */ 
void *__dso_handle = NULL; 
 
/*! @module VCDiff 
 *! 
 *! Pike glue for the open-vcdiff differential compression library. 
 *! http://code.google.com/p/open-vcdiff/ 
 *! 
 *! Encoding and decoding relies on a common string that the differences are 
 *! computed against - this string is called the dictionary. 
 *! 
 *! Basic usage: 
 *! @code 
 *!   string dict = "abcdef"; 
 *!   VCDiff.Encoder encoder = VCDiff.Encoder (dict); 
 *!   string encoded = encoder->encode ("abcdefghijklmnopqrstuvwxyz"); 
 *!   VCDiff.Decoder decoder = VCDiff.Decoder (dict); 
 *!   string decoded = decoder->decode (encoded); 
 *! @endcode 
 */ 
 
PIKECLASS Encoder { 
  CVAR void *dict; 
  CVAR void *vcencoder; 
  CVAR int test; 
  CVAR int encoder_state; 
 
  PIKEFUN void create (string dictionary) 
  { 
    void *_dict; 
    void *_vcencoder; 
    int dict_len; 
    char *dict_str; 
 
    if (dictionary->size_shift) 
      Pike_error("Wide strings not supported.\n"); 
 
    if (THIS->dict != NULL) 
      vcdiff_free_dictionary (THIS->dict); 
    if (THIS->vcencoder != NULL) 
      vcdiff_free_encoder (THIS->vcencoder); 
 
    THREADS_ALLOW(); 
    _dict = vcdiff_init_dictionary (dictionary->str, dictionary->len); 
    THREADS_DISALLOW(); 
 
    if (_dict != NULL) { 
      _vcencoder = vcdiff_init_encoder (_dict); 
      THIS->dict = _dict; 
      THIS->vcencoder = _vcencoder; 
      THIS->encoder_state = 1; 
    } else { 
      Pike_error ("Initializing dictionary failed.\n"); 
    } 
  } 
 
  PIKEFUN string encode (string data) 
  { 
    push_int (1); 
    apply (Pike_fp->current_object, "encode_chunk", 2); 
  } 
 
  PIKEFUN string encode_chunk (void|string data, void|int(0..1) flush) 
  { 
    char *in_buf = NULL; 
    int in_buf_len = 0; 
 
    char *out_buf = NULL; 
    int out_buf_len = 0; 
 
    char *trailer = NULL; 
    int trailer_len = 0; 
 
    void *vcenc = THIS->vcencoder; 
    int *enc_state_ptr = &THIS->encoder_state; 
 
    struct pike_string *s; 
    struct string_builder sb; 
 
    if (THIS->encoder_state == 0) { 
      Pike_error ("Calling encode_chunk in un-initialized object.\n"); 
    } 
 
    if (data) { 
      if (data->size_shift) 
        Pike_error("Wide strings not supported.\n"); 
      in_buf_len = data->len; 
      in_buf = data->str; 
    } else if (THIS->encoder_state == 3) { 
      RETURN make_shared_binary_string ("", 0); 
    } 
 
    THREADS_ALLOW(); 
    vcdiff_encode_chunk (vcenc, in_buf, in_buf_len, 
                         &out_buf, &out_buf_len, enc_state_ptr); 
    THREADS_DISALLOW(); 
 
    if (flush) { 
      // Flush internal buffers. 
      vcdiff_encode_chunk (vcenc, NULL, 0, 
                           &trailer, &trailer_len, enc_state_ptr); 
    } 
 
    init_string_builder (&sb, 0); 
    string_builder_binary_strcat (&sb, out_buf, out_buf_len); 
    free (out_buf); 
 
    if (trailer) { 
      string_builder_binary_strcat (&sb, trailer, trailer_len); 
      free (trailer); 
    } 
 
    s = finish_string_builder (&sb); 
    RETURN s; 
  } 
 
  INIT { 
    THIS->dict = NULL; 
    THIS->vcencoder = NULL; 
    THIS->encoder_state = 0; 
  } 
 
  EXIT { 
    if (THIS->dict != NULL) 
      vcdiff_free_dictionary (THIS->dict); 
    if (THIS->vcencoder != NULL) 
      vcdiff_free_encoder (THIS->vcencoder); 
  } 
} 
 
PIKECLASS Decoder { 
  PIKEVAR string dictionary; 
  CVAR void *vcdecoder; 
  CVAR int decoder_state; 
  CVAR char *dict_str; 
  CVAR int dict_len; 
 
  PIKEFUN void create (string dictionary) 
  { 
    if (dictionary->size_shift) 
      Pike_error("Wide strings not supported.\n"); 
 
    if (THIS->vcdecoder != NULL) 
      vcdiff_free_decoder (THIS->vcdecoder); 
 
    THIS->dict_len = dictionary->len; 
    THIS->dict_str = (char *)malloc (dictionary->len); 
    memcpy (THIS->dict_str, dictionary->str, dictionary->len); 
 
    THIS->vcdecoder = vcdiff_init_decoder (THIS->dict_str, THIS->dict_len); 
 
    THIS->decoder_state = 2; 
  } 
 
  PIKEFUN string decode (string data) 
  { 
    push_int (1); 
    apply (Pike_fp->current_object, "decode_chunk", 2); 
  } 
 
  PIKEFUN string decode_chunk (void|string data, void|int(0..1) flush) 
  { 
    char *in_buf = NULL; 
    int in_buf_len = 0; 
 
    char *out_buf = NULL; 
    int out_buf_len = 0; 
 
    char *trailer = NULL; 
    int trailer_len = 0; 
 
    void *vcdec = THIS->vcdecoder; 
    int *dec_state_ptr = &THIS->decoder_state; 
 
    struct pike_string *s; 
    struct string_builder sb; 
    int res = 0; 
 
    if (THIS->decoder_state == 0) { 
      Pike_error ("Calling decode_chunk in un-initialized object.\n"); 
    } 
 
    if (data) { 
      if (data->size_shift) 
        Pike_error("Wide strings not supported.\n"); 
      in_buf_len = data->len; 
      in_buf = data->str; 
    } else if (THIS->decoder_state == 3) { 
      RETURN make_shared_binary_string ("", 0); 
    } 
 
    THREADS_ALLOW(); 
    res = vcdiff_decode_chunk (vcdec, in_buf, in_buf_len, 
                               &out_buf, &out_buf_len, dec_state_ptr); 
    THREADS_DISALLOW(); 
    if (!res) 
      Pike_error ("vcdiff_decode_chunk failed\n"); 
 
    if (flush) { 
      // Flush internal buffers. 
      vcdiff_decode_chunk (vcdec, NULL, 0, 
                           &trailer, &trailer_len, dec_state_ptr); 
    } 
 
    init_string_builder (&sb, 0); 
    string_builder_binary_strcat (&sb, out_buf, out_buf_len); 
    free (out_buf); 
 
    if (trailer) { 
      string_builder_binary_strcat (&sb, trailer, trailer_len); 
      free (trailer); 
    } 
 
    s = finish_string_builder (&sb); 
 
    RETURN s; 
  } 
 
  INIT { 
    THIS->vcdecoder = NULL; 
    THIS->decoder_state = 0; 
  } 
 
  EXIT { 
    if (THIS->vcdecoder != NULL) 
      vcdiff_free_decoder (THIS->vcdecoder); 
    if (THIS->dict_str != NULL) 
      free (THIS->dict_str); 
  } 
} 
 
/*! @endmodule */ 
 
#endif /* HAVE_VCDIFF */ 
 
PIKE_MODULE_INIT 
{ 
#ifdef HAVE_VCDIFF 
  INIT 
#endif 
} 
 
PIKE_MODULE_EXIT 
{ 
#ifdef HAVE_VCDIFF 
  EXIT 
#endif 
}