summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSascha Schumann <sas@php.net>1999-05-16 11:12:23 +0000
committerSascha Schumann <sas@php.net>1999-05-16 11:12:23 +0000
commited8a7dff62baac1396edd66770bfc70f2f5f8097 (patch)
tree71048e61c215ab524b0233a20db9e5b7c8a81f13
parentd886b84ef4c6ca83ca73954c8f68bec049c5c672 (diff)
downloadphp-git-ed8a7dff62baac1396edd66770bfc70f2f5f8097.tar.gz
add fast bin2hex string function
-rw-r--r--ext/standard/php3_string.h1
-rw-r--r--ext/standard/string.c44
2 files changed, 45 insertions, 0 deletions
diff --git a/ext/standard/php3_string.h b/ext/standard/php3_string.h
index 04c2f79f95..af90dbec5f 100644
--- a/ext/standard/php3_string.h
+++ b/ext/standard/php3_string.h
@@ -79,6 +79,7 @@ extern void php3_setlocale(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_stristr(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_chunk_split(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_parsestr(INTERNAL_FUNCTION_PARAMETERS);
+PHP_FUNCTION(bin2hex);
extern PHPAPI char *_php3_strtoupper(char *s);
extern PHPAPI char *_php3_strtolower(char *s);
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 133f817f17..2964d463ab 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -43,6 +43,50 @@
#include "zend_execute.h"
#include "php_globals.h"
+static char hexconvtab[] = "0123456789abcdef";
+
+static char *php_bin2hex(const char *old, const size_t oldlen, size_t *newlen)
+{
+ char *new = NULL;
+ int i, j;
+
+ new = (char *) emalloc(oldlen * 2 * sizeof(char));
+ if(!new) {
+ return new;
+ }
+
+ for(i = j = 0; i < oldlen; i++) {
+ new[j++] = hexconvtab[old[i] >> 4];
+ new[j++] = hexconvtab[old[i] & 15];
+ }
+
+ if(newlen) *newlen = oldlen * 2 * sizeof(char);
+
+ return new;
+}
+
+/* proto bin2hex(string data)
+ converts the binary representation of data to hex */
+PHP_FUNCTION(bin2hex)
+{
+ pval *data;
+ char *new;
+ size_t newlen;
+
+ if(ARG_COUNT(ht) != 1 || getParameters(ht, 1, &data) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_string(data);
+
+ new = php_bin2hex(data->value.str.val, data->value.str.len, &newlen);
+
+ if(!new) {
+ RETURN_FALSE;
+ }
+
+ RETURN_STRINGL(new, newlen, 0);
+}
/* {{{ proto int strlen(string str)
Get string length */