summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorAntony Dovgal <tony2001@php.net>2008-08-14 10:12:21 +0000
committerAntony Dovgal <tony2001@php.net>2008-08-14 10:12:21 +0000
commita61c0c84141981e4ca6764319b3d05b18e14b895 (patch)
tree005425fc32a5a25d033b49f5cc2e244d9884db6e /ext
parent593e7c7da413ef239c33588ced182087503f8234 (diff)
downloadphp-git-a61c0c84141981e4ca6764319b3d05b18e14b895.tar.gz
MFH: add quoted_printable_encode()
Diffstat (limited to 'ext')
-rw-r--r--ext/standard/basic_functions.c7
-rw-r--r--ext/standard/quot_print.c67
-rw-r--r--ext/standard/quot_print.h2
-rw-r--r--ext/standard/tests/strings/quoted_printable_encode_001.phpt33
-rw-r--r--ext/standard/tests/strings/quoted_printable_encode_002.phptbin0 -> 7290 bytes
5 files changed, 109 insertions, 0 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 8372e1dcc6..4145ed64e1 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -2210,6 +2210,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_quoted_printable_decode, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
+/* {{{ quot_print.c */
+static
+ZEND_BEGIN_ARG_INFO(arginfo_quoted_printable_encode, 0)
+ ZEND_ARG_INFO(0, str)
+ZEND_END_ARG_INFO()
+/* }}} */
/* {{{ rand.c */
static
ZEND_BEGIN_ARG_INFO_EX(arginfo_srand, 0, 0, 0)
@@ -3371,6 +3377,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
#endif
PHP_FE(quoted_printable_decode, arginfo_quoted_printable_decode)
+ PHP_FE(quoted_printable_encode, arginfo_quoted_printable_encode)
PHP_FE(convert_cyr_string, arginfo_convert_cyr_string)
PHP_FE(get_current_user, arginfo_get_current_user)
PHP_FE(set_time_limit, arginfo_set_time_limit)
diff --git a/ext/standard/quot_print.c b/ext/standard/quot_print.c
index 47393e78ca..0187368190 100644
--- a/ext/standard/quot_print.c
+++ b/ext/standard/quot_print.c
@@ -143,6 +143,53 @@ PHPAPI unsigned char *php_quot_print_decode(const unsigned char *str, size_t len
}
/* }}} */
+#define PHP_QPRINT_MAXL 75
+
+PHPAPI unsigned char *php_quot_print_encode(const unsigned char *str, size_t length, size_t *ret_length) /* {{{ */
+{
+ unsigned long lp = 0;
+ unsigned char c, *ret, *d;
+ char *hex = "0123456789ABCDEF";
+
+ ret = safe_emalloc(1, 3 * length + 3 * (((3 * length)/PHP_QPRINT_MAXL) + 1), 0);
+ d = ret;
+
+ while (length--) {
+ if (((c = *str++) == '\015') && (*str == '\012') && length > 0) {
+ *d++ = '\015';
+ *d++ = *str++;
+ length--;
+ lp = 0;
+ } else {
+ if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') || ((c == ' ') && (*str == '\015'))) {
+ if ((lp += 3) > PHP_QPRINT_MAXL) {
+ *d++ = '=';
+ *d++ = '\015';
+ *d++ = '\012';
+ lp = 3;
+ }
+ *d++ = '=';
+ *d++ = hex[c >> 4];
+ *d++ = hex[c & 0xf];
+ } else {
+ if ((++lp) > PHP_QPRINT_MAXL) {
+ *d++ = '=';
+ *d++ = '\015';
+ *d++ = '\012';
+ lp = 1;
+ }
+ *d++ = c;
+ }
+ }
+ }
+ *d = '\0';
+ *ret_length = d - ret;
+
+ ret = erealloc(ret, *ret_length + 1);
+ return ret;
+}
+/* }}} */
+
/*
*
* Decoding Quoted-printable string.
@@ -209,6 +256,26 @@ PHP_FUNCTION(quoted_printable_decode)
}
/* }}} */
+/* {{{ proto string quoted_printable_encode(string str) */
+PHP_FUNCTION(quoted_printable_encode)
+{
+ char *str, *new_str;
+ int str_len;
+ size_t new_str_len;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) != SUCCESS) {
+ return;
+ }
+
+ if (!str_len) {
+ RETURN_EMPTY_STRING();
+ }
+
+ new_str = (char *)php_quot_print_encode((unsigned char *)str, (size_t)str_len, &new_str_len);
+ RETURN_STRINGL(new_str, new_str_len, 0);
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4
diff --git a/ext/standard/quot_print.h b/ext/standard/quot_print.h
index a98ff64ea7..ffc2579dcd 100644
--- a/ext/standard/quot_print.h
+++ b/ext/standard/quot_print.h
@@ -22,7 +22,9 @@
#define QUOT_PRINT_H
PHPAPI unsigned char *php_quot_print_decode(const unsigned char *str, size_t length, size_t *ret_length, int replace_us_by_ws);
+PHPAPI unsigned char *php_quot_print_encode(const unsigned char *str, size_t length, size_t *ret_length);
PHP_FUNCTION(quoted_printable_decode);
+PHP_FUNCTION(quoted_printable_encode);
#endif /* QUOT_PRINT_H */
diff --git a/ext/standard/tests/strings/quoted_printable_encode_001.phpt b/ext/standard/tests/strings/quoted_printable_encode_001.phpt
new file mode 100644
index 0000000000..6a22da8afc
--- /dev/null
+++ b/ext/standard/tests/strings/quoted_printable_encode_001.phpt
@@ -0,0 +1,33 @@
+--TEST--
+quoted_printable_encode() tests - 1
+--FILE--
+<?php
+
+var_dump(quoted_printable_encode());
+var_dump(quoted_printable_encode(""));
+var_dump(quoted_printable_encode("test"));
+var_dump(quoted_printable_encode("test", "more"));
+
+$a = array("str");
+var_dump(quoted_printable_encode($a));
+var_dump(quoted_printable_encode(1));
+var_dump(quoted_printable_encode(NULL));
+var_dump(quoted_printable_encode(false));
+
+echo "Done\n";
+?>
+--EXPECTF--
+Warning: quoted_printable_encode() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+string(0) ""
+string(4) "test"
+
+Warning: quoted_printable_encode() expects exactly 1 parameter, 2 given in %s on line %d
+NULL
+
+Warning: quoted_printable_encode() expects parameter 1 to be string, array given in %s on line %d
+NULL
+string(1) "1"
+string(0) ""
+string(0) ""
+Done
diff --git a/ext/standard/tests/strings/quoted_printable_encode_002.phpt b/ext/standard/tests/strings/quoted_printable_encode_002.phpt
new file mode 100644
index 0000000000..5380eb0bff
--- /dev/null
+++ b/ext/standard/tests/strings/quoted_printable_encode_002.phpt
Binary files differ