zipruby 0.2.8 → 0.2.9

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/zipruby.c CHANGED
@@ -151,16 +151,16 @@ static int write_from_proc(VALUE proc, int fd) {
151
151
  break;
152
152
  }
153
153
 
154
- if (RSTRING(src)->len < 1) {
154
+ if (RSTRING_LEN(src) < 1) {
155
155
  break;
156
156
  }
157
157
 
158
158
  #ifdef _WIN32
159
- if (_write(fd, StringValuePtr(src), RSTRING(src)->len) == -1) {
159
+ if (_write(fd, RSTRING_PTR(src), RSTRING_LEN(src)) == -1) {
160
160
  return -1;
161
161
  }
162
162
  #else
163
- if (write(fd, StringValuePtr(src), RSTRING(src)->len) == -1) {
163
+ if (write(fd, RSTRING_PTR(src), RSTRING_LEN(src)) == -1) {
164
164
  return -1;
165
165
  }
166
166
  #endif
@@ -214,15 +214,15 @@ static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
214
214
  static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
215
215
  static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
216
216
  static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
217
- static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self);
217
+ static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io);
218
218
  static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
219
219
  static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self);
220
220
  static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self);
221
- static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file);
221
+ static VALUE zipruby_archive_replace_io(int argc, VALUE* argv, VALUE self);
222
222
  static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self);
223
223
  static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self);
224
224
  static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self);
225
- static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE self);
225
+ static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self);
226
226
  static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self);
227
227
  static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self);
228
228
  static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
@@ -264,18 +264,18 @@ void Init_zipruby_archive() {
264
264
  rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
265
265
  rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
266
266
  rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
267
- rb_define_method(Archive, "add_filep", zipruby_archive_add_filep, -1);
267
+ rb_define_method(Archive, "add_io", zipruby_archive_add_io, 2);
268
268
  rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
269
269
  rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, -1);
270
270
  rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, -1);
271
- rb_define_method(Archive, "replace_filep", zipruby_archive_replace_filep, 2);
271
+ rb_define_method(Archive, "replace_io", zipruby_archive_replace_io, -1);
272
272
  rb_define_method(Archive, "replace", zipruby_archive_replace_function, -1);
273
273
  rb_define_method(Archive, "add_or_replace_buffer", zipruby_archive_add_or_replace_buffer, -1);
274
274
  rb_define_method(Archive, "add_or_replace_file", zipruby_archive_add_or_replace_file, -1);
275
- rb_define_method(Archive, "add_or_replace_filep", zipruby_archive_add_or_replace_filep, -1);
275
+ rb_define_method(Archive, "add_or_replace_io", zipruby_archive_add_or_replace_io, -1);
276
276
  rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
277
277
  rb_define_method(Archive, "update", zipruby_archive_update, -1);
278
- rb_define_method(Archive, "<<", zipruby_archive_add_filep, -1);
278
+ rb_define_method(Archive, "<<", zipruby_archive_add_io, 2);
279
279
  rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
280
280
  rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
281
281
  rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
@@ -341,10 +341,10 @@ static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
341
341
  archive = rb_funcall(Archive, rb_intern("new"), 0);
342
342
  Data_Get_Struct(archive, struct zipruby_archive, p_archive);
343
343
 
