@@ -799,6 +799,33 @@ rb_fiddle_ptr_s_to_ptr(VALUE self, VALUE val)
799799 return ptr ;
800800}
801801
802+ /*
803+ * call-seq:
804+ * Fiddle::Pointer.read(address, len) => string
805+ *
806+ * Or read the memory at address +address+ with length +len+ and return a
807+ * string with that memory
808+ */
809+
810+ static VALUE
811+ rb_fiddle_ptr_read_mem (VALUE klass , VALUE address , VALUE len )
812+ {
813+ return rb_str_new ((char * )NUM2PTR (address ), NUM2ULONG (len ));
814+ }
815+
816+ /*
817+ * call-seq:
818+ * Fiddle::Pointer.write(address, str)
819+ *
820+ * Write bytes in +str+ to the location pointed to by +address+.
821+ */
822+ static VALUE
823+ rb_fiddle_ptr_write_mem (VALUE klass , VALUE addr , VALUE str )
824+ {
825+ memcpy (NUM2PTR (addr ), StringValuePtr (str ), RSTRING_LEN (str ));
826+ return str ;
827+ }
828+
802829void
803830Init_fiddle_pointer (void )
804831{
@@ -815,6 +842,8 @@ Init_fiddle_pointer(void)
815842 rb_define_singleton_method (rb_cPointer , "malloc" , rb_fiddle_ptr_s_malloc , -1 );
816843 rb_define_singleton_method (rb_cPointer , "to_ptr" , rb_fiddle_ptr_s_to_ptr , 1 );
817844 rb_define_singleton_method (rb_cPointer , "[]" , rb_fiddle_ptr_s_to_ptr , 1 );
845+ rb_define_singleton_method (rb_cPointer , "read" , rb_fiddle_ptr_read_mem , 2 );
846+ rb_define_singleton_method (rb_cPointer , "write" , rb_fiddle_ptr_write_mem , 2 );
818847 rb_define_method (rb_cPointer , "initialize" , rb_fiddle_ptr_initialize , -1 );
819848 rb_define_method (rb_cPointer , "free=" , rb_fiddle_ptr_free_set , 1 );
820849 rb_define_method (rb_cPointer , "free" , rb_fiddle_ptr_free_get , 0 );
0 commit comments