c - PHP extension for Linux: reality check needed! -
okay, i've written first functional php extension. worked proof-of-concept only. i'm writing 1 boss wants.
what i'd know, php-heads out there, whether code makes sense. have got grasp of things emalloc
, like, or there stuff there that's going turn around later , try bite hand off?
below code 1 of functions. returns base64 of string has been blowfish encrypted. when function called, supplied 2 strings, text encrypt , encode, , key encryption phase. it's not using php's own base64 functions because, @ point, don't know how link them. , it's not using php's own mcrypt functions same reason. instead, links in ssleay bf_ecb_encrypt
functions.
php_function(blowfish_base64_encode) { char *psdata = null; char *pskey = null; int argc = zend_num_args(); int psdata_len; int pskey_len; char *buffer = null; char *pbuffer = null; char *encoded = null; bf_key context; int = 0; unsigned char block[ 8 ]; unsigned char * pblock = block; char *plaintext; int plaintext_len; int cipher_len = 0; if (zend_parse_parameters(argc tsrmls_cc, "ss", &psdata, &psdata_len, &pskey, &pskey_len) == failure) return; buffer = (char *) emalloc( psdata_len * 2 ); pbuffer = buffer; encoded = (char *) emalloc( psdata_len * 4 ); bf_set_key( &context, pskey_len, pskey ); plaintext = psdata; plaintext_len = psdata_len; (;;) { if (plaintext_len--) { block[ i++ ] = *plaintext++; if (i == 8 ) { bf_ecb_encrypt( block, pbuffer, &context, bf_encrypt ); pbuffer += 8; cipher_len += 8; memset( block, 0, 8 ); = 0; } } else { bf_ecb_encrypt( block, pbuffer, &context, bf_encrypt ); cipher_len += 8; break; } } b64_encode( encoded, buffer, cipher_len ); return_stringl( encoded, strlen( encoded ), 0 ); }
you'll notice have 2 emalloc
calls, encoded
, buffer
. encoded
passed caller, i'm concerned buffer won't freed. case? should use malloc/free buffer
?
if there other glaring errors, i'd appreciate knowing.
you'll notice have 2 emalloc calls,
encoded
,buffer
.encoded
passed caller, i'm concernedbuffer
won't freed. case? should usemalloc
/free
buffer
?
yes, should free efree
before returning.
although php has safety net , memory allocated emalloc
freed @ end of request, it's still bug leak memory and, depending warned if running debug build report_memleaks = on
.
Comments
Post a Comment