xlsxwriter 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +40 -0
  3. data/ext/xlsxwriter/chart.c +105 -0
  4. data/ext/xlsxwriter/chart.h +27 -0
  5. data/ext/xlsxwriter/extconf.rb +14 -0
  6. data/ext/xlsxwriter/format.c +67 -0
  7. data/ext/xlsxwriter/format.h +9 -0
  8. data/ext/xlsxwriter/libxlsxwriter/LICENSE.txt +89 -0
  9. data/ext/xlsxwriter/libxlsxwriter/Makefile +141 -0
  10. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter.h +23 -0
  11. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/app.h +79 -0
  12. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/chart.h +1093 -0
  13. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/common.h +336 -0
  14. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/content_types.h +74 -0
  15. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/core.h +51 -0
  16. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/custom.h +52 -0
  17. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/drawing.h +111 -0
  18. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/format.h +1214 -0
  19. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/hash_table.h +76 -0
  20. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/packager.h +80 -0
  21. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/relationships.h +77 -0
  22. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/shared_strings.h +83 -0
  23. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/styles.h +77 -0
  24. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/theme.h +47 -0
  25. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/third_party/ioapi.h +215 -0
  26. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/third_party/queue.h +694 -0
  27. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/third_party/tmpfileplus.h +53 -0
  28. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/third_party/tree.h +801 -0
  29. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/third_party/zip.h +375 -0
  30. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/utility.h +166 -0
  31. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/workbook.h +751 -0
  32. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/worksheet.h +2641 -0
  33. data/ext/xlsxwriter/libxlsxwriter/include/xlsxwriter/xmlwriter.h +178 -0
  34. data/ext/xlsxwriter/libxlsxwriter/lib/.gitignore +0 -0
  35. data/ext/xlsxwriter/libxlsxwriter/src/Makefile +125 -0
  36. data/ext/xlsxwriter/libxlsxwriter/src/app.c +439 -0
  37. data/ext/xlsxwriter/libxlsxwriter/src/chart.c +3420 -0
  38. data/ext/xlsxwriter/libxlsxwriter/src/content_types.c +341 -0
  39. data/ext/xlsxwriter/libxlsxwriter/src/core.c +293 -0
  40. data/ext/xlsxwriter/libxlsxwriter/src/custom.c +224 -0
  41. data/ext/xlsxwriter/libxlsxwriter/src/drawing.c +746 -0
  42. data/ext/xlsxwriter/libxlsxwriter/src/format.c +728 -0
  43. data/ext/xlsxwriter/libxlsxwriter/src/hash_table.c +223 -0
  44. data/ext/xlsxwriter/libxlsxwriter/src/packager.c +877 -0
  45. data/ext/xlsxwriter/libxlsxwriter/src/relationships.c +242 -0
  46. data/ext/xlsxwriter/libxlsxwriter/src/shared_strings.c +264 -0
  47. data/ext/xlsxwriter/libxlsxwriter/src/styles.c +1086 -0
  48. data/ext/xlsxwriter/libxlsxwriter/src/theme.c +348 -0
  49. data/ext/xlsxwriter/libxlsxwriter/src/utility.c +512 -0
  50. data/ext/xlsxwriter/libxlsxwriter/src/workbook.c +1895 -0
  51. data/ext/xlsxwriter/libxlsxwriter/src/worksheet.c +4992 -0
  52. data/ext/xlsxwriter/libxlsxwriter/src/xmlwriter.c +355 -0
  53. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/Makefile +44 -0
  54. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/crypt.h +131 -0
  55. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/ioapi.c +247 -0
  56. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/ioapi.h +209 -0
  57. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/iowin32.c +456 -0
  58. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/iowin32.h +28 -0
  59. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/miniunz.c +660 -0
  60. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/minizip.c +520 -0
  61. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/mztools.c +291 -0
  62. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/mztools.h +37 -0
  63. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/unzip.c +2125 -0
  64. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/unzip.h +437 -0
  65. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/zip.c +2007 -0
  66. data/ext/xlsxwriter/libxlsxwriter/third_party/minizip/zip.h +367 -0
  67. data/ext/xlsxwriter/libxlsxwriter/third_party/tmpfileplus/Makefile +42 -0
  68. data/ext/xlsxwriter/libxlsxwriter/third_party/tmpfileplus/tmpfileplus.c +342 -0
  69. data/ext/xlsxwriter/libxlsxwriter/third_party/tmpfileplus/tmpfileplus.h +53 -0
  70. data/ext/xlsxwriter/workbook.c +257 -0
  71. data/ext/xlsxwriter/workbook.h +42 -0
  72. data/ext/xlsxwriter/workbook_properties.c +103 -0
  73. data/ext/xlsxwriter/workbook_properties.h +10 -0
  74. data/ext/xlsxwriter/worksheet.c +1064 -0
  75. data/ext/xlsxwriter/worksheet.h +74 -0
  76. data/ext/xlsxwriter/xlsxwriter.c +239 -0
  77. data/lib/xlsxwriter.rb +6 -0
  78. data/lib/xlsxwriter/version.rb +3 -0
  79. data/lib/xlsxwriter/worksheet.rb +72 -0
  80. data/test/run-test.rb +11 -0
  81. data/test/support/xlsx_comparable.rb +109 -0
  82. data/test/test-array-formula.rb +33 -0
  83. data/test/test-autofilter.rb +70 -0
  84. data/test/test-chart-area.rb +25 -0
  85. data/test/test-data.rb +65 -0
  86. data/test/test-default-row.rb +25 -0
  87. data/test/test-defined-name.rb +46 -0
  88. data/test/test-escapes.rb +33 -0
  89. data/test/test-fit-to-pages.rb +21 -0
  90. data/test/test-formatting.rb +137 -0
  91. data/test/test-gridlines.rb +15 -0
  92. data/test/test-hyperlink.rb +67 -0
  93. data/test/test-image.rb +84 -0
  94. data/test/test-merge-range.rb +18 -0
  95. data/test/test-misc.rb +29 -0
  96. data/test/test-optimize.rb +32 -0
  97. data/test/test-page-breaks.rb +13 -0
  98. data/test/test-page-setup.rb +28 -0
  99. data/test/test-panes.rb +45 -0
  100. data/test/test-print-area.rb +19 -0
  101. data/test/test-print-options.rb +61 -0
  102. data/test/test-print-scale.rb +12 -0
  103. data/test/test-properties.rb +51 -0
  104. data/test/test-protect.rb +27 -0
  105. data/test/test-repeat.rb +23 -0
  106. data/test/test-row-col-format.rb +35 -0
  107. data/test/test-set-selection.rb +13 -0
  108. data/test/test-set-start-page.rb +13 -0
  109. data/test/test-simple.rb +62 -0
  110. data/test/test-types.rb +17 -0
  111. data/test/xlsx-func-testcase.rb +36 -0
  112. metadata +228 -0
