nobu.nokada
12/23/2004 6:11:00 PM
Hi,
At Fri, 24 Dec 2004 01:38:45 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:124370]:
> |I have the same problem. A 'Marshal.dump(md5obj)' would be nice.
> |martinus
>
> Let me put it in my todo list.
It seems quite simple.
Index: ext/digest/digest.c
===================================================================
RCS file: /cvs/ruby/src/ruby/ext/digest/digest.c,v
retrieving revision 1.15
diff -U2 -p -r1.15 digest.c
--- ext/digest/digest.c 17 Sep 2004 09:24:11 -0000 1.15
+++ ext/digest/digest.c 23 Dec 2004 17:36:19 -0000
@@ -289,4 +289,36 @@ rb_digest_base_equal(self, other)
}
+static VALUE
+rb_digest_base_mdump(self)
+ VALUE self;
+{
+ algo_t *algo;
+ void *pctx;
+
+ algo = get_digest_base_metadata(rb_obj_class(self));
+ Data_Get_Struct(self, void, pctx);
+ return rb_str_new(pctx, algo->ctx_size);
+}
+
+static VALUE
+rb_digest_base_mload(self, data)
+ VALUE self, data;
+{
+ algo_t *algo;
+ void *pctx;
+ size_t len;
+
+ StringValue(data);
+ algo = get_digest_base_metadata(rb_obj_class(self));
+ Data_Get_Struct(self, void, pctx);
+ len = algo->ctx_size;
+ if (RSTRING(data)->len != len) {
+ rb_raise(rb_eArgError, "wrong size digest context (%ld for %ld)",
+ RSTRING(data)->len, len);
+ }
+ memcpy(pctx, RSTRING(data)->ptr, len);
+ return self;
+}
+
/*
* Init
@@ -312,4 +344,6 @@ Init_digest()
rb_define_method(cDigest_Base, "to_s", rb_digest_base_hexdigest, 0);
rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);
+ rb_define_method(cDigest_Base, "marshal_dump", rb_digest_base_mdump, 0);
+ rb_define_method(cDigest_Base, "marshal_load", rb_digest_base_mload, 1);
id_metadata = rb_intern("metadata");
--
Nobu Nakada