-
Notifications
You must be signed in to change notification settings - Fork 2
/
qrencode.c
292 lines (251 loc) · 8.31 KB
/
qrencode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2007 San Tai (vanilla) Hsu |
+----------------------------------------------------------------------+
| This source file is subject to the BSD license, that is bundled with |
| this package in the file LICENSE |
+----------------------------------------------------------------------+
| Author: San Tai (vanilla) Hsu ([email protected]> |
+----------------------------------------------------------------------+
*/
/**
* $file qrencode.c
* @brief source file of php qrencode extension.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_qrencode.h"
#include "php_open_temporary_file.h"
#include <qrencode.h>
#include <png.h>
static int le_qr;
typedef struct
{
QRcode *c;
} php_qrcode;
static void qr_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
zend_function_entry qrencode_functions[] = {
PHP_FE(qr_encode, NULL)
PHP_FE(qr_save, NULL)
{NULL, NULL, NULL}
};
zend_module_entry qrencode_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"qrencode",
qrencode_functions,
PHP_MINIT(qrencode),
PHP_MSHUTDOWN(qrencode),
NULL,
NULL,
PHP_MINFO(qrencode),
#if ZEND_MODULE_API_NO >= 20010901
"0.3",
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_QRENCODE
ZEND_GET_MODULE(qrencode)
#endif
PHP_MINIT_FUNCTION(qrencode)
{
le_qr = zend_register_list_destructors_ex(qr_dtor, NULL, "qr", module_number);
REGISTER_LONG_CONSTANT ("QR_MODE_NUL", QR_MODE_NUL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_MODE_NUM", QR_MODE_NUM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_MODE_AN", QR_MODE_AN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_MODE_8", QR_MODE_8, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_MODE_KANJI", QR_MODE_KANJI, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_ECLEVEL_L", QR_ECLEVEL_L, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_ECLEVEL_M", QR_ECLEVEL_M, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_ECLEVEL_Q", QR_ECLEVEL_Q, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT ("QR_ECLEVEL_H", QR_ECLEVEL_H, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(qrencode)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(qrencode)
{
php_info_print_table_start();
php_info_print_table_header(2, "qrencode support", "enabled");
php_info_print_table_end();
}
/* {{{ resource qr_encode (string text, [int version, int level, int mode, int casesensitive]); */
/**
* @brief main function to encode text to qr code.
* @param text the string want to encode.
* @param version qrcode version
* @param level level
* @param mode mode, could be QR_MODE_NUM, QR_MODE_AN, QR_MODE_8, QR_MODE_KANJI.
* @param casesensitive casesentive, if you want the 8 bit mode, must set this on.
* @return
*/
PHP_FUNCTION(qr_encode)
{
php_qrcode *qr = NULL;
long version = 1, level = QR_ECLEVEL_L, mode = QR_MODE_8, casesensitive = 1;
const char *text;
int text_len;
if (zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s|llll", &text, &text_len, &version, &level, &mode, &casesensitive) == FAILURE)
RETURN_FALSE;
qr = (php_qrcode *) emalloc (sizeof (php_qrcode));
if (mode == QR_MODE_8)
qr->c = QRcode_encodeString8bit(text, version, level);
else
qr->c = QRcode_encodeString(text, version, level, mode, casesensitive);
if (qr->c == NULL)
{
efree (qr);
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE (return_value, qr, le_qr);
}
/* }}} */
/* {{{ int qr_save (resource link, [string filename, int size, int margin]); */
/**
* @brief save function for qrencode.
* @param link qrcode resource link.
* @param filename filename want to save, it could be empty, and will show the content directly.
* @param size size
* @param margin margin.
* @return
*/
PHP_FUNCTION(qr_save)
{
zval *link = NULL;
long size = 3, margin = 4;
const char *fn = NULL;
int fn_len = 0;
FILE *fp = NULL;
png_structp png_ptr;
png_infop info_ptr;
unsigned char *row, *p, *q;
int x, y, xx, yy, bit;
int realwidth;
char *path;
int b;
char buf[4096];
if (zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "r|sll", &link, &fn, &fn_len, &size, &margin) == FAILURE )
RETURN_FALSE;
if (link)
{
php_qrcode *qr = NULL;
ZEND_FETCH_RESOURCE2 (qr, php_qrcode *, &link, -1, "qr handle", le_qr, NULL);
if (fn != NULL && fn_len > 0)
{
fp = VCWD_FOPEN (fn, "wb");
if (!fp)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing.", fn);
RETURN_FALSE;
}
}
else
{
fp = php_open_temporary_file (NULL, NULL, &path TSRMLS_CC);
if (!fp)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file for writing.");
RETURN_FALSE;
}
}
realwidth = (qr->c->width + margin * 2) * size;
row = (unsigned char *) emalloc ((realwidth + 7) / 8);
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png_ptr == NULL)
{
fclose(fp);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize PNG writer.");
RETURN_FALSE;
}
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL)
{
fclose(fp);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize PNG write.");
RETURN_FALSE;
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write PNG image.");
RETURN_FALSE;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, realwidth, realwidth, 1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
memset(row, 0xff, (realwidth + 7) / 8);
for(y = 0; y < margin * size; y++)
png_write_row(png_ptr, row);
p = qr->c->data;
for(y = 0; y < qr->c->width; y++)
{
bit = 7;
memset(row, 0xff, (realwidth + 7) / 8);
q = row;
q += margin * size / 8;
bit = 7 - (margin * size % 8);
for(x = 0; x < qr->c->width; x++)
{
for(xx = 0; xx < size; xx++)
{
*q ^= (*p & 1) << bit;
bit--;
if(bit < 0)
{
q++;
bit = 7;
}
}
p++;
}
for(yy=0; yy<size; yy++)
png_write_row(png_ptr, row);
}
memset(row, 0xff, (realwidth + 7) / 8);
for(y=0; y<margin * size; y++)
png_write_row(png_ptr, row);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
efree (row);
if (fn != NULL && fn_len > 0)
{
fflush (fp);
fclose (fp);
}
else
{
fseek (fp, 0, SEEK_SET);
#if APACHE && defined(CHARSET_EBCDIC)
ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
#endif
while ((b = fread (buf, 1, sizeof(buf), fp)) > 0)
php_write (buf, b TSRMLS_CC);
fclose (fp);
VCWD_UNLINK ((const char *)path);
efree (path);
}
RETURN_TRUE;
}
else
RETURN_FALSE;
}
/* }}} */
static void qr_dtor (zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_qrcode *qr = (php_qrcode *) rsrc->ptr;
if (qr->c)
QRcode_free (qr->c);
efree (qr);
}
/*
* vim:ts=4:expandtab:sw=4
*/