@@ -0,0 +1,355 @@
1
+ /*****************************************************************************
2
+ * xmlwriter - A base library for libxlsxwriter libraries.
3
+ *
4
+ * Used in conjunction with the libxlsxwriter library.
5
+ *
6
+ * Copyright 2014-2017, John McNamara, jmcnamara@cpan.org. See LICENSE.txt.
7
+ *
8
+ */
9
+
10
+ #include <stdio.h>
11
+ #include <string.h>
12
+ #include <stdlib.h>
13
+ #include "xlsxwriter/xmlwriter.h"
14
+
15
+ #define LXW_AMP "&amp;"
16
+ #define LXW_LT "&lt;"
17
+ #define LXW_GT "&gt;"
18
+ #define LXW_QUOT "&quot;"
19
+
20
+ /* Defines. */
21
+ #define LXW_MAX_ENCODED_ATTRIBUTE_LENGTH (LXW_MAX_ATTRIBUTE_LENGTH*6)
22
+
23
+ /* Forward declarations. */
24
+ STATIC char *_escape_attributes(struct xml_attribute *attribute);
25
+
26
+ char *lxw_escape_data(const char *data);
27
+
28
+ STATIC void _fprint_escaped_attributes(FILE * xmlfile,
29
+ struct xml_attribute_list *attributes);
30
+
31
+ STATIC void _fprint_escaped_data(FILE * xmlfile, const char *data);
32
+
33
+ /*
34
+ * Write the XML declaration.
35
+ */
36
+ void
37
+ lxw_xml_declaration(FILE * xmlfile)
38
+ {
39
+ fprintf(xmlfile, "<?xml version=\"1.0\" "
40
+ "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
41
+ }
42
+
43
+ /*
44
+ * Write an XML start tag with optional attributes.
45
+ */
46
+ void
47
+ lxw_xml_start_tag(FILE * xmlfile,
48
+ const char *tag, struct xml_attribute_list *attributes)
49
+ {
50
+ fprintf(xmlfile, "<%s", tag);
51
+
52
+ _fprint_escaped_attributes(xmlfile, attributes);
53
+
54
+ fprintf(xmlfile, ">");
55
+ }
56
+
57
+ /*
58
+ * Write an XML start tag with optional, unencoded, attributes.
59
+ * This is a minor speed optimization for elements that don't need encoding.
60
+ */
61
+ void
62
+ lxw_xml_start_tag_unencoded(FILE * xmlfile,
63
+ const char *tag,
64
+ struct xml_attribute_list *attributes)
65
+ {
66
+ struct xml_attribute *attribute;
67
+
68
+ fprintf(xmlfile, "<%s", tag);
69
+
70
+ if (attributes) {
71
+ STAILQ_FOREACH(attribute, attributes, list_entries) {
72
+ fprintf(xmlfile, " %s=\"%s\"", attribute->key, attribute->value);
73
+ }
74
+ }
75
+
76
+ fprintf(xmlfile, ">");
77
+ }
78
+
79
+ /*
80
+ * Write an XML end tag.
81
+ */
82
+ void
83
+ lxw_xml_end_tag(FILE * xmlfile, const char *tag)
84
+ {
85
+ fprintf(xmlfile, "</%s>", tag);
86
+ }
87
+
88
+ /*
89
+ * Write an empty XML tag with optional attributes.
90
+ */
91
+ void
92
+ lxw_xml_empty_tag(FILE * xmlfile,
93
+ const char *tag, struct xml_attribute_list *attributes)
94
+ {
95
+ fprintf(xmlfile, "<%s", tag);
96
+
97
+ _fprint_escaped_attributes(xmlfile, attributes);
98
+
99
+ fprintf(xmlfile, "/>");
100
+ }
101
+
102
+ /*
103
+ * Write an XML start tag with optional, unencoded, attributes.
104
+ * This is a minor speed optimization for elements that don't need encoding.
105
+ */
106
+ void
107
+ lxw_xml_empty_tag_unencoded(FILE * xmlfile,
108
+ const char *tag,
109
+ struct xml_attribute_list *attributes)
110
+ {
111
+ struct xml_attribute *attribute;
112
+
113
+ fprintf(xmlfile, "<%s", tag);
114
+
115
+ if (attributes) {
116
+ STAILQ_FOREACH(attribute, attributes, list_entries) {
117
+ fprintf(xmlfile, " %s=\"%s\"", attribute->key, attribute->value);
118
+ }
119
+ }
120
+
121
+ fprintf(xmlfile, "/>");
122
+ }
123
+
124
+ /*
125
+ * Write an XML element containing data with optional attributes.
126
+ */
127
+ void
128
+ lxw_xml_data_element(FILE * xmlfile,
129
+ const char *tag,
130
+ const char *data, struct xml_attribute_list *attributes)
131
+ {
132
+ fprintf(xmlfile, "<%s", tag);
133
+
134
+ _fprint_escaped_attributes(xmlfile, attributes);
135
+
136
+ fprintf(xmlfile, ">");
137
+
138
+ _fprint_escaped_data(xmlfile, data);
139
+
140
+ fprintf(xmlfile, "</%s>", tag);
141
+ }
142
+
143
+ /*
144
+ * Escape XML characters in attributes.
145
+ */
146
+ STATIC char *
147
+ _escape_attributes(struct xml_attribute *attribute)
148
+ {
149
+ char *encoded = (char *) calloc(LXW_MAX_ENCODED_ATTRIBUTE_LENGTH, 1);
150
+ char *p_encoded = encoded;
151
+ char *p_attr = attribute->value;
152
+
153
+ while (*p_attr) {
154
+ switch (*p_attr) {
155
+ case '&':
156
+ strncat(p_encoded, LXW_AMP, sizeof(LXW_AMP) - 1);
157
+ p_encoded += sizeof(LXW_AMP) - 1;
158
+ break;
159
+ case '<':
160
+ strncat(p_encoded, LXW_LT, sizeof(LXW_LT) - 1);
161
+ p_encoded += sizeof(LXW_LT) - 1;
162
+ break;
163
+ case '>':
164
+ strncat(p_encoded, LXW_GT, sizeof(LXW_GT) - 1);
165
+ p_encoded += sizeof(LXW_GT) - 1;
166
+ break;
167
+ case '"':
168
+ strncat(p_encoded, LXW_QUOT, sizeof(LXW_QUOT) - 1);
169
+ p_encoded += sizeof(LXW_QUOT) - 1;
170
+ break;
171
+ default:
172
+ *p_encoded = *p_attr;
173
+ p_encoded++;
174
+ break;
175
+ }
176
+ p_attr++;
177
+ }
178
+
179
+ return encoded;
180
+ }
181
+
182
+ /*
183
+ * Escape XML characters in data sections of tags.
184
+ * Note, this is different from _escape_attributes()
185
+ * in that double quotes are not escaped by Excel.
186
+ */
187
+ char *
188
+ lxw_escape_data(const char *data)
189
+ {
190
+ size_t encoded_len = (strlen(data) * 5 + 1);
191
+
192
+ char *encoded = (char *) calloc(encoded_len, 1);
193
+ char *p_encoded = encoded;
194
+
195
+ while (*data) {
196
+ switch (*data) {
197
+ case '&':
198
+ strncat(p_encoded, LXW_AMP, sizeof(LXW_AMP) - 1);
199
+ p_encoded += sizeof(LXW_AMP) - 1;
200
+ break;
201
+ case '<':
202
+ strncat(p_encoded, LXW_LT, sizeof(LXW_LT) - 1);
203
+ p_encoded += sizeof(LXW_LT) - 1;
204
+ break;
205
+ case '>':
206
+ strncat(p_encoded, LXW_GT, sizeof(LXW_GT) - 1);
207
+ p_encoded += sizeof(LXW_GT) - 1;
208
+ break;
209
+ default:
210
+ *p_encoded = *data;
211
+ p_encoded++;
212
+ break;
213
+ }
214
+ data++;
215
+ }
216
+
217
+ return encoded;
218
+ }
219
+
220
+ /*
221
+ * Escape control characters in strings with with _xHHHH_.
222
+ */
223
+ char *
224
+ lxw_escape_control_characters(const char *string)
225
+ {
226
+ size_t escape_len = sizeof("_xHHHH_") - 1;
227
+ size_t encoded_len = (strlen(string) * escape_len + 1);
228
+
229
+ char *encoded = (char *) calloc(encoded_len, 1);
230
+ char *p_encoded = encoded;
231
+
232
+ while (*string) {
233
+ switch (*string) {
234
+ case '\x01':
235
+ case '\x02':
236
+ case '\x03':
237
+ case '\x04':
238
+ case '\x05':
239
+ case '\x06':
240
+ case '\x07':
241
+ case '\x08':
242
+ case '\x0B':
243
+ case '\x0C':
244
+ case '\x0D':
245
+ case '\x0E':
246
+ case '\x0F':
247
+ case '\x10':
248
+ case '\x11':
249
+ case '\x12':
250
+ case '\x13':
251
+ case '\x14':
252
+ case '\x15':
253
+ case '\x16':
254
+ case '\x17':
255
+ case '\x18':
256
+ case '\x19':
257
+ case '\x1A':
258
+ case '\x1B':
259
+ case '\x1C':
260
+ case '\x1D':
261
+ case '\x1E':
262
+ case '\x1F':
263
+ lxw_snprintf(p_encoded, escape_len + 1, "_x%04X_", *string);
264
+ p_encoded += escape_len;
265
+ break;
266
+ default:
267
+ *p_encoded = *string;
268
+ p_encoded++;
269
+ break;
270
+ }
271
+ string++;
272
+ }
273
+
274
+ return encoded;
275
+ }
276
+
277
+ /* Write out escaped attributes. */
278
+ STATIC void
279
+ _fprint_escaped_attributes(FILE * xmlfile,
280
+ struct xml_attribute_list *attributes)
281
+ {
282
+ struct xml_attribute *attribute;
283
+
284
+ if (attributes) {
285
+ STAILQ_FOREACH(attribute, attributes, list_entries) {
286
+ fprintf(xmlfile, " %s=", attribute->key);
287
+
288
+ if (!strpbrk(attribute->value, "&<>\"")) {
289
+ fprintf(xmlfile, "\"%s\"", attribute->value);
290
+ }
291
+ else {
292
+ char *encoded = _escape_attributes(attribute);
293
+
294
+ if (encoded) {
295
+ fprintf(xmlfile, "\"%s\"", encoded);
296
+
297
+ free(encoded);
298
+ }
299
+ }
300
+ }
301
+ }
302
+ }
303
+
304
+ /* Write out escaped XML data. */
305
+ STATIC void
306
+ _fprint_escaped_data(FILE * xmlfile, const char *data)
307
+ {
308
+ /* Escape the data section of the XML element. */
309
+ if (!strpbrk(data, "&<>")) {
310
+ fprintf(xmlfile, "%s", data);
311
+ }
312
+ else {
313
+ char *encoded = lxw_escape_data(data);
314
+ if (encoded) {
315
+ fprintf(xmlfile, "%s", encoded);
316
+ free(encoded);
317
+ }
318
+ }
319
+ }
320
+
321
+ /* Create a new string XML attribute. */
322
+ struct xml_attribute *
323
+ lxw_new_attribute_str(const char *key, const char *value)
324
+ {
325
+ struct xml_attribute *attribute = malloc(sizeof(struct xml_attribute));
326
+
327
+ LXW_ATTRIBUTE_COPY(attribute->key, key);
328
+ LXW_ATTRIBUTE_COPY(attribute->value, value);
329
+
330
+ return attribute;
331
+ }
332
+
333
+ /* Create a new integer XML attribute. */
334
+ struct xml_attribute *
335
+ lxw_new_attribute_int(const char *key, uint32_t value)
336
+ {
337
+ struct xml_attribute *attribute = malloc(sizeof(struct xml_attribute));
338
+
339
+ LXW_ATTRIBUTE_COPY(attribute->key, key);
340
+ lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%d", value);
341
+
342
+ return attribute;
343
+ }
344
+
345
+ /* Create a new double XML attribute. */
346
+ struct xml_attribute *
347
+ lxw_new_attribute_dbl(const char *key, double value)
348
+ {
349
+ struct xml_attribute *attribute = malloc(sizeof(struct xml_attribute));
350
+
351
+ LXW_ATTRIBUTE_COPY(attribute->key, key);
352
+ lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%.16g", value);
353
+
354
+ return attribute;
355
+ }
@@ -0,0 +1,44 @@
1
+ ###############################################################################
2
+ #
3
+ # Simplied Makefile to build the minizip objects for the libxlsxwriter library.
4
+ #
5
+
6
+ # Keep the output quiet by default.
7
+ Q=@
8
+ ifdef V
9
+ Q=
10
+ endif
11
+
12
+ UNAME := $(shell uname)
13
+
14
+ # Check for MinGW/MinGW64/Cygwin environments.
15
+ ifneq (,$(findstring MINGW, $(UNAME)))
16
+ MING_LIKE = y
17
+ endif
18
+ ifneq (,$(findstring MSYS, $(UNAME)))
19
+ MING_LIKE = y
20
+ endif
21
+ ifneq (,$(findstring CYGWIN, $(UNAME)))
22
+ MING_LIKE = y
23
+ endif
24
+
25
+ FPIC = -fPIC
26
+ CFLAGS += -O3 -DNOCRYPT -DNOUNCRYPT
27
+
28
+ # Change make options on MinGW/MinGW64/Cygwin.
29
+ ifdef MING_LIKE
30
+ FPIC =
31
+ CC = gcc
32
+ CFLAGS += -DUSE_FILE32API
33
+ endif
34
+
35
+ all: ioapi.o zip.o ioapi.so zip.so
36
+
37
+ %.o : %.c
38
+ $(Q)$(CC) -c $(CFLAGS) $<
39
+
40
+ %.so : %.c
41
+ $(Q)$(CC) $(FPIC) -c $(CFLAGS) $< -o $@
42
+
43
+ clean:
44
+ $(Q)/bin/rm -f *.o *.so
@@ -0,0 +1,131 @@
1
+ /* crypt.h -- base code for crypt/uncrypt ZIPfile
2
+
3
+
4
+ Version 1.01e, February 12th, 2005
5
+
6
+ Copyright (C) 1998-2005 Gilles Vollant
7
+
8
+ This code is a modified version of crypting code in Infozip distribution
9
+
10
+ The encryption/decryption parts of this source code (as opposed to the
11
+ non-echoing password parts) were originally written in Europe. The
12
+ whole source package can be freely distributed, including from the USA.
13
+ (Prior to January 2000, re-export from the US was a violation of US law.)
14
+
15
+ This encryption code is a direct transcription of the algorithm from
16
+ Roger Schlafly, described by Phil Katz in the file appnote.txt. This
17
+ file (appnote.txt) is distributed with the PKZIP program (even in the
18
+ version without encryption capabilities).
19
+
20
+ If you don't need crypting in your application, just define symbols
21
+ NOCRYPT and NOUNCRYPT.
22
+
23
+ This code support the "Traditional PKWARE Encryption".
24
+
25
+ The new AES encryption added on Zip format by Winzip (see the page
26
+ http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong
27
+ Encryption is not supported.
28
+ */
29
+
30
+ #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
31
+
32
+ /***********************************************************************
33
+ * Return the next byte in the pseudo-random sequence
34
+ */
35
+ static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab)
36
+ {
37
+ unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
38
+ * unpredictable manner on 16-bit systems; not a problem
39
+ * with any known compiler so far, though */
40
+
41
+ temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
42
+ return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
43
+ }
44
+
45
+ /***********************************************************************
46
+ * Update the encryption keys with the next byte of plain text
47
+ */
48
+ static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c)
49
+ {
50
+ (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
51
+ (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
52
+ (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
53
+ {
54
+ register int keyshift = (int)((*(pkeys+1)) >> 24);
55
+ (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
56
+ }
57
+ return c;
58
+ }
59
+
60
+
61
+ /***********************************************************************
62
+ * Initialize the encryption keys and the random header according to
63
+ * the given password.
64
+ */
65
+ static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t* pcrc_32_tab)
66
+ {
67
+ *(pkeys+0) = 305419896L;
68
+ *(pkeys+1) = 591751049L;
69
+ *(pkeys+2) = 878082192L;
70
+ while (*passwd != '\0') {
71
+ update_keys(pkeys,pcrc_32_tab,(int)*passwd);
72
+ passwd++;
73
+ }
74
+ }
75
+
76
+ #define zdecode(pkeys,pcrc_32_tab,c) \
77
+ (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
78
+
79
+ #define zencode(pkeys,pcrc_32_tab,c,t) \
80
+ (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
81
+
82
+ #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
83
+
84
+ #define RAND_HEAD_LEN 12
85
+ /* "last resort" source for second part of crypt seed pattern */
86
+ # ifndef ZCR_SEED2
87
+ # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
88
+ # endif
89
+
90
+ static int crypthead(const char* passwd, /* password string */
91
+ unsigned char* buf, /* where to write header */
92
+ int bufSize,
93
+ unsigned long* pkeys,
94
+ const z_crc_t* pcrc_32_tab,
95
+ unsigned long crcForCrypting)
96
+ {
97
+ int n; /* index in random header */
98
+ int t; /* temporary */
99
+ int c; /* random byte */
100
+ unsigned char header[RAND_HEAD_LEN-2]; /* random header */
101
+ static unsigned calls = 0; /* ensure different random header each time */
102
+
103
+ if (bufSize<RAND_HEAD_LEN)
104
+ return 0;
105
+
106
+ /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
107
+ * output of rand() to get less predictability, since rand() is
108
+ * often poorly implemented.
109
+ */
110
+ if (++calls == 1)
111
+ {
112
+ srand((unsigned)(time(NULL) ^ ZCR_SEED2));
113
+ }
114
+ init_keys(passwd, pkeys, pcrc_32_tab);
115
+ for (n = 0; n < RAND_HEAD_LEN-2; n++)
116
+ {
117
+ c = (rand() >> 7) & 0xff;
118
+ header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
119
+ }
120
+ /* Encrypt random header (last two bytes is high word of crc) */
121
+ init_keys(passwd, pkeys, pcrc_32_tab);
122
+ for (n = 0; n < RAND_HEAD_LEN-2; n++)
123
+ {
124
+ buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
125
+ }
126
+ buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
127
+ buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
128
+ return n;
129
+ }
130
+
131
+ #endif