344
- if ((p_archive->archive = zip_open(StringValuePtr(path), i_flags, &errorp)) == NULL) {
344
+ if ((p_archive->archive = zip_open(RSTRING_PTR(path), i_flags, &errorp)) == NULL) {
345
345
  char errstr[ERRSTR_BUFSIZE];
346
346
  zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
347
- rb_raise(Error, "Open archive failed - %s: %s", StringValuePtr(path), errstr);
347
+ rb_raise(Error, "Open archive failed - %s: %s", RSTRING_PTR(path), errstr);
348
348
  }
349
349
 
350
350
  p_archive->path = path;
@@ -374,7 +374,7 @@ static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
374
374
  struct zipruby_archive *p_archive;
375
375
  void *data = NULL;
376
376
  int len = 0, i_flags = 0;
377
- int errorp;
377
+ int errorp;
378
378
 
379
379
  rb_scan_args(argc, argv, "02", &buffer, &flags);
380
380
 
@@ -394,8 +394,8 @@ static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
394
394
 
395
395
  i_flags = (i_flags | ZIP_TRUNC);
396
396
  } else if (TYPE(buffer) == T_STRING) {
397
- data = StringValuePtr(buffer);
398
- len = RSTRING(buffer)->len;
397
+ data = RSTRING_PTR(buffer);
398
+ len = RSTRING_LEN(buffer);
399
399
  } else if (rb_obj_is_instance_of(buffer, rb_cProc)) {
400
400
  data = (void *) buffer;
401
401
  len = -1;
@@ -439,60 +439,66 @@ static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
439
439
 
440
440
  /* */
441
441
  static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
442
+ int res;
442
443
  int errorp, wrongpwd;
443
444
  long pwdlen;
444
445
 
445
446
  Check_Type(path, T_STRING);
446
447
  Check_Type(password, T_STRING);
447
- pwdlen = RSTRING(password)->len;
448
+ pwdlen = RSTRING_LEN(password);
448
449
 
449
450
  if (pwdlen < 1) {
450
- rb_raise(Error, "Decrypt archive failed - %s: Password is empty", StringValuePtr(path));
451
+ rb_raise(Error, "Decrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
451
452
  } else if (pwdlen > 0xff) {
452
- rb_raise(Error, "Decrypt archive failed - %s: Password is too long", StringValuePtr(path));
453
+ rb_raise(Error, "Decrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
453
454
  }
454
455
 
455
- if (zip_decrypt(StringValuePtr(path), StringValuePtr(password), pwdlen, &errorp, &wrongpwd) == -1) {
456
+ res = zip_decrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp, &wrongpwd);
457
+
458
+ if (res == -1) {
456
459
  if (wrongpwd) {
457
- rb_raise(Error, "Decrypt archive failed - %s: Wrong password", StringValuePtr(path));
460
+ rb_raise(Error, "Decrypt archive failed - %s: Wrong password", RSTRING_PTR(path));
458
461
  } else {
459
462
  char errstr[ERRSTR_BUFSIZE];
460
463
  zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
461
- rb_raise(Error, "Decrypt archive failed - %s: %s", StringValuePtr(path), errstr);
464
+ rb_raise(Error, "Decrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
462
465
  }
463
466
  }
464
467
 
465
- return Qnil;
468
+ return (res > 0) ? Qtrue : Qfalse;
466
469
  }
467
470
 
468
471
  /* */
469
472
  static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password) {
473
+ int res;
470
474
  int errorp;
471
475
  long pwdlen;
472
476
 
473
477
  Check_Type(path, T_STRING);
474
478
  Check_Type(password, T_STRING);
475
- pwdlen = RSTRING(password)->len;
479
+ pwdlen = RSTRING_LEN(password);
476
480
 
477
481
  if (pwdlen < 1) {
478
- rb_raise(Error, "Encrypt archive failed - %s: Password is empty", StringValuePtr(path));
482
+ rb_raise(Error, "Encrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
479
483
  } else if (pwdlen > 0xff) {
480
- rb_raise(Error, "Encrypt archive failed - %s: Password is too long", StringValuePtr(path));
484
+ rb_raise(Error, "Encrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
481
485
  }
482
486
 
483
- if (zip_encrypt(StringValuePtr(path), StringValuePtr(password), pwdlen, &errorp) == -1) {
487
+ res = zip_encrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp);
488
+
489
+ if (res == -1) {
484
490
  char errstr[ERRSTR_BUFSIZE];
485
491
  zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
486
- rb_raise(Error, "Encrypt archive failed - %s: %s", StringValuePtr(path), errstr);
492
+ rb_raise(Error, "Encrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
487
493
  }
488
494
 
489
- return Qnil;
495
+ return (res > 0) ? Qtrue : Qfalse;
490
496
  }
491
497
 
492
498
  /* */
493
499
  static VALUE zipruby_archive_close(VALUE self) {
494
500
  struct zipruby_archive *p_archive;
495
- int changed, survivors;
501
+ int changed, survivors;
496
502
 
497
503
  if (!zipruby_archive_is_open(self)) {
498
504
  return Qfalse;
@@ -601,25 +607,25 @@ static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source) {
601
607
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
602
608
  Check_Archive(p_archive);
603
609
 
604
- len = RSTRING(source)->len;
610
+ len = RSTRING_LEN(source);
605
611
 
606
612
  if ((data = malloc(len)) == NULL) {
607
613
  rb_raise(rb_eRuntimeError, "Add file failed: Cannot allocate memory");
608
614
  }
609
615
 
610
616
  memset(data, 0, len);
611
- memcpy(data, StringValuePtr(source), len);
617
+ memcpy(data, RSTRING_PTR(source), len);
612
618
 
613
619
  if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
614
620
  free(data);
615
- rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
621
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
616
622
  }
617
623
 
618
- if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
624
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
619
625
  zip_source_free(zsource);
620
626
  zip_unchange_all(p_archive->archive);
621
627
  zip_unchange_archive(p_archive->archive);
622
- rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
628
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
623
629
  }
624
630
 
625
631
  return Qnil;
@@ -636,7 +642,7 @@ static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self) {
636
642
 
637
643
  rb_scan_args(argc, argv, "21", &index, &source, &flags);
638
644
 
639
- if (!rb_obj_is_instance_of(index, rb_cString) && !rb_obj_is_instance_of(index, rb_cFixnum)) {
645
+ if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
640
646
  rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
641
647
  }
642
648
 
@@ -648,19 +654,19 @@ static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self) {
648
654
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
649
655
  Check_Archive(p_archive);
650
656
 
651
- if (rb_obj_is_instance_of(index, rb_cFixnum)) {
657
+ if (FIXNUM_P(index)) {
652
658
  i_index = NUM2INT(index);
653
- } else if ((i_index = zip_name_locate(p_archive->archive, StringValuePtr(index), i_flags)) == -1) {
654
- rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", StringValuePtr(index));
659
+ } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
660
+ rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
655
661
  }
656
662
 
657
- len = RSTRING(source)->len;
663
+ len = RSTRING_LEN(source);
658
664
 
659
665
  if ((data = malloc(len)) == NULL) {
660
666
  rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
661
667
  }
662
668
 
663
- memcpy(data, StringValuePtr(source), len);
669
+ memcpy(data, RSTRING_PTR(source), len);
664
670
 
665
671
  if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
666
672
  free(data);
@@ -693,7 +699,7 @@ static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE
693
699
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
694
700
  Check_Archive(p_archive);
695
701
 
696
- index = zip_name_locate(p_archive->archive, StringValuePtr(name), i_flags);
702
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
697
703
 
698
704
  if (index >= 0) {
699
705
  VALUE _args[] = { INT2NUM(index), source };
@@ -726,15 +732,15 @@ static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self) {
726
732
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
727
733
  Check_Archive(p_archive);
728
734
 
729
- if ((zsource = zip_source_file(p_archive->archive, StringValuePtr(fname), 0, -1)) == NULL) {
730
- rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
735
+ if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
736
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
731
737
  }
732
738
 
733
- if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
739
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
734
740
  zip_source_free(zsource);
735
741
  zip_unchange_all(p_archive->archive);
736
742
  zip_unchange_archive(p_archive->archive);
737
- rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
743
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
738
744
  }
739
745
 
740
746
  return Qnil;
@@ -749,7 +755,7 @@ static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self) {
749
755
 
750
756
  rb_scan_args(argc, argv, "21", &index, &fname, &flags);
751
757
 
752
- if (!rb_obj_is_instance_of(index, rb_cString) && !rb_obj_is_instance_of(index, rb_cFixnum)) {
758
+ if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
753
759
  rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
754
760
  }
755
761
 
@@ -761,13 +767,13 @@ static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self) {
761
767
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
762
768
  Check_Archive(p_archive);
763
769
 
764
- if (rb_obj_is_instance_of(index, rb_cFixnum)) {
770
+ if (FIXNUM_P(index)) {
765
771
  i_index = NUM2INT(index);
766
- } else if ((i_index = zip_name_locate(p_archive->archive, StringValuePtr(index), i_flags)) == -1) {
767
- rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", StringValuePtr(index));
772
+ } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
773
+ rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
768
774
  }
769
775
 
770
- if ((zsource = zip_source_file(p_archive->archive, StringValuePtr(fname), 0, -1)) == NULL) {
776
+ if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
771
777
  rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
772
778
  }
773
779
 
@@ -813,7 +819,7 @@ static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE se
813
819
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
814
820
  Check_Archive(p_archive);
815
821
 
816
- index = zip_name_locate(p_archive->archive, StringValuePtr(name), i_flags);
822
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
817
823
 
818
824
  if (index >= 0) {
819
825
  VALUE _args[] = { INT2NUM(index), fname };
@@ -825,63 +831,38 @@ static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE se
825
831
  }
826
832
 
827
833
  /* */
828
- static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self) {
829
- VALUE name, file, source;
830
-
831
- rb_scan_args(argc, argv, "11", &name, &file);
832
-
833
- if (NIL_P(file)) {
834
- file = name;
835
- name = Qnil;
836
- }
837
-
838
- Check_Type(file, T_FILE);
839
-
840
- if (NIL_P(name)) {
841
- name = rb_funcall(rb_cFile, rb_intern("basename"), 1, rb_funcall(file, rb_intern("path"), 0));
842
- }
834
+ static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io) {
835
+ VALUE source;
843
836
 
844
- source = rb_funcall(file, rb_intern("read"), 0);
837
+ Check_IO(io);
838
+ source = rb_funcall(io, rb_intern("read"), 0);
845
839
 
846
840
  return zipruby_archive_add_buffer(self, name, source);
847
841
  }
848
842
 
849
843
  /* */
850
- static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file) {
851
- VALUE source;
852
- VALUE _args[2];
844
+ static VALUE zipruby_archive_replace_io(int argc, VALUE *argv, VALUE self) {
845
+ VALUE source, io, index, flags;
846
+ VALUE _args[3];
853
847
 
854
- Check_Type(file, T_FILE);
855
- source = rb_funcall(file, rb_intern("read"), 0);
848
+ rb_scan_args(argc, argv, "21", &index, &io, &flags);
849
+ Check_IO(io);
850
+ source = rb_funcall(io, rb_intern("read"), 0);
856
851
 
857
852
  _args[0] = index;
858
853
  _args[1] = source;
859
- return zipruby_archive_replace_buffer(2, _args, self);
854
+ _args[2] = flags;
855
+ return zipruby_archive_replace_file(2, _args, self);
860
856
  }
861
857
 
862
858
  /* */
863
- static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE self) {
864
- VALUE name, file, flags;
859
+ static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self) {
860
+ VALUE name, io, flags;
865
861
  struct zipruby_archive *p_archive;
866
862
  int index, i_flags = 0;
867
863
 
868
- rb_scan_args(argc, argv, "12", &name, &file, &flags);
869
-
870
- if (NIL_P(flags) && FIXNUM_P(file)) {
871
- flags = file;
872
- file = Qnil;
873
- }
874
-
875
- if (NIL_P(file)) {
876
- file = name;
877
- name = Qnil;
878
- }
879
-
880
- Check_Type(file, T_FILE);
881
-
882
- if (NIL_P(name)) {
883
- name = rb_funcall(rb_cFile, rb_intern("basename"), 1, rb_funcall(file, rb_intern("path"), 0));
884
- }
864
+ rb_scan_args(argc, argv, "21", &name, &io, &flags);
865
+ Check_IO(io);
885
866
 
886
867
  if (!NIL_P(flags)) {
887
868
  i_flags = NUM2INT(flags);
@@ -891,13 +872,13 @@ static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE s
891
872
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
892
873
  Check_Archive(p_archive);
893
874
 
894
- index = zip_name_locate(p_archive->archive, StringValuePtr(name), i_flags);
875
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
895
876
 
896
877
  if (index >= 0) {
897
- return zipruby_archive_replace_filep(self, INT2NUM(index), file);
878
+ VALUE _args[] = {INT2NUM(index), io, flags};
879
+ return zipruby_archive_replace_io(2, _args, self);
898
880
  } else {
899
- VALUE _args[] = { name, file };
900
- return zipruby_archive_add_filep(2, _args, self);
881
+ return zipruby_archive_add_io(self ,name, io);
901
882
  }
902
883
  }
903
884
 
@@ -910,6 +891,7 @@ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
910
891
 
911
892
  rb_scan_args(argc, argv, "11", &name, &mtime);
912
893
  rb_need_block();
894
+ Check_Type(name, T_STRING);
913
895
 
914
896
  if (NIL_P(mtime)) {
915
897
  mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
@@ -923,7 +905,7 @@ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
923
905
  if ((z = malloc(sizeof(struct read_proc))) == NULL) {
924
906
  zip_unchange_all(p_archive->archive);
925
907
  zip_unchange_archive(p_archive->archive);
926
- rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", StringValuePtr(name));
908
+ rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", RSTRING_PTR(name));
927
909
  }
928
910
 
929
911
  z->proc = rb_block_proc();
@@ -931,14 +913,14 @@ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
931
913
 
932
914
  if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
933
915
  free(z);
934
- rb_raise(Error, "Add failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
916
+ rb_raise(Error, "Add failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
935
917
  }
936
918
 
937
- if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
919
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
938
920
  zip_source_free(zsource);
939
921
  zip_unchange_all(p_archive->archive);
940
922
  zip_unchange_archive(p_archive->archive);
941
- rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
923
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
942
924
  }
943
925
 
944
926
  return Qnil;
@@ -1009,7 +991,7 @@ static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALU
1009
991
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
1010
992
  Check_Archive(p_archive);
1011
993
 
1012
- index = zip_name_locate(p_archive->archive, StringValuePtr(name), i_flags);
994
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
1013
995
 
1014
996
  if (index >= 0) {
1015
997
  VALUE _args[] = { INT2NUM(index), mtime };
@@ -1157,8 +1139,8 @@ static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment) {
1157
1139
 
1158
1140
  if (!NIL_P(comment)) {
1159
1141
  Check_Type(comment, T_STRING);
1160
- s_comment = StringValuePtr(comment);
1161
- len = RSTRING(comment)->len;
1142
+ s_comment = RSTRING_PTR(comment);
1143
+ len = RSTRING_LEN(comment);
1162
1144
  }
1163
1145
 
1164
1146
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
@@ -1189,7 +1171,7 @@ static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
1189
1171
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
1190
1172
  Check_Archive(p_archive);
1191
1173
 
1192
- return INT2NUM(zip_name_locate(p_archive->archive, StringValuePtr(fname), i_flags));
1174
+ return INT2NUM(zip_name_locate(p_archive->archive, RSTRING_PTR(fname), i_flags));
1193
1175
  }
1194
1176
 
1195
1177
  /* */
@@ -1222,8 +1204,8 @@ static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment
1222
1204
 
1223
1205
  if (!NIL_P(comment)) {
1224
1206
  Check_Type(comment, T_STRING);
1225
- s_comment = StringValuePtr(comment);
1226
- len = RSTRING(comment)->len;
1207
+ s_comment = RSTRING_PTR(comment);
1208
+ len = RSTRING_LEN(comment);
1227
1209
  }
1228
1210
 
1229
1211
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
@@ -1258,10 +1240,11 @@ static VALUE zipruby_archive_fdelete(VALUE self, VALUE index) {
1258
1240
  static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name) {
1259
1241
  struct zipruby_archive *p_archive;
1260
1242
 
1243
+ Check_Type(name, T_STRING);
1261
1244
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
1262
1245
  Check_Archive(p_archive);
1263
1246
 
1264
- if (zip_rename(p_archive->archive, NUM2INT(index), StringValuePtr(name)) == -1) {
1247
+ if (zip_rename(p_archive->archive, NUM2INT(index), RSTRING_PTR(name)) == -1) {
1265
1248
  zip_unchange_all(p_archive->archive);
1266
1249
  zip_unchange_archive(p_archive->archive);
1267
1250
  rb_raise(Error, "Rename file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
@@ -1350,7 +1333,7 @@ static VALUE zipruby_archive_each(VALUE self) {
1350
1333
  /* */
1351
1334
  static VALUE zipruby_archive_commit(VALUE self) {
1352
1335
  struct zipruby_archive *p_archive;
1353
- int changed, survivors;
1336
+ int changed, survivors;
1354
1337
  int errorp;
1355
1338
 
1356
1339
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
@@ -1371,7 +1354,7 @@ static VALUE zipruby_archive_commit(VALUE self) {
1371
1354
  p_archive->archive = NULL;
1372
1355
  p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1373
1356
 
1374
- if ((p_archive->archive = zip_open(StringValuePtr(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1357
+ if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1375
1358
  char errstr[ERRSTR_BUFSIZE];
1376
1359
  zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1377
1360
  rb_raise(Error, "Commit archive failed: %s", errstr);
@@ -1390,13 +1373,14 @@ static VALUE zipruby_archive_is_open(VALUE self) {
1390
1373
 
1391
1374
  /* */
1392
1375
  static VALUE zipruby_archive_decrypt(VALUE self, VALUE password) {
1376
+ VALUE retval;
1393
1377
  struct zipruby_archive *p_archive;
1394
1378
  long pwdlen;
1395
- int changed, survivors;
1379
+ int changed, survivors;
1396
1380
  int errorp;
1397
1381
 
1398
1382
  Check_Type(password, T_STRING);
1399
- pwdlen = RSTRING(password)->len;
1383
+ pwdlen = RSTRING_LEN(password);
1400
1384
 
1401
1385
  if (pwdlen < 1) {
1402
1386
  rb_raise(Error, "Decrypt archive failed: Password is empty");
@@ -1422,26 +1406,27 @@ static VALUE zipruby_archive_decrypt(VALUE self, VALUE password) {
1422
1406
  p_archive->archive = NULL;
1423
1407
  p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1424
1408
 
1425
- zipruby_archive_s_decrypt(Archive, p_archive->path, password);
1409
+ retval = zipruby_archive_s_decrypt(Archive, p_archive->path, password);
1426
1410
 
1427
- if ((p_archive->archive = zip_open(StringValuePtr(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1411
+ if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1428
1412
  char errstr[ERRSTR_BUFSIZE];
1429
1413
  zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1430
1414
  rb_raise(Error, "Decrypt archive failed: %s", errstr);
1431
1415
  }
1432
1416
 
1433
- return Qnil;
1417
+ return retval;
1434
1418
  }
1435
1419
 
1436
1420
  /* */
1437
1421
  static VALUE zipruby_archive_encrypt(VALUE self, VALUE password) {
1422
+ VALUE retval;
1438
1423
  struct zipruby_archive *p_archive;
1439
1424
  long pwdlen;
1440
- int changed, survivors;
1425
+ int changed, survivors;
1441
1426
  int errorp;
1442
1427
 
1443
1428
  Check_Type(password, T_STRING);
1444
- pwdlen = RSTRING(password)->len;
1429
+ pwdlen = RSTRING_LEN(password);
1445
1430
 
1446
1431
  if (pwdlen < 1) {
1447
1432
  rb_raise(Error, "Encrypt archive failed: Password is empty");
@@ -1467,17 +1452,18 @@ static VALUE zipruby_archive_encrypt(VALUE self, VALUE password) {
1467
1452
  p_archive->archive = NULL;
1468
1453
  p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1469
1454
 
1470
- zipruby_archive_s_encrypt(Archive, p_archive->path, password);
1455
+ retval = zipruby_archive_s_encrypt(Archive, p_archive->path, password);
1471
1456
 
1472
- if ((p_archive->archive = zip_open(StringValuePtr(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1457
+ if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1473
1458
  char errstr[ERRSTR_BUFSIZE];
1474
1459
  zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1475
1460
  rb_raise(Error, "Encrypt archive failed: %s", errstr);
1476
1461
  }
1477
1462
 
1478
- return Qnil;
1463
+ return retval;
1479
1464
  }
1480
1465
 
1466
+ /* */
1481
1467
  static VALUE zipruby_archive_read(VALUE self) {
1482
1468
  VALUE retval = Qnil;
1483
1469
  struct zipruby_archive *p_archive;
@@ -1491,17 +1477,17 @@ static VALUE zipruby_archive_read(VALUE self) {
1491
1477
  if (NIL_P(p_archive->path)) {
1492
1478
  rb_raise(rb_eRuntimeError, "invalid Zip::Archive");
1493
1479
  }
1494
-
1495
- #ifdef _WIN32
1496
- if (fopen_s(&fzip ,StringValuePtr(p_archive->path), "rb") != 0) {
1480
+
1481
+ #ifdef _WIN32
1482
+ if (fopen_s(&fzip, RSTRING_PTR(p_archive->path), "rb") != 0) {
1497
1483
  rb_raise(Error, "Read archive failed: Cannot open archive");
1498
- }
1499
- #else
1500
- if ((fzip = fopen(StringValuePtr(p_archive->path), "rb")) == NULL) {
1484
+ }
1485
+ #else
1486
+ if ((fzip = fopen(RSTRING_PTR(p_archive->path), "rb")) == NULL) {
1501
1487
  rb_raise(Error, "Read archive failed: Cannot open archive");
1502
- }
1503
- #endif
1504
-
1488
+ }
1489
+ #endif
1490
+
1505
1491
  block_given = rb_block_given_p();
1506
1492
 
1507
1493
  while ((n = fread(buf, 1, sizeof(buf), fzip)) > 0) {
@@ -1519,10 +1505,10 @@ static VALUE zipruby_archive_read(VALUE self) {
1519
1505
  #ifdef RUBY_WIN32_H
1520
1506
  #undef fclose
1521
1507
  fclose(fzip);
1522
- #define fclose(f) rb_w32_fclose(f)
1523
- #else
1508
+ #define fclose(f) rb_w32_fclose(f)
1509
+ #else
1524
1510
  fclose(fzip);
1525
- #endif
1511
+ #endif
1526
1512
 
1527
1513
  if (n == -1) {
1528
1514
  rb_raise(Error, "Read archive failed");
@@ -1646,7 +1632,7 @@ static VALUE zipruby_file_initialize(int argc, VALUE *argv, VALUE self) {
1646
1632
  }
1647
1633
 
1648
1634
  switch (TYPE(index)) {
1649
- case T_STRING: fname = StringValuePtr(index); break;
1635
+ case T_STRING: fname = RSTRING_PTR(index); break;
1650
1636
  case T_FIXNUM: i_index = NUM2INT(index); break;
1651
1637
  default:
1652
1638
  rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
@@ -1802,8 +1788,8 @@ static VALUE zipruby_file_set_comment(VALUE self, VALUE comment) {
1802
1788
 
1803
1789
  if (!NIL_P(comment)) {
1804
1790
  Check_Type(comment, T_STRING);
1805
- s_comment = StringValuePtr(comment);
1806
- len = RSTRING(comment)->len;
1791
+ s_comment = RSTRING_PTR(comment);
1792
+ len = RSTRING_LEN(comment);
1807
1793
  }
1808
1794
 
1809
1795
  Data_Get_Struct(self, struct zipruby_file, p_file);
@@ -1838,10 +1824,11 @@ static VALUE zipruby_file_delete(VALUE self) {
1838
1824
  static VALUE zipruby_file_rename(VALUE self, VALUE name) {
1839
1825
  struct zipruby_file *p_file;
1840
1826
 
1827
+ Check_Type(name, T_STRING);
1841
1828
  Data_Get_Struct(self, struct zipruby_file, p_file);
1842
1829
  Check_File(p_file);
1843
1830
 
1844
- if (zip_rename(p_file->archive, p_file->sb->index, StringValuePtr(name)) == -1) {
1831
+ if (zip_rename(p_file->archive, p_file->sb->index, RSTRING_PTR(name)) == -1) {
1845
1832
  zip_unchange_all(p_file->archive);
1846
1833
  zip_unchange_archive(p_file->archive);
1847
1834
  rb_raise(Error, "Rename file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
@@ -2009,7 +1996,7 @@ static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self) {
2009
1996
  }
2010
1997
 
2011
1998
  switch (TYPE(index)) {
2012
- case T_STRING: fname = StringValuePtr(index); break;
1999
+ case T_STRING: fname = RSTRING_PTR(index); break;
2013
2000
  case T_FIXNUM: i_index = NUM2INT(index); break;
2014
2001
  default:
2015
2002
  rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
@@ -2176,11 +2163,11 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
2176
2163
  return 0;
2177
2164
  }
2178
2165
 
2179
- n = RSTRING(src)->len;
2166
+ n = RSTRING_LEN(src);
2180
2167
 
2181
2168
  if (n > 0) {
2182
2169
  n = (n > len) ? len : n;
2183
- memcpy(buf, StringValuePtr(src), n);
2170
+ memcpy(buf, RSTRING_PTR(src), n);
2184
2171
  }
2185
2172
 
2186
2173
  return n;