ox-bundlecachetest 2.14.23

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 (55) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +751 -0
  3. data/LICENSE +21 -0
  4. data/README.md +351 -0
  5. data/ext/ox/attr.h +78 -0
  6. data/ext/ox/base64.c +105 -0
  7. data/ext/ox/base64.h +18 -0
  8. data/ext/ox/buf.h +162 -0
  9. data/ext/ox/builder.c +948 -0
  10. data/ext/ox/cache.c +351 -0
  11. data/ext/ox/cache.h +21 -0
  12. data/ext/ox/cache8.c +106 -0
  13. data/ext/ox/cache8.h +23 -0
  14. data/ext/ox/dump.c +1260 -0
  15. data/ext/ox/err.c +46 -0
  16. data/ext/ox/err.h +36 -0
  17. data/ext/ox/extconf.rb +47 -0
  18. data/ext/ox/gen_load.c +342 -0
  19. data/ext/ox/hash_load.c +309 -0
  20. data/ext/ox/helper.h +84 -0
  21. data/ext/ox/intern.c +157 -0
  22. data/ext/ox/intern.h +25 -0
  23. data/ext/ox/obj_load.c +809 -0
  24. data/ext/ox/ox.c +1649 -0
  25. data/ext/ox/ox.h +245 -0
  26. data/ext/ox/parse.c +1197 -0
  27. data/ext/ox/sax.c +1570 -0
  28. data/ext/ox/sax.h +69 -0
  29. data/ext/ox/sax_as.c +270 -0
  30. data/ext/ox/sax_buf.c +209 -0
  31. data/ext/ox/sax_buf.h +204 -0
  32. data/ext/ox/sax_hint.c +207 -0
  33. data/ext/ox/sax_hint.h +40 -0
  34. data/ext/ox/sax_stack.h +113 -0
  35. data/ext/ox/slotcache.c +158 -0
  36. data/ext/ox/slotcache.h +19 -0
  37. data/ext/ox/special.c +390 -0
  38. data/ext/ox/special.h +14 -0
  39. data/ext/ox/type.h +39 -0
  40. data/lib/ox/bag.rb +103 -0
  41. data/lib/ox/cdata.rb +10 -0
  42. data/lib/ox/comment.rb +11 -0
  43. data/lib/ox/doctype.rb +11 -0
  44. data/lib/ox/document.rb +28 -0
  45. data/lib/ox/element.rb +464 -0
  46. data/lib/ox/error.rb +25 -0
  47. data/lib/ox/hasattrs.rb +54 -0
  48. data/lib/ox/instruct.rb +34 -0
  49. data/lib/ox/node.rb +23 -0
  50. data/lib/ox/raw.rb +12 -0
  51. data/lib/ox/sax.rb +97 -0
  52. data/lib/ox/version.rb +4 -0
  53. data/lib/ox/xmlrpc_adapter.rb +33 -0
  54. data/lib/ox.rb +79 -0
  55. metadata +128 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Peter Ohler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,351 @@
