zipruby 0.2.1-mswin32 → 0.2.2-mswin32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of zipruby might be problematic. Click here for more details.

data/README.txt CHANGED
@@ -21,7 +21,7 @@ gem install zipruby
21
21
  https://rubyforge.org/frs/?group_id=6124
22
22
 
23
23
  == Example
24
- === reading zip archives
24
+ === reading zip archive
25
25
 
26
26
  require 'zipruby'
27
27
 
@@ -64,7 +64,7 @@ https://rubyforge.org/frs/?group_id=6124
64
64
  end
65
65
  end
66
66
 
67
- === creating zip archives
67
+ === creating zip archive
68
68
 
69
69
  require 'zipruby'
70
70
 
@@ -94,7 +94,7 @@ https://rubyforge.org/frs/?group_id=6124
94
94
  # args: <entry name>, <source>
95
95
  end
96
96
 
97
- === modifying zip archives
97
+ === modifying zip archive
98
98
 
99
99
  require 'zipruby'
100
100
 
@@ -130,10 +130,14 @@ https://rubyforge.org/frs/?group_id=6124
130
130
  end
131
131
  end
132
132
 
133
- === decrypt zip archives
133
+ === encrypt/decrypt zip archive
134
134
 
135
135
  require 'zipruby'
136
136
 
137
+ # encrypt
138
+ Zip::Archive.encrypt('filename.zip', 'password')
139
+
140
+ # decrypt
137
141
  Zip::Archive.decrypt('filename.zip', 'password')
138
142
 
139
143
  == License
Binary file
data/zipruby.c CHANGED
@@ -29,6 +29,7 @@ static VALUE zipruby_archive_alloc(VALUE klass);
29
29
  static void zipruby_archive_free(struct zipruby_archive *p);
30
30
  static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
31
31
  static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password);
32
+ static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password);
32
33
  static VALUE zipruby_archive_close(VALUE self);
33
34
  static VALUE zipruby_archive_num_files(VALUE self);
34
35
  static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
@@ -69,6 +70,7 @@ void Init_zipruby_archive() {
69
70
  rb_include_module(Archive, rb_mEnumerable);
70
71
  rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
71
72
  rb_define_singleton_method(Archive, "decrypt", zipruby_archive_s_decrypt, 2);
73
+ rb_define_singleton_method(Archive, "encrypt", zipruby_archive_s_encrypt, 2);
72
74
  rb_define_method(Archive, "close", zipruby_archive_close, 0);
73
75
  rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
74
76
  rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
@@ -157,11 +159,19 @@ static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
157
159
  /* */
158
160
  static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
159
161
  int errorp, wrongpwd;
162
+ long pwdlen;
160
163
 
161
164
  Check_Type(path, T_STRING);
162
165
  Check_Type(password, T_STRING);
166
+ pwdlen = RSTRING(password)->len;
163
167
 
164
- if (zip_decrypt(StringValuePtr(path), StringValuePtr(password), RSTRING(password)->len, &errorp, &wrongpwd) == -1) {
168
+ if (pwdlen < 1) {
169
+ rb_raise(Error, "Decrypt archive failed - %s: Password is empty", StringValuePtr(path));
170
+ } else if (pwdlen > 0xff) {
171
+ rb_raise(Error, "Decrypt archive failed - %s: Password is too long", StringValuePtr(path));
172
+ }
173
+
174
+ if (zip_decrypt(StringValuePtr(path), StringValuePtr(password), pwdlen, &errorp, &wrongpwd) == -1) {
165
175
  if (wrongpwd) {
166
176
  rb_raise(Error, "Decrypt archive failed - %s: Wrong password", StringValuePtr(path));
167
177
  } else {
@@ -174,6 +184,30 @@ static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
174
184
  return Qnil;
175
185
  }
176
186
 
187
+ /* */
188
+ static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password) {
189
+ int errorp;
190
+ long pwdlen;
191
+
192
+ Check_Type(path, T_STRING);
193
+ Check_Type(password, T_STRING);
194
+ pwdlen = RSTRING(password)->len;
195
+
196
+ if (pwdlen < 1) {
197
+ rb_raise(Error, "Encrypt archive failed - %s: Password is empty", StringValuePtr(path));
198
+ } else if (pwdlen > 0xff) {
199
+ rb_raise(Error, "Encrypt archive failed - %s: Password is too long", StringValuePtr(path));
200
+ }
201
+
202
+ if (zip_encrypt(StringValuePtr(path), StringValuePtr(password), pwdlen, &errorp) == -1) {
203
+ char errstr[ERRSTR_BUFSIZE];
204
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
205
+ rb_raise(Error, "Encrypt archive failed - %s: %s", StringValuePtr(path), errstr);
206
+ }
207
+
208
+ return Qnil;
209
+ }
210
+
177
211
  /* */
178
212
  static VALUE zipruby_archive_close(VALUE self) {
179
213
  struct zipruby_archive *p_archive;
@@ -845,6 +879,7 @@ void Init_zipruby_error() {
845
879
  #endif
846
880
 
847
881
  #include "zip.h"
882
+ #include "zipint.h"
848
883
  #include "zipruby.h"
849
884
  #include "zipruby_archive.h"
850
885
  #include "zipruby_file.h"
@@ -1016,6 +1051,10 @@ static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self) {
1016
1051
  Check_File(p_file);
1017
1052
  zip_stat_init(&sb);
1018
1053
 
1054
+ if (p_file->archive->cdir->entry[0].bitflags & ZIP_GPBF_ENCRYPTED) {
1055
+ rb_raise(Error, "Read file failed: File encrypted");
1056
+ }
1057
+
1019
1058
  if (zip_stat_index(p_file->archive, p_file->sb->index, 0, &sb)) {
1020
1059
  rb_raise(Error, "Read file failed: %s", zip_strerror(p_file->archive));
1021
1060
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: mswin32
6
6
  authors:
7
7
  - winebarrel