Skip to content

Commit

Permalink
Fix UTF-8 encoding in gpgdata write callback
Browse files Browse the repository at this point in the history
This change associates the buffer allocated in the gpgme_data write
callback with the UTF-8 encoding. If there is a configured internal
encoding (e.g. Encoding.default_internal), then the string is exported
to that encoding.
  • Loading branch information
ice799 committed Jun 30, 2018
1 parent 387c775 commit 5eb4513
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ext/gpgme/gpgme_n.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "ruby.h"
#ifdef HAVE_RUBY_ENCODING_H
#include "ruby/encoding.h"
static rb_encoding *utf8Encoding;
#endif
#include "gpgme.h"
#include <errno.h>
Expand Down Expand Up @@ -346,6 +347,14 @@ write_cb (void *handle, const void *buffer, size_t size)
vhook_value = RARRAY_PTR(vcb)[1];
vbuffer = rb_str_new (buffer, size);

#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_enc_associate(vbuffer, utf8Encoding);
if (default_internal_enc) {
vbuffer = rb_str_export_to_enc(vbuffer, default_internal_enc);
}
#endif

vnwrite = rb_funcall (vcbs, rb_intern ("write"), 3,
vhook_value, vbuffer, LONG2NUM(size));
return NUM2LONG(vnwrite);
Expand Down Expand Up @@ -3068,4 +3077,8 @@ Init_gpgme_n (void)
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_PKCS12",
INT2FIX(GPGME_EXPORT_MODE_PKCS12));
#endif

#ifdef HAVE_RUBY_ENCODING_H
utf8Encoding = rb_utf8_encoding();
#endif
}

0 comments on commit 5eb4513

Please sign in to comment.