1
+ # Ox gem
2
+ A fast XML parser and Object marshaller as a Ruby gem.
3
+
4
+ [![CI](https://github.com/ohler55/ox/actions/workflows/CI.yml/badge.svg)](https://github.com/ohler55/ox/actions/workflows/CI.yml)
5
+
6
+ ## Installation
7
+ gem install ox
8
+
9
+ ## Documentation
10
+
11
+ *Documentation*: http://www.ohler.com/ox
12
+
13
+ ## Source
14
+
15
+ *GitHub* *repo*: https://github.com/ohler55/ox
16
+
17
+ *RubyGems* *repo*: https://rubygems.org/gems/ox
18
+
19
+ ## Support
20
+
21
+ [Get supported Ox with a Tidelift Subscription.](https://tidelift.com/subscription/pkg/rubygems-ox?utm_source=rubygems-ox&utm_medium=referral&utm_campaign=readme) Security updates are [supported](https://tidelift.com/security).
22
+
23
+ ## Links of Interest
24
+
25
+ [Ruby XML Gem Comparison](http://www.ohler.com/dev/xml_with_ruby/xml_with_ruby.html) for a performance comparison between Ox, Nokogiri, and LibXML.
26
+
27
+ [Fast Ruby XML Serialization](http://www.ohler.com/dev/ruby_object_xml_serialization/ruby_object_xml_serialization.html) to see how Ox can be used as a faster replacement for Marshal.
28
+
29
+ *Fast JSON parser and marshaller on RubyGems*: https://rubygems.org/gems/oj
30
+
31
+ *Fast JSON parser and marshaller on GitHub*: https://github.com/ohler55/oj
32
+
33
+ ## Release Notes
34
+
35
+ See [CHANGELOG.md](CHANGELOG.md)
36
+
37
+ ## Description
38
+
39
+ Optimized XML (Ox), as the name implies was written to provide speed optimized
40
+ XML and now HTML handling. It was designed to be an alternative to Nokogiri and other Ruby
41
+ XML parsers in generic XML parsing and as an alternative to Marshal for Object
42
+ serialization.
43
+
44
+ Unlike some other Ruby XML parsers, Ox is self contained. Ox uses nothing
45
+ other than standard C libraries so version issues with libXml are not an
46
+ issue.
47
+
48
+ Marshal uses a binary format for serializing Objects. That binary format
49
+ changes with releases making Marshal dumped Object incompatible between some
50
+ versions. The use of a binary format make debugging message streams or file
51
+ contents next to impossible unless the same version of Ruby and only Ruby is
52
+ used for inspecting the serialize Object. Ox on the other hand uses human
53
+ readable XML. Ox also includes options that allow strict, tolerant, or a mode
54
+ that automatically defines missing classes.
55
+
56
+ It is possible to write an XML serialization gem with Nokogiri or other XML
57
+ parsers but writing such a package in Ruby results in a module significantly
58
+ slower than Marshal. This is what triggered the start of Ox development.
59
+
60
+ Ox handles XML documents in three ways. It is a generic XML parser and writer,
61
+ a fast Object / XML marshaller, and a stream SAX parser. Ox was written for
62
+ speed as a replacement for Nokogiri, Ruby LibXML, and for Marshal.
63
+
64
+ As an XML parser it is 2 or more times faster than Nokogiri and as a generic
65
+ XML writer it is as much as 20 times faster than Nokogiri. Of course different
66
+ files may result in slightly different times.
67
+
68
+ As an Object serializer Ox is up to 6 times faster than the standard Ruby
69
+ Marshal.dump() and up to 3 times faster than Marshal.load().
70
+
71
+ The SAX like stream parser is 40 times faster than Nokogiri and more than 13
72
+ times faster than LibXML when validating a file with minimal Ruby
73
+ callbacks. Unlike Nokogiri and LibXML, Ox can be tuned to use only the SAX
74
+ callbacks that are of interest to the caller. (See the perf_sax.rb file for an
75
+ example.)
76
+
77
+ Ox is compatible with Ruby 2.3, 2.4, 2.5, 2.6, 2.7, 3.0.
78
+
79
+ ### Object Dump Sample:
80
+
81
+ ```ruby
82
+ require 'ox'
83
+
84
+ class Sample
85
+ attr_accessor :a, :b, :c
86
+
87
+ def initialize(a, b, c)
88
+ @a = a
89
+ @b = b
90
+ @c = c
91
+ end
92
+ end
93
+
94
+ # Create Object
95
+ obj = Sample.new(1, "bee", ['x', :y, 7.0])
96
+ # Now dump the Object to an XML String.
97
+ xml = Ox.dump(obj)
98
+ # Convert the object back into a Sample Object.
99
+ obj2 = Ox.parse_obj(xml)
100
+ ```
101
+
102
+ ### Generic XML Writing and Parsing:
103
+
104
+ ```ruby
105
+ require 'ox'
106
+
107
+ doc = Ox::Document.new
108
+
109
+ instruct = Ox::Instruct.new(:xml)
110
+ instruct[:version] = '1.0'
111
+ instruct[:encoding] = 'UTF-8'
112
+ instruct[:standalone] = 'yes'
113
+ doc << instruct
114
+
115
+ top = Ox::Element.new('top')
116
+ top[:name] = 'sample'
117
+ doc << top
118
+
119
+ mid = Ox::Element.new('middle')
120
+ mid[:name] = 'second'
121
+ top << mid
122
+
123
+ bot = Ox::Element.new('bottom')
124
+ bot[:name] = 'third'
125
+ bot << 'text at bottom'
126
+ mid << bot
127
+
128
+ other_elements = Ox::Element.new('otherElements')
129
+ other_elements << Ox::CData.new('<sender>John Smith</sender>')
130
+ other_elements << Ox::Comment.new('Director\'s commentary')
131
+ # other_elements << Ox::DocType.new('content')
132
+ other_elements << Ox::Raw.new('<warning>Be carefull with this! Direct inject into XML!</warning>')
133
+ top << other_elements
134
+
135
+
136
+ xml = Ox.dump(doc)
137
+
138
+ # xml =
139
+ # <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
140
+ # <top name="sample">
141
+ # <middle name="second">
142
+ # <bottom name="third">text at bottom</bottom>
143
+ # </middle>
144
+ # <otherElements>
145
+ # <![CDATA[<sender>John Smith</sender>]]>
146
+ # <!-- Director's commentary -->
147
+ # <warning>Be carefull with this! Direct inject into XML!</warning>
148
+ # </otherElements>
149
+ # </top>
150
+ ```
151
+
152
+ ### HTML Parsing:
153
+
154
+ Ox can be used to parse HTML with a few options changes. HTML is often loose in
155
+ regard to conformance. For HTML parsing try these options.
156
+
157
+ ```ruby
158
+ Ox.default_options = {
159
+ mode: :generic,
160
+ effort: :tolerant,
161
+ smart: true
162
+ }
163
+ ```
164
+
165
+ ### SAX XML Parsing:
166
+
167
+ ```ruby
168
+ require 'stringio'
169
+ require 'ox'
170
+
171
+ class Sample < ::Ox::Sax
172
+ def start_element(name); puts "start: #{name}"; end
173
+ def end_element(name); puts "end: #{name}"; end
174
+ def attr(name, value); puts " #{name} => #{value}"; end
175
+ def text(value); puts "text #{value}"; end
176
+ end
177
+
178
+ io = StringIO.new(%{
179
+ <top name="sample">
180
+ <middle name="second">
181
+ <bottom name="third"/>
182
+ </middle>
183
+ </top>
184
+ })
185
+
186
+ handler = Sample.new()
187
+ Ox.sax_parse(handler, io)
188
+ # outputs
189
+ # start: top
190
+ # name => sample
191
+ # start: middle
192
+ # name => second
193
+ # start: bottom
194
+ # name => third
195
+ # end: bottom
196
+ # end: middle
197
+ # end: top
198
+ ```
199
+
200
+ ### Yielding results immediately while SAX XML Parsing:
201
+
202
+ ```ruby
203
+ require 'stringio'
204
+ require 'ox'
205
+
206
+ class Yielder < ::Ox::Sax
207
+ def initialize(block); @yield_to = block; end
208
+ def start_element(name); @yield_to.call(name); end
209
+ end
210
+
211
+ io = StringIO.new(%{
212
+ <top name="sample">
213
+ <middle name="second">
214
+ <bottom name="third"/>
215
+ </middle>
216
+ </top>
217
+ })
218
+
219
+ proc = Proc.new { |name| puts name }
220
+ handler = Yielder.new(proc)
221
+ puts "before parse"
222
+ Ox.sax_parse(handler, io)
223
+ puts "after parse"
224
+ # outputs
225
+ # before parse
226
+ # top
227
+ # middle
228
+ # bottom
229
+ # after parse
230
+ ```
231
+
232
+ ### Parsing XML into a Hash (fast)
233
+
234
+ ```ruby
235
+ require 'ox'
236
+
237
+ xml = %{
238
+ <top name="sample">
239
+ <middle name="second">
240
+ <bottom name="third">Rock bottom</bottom>
241
+ </middle>
242
+ </top>
243
+ }
244
+
245
+ puts Ox.load(xml, mode: :hash)
246
+ puts Ox.load(xml, mode: :hash_no_attrs)
247
+
248
+ #{:top=>[{:name=>"sample"}, {:middle=>[{:name=>"second"}, {:bottom=>[{:name=>"third"}, "Rock bottom"]}]}]}
249
+ #{:top=>{:middle=>{:bottom=>"Rock bottom"}}}
250
+ ```
251
+
252
+ ### Object XML format
253
+
254
+ The XML format used for Object encoding follows the structure of the
255
+ Object. Each XML element is encoded so that the XML element name is a type
256
+ indicator. Attributes of the element provide additional information such as
257
+ the Class if relevant, the Object attribute name, and Object ID if
258
+ necessary.
259
+
260
+ The type indicator map is:
261
+
262
+ - **a** => `Array`
263
+ - **b** => `Base64` - only for legacy loads
264
+ - **c** => `Class`
265
+ - **f** => `Float`
266
+ - **g** => `Regexp`
267
+ - **h** => `Hash`
268
+ - **i** => `Fixnum`
269
+ - **j** => `Bignum`
270
+ - **l** => `Rational`
271
+ - **m** => `Symbol`
272
+ - **n** => `FalseClass`
273
+ - **o** => `Object`
274
+ - **p** => `Ref`
275
+ - **r** => `Range`
276
+ - **s** => `String`
277
+ - **t** => `Time`
278
+ - **u** => `Struct`
279
+ - **v** => `Complex`
280
+ - **x** => `Raw`
281
+ - **y** => `TrueClass`
282
+ - **z** => `NilClass`
283
+
284
+ If the type is an Object, type 'o' then an attribute named 'c' should be set
285
+ with the full Class name including the Module names. If the XML element
286
+ represents an Object then a sub-elements is included for each attribute of
287
+ the Object. An XML element attribute 'a' is set with a value that is the
288
+ name of the Ruby Object attribute. In all cases, except for the Exception
289
+ attribute hack the attribute names begin with an @ character. (Exception are
290
+ strange in that the attributes of the Exception Class are not named with a @
291
+ suffix. A hack since it has to be done in C and can not be done through the
292
+ interpreter.)
293
+
294
+ Values are encoded as the text portion of an element or in the sub-elements
295
+ of the principle. For example, a Fixnum is encoded as:
296
+ ```xml
297
+ <i>123</i>
298
+ ```
299
+ An Array has sub-elements and is encoded similar to this example.
300
+ ```xml
301
+ <a>
302
+ <i>1</i>
303
+ <s>abc</s>
304
+ </a>
305
+ ```
306
+ A Hash is encoded with an even number of elements where the first element is
307
+ the key and the second is the value. This is repeated for each entry in the
308
+ Hash. An example is of { 1 => 'one', 2 => 'two' } encoding is:
309
+ ```xml
310
+ <h>
311
+ <i>1</i>
312
+ <s>one</s>
313
+ <i>2</i>
314
+ <s>two</s>
315
+ </h>
316
+ ```
317
+
318
+ Ox supports circular references where attributes of one Object can refer to
319
+ an Object that refers back to the first Object. When this option is used an
320
+ Object ID is added to each XML Object element as the value of the 'a'
321
+ attribute.
322
+
323
+ ## Contributors
324
+
325
+ ### Code Contributors
326
+
327
+ This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
328
+ <a href="https://github.com/ohler55/ox/graphs/contributors"><img src="https://opencollective.com/ohler/contributors.svg?width=890&button=false" /></a>
329
+
330
+ ### Financial Contributors
331
+
332
+ Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/ohler/contribute)]
333
+
334
+ #### Individuals
335
+
336
+ <a href="https://opencollective.com/ohler"><img src="https://opencollective.com/ohler/individuals.svg?width=890"></a>
337
+
338
+ #### Organizations
339
+
340
+ Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/ohler/contribute)]
341
+
342
+ <a href="https://opencollective.com/ohler/organization/0/website"><img src="https://opencollective.com/ohler/organization/0/avatar.svg"></a>
343
+ <a href="https://opencollective.com/ohler/organization/1/website"><img src="https://opencollective.com/ohler/organization/1/avatar.svg"></a>
344
+ <a href="https://opencollective.com/ohler/organization/2/website"><img src="https://opencollective.com/ohler/organization/2/avatar.svg"></a>
345
+ <a href="https://opencollective.com/ohler/organization/3/website"><img src="https://opencollective.com/ohler/organization/3/avatar.svg"></a>
346
+ <a href="https://opencollective.com/ohler/organization/4/website"><img src="https://opencollective.com/ohler/organization/4/avatar.svg"></a>
347
+ <a href="https://opencollective.com/ohler/organization/5/website"><img src="https://opencollective.com/ohler/organization/5/avatar.svg"></a>
348
+ <a href="https://opencollective.com/ohler/organization/6/website"><img src="https://opencollective.com/ohler/organization/6/avatar.svg"></a>
349
+ <a href="https://opencollective.com/ohler/organization/7/website"><img src="https://opencollective.com/ohler/organization/7/avatar.svg"></a>
350
+ <a href="https://opencollective.com/ohler/organization/8/website"><img src="https://opencollective.com/ohler/organization/8/avatar.svg"></a>
351
+ <a href="https://opencollective.com/ohler/organization/9/website"><img src="https://opencollective.com/ohler/organization/9/avatar.svg"></a>
data/ext/ox/attr.h ADDED
@@ -0,0 +1,78 @@
1
+ /* attr.h
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #ifndef OX_ATTR_H
7
+ #define OX_ATTR_H
8
+
9
+ #include <ruby.h>
10
+
11
+ #define ATTR_STACK_INC 8
12
+
13
+ typedef struct _attr {
14
+ const char *name;
15
+ const char *value;
16
+ } *Attr;
17
+
18
+ typedef struct _attrStack {
19
+ struct _attr base[ATTR_STACK_INC];
20
+ Attr head; /* current stack */
21
+ Attr end; /* stack end */
22
+ Attr tail; /* pointer to one past last element name on stack */
23
+ } *AttrStack;
24
+
25
+ inline static void attr_stack_init(AttrStack stack) {
26
+ stack->head = stack->base;
27
+ stack->end = stack->base + sizeof(stack->base) / sizeof(struct _attr);
28
+ stack->tail = stack->head;
29
+ stack->head->name = 0;
30
+ }
31
+
32
+ inline static int attr_stack_empty(AttrStack stack) {
33
+ return (stack->head == stack->tail);
34
+ }
35
+
36
+ inline static void attr_stack_cleanup(AttrStack stack) {
37
+ if (stack->base != stack->head) {
38
+ xfree(stack->head);
39
+ stack->head = stack->base;
40
+ }
41
+ }
42
+
43
+ inline static void attr_stack_push(AttrStack stack, const char *name, const char *value) {
44
+ if (stack->end <= stack->tail + 1) {
45
+ size_t len = stack->end - stack->head;
46
+ size_t toff = stack->tail - stack->head;
47
+
48
+ if (stack->base == stack->head) {
49
+ stack->head = ALLOC_N(struct _attr, len + ATTR_STACK_INC);
50
+ memcpy(stack->head, stack->base, sizeof(struct _attr) * len);
51
+ } else {
52
+ REALLOC_N(stack->head, struct _attr, len + ATTR_STACK_INC);
53
+ }
54
+ stack->tail = stack->head + toff;
55
+ stack->end = stack->head + len + ATTR_STACK_INC;
56
+ }
57
+ stack->tail->name = name;
58
+ stack->tail->value = value;
59
+ stack->tail++;
60
+ stack->tail->name = 0; // terminate
61
+ }
62
+
63
+ inline static Attr attr_stack_peek(AttrStack stack) {
64
+ if (stack->head < stack->tail) {
65
+ return stack->tail - 1;
66
+ }
67
+ return 0;
68
+ }
69
+
70
+ inline static Attr attr_stack_pop(AttrStack stack) {
71
+ if (stack->head < stack->tail) {
72
+ stack->tail--;
73
+ return stack->tail;
74
+ }
75
+ return 0;
76
+ }
77
+
78
+ #endif /* OX_ATTR_H */
data/ext/ox/base64.c ADDED
@@ -0,0 +1,105 @@
1
+ /* base64.c
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include "base64.h"
7
+
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+
11
+ static char digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
+
13
+ /* invalid or terminating characters are set to 'X' or \x58 */
14
+ static uchar s_digits[256] = "\
15
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
16
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
17
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x3E\x58\x58\x58\x3F\
18
+ \x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x58\x58\x58\x58\x58\x58\
19
+ \x58\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\
20
+ \x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x58\x58\x58\x58\x58\
21
+ \x58\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\
22
+ \x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x58\x58\x58\x58\x58\
23
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
24
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
25
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
26
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
27
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
28
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
29
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\
30
+ \x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58\x58";
31
+
32
+ void to_base64(const uchar *src, int len, char *b64) {
33
+ const uchar *end3;
34
+ int len3 = len % 3;
35
+ uchar b1, b2, b3;
36
+
37
+ end3 = src + (len - len3);
38
+ while (src < end3) {
39
+ b1 = *src++;
40
+ b2 = *src++;
41
+ b3 = *src++;
42
+ *b64++ = digits[(uchar)(b1 >> 2)];
43
+ *b64++ = digits[(uchar)(((b1 & 0x03) << 4) | (b2 >> 4))];
44
+ *b64++ = digits[(uchar)(((b2 & 0x0F) << 2) | (b3 >> 6))];
45
+ *b64++ = digits[(uchar)(b3 & 0x3F)];
46
+ }
47
+ if (1 == len3) {
48
+ b1 = *src++;
49
+ *b64++ = digits[b1 >> 2];
50
+ *b64++ = digits[(b1 & 0x03) << 4];
51
+ *b64++ = '=';
52
+ *b64++ = '=';
53
+ } else if (2 == len3) {
54
+ b1 = *src++;
55
+ b2 = *src++;
56
+ *b64++ = digits[b1 >> 2];
57
+ *b64++ = digits[((b1 & 0x03) << 4) | (b2 >> 4)];
58
+ *b64++ = digits[(b2 & 0x0F) << 2];
59
+ *b64++ = '=';
60
+ }
61
+ *b64 = '\0';
62
+ }
63
+
64
+ unsigned long b64_orig_size(const char *text) {
65
+ const char *start = text;
66
+ unsigned long size = 0;
67
+
68
+ if ('\0' != *text) {
69
+ for (; 0 != *text; text++) {
70
+ }
71
+ size = (text - start) * 3 / 4;
72
+ text--;
73
+ if ('=' == *text) {
74
+ size--;
75
+ text--;
76
+ if ('=' == *text) {
77
+ size--;
78
+ }
79
+ }
80
+ }
81
+ return size;
82
+ }
83
+
84
+ void from_base64(const char *b64, uchar *str) {
85
+ uchar b0, b1, b2, b3;
86
+
87
+ while (1) {
88
+ if ('X' == (b0 = s_digits[(uchar)*b64++])) {
89
+ break;
90
+ }
91
+ if ('X' == (b1 = s_digits[(uchar)*b64++])) {
92
+ break;
93
+ }
94
+ *str++ = (b0 << 2) | ((b1 >> 4) & 0x03);
95
+ if ('X' == (b2 = s_digits[(uchar)*b64++])) {
96
+ break;
97
+ }
98
+ *str++ = (b1 << 4) | ((b2 >> 2) & 0x0F);
99
+ if ('X' == (b3 = s_digits[(uchar)*b64++])) {
100
+ break;
101
+ }
102
+ *str++ = (b2 << 6) | b3;
103
+ }
104
+ *str = '\0';
105
+ }
data/ext/ox/base64.h ADDED
@@ -0,0 +1,18 @@
1
+ /* base64.h
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #ifndef BASE64_H
7
+ #define BASE64_H
8
+
9
+ typedef unsigned char uchar;
10
+
11
+ #define b64_size(len) ((len + 2) / 3 * 4)
12
+
13
+ extern unsigned long b64_orig_size(const char *text);
14
+
15
+ extern void to_base64(const uchar *src, int len, char *b64);
16
+ extern void from_base64(const char *b64, uchar *str);
17
+
18
+ #endif /* BASE64_H */