]> granicus.if.org Git - php/commitdiff
[DOC] add quoted_printable_encode()
authorAntony Dovgal <tony2001@php.net>
Thu, 14 Aug 2008 10:11:26 +0000 (10:11 +0000)
committerAntony Dovgal <tony2001@php.net>
Thu, 14 Aug 2008 10:11:26 +0000 (10:11 +0000)
ext/standard/basic_functions.c
ext/standard/quot_print.c
ext/standard/quot_print.h
ext/standard/tests/strings/quoted_printable_encode_001.phpt [new file with mode: 0644]
ext/standard/tests/strings/quoted_printable_encode_002.phpt [new file with mode: 0644]

index 328e72ee8b25a26bfc1c640f60c8d472ac6d9a13..0768c7ef684743dc204cdb40e4651c21909002e8 100644 (file)
@@ -2195,6 +2195,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)
@@ -3405,6 +3411,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)
index 193768d83b453b051e7f69aef5e5de4fa78ab48b..4b02fff703fb1390dd6d328ea598232926376e24 100644 (file)
@@ -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) U */
+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
index a98ff64ea7acd5d87d3ae02578e391882a12d08c..ffc2579dcd0e4e29b2e57ce87faf8e8603b63113 100644 (file)
@@ -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 (file)
index 0000000..1558fc7
--- /dev/null
@@ -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 binary 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 (file)
index 0000000..4ddab2e
Binary files /dev/null and b/ext/standard/tests/strings/quoted_printable_encode_002.phpt differ