aboutsummaryrefslogtreecommitdiff
path: root/src/pl/plperl/plperl_helpers.h
diff options
context:
space:
mode:
authorAndrew Dunstan <andrew@dunslane.net>2011-02-06 17:29:26 -0500
committerAndrew Dunstan <andrew@dunslane.net>2011-02-06 17:29:26 -0500
commit50d89d422f9c68a52a6964e5468e8eb4f90b1d95 (patch)
tree88b3bced2c0e54c12241b60853e33811079f9ce2 /src/pl/plperl/plperl_helpers.h
parent5ed45ac09c604555740e7365a722b641f3bce498 (diff)
downloadpostgresql-50d89d422f9c68a52a6964e5468e8eb4f90b1d95.tar.gz
postgresql-50d89d422f9c68a52a6964e5468e8eb4f90b1d95.zip
Force strings passed to and from plperl to be in UTF8 encoding.
String are converted to UTF8 on the way into perl and to the database encoding on the way back. This avoids a number of observed anomalies, and ensures Perl a consistent view of the world. Some minor code cleanups are also accomplished. Alex Hunsaker, reviewed by Andy Colson.
Diffstat (limited to 'src/pl/plperl/plperl_helpers.h')
-rw-r--r--src/pl/plperl/plperl_helpers.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/pl/plperl/plperl_helpers.h b/src/pl/plperl/plperl_helpers.h
new file mode 100644
index 00000000000..4480ce8f5eb
--- /dev/null
+++ b/src/pl/plperl/plperl_helpers.h
@@ -0,0 +1,69 @@
+#ifndef PL_PERL_HELPERS_H
+#define PL_PERL_HELPERS_H
+
+/*
+ * convert from utf8 to database encoding
+ */
+static inline char *
+utf_u2e(const char *utf8_str, size_t len)
+{
+ char *ret = (char*)pg_do_encoding_conversion((unsigned char*)utf8_str, len, PG_UTF8, GetDatabaseEncoding());
+ if (ret == utf8_str)
+ ret = pstrdup(ret);
+ return ret;
+}
+
+/*
+ * convert from database encoding to utf8
+ */
+static inline char *
+utf_e2u(const char *str)
+{
+ char *ret = (char*)pg_do_encoding_conversion((unsigned char*)str, strlen(str), GetDatabaseEncoding(), PG_UTF8);
+ if (ret == str)
+ ret = pstrdup(ret);
+ return ret;
+}
+
+
+/*
+ * Convert an SV to a char * in the current database encoding
+ */
+static inline char *
+sv2cstr(SV *sv)
+{
+ char *val;
+ STRLEN len;
+
+ /*
+ * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
+ */
+ val = SvPVutf8(sv, len);
+
+ /*
+ * we use perls length in the event we had an embedded null byte to ensure
+ * we error out properly
+ */
+ return utf_u2e(val, len);
+}
+
+/*
+ * Create a new SV from a string assumed to be in the current database's
+ * encoding.
+ */
+
+static inline SV *
+cstr2sv(const char *str)
+{
+ SV *sv;
+ char *utf8_str = utf_e2u(str);
+
+ sv = newSVpv(utf8_str, 0);
+ SvUTF8_on(sv);
+
+ pfree(utf8_str);
+
+ return sv;
+}
+
+#endif /* PL_PERL_HELPERS_H */