Skip to content

Commit 22cd718

Browse files
committed
add encodeData
1 parent 42cecbf commit 22cd718

File tree

4 files changed

+91
-29
lines changed

4 files changed

+91
-29
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ $filecontent = DCode::qrcode("HELLO");
2121
file_put_contents("test.png", $filecontent);
2222
$filecontent = DCode::qrcode8bit("HELLO WORLD");
2323
file_put_contents("test1.png", $filecontent);
24+
$filecontent = DCode::qrcodedata(strlen("HELLO WORLD"), "HELLO WORLD");
25+
file_put_contents("test2.png", $filecontent);
2426
||
2527
dcode_encrypt($src, $key = "THIS IS SHIT", $ckeylength = 8, $expire = 0);
2628
dcode_decrypt($src, $key = "THIS IS SHIT", $ckeylength = 8);
2729
$filecontent = dcode_qrcode("HELLO");
2830
file_put_contents("test.png", $filecontent);
2931
$filecontent = dcode_qrcode8bit("HELLO 8bit");
3032
file_put_contents("test1.png", $filecontent);
33+
$filecontent = dcode_qrcodedata(strlen("HELLO 8bit"), "HELLO 8bit");
34+
file_put_contents("test2.png", $filecontent);
3135
```
3236

3337
## Install

dcode.c

+84-28
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_dcode_qrcode8bit, 0, 0, 1)
7777
ZEND_ARG_INFO(0, level)
7878
ZEND_END_ARG_INFO()
7979

80+
ZEND_BEGIN_ARG_INFO_EX(arginfo_dcode_qrcodedata, 0, 0, 2)
81+
ZEND_ARG_INFO(0, size)
82+
ZEND_ARG_INFO(0, data)
83+
ZEND_ARG_INFO(0, version)
84+
ZEND_ARG_INFO(0, level)
85+
ZEND_END_ARG_INFO()
8086
/** }}} */
8187

8288
/* {{{ dcode_functions[]
@@ -88,6 +94,7 @@ const zend_function_entry dcode_functions[] = {
8894
PHP_FE(dcode_decrypt, arginfo_dcode_decrypt)
8995
PHP_FE(dcode_qrcode, arginfo_dcode_qrcode)
9096
PHP_FE(dcode_qrcode8bit, arginfo_dcode_qrcode8bit)
97+
PHP_FE(dcode_qrcodedata, arginfo_dcode_qrcodedata)
9198
PHP_FE_END
9299
};
93100

@@ -96,6 +103,7 @@ const zend_function_entry dcode_methods[] = {
96103
PHP_ME(dcode, decrypt, arginfo_dcode_decrypt, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
97104
PHP_ME(dcode, qrcode, arginfo_dcode_qrcode, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
98105
PHP_ME(dcode, qrcode8bit, arginfo_dcode_qrcode8bit, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
106+
PHP_ME(dcode, qrcodedata, arginfo_dcode_qrcodedata, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
99107
PHP_FE_END
100108
};
101109
/* }}} */
@@ -369,6 +377,30 @@ static char* dcode_write_to_png(QRcode *qrcode, int size, int margin, int *pp_le
369377
}
370378
/** }}} */
371379

380+
/** {{{ dcode_qrcode_error(QRcode * TSRMLS_DC)
381+
* process qrcode result
382+
* Return TRUE or FALSE */
383+
static bool dcode_qrcode_error(QRcode *qcode TSRMLS_DC) {
384+
if (qcode == NULL)
385+
{
386+
if (errno == EINVAL) {
387+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode encode error, error invalid input object");
388+
}
389+
else if (errno == ENOMEM) {
390+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode encode error, unable to allocate memory for input objects");
391+
}
392+
else if (errno == ERANGE) {
393+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode encode error, input data is too large");
394+
}
395+
else {
396+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode encode error, errno : %d", errno);
397+
}
398+
return 0;
399+
}
400+
return 1;
401+
}
402+
/** }}} */
403+
372404
/** {{{ DCode::encrypt($src, $sec_key = "THIS IS SHIT", $sec_rand_key_len = 8, $expire = 0)
373405
Return False or String */
374406
PHP_METHOD(dcode, encrypt)
@@ -671,23 +703,9 @@ PHP_METHOD(dcode, qrcode)
671703

672704
QRcode *qcode = NULL;
673705
qcode = QRcode_encodeString(str_encode, version, (QRecLevel) level, (QRencodeMode) mode, casesensitive);
674-
if (qcode == NULL)
675-
{
676-
if (errno == EINVAL) {
677-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, error invalid input object");
678-
}
679-
else if (errno == ENOMEM) {
680-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, unable to allocate memory for input objects");
681-
}
682-
else if (errno == ERANGE) {
683-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, input data is too large");
684-
}
685-
else {
686-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, errno : %d", errno);
687-
}
706+
if (!dcode_qrcode_error(qcode TSRMLS_CC)) {
688707
RETURN_FALSE;
689708
}
690-
691709
int pp_len;
692710
char *pp = dcode_write_to_png(qcode, 3, 4, &pp_len);
693711
QRcode_free(qcode);
@@ -723,20 +741,48 @@ PHP_METHOD(dcode, qrcode8bit)
723741

724742
QRcode *qcode = NULL;
725743
qcode = QRcode_encodeString8bit(src, version, (QRecLevel) level);
726-
if (qcode == NULL)
744+
if (!dcode_qrcode_error(qcode TSRMLS_CC)) {
745+
RETURN_FALSE;
746+
}
747+
748+
int pp_len;
749+
char *pp = dcode_write_to_png(qcode, 3, 4, &pp_len);
750+
QRcode_free(qcode);
751+
qcode = NULL;
752+
753+
if (pp)
727754
{
728-
if (errno == EINVAL) {
729-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, error invalid input object");
730-
}
731-
else if (errno == ENOMEM) {
732-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, unable to allocate memory for input objects");
733-
}
734-
else if (errno == ERANGE) {
735-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, input data is too large");
736-
}
737-
else {
738-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "DCode::qrcode error, errno : %d", errno);
739-
}
755+
ZVAL_STRINGL(return_value, pp, pp_len, 1);
756+
efree(pp);
757+
return;
758+
}
759+
else {
760+
RETURN_FALSE;
761+
}
762+
}
763+
/** }}} */
764+
765+
/** {{{ DCode::qrcodedata($size, $data, $version = 0, $level = QR_ECLEVEL_L)
766+
* Encode byte stream (may include '\0') in 8-bit mode.
767+
* Return qrcode string
768+
* example: file_put_contents("test.png", DCode::qrcodedata(strlen("hello"), "hello"));
769+
*/
770+
PHP_METHOD(dcode, qrcodedata)
771+
{
772+
int size;
773+
char *data;
774+
int version = 0;
775+
int level = (int) QR_ECLEVEL_L;
776+
int data_len;
777+
778+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|ll", &size, &data, &data_len, &version, &level) == FAILURE) {
779+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid arguments");
780+
RETURN_FALSE;
781+
}
782+
783+
QRcode *qcode = NULL;
784+
qcode = QRcode_encodeData(size, (const unsigned char*)data, version, (QRecLevel) level);
785+
if (!dcode_qrcode_error(qcode TSRMLS_CC)) {
740786
RETURN_FALSE;
741787
}
742788

@@ -793,6 +839,16 @@ PHP_FUNCTION(dcode_qrcode8bit)
793839
}
794840
/** }}} */
795841

842+
/** {{{ DCode::qrcodedata($size, $data, $version = 0, $level = QR_ECLEVEL_L)
843+
* Return qrcode string
844+
* example: file_put_contents("test.png", DCode::qrcodedata("hello"));
845+
*/
846+
PHP_FUNCTION(dcode_qrcodedata)
847+
{
848+
PHP_MN(dcode_qrcodedata)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
849+
}
850+
/** }}} */
851+
796852
/*
797853
* Local variables:
798854
* tab-width: 4

dcode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static char* dcode_microtime(char *);
3737
static long dcode_time();
3838
static void dcode_png_writer(png_structp, png_bytep, png_size_t);
3939
static char* dcode_write_to_png(QRcode *, int, int, int *);
40-
40+
static bool dcode_qrcode_error(QRcode * TSRMLS_DC);
4141
#endif
4242
/*
4343
* Local variables:

php_dcode.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ PHP_FUNCTION(dcode_encrypt);
4848
PHP_FUNCTION(dcode_decrypt);
4949
PHP_FUNCTION(dcode_qrcode);
5050
PHP_FUNCTION(dcode_qrcode8bit);
51+
PHP_FUNCTION(dcode_qrcodedata);
5152

5253
PHP_METHOD(dcode, encrypt);
5354
PHP_METHOD(dcode, decrypt);
5455
PHP_METHOD(dcode, qrcode);
5556
PHP_METHOD(dcode, qrcode8bit);
57+
PHP_METHOD(dcode, qrcodedata);
5658

5759
/*
5860
Declare any global variables you may need between the BEGIN

0 commit comments

Comments
 (0)