Skip to content

Commit a98efcd

Browse files
committed
Adding new function to convert from binary to GUID format
1 parent 5884ff3 commit a98efcd

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

ext/mssql/php_mssql.c

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| obtain it through the world-wide-web, please send a note to |
1313
| license@php.net so we can mail you a copy immediately. |
1414
+----------------------------------------------------------------------+
15-
| Authors: Frank M. Kromann frank@frontbase.com> |
15+
| Authors: Frank M. Kromann <frank@frontbase.com> |
1616
+----------------------------------------------------------------------+
1717
*/
1818

@@ -76,6 +76,7 @@ function_entry mssql_functions[] = {
7676
PHP_FE(mssql_init, NULL)
7777
PHP_FE(mssql_bind, a3_arg_force_ref)
7878
PHP_FE(mssql_execute, NULL)
79+
PHP_FE(mssql_guid_string, NULL)
7980
{NULL, NULL, NULL}
8081
};
8182

@@ -775,7 +776,7 @@ static void php_mssql_get_column_content_with_type(mssql_link *mssql_ptr,int off
775776
result->value.lval = (long) anyintcol(offset);
776777
result->type = IS_LONG;
777778
break;
778-
}
779+
}
779780
case SQLCHAR:
780781
case SQLVARCHAR:
781782
case SQLTEXT: {
@@ -2125,4 +2126,73 @@ PHP_FUNCTION(mssql_execute)
21252126
}
21262127
/* }}} */
21272128

2129+
/* {{{ proto string mssql_guid_string(string binary [,int short_format])
2130+
Converts a 16 byte binary GUID to a string */
2131+
PHP_FUNCTION(mssql_guid_string)
2132+
{
2133+
zval **binary, **short_format;
2134+
int sf = 0;
2135+
char buffer[32+1];
2136+
char buffer2[36+1];
2137+
MSSQLLS_FETCH();
2138+
2139+
switch(ZEND_NUM_ARGS()) {
2140+
case 1:
2141+
if (zend_get_parameters_ex(1, &binary)==FAILURE) {
2142+
RETURN_FALSE;
2143+
}
2144+
convert_to_string_ex(binary);
2145+
break;
2146+
case 2:
2147+
if (zend_get_parameters_ex(2, &binary, &short_format)==FAILURE) {
2148+
RETURN_FALSE;
2149+
}
2150+
convert_to_string_ex(binary);
2151+
convert_to_long_ex(short_format);
2152+
sf = (*short_format)->value.lval;
2153+
break;
2154+
2155+
default:
2156+
WRONG_PARAM_COUNT;
2157+
break;
2158+
}
2159+
2160+
dbconvert(NULL, SQLBINARY, (BYTE*)(*binary)->value.str.val, 16, SQLCHAR, buffer, -1);
2161+
2162+
if (sf) {
2163+
php_strtoupper(buffer, 32);
2164+
RETURN_STRING(buffer, 1);
2165+
}
2166+
else {
2167+
int i;
2168+
for (i=0; i<4; i++) {
2169+
buffer2[2*i] = buffer[6-2*i];
2170+
buffer2[2*i+1] = buffer[7-2*i];
2171+
}
2172+
buffer2[8] = '-';
2173+
for (i=0; i<2; i++) {
2174+
buffer2[9+2*i] = buffer[10-2*i];
2175+
buffer2[10+2*i] = buffer[11-2*i];
2176+
}
2177+
buffer2[13] = '-';
2178+
for (i=0; i<2; i++) {
2179+
buffer2[14+2*i] = buffer[14-2*i];
2180+
buffer2[15+2*i] = buffer[15-2*i];
2181+
}
2182+
buffer2[18] = '-';
2183+
for (i=0; i<4; i++) {
2184+
buffer2[19+i] = buffer[16+i];
2185+
}
2186+
buffer2[23] = '-';
2187+
for (i=0; i<12; i++) {
2188+
buffer2[24+i] = buffer[20+i];
2189+
}
2190+
buffer2[36] = 0;
2191+
2192+
php_strtoupper(buffer2, 36);
2193+
RETURN_STRING(buffer2, 1);
2194+
}
2195+
}
2196+
/* }}} */
2197+
21282198
#endif

ext/mssql/php_mssql.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| obtain it through the world-wide-web, please send a note to |
1313
| license@php.net so we can mail you a copy immediately. |
1414
+----------------------------------------------------------------------+
15-
| Authors: Frank M. Kromann frank@frontbase.com> |
15+
| Authors: Frank M. Kromann <frank@frontbase.com> |
1616
+----------------------------------------------------------------------+
1717
*/
1818

@@ -83,6 +83,7 @@ PHP_FUNCTION(mssql_min_message_severity);
8383
PHP_FUNCTION(mssql_init);
8484
PHP_FUNCTION(mssql_bind);
8585
PHP_FUNCTION(mssql_execute);
86+
PHP_FUNCTION(mssql_guid_string);
8687

8788
typedef struct mssql_link {
8889
LOGINREC *login;

0 commit comments

Comments
 (0)