php_serializer 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 896053e81f7ec13433591a69fd084decd20dccc2
4
+ data.tar.gz: 1bf0f3f0d89c13a3892ee1900210ca627c47ddd3
5
+ SHA512:
6
+ metadata.gz: 37c9e2bcf7e4d0c10d67f43a3161a3f340447c4c2b5dce57f4e5b77522076d12d291520305eee588a35d166d279a028d122f5f2aeed5b68a111328794bde841c
7
+ data.tar.gz: 96636d63dc77461f8e8132715f969cee8bc05d3115bea7e74edfd160d91ce9956b68d4a146c3d52a31644d399925463028bd8ff817ec04fdf8eba7e814416abd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Vijayakumar Ramachandran
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,33 @@
1
+ # PhpSerializer
2
+
3
+ Native PHP serializer and unserializer(Note: currently only supports PHP primitive data-types) for Ruby and it is heavily inspired by PHP source code.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'php_serializer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install php_serializer
20
+
21
+ ## Usage
22
+
23
+ ``` ruby
24
+ PhpSerializer.serialize('example')
25
+ PhpSerializer.unserialize('s:7:"example";')
26
+ ```
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vijayrsv/php_serializer.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+
3
+ dir_config('php_serializer')
4
+ create_makefile('php_serializer/php_serializer')
@@ -0,0 +1,249 @@
1
+ #include<ruby.h>
2
+ #include<php_serializer.h>
3
+
4
+ void Init_php_serializer()
5
+ {
6
+ VALUE php_serializer_module = rb_define_module("PhpSerializer");
7
+ rb_define_singleton_method(php_serializer_module, "serialize", serialize, 1);
8
+ rb_define_singleton_method(php_serializer_module, "unserialize", unserialize, 1);
9
+ }
10
+
11
+ static inline void serialize_long(php_str *buf, long val)
12
+ {
13
+ php_str_appendl(buf, "i:", 2);
14
+ php_str_append_long(buf, val);
15
+ php_str_appendc(buf, ';');
16
+ }
17
+
18
+ static inline void serialize_float(php_str *buf, VALUE flt)
19
+ {
20
+ VALUE str = Qnil;
21
+ str = rb_funcall(flt, rb_intern("to_s"), 0);
22
+ php_str_appendl(buf, "d:", 2);
23
+ php_str_appendl(buf, RSTRING_PTR(str), RSTRING_LEN(str));
24
+ php_str_appendc(buf, ';');
25
+ }
26
+
27
+ static inline void serialize_string(php_str *buf, char *str, int len)
28
+ {
29
+ php_str_appendl(buf, "s:", 2);
30
+ php_str_append_long(buf, len);
31
+ php_str_appendl(buf, ":\"", 2);
32
+ php_str_appendl(buf, str, len);
33
+ php_str_appendl(buf, "\";", 2);
34
+ }
35
+
36
+ static inline void serialize_array(php_str *buf, VALUE arr)
37
+ {
38
+ long arr_len = RARRAY_LEN(arr);
39
+ php_str_appendl(buf, "a:", 2);
40
+ php_str_append_long(buf, arr_len);
41
+ php_str_appendl(buf, ":{", 2);
42
+ for(long i=0; i<arr_len; i++) {
43
+ php_str_appendl(buf, "i:", 2);
44
+ php_str_append_long(buf, i);
45
+ php_str_appendl(buf, ";", 1);
46
+ serialize_intern(buf, RARRAY_AREF(arr, i));
47
+ }
48
+ php_str_appendl(buf, "}", 1);
49
+ }
50
+
51
+ static inline int rb_hash_each(VALUE key, VALUE val, VALUE rbuf)
52
+ {
53
+ php_str buf = {0};
54
+ serialize_intern(&buf, key);
55
+ serialize_intern(&buf, val);
56
+ rb_str_cat(rbuf, buf.c, buf.len);
57
+ php_str_free(&buf);
58
+ return ST_CONTINUE;
59
+ }
60
+
61
+ static inline void serialize_hash(php_str *buf, VALUE hash)
62
+ {
63
+ long hash_size;
64
+ VALUE rbuf = rb_str_new_cstr("");
65
+ hash_size = RHASH_SIZE(hash);
66
+ php_str_appendl(buf, "a:", 2);
67
+ php_str_append_long(buf, hash_size);
68
+ php_str_appendl(buf, ":{", 2);
69
+ rb_hash_foreach(hash, rb_hash_each, rbuf);
70
+ php_str_appendl(buf, StringValueCStr(rbuf), RSTRING_LEN(rbuf));
71
+ php_str_appendl(buf, "}", 1);
72
+ }
73
+
74
+ static inline void serialize_symbol(php_str *buf, VALUE sym)
75
+ {
76
+ VALUE str = rb_sym_to_s(sym);
77
+ serialize_string(buf, RSTRING_PTR(str), RSTRING_LEN(str));
78
+ }
79
+
80
+ static void serialize_intern(php_str *buf, VALUE in_data) {
81
+ switch (TYPE(in_data)) {
82
+ case T_NIL:
83
+ php_str_appendl(buf, "N;", 2);
84
+ break;
85
+ case T_TRUE:
86
+ php_str_appendl(buf, "b:1;", 4);
87
+ break;
88
+ case T_FALSE:
89
+ php_str_appendl(buf, "b:0;", 4);
90
+ break;
91
+ case T_FIXNUM:
92
+ serialize_long(buf, FIX2LONG(in_data));
93
+ break;
94
+ case T_BIGNUM:
95
+ serialize_long(buf, NUM2LONG(in_data));
96
+ break;
97
+ case T_STRING:
98
+ serialize_string(buf, RSTRING_PTR(in_data), RSTRING_LEN(in_data));
99
+ break;
100
+ case T_SYMBOL:
101
+ serialize_symbol(buf, in_data);
102
+ break;
103
+ case T_ARRAY:
104
+ serialize_array(buf, in_data);
105
+ break;
106
+ case T_HASH:
107
+ serialize_hash(buf, in_data);
108
+ break;
109
+ case T_FLOAT:
110
+ serialize_float(buf, in_data);
111
+ break;
112
+ default:
113
+ /* raise exception */
114
+ rb_raise(rb_eTypeError, "%s is not valid value", RSTRING_PTR(rb_any_to_s(rb_obj_class(in_data))));
115
+ break;
116
+ }
117
+ }
118
+
119
+ static inline long get_lval(char **src, char delimiter, long *count, long limit) {
120
+ long val = 0;
121
+ int is_negative = 0;
122
+ while(1) {
123
+ if(*count >= limit) {
124
+ rb_raise(rb_eTypeError, "Incomplete Serialization.");
125
+ } else if (**src >= '0' && **src <= '9') {
126
+ val = (val * 10) + (**src - 48);
127
+ } else if (**src == delimiter) {
128
+ if (is_negative)
129
+ val *= -1;
130
+ return val;
131
+ } else if (**src == '-') {
132
+ is_negative = 1;
133
+ } else if (**src == '+') {
134
+ //do nothing;
135
+ } else {
136
+ rb_raise(rb_eTypeError, "Integer is expected.");
137
+ }
138
+ (*src)++;
139
+ (*count)++;
140
+ }
141
+ }
142
+
143
+ static inline VALUE get_dval(char **src, char delimiter, long *count, long limit) {
144
+ VALUE val = Qnil;
145
+ php_str buf = {0};
146
+ char *c;
147
+ while(1) {
148
+ c = *src;
149
+ if(*count >= limit) {
150
+ rb_raise(rb_eTypeError, "Incomplete Serialization.");
151
+ } else if (*c >= '0' && *c <= '9') {
152
+ php_str_appendl(&buf, c, 1);
153
+ } else if (*c == delimiter) {
154
+ val = rb_str_new(buf.c, buf.len);
155
+ php_str_free(&buf);
156
+ return DBL2NUM(rb_str_to_dbl(val, 0));
157
+ } else if (*c == '-' || *c == '+' || *c == '.' || *c == 'e' || *c == 'E') {
158
+ php_str_appendl(&buf, c, 1);
159
+ } else {
160
+ rb_raise(rb_eTypeError, "Malformed decimal value.");
161
+ }
162
+ (*src)++;
163
+ (*count)++;
164
+ }
165
+ }
166
+
167
+ static VALUE unserialize_intern(char **str_ptr_ptr, long *count, long limit) {
168
+ char type;
169
+ long _lval;
170
+ int is_array = 1;
171
+ VALUE val = Qnil;
172
+ VALUE hash = Qnil;
173
+ VALUE array = Qnil;
174
+ VALUE h_key = Qnil;
175
+ VALUE h_val = Qnil;
176
+
177
+ if (*count >= limit)
178
+ return val;
179
+
180
+ type = **str_ptr_ptr;
181
+ (*str_ptr_ptr) += 2;
182
+ (*count) += 2;
183
+
184
+ switch(type) {
185
+ case 'i':
186
+ _lval = get_lval(str_ptr_ptr, ';', count, limit);
187
+ val = LONG2FIX(_lval);
188
+ (*count)++;
189
+ break;
190
+ case 'b':
191
+ _lval = get_lval(str_ptr_ptr, ';', count, limit);
192
+ val = _lval ? Qtrue : Qfalse;
193
+ (*count)++;
194
+ break;
195
+ case 's':
196
+ _lval = get_lval(str_ptr_ptr, ':', count, limit);
197
+ (*str_ptr_ptr) += 2;
198
+ val = rb_str_new(*str_ptr_ptr, _lval);
199
+ (*str_ptr_ptr) += (_lval + 1);
200
+ (*count) += (_lval + 4);
201
+ break;
202
+ case 'd':
203
+ val = get_dval(str_ptr_ptr, ';', count, limit);
204
+ (*count)++;
205
+ break;
206
+ case 'N':
207
+ //Do nothing for NULL;
208
+ break;
209
+ case 'a':
210
+ _lval = get_lval(str_ptr_ptr, ':', count, limit);
211
+ (*str_ptr_ptr) += 2;
212
+ (*count) += 2;
213
+ hash = rb_hash_new();
214
+ array = rb_ary_new();
215
+ for(long i = 0; i < _lval; i++) {
216
+ h_key = unserialize_intern(str_ptr_ptr, count, limit);
217
+ h_val = unserialize_intern(str_ptr_ptr, count, limit);
218
+ rb_hash_aset(hash, h_key, h_val);
219
+ if (TYPE(h_key) != T_FIXNUM || FIX2LONG(h_key) != i)
220
+ is_array = 0;
221
+ if (is_array)
222
+ rb_ary_push(array, h_val);
223
+ }
224
+ val = is_array ? array : hash;
225
+ (*count)++;
226
+ break;
227
+ default:
228
+ rb_raise(rb_eTypeError, "Invalid Serialization.");
229
+ break;
230
+ }
231
+ if(*count < limit && val != Qnil)
232
+ (*str_ptr_ptr)++;
233
+ return val;
234
+ }
235
+
236
+ static VALUE serialize(VALUE module, VALUE input) {
237
+ php_str buf = {0};
238
+ VALUE serialized_data = Qnil;
239
+ serialize_intern(&buf, input);
240
+ serialized_data = rb_str_new(buf.c, buf.len);
241
+ php_str_free(&buf);
242
+ return serialized_data;
243
+ }
244
+
245
+ static VALUE unserialize(VALUE module, VALUE input) {
246
+ char *str_p = RSTRING_PTR(input);
247
+ long count = 0, limit = RSTRING_LEN(input);
248
+ return unserialize_intern(&str_p, &count, limit);
249
+ }
@@ -0,0 +1,122 @@
1
+ #ifndef PHP_SERIALIZER_H
2
+ #define PHP_SERIALIZER_H
3
+ #include <sys/types.h>
4
+
5
+ typedef struct {
6
+ char *c;
7
+ size_t len;
8
+ size_t a; //allocated_size
9
+ } php_str;
10
+
11
+ static VALUE serialize(VALUE module, VALUE in_data);
12
+ static VALUE unserialize(VALUE module, VALUE in_data);
13
+ static void serialize_intern(php_str *buf, VALUE in_data);
14
+ static VALUE unserialize_intern(char **str_p, long *count, long limit);
15
+
16
+ #define PHP_STR_PREALLOC 128
17
+ #define PHP_STR_START_SIZE 78
18
+ #define PHP_STR_REALLOC(a,b) realloc((a),(b))
19
+
20
+ #define php_str_0(x) do { \
21
+ if ((x)->c) { \
22
+ (x)->c[(x)->len] = '\0'; \
23
+ } \
24
+ } while (0)
25
+
26
+ #define php_str_appendc(dest, c) \
27
+ php_str_appendc_ex((dest), (c))
28
+
29
+ #define php_str_appendc_ex(dest, ch) do { \
30
+ register size_t __nl; \
31
+ php_str_alloc4((dest), 1, __nl); \
32
+ (dest)->len = __nl; \
33
+ ((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch); \
34
+ } while (0)
35
+
36
+ #define php_STR_DO_REALLOC(d) \
37
+ (d)->c = PHP_STR_REALLOC((d)->c, (d)->a + 1)
38
+
39
+ #define php_str_alloc4(d, n, newlen) do { \
40
+ if (!(d)->c) { \
41
+ (d)->len = 0; \
42
+ newlen = (n); \
43
+ (d)->a = newlen < PHP_STR_START_SIZE \
44
+ ? PHP_STR_START_SIZE \
45
+ : (newlen >= (INT_MAX - PHP_STR_PREALLOC)? newlen \
46
+ : (newlen + PHP_STR_PREALLOC)); \
47
+ php_STR_DO_REALLOC(d); \
48
+ } else { \
49
+ newlen = (d)->len + (n); \
50
+ if (newlen >= (d)->a) { \
51
+ (d)->a = newlen + PHP_STR_PREALLOC; \
52
+ if ((d)->a >= INT_MAX) { \
53
+ rb_raise(rb_eTypeError, "String size overflow"); \
54
+ } \
55
+ php_STR_DO_REALLOC(d); \
56
+ } \
57
+ } \
58
+ } while (0)
59
+
60
+ #define php_str_appendl(dest, src, len) \
61
+ php_str_appendl_ex((dest), (src), (len))
62
+
63
+ #define php_str_appendl_ex(dest, src, nlen) do { \
64
+ register size_t __nl; \
65
+ php_str *__dest = (php_str *) (dest); \
66
+ \
67
+ php_str_alloc4(__dest, (nlen), __nl); \
68
+ memcpy(__dest->c + __dest->len, (src), (nlen)); \
69
+ __dest->len = __nl; \
70
+ } while (0)
71
+
72
+ /* input: buf points to the END of the buffer */
73
+ #define php_str_print_unsigned4(buf, num, vartype, result) do { \
74
+ char *__p = (buf); \
75
+ vartype __num = (num); \
76
+ *__p = '\0'; \
77
+ do { \
78
+ *--__p = (char) (__num % 10) + '0'; \
79
+ __num /= 10; \
80
+ } while (__num > 0); \
81
+ result = __p; \
82
+ } while (0)
83
+
84
+ /* buf points to the END of the buffer */
85
+ #define php_str_print_long4(buf, num, vartype, result) do { \
86
+ if (num < 0) { \
87
+ /* this might cause problems when dealing with LONG_MIN \
88
+ * and machines which don't support long long. Works \
89
+ * flawlessly on 32bit x86 */ \
90
+ php_str_print_unsigned4((buf), -(num), vartype, (result)); \
91
+ *--(result) = '-'; \
92
+ } else { \
93
+ php_str_print_unsigned4((buf), (num), vartype, (result)); \
94
+ } \
95
+ } while (0)
96
+
97
+ #define php_str_append_generic_ex(dest, num, vartype, func) do { \
98
+ char __b[32]; \
99
+ char *__t; \
100
+ php_str_print##func##4 (__b + sizeof(__b) - 1, (num), vartype, __t); \
101
+ php_str_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t); \
102
+ } while (0)
103
+
104
+ #define php_str_append_long(dest, val) \
105
+ php_str_append_long_ex((dest), (val))
106
+
107
+ #define php_str_append_long_ex(dest, num) \
108
+ php_str_append_generic_ex((dest), (num), unsigned long, _long)
109
+
110
+ #define php_str_free(s) \
111
+ php_str_free_ex((s))
112
+
113
+ #define php_str_free_ex(s) do { \
114
+ php_str *__s = (php_str *) (s); \
115
+ if (__s->c) { \
116
+ free(__s->c); \
117
+ __s->c = NULL; \
118
+ } \
119
+ __s->a = __s->len = 0; \
120
+ } while (0)
121
+
122
+ #endif
@@ -0,0 +1,3 @@
1
+ module PhpSerializer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "php_serializer/php_serializer"
2
+ require "php_serializer/version"
3
+
4
+ module PhpSerializer
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "php_serializer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "php_serializer"
8
+ spec.version = PhpSerializer::VERSION
9
+ spec.authors = ["vijayrsv"]
10
+ spec.email = ["vijayrepomailzbox@gmail.com"]
11
+
12
+ spec.summary = %q{}
13
+ spec.description = %q{Native PHP serializer and unserializer(Note: currently only supports PHP primitive data-types) for Ruby and it is heavily inspired by PHP source code.}
14
+ spec.homepage = "https://github.com/vijayrsv/php_serializer"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.extensions = %w[ext/php_serializer/extconf.rb]
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.15"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: php_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vijayrsv
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: 'Native PHP serializer and unserializer(Note: currently only supports
42
+ PHP primitive data-types) for Ruby and it is heavily inspired by PHP source code.'
43
+ email:
44
+ - vijayrepomailzbox@gmail.com
45
+ executables: []
46
+ extensions:
47
+ - ext/php_serializer/extconf.rb
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - LICENSE.txt
52
+ - README.md
53
+ - ext/php_serializer/extconf.rb
54
+ - ext/php_serializer/php_serializer.c
55
+ - ext/php_serializer/php_serializer.h
56
+ - lib/php_serializer.rb
57
+ - lib/php_serializer/version.rb
58
+ - php_serializer.gemspec
59
+ homepage: https://github.com/vijayrsv/php_serializer
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ allowed_push_host: https://rubygems.org
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.6.10
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: ''
84
+ test_files: []