@@ -295,25 +295,59 @@ ossl_cipher_pkcs5_keyivgen(int argc, VALUE *argv, VALUE self)
295295 return Qnil ;
296296}
297297
298+
299+ /*
300+ * call-seq:
301+ * cipher << data -> string
302+ *
303+ * === Parameters
304+ * +data+ is a nonempty string.
305+ *
306+ * This method is deprecated and not available in 1.9.x or later.
307+ */
308+ static VALUE
309+ ossl_cipher_update_deprecated (VALUE self , VALUE data )
310+ {
311+ char * cname ;
312+
313+ cname = rb_class2name (rb_obj_class (self ));
314+ rb_warning ("%s#<< is deprecated; use %s#update instead" , cname , cname );
315+ return rb_funcall (self , rb_intern ("update" ), 1 , data );
316+ }
317+
318+
298319/*
299320 * call-seq:
300- * cipher.update(string ) -> aString
321+ * cipher.update(data [, buffer] ) -> string or buffer
301322 *
323+ * === Parameters
324+ * +data+ is a nonempty string.
325+ * +buffer+ is an optional string to store the result.
302326 */
303327static VALUE
304- ossl_cipher_update (VALUE self , VALUE data )
328+ ossl_cipher_update (int argc , VALUE * argv , VALUE self )
305329{
306330 EVP_CIPHER_CTX * ctx ;
307331 char * in ;
308332 int in_len , out_len ;
309- VALUE str ;
333+ VALUE data , str ;
334+
335+ rb_scan_args (argc , argv , "11" , & data , & str );
310336
311337 StringValue (data );
312338 in = RSTRING_PTR (data );
313339 if ((in_len = RSTRING_LEN (data )) == 0 )
314340 rb_raise (rb_eArgError , "data must not be empty" );
315341 GetCipher (self , ctx );
316- str = rb_str_new (0 , in_len + EVP_CIPHER_CTX_block_size (ctx ));
342+ out_len = in_len + EVP_CIPHER_CTX_block_size (ctx );
343+
344+ if (NIL_P (str )) {
345+ str = rb_str_new (0 , out_len );
346+ } else {
347+ StringValue (str );
348+ rb_str_resize (str , out_len );
349+ }
350+
317351 if (!EVP_CipherUpdate (ctx , RSTRING_PTR (str ), & out_len , in , in_len ))
318352 ossl_raise (eCipherError , NULL );
319353 assert (out_len < RSTRING_LEN (str ));
@@ -518,7 +552,10 @@ Init_ossl_cipher(void)
518552 rb_define_method (cCipher , "encrypt" , ossl_cipher_encrypt , -1 );
519553 rb_define_method (cCipher , "decrypt" , ossl_cipher_decrypt , -1 );
520554 rb_define_method (cCipher , "pkcs5_keyivgen" , ossl_cipher_pkcs5_keyivgen , -1 );
521- rb_define_method (cCipher , "update" , ossl_cipher_update , 1 );
555+ rb_define_method (cCipher , "update" , ossl_cipher_update , -1 );
556+ #if RUBY_VERSION_CODE < 190
557+ rb_define_method (cCipher , "<<" , ossl_cipher_update_deprecated , 1 );
558+ #endif
522559 rb_define_method (cCipher , "final" , ossl_cipher_final , 0 );
523560 rb_define_method (cCipher , "name" , ossl_cipher_name , 0 );
524561 rb_define_method (cCipher , "key=" , ossl_cipher_set_key , 1 );
@@ -528,6 +565,5 @@ Init_ossl_cipher(void)
528565 rb_define_method (cCipher , "iv_len" , ossl_cipher_iv_length , 0 );
529566 rb_define_method (cCipher , "block_size" , ossl_cipher_block_size , 0 );
530567 rb_define_method (cCipher , "padding=" , ossl_cipher_set_padding , 1 );
531-
532- rb_define_const (mCipher , "PKCS5_SALT_LEN" , PKCS5_SALT_LEN );
533568}
569+
0 commit comments