Skip to content

Commit e2cc242

Browse files
committed
limit maxsize upto 2048
1 parent 05eab5a commit e2cc242

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

ngx_http_qrcode_module.c

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*===============================================================
22
* Copyright (C) 2012 All rights reserved.
3-
*
3+
*
44
* Filename:ngx_http_qrcode_module.c
55
* Author :dcshi
66
* Created:2012-12-09
@@ -45,92 +45,92 @@ ngx_http_qrcode_urlencode_txt(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
4545
static char *
4646
ngx_http_qrcode_gen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
4747

48-
static ngx_int_t
48+
static ngx_int_t
4949
ngx_http_qrcode_handler(ngx_http_request_t* r);
5050

5151
static void *
5252
ngx_http_qrcode_create_loc_conf(ngx_conf_t *cf);
5353

54-
static char *
54+
static char *
5555
ngx_http_qrcode_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);
5656

5757
static char *
58-
ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
58+
ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
5959
ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
6060

6161
static ngx_command_t ngx_http_qrcode_commands[] = {
62-
{
62+
{
6363
ngx_string("qrcode_fg_color"),
6464
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
6565
ngx_http_qrcode_fg_color,
6666
NGX_HTTP_LOC_CONF_OFFSET,
6767
offsetof(ngx_http_qrcode_loc_conf_t, fg_color),
6868
NULL },
69-
{
69+
{
7070
ngx_string("qrcode_bg_color"),
7171
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
7272
ngx_http_qrcode_bg_color,
7373
NGX_HTTP_LOC_CONF_OFFSET,
7474
offsetof(ngx_http_qrcode_loc_conf_t, bg_color),
7575
NULL },
76-
{
76+
{
7777
ngx_string("qrcode_level"),
7878
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
7979
ngx_http_qrcode_level,
8080
NGX_HTTP_LOC_CONF_OFFSET,
8181
offsetof(ngx_http_qrcode_loc_conf_t, level),
8282
NULL },
83-
{
83+
{
8484
ngx_string("qrcode_hint"),
8585
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
8686
ngx_http_qrcode_hint,
8787
NGX_HTTP_LOC_CONF_OFFSET,
8888
offsetof(ngx_http_qrcode_loc_conf_t, hint),
8989
NULL },
90-
{
90+
{
9191
ngx_string("qrcode_size"),
9292
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
9393
ngx_http_qrcode_size,
9494
NGX_HTTP_LOC_CONF_OFFSET,
9595
offsetof(ngx_http_qrcode_loc_conf_t, size),
9696
NULL },
97-
{
97+
{
9898
ngx_string("qrcode_margin"),
9999
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
100100
ngx_http_qrcode_margin,
101101
NGX_HTTP_LOC_CONF_OFFSET,
102102
offsetof(ngx_http_qrcode_loc_conf_t, margin),
103103
NULL },
104-
{
104+
{
105105
ngx_string("qrcode_version"),
106106
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
107107
ngx_http_qrcode_version,
108108
NGX_HTTP_LOC_CONF_OFFSET,
109109
offsetof(ngx_http_qrcode_loc_conf_t, version),
110110
NULL },
111-
{
111+
{
112112
ngx_string("qrcode_casesensitive"),
113113
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
114114
ngx_http_qrcode_casesensitive,
115115
NGX_HTTP_LOC_CONF_OFFSET,
116116
offsetof(ngx_http_qrcode_loc_conf_t, casesensitive),
117117
NULL },
118-
{
118+
{
119119
ngx_string("qrcode_txt"),
120120
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
121121
ngx_http_qrcode_txt,
122122
NGX_HTTP_LOC_CONF_OFFSET,
123123
offsetof(ngx_http_qrcode_loc_conf_t, txt),
124124
NULL },
125-
{
125+
{
126126
ngx_string("qrcode_urlencode_txt"),
127127
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
128128
ngx_http_qrcode_urlencode_txt,
129129
NGX_HTTP_LOC_CONF_OFFSET,
130130
offsetof(ngx_http_qrcode_loc_conf_t, txt),
131131
NULL },
132132

133-
{
133+
{
134134
ngx_string("qrcode_gen"),
135135
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1|NGX_CONF_NOARGS,
136136
ngx_http_qrcode_gen,
@@ -173,7 +173,7 @@ ngx_http_qrcode_create_loc_conf(ngx_conf_t *cf)
173173

174174
qlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_qrcode_loc_conf_t));
175175
if (qlcf == NULL) {
176-
ngx_log_error(NGX_LOG_ERR, cf->log, 0,
176+
ngx_log_error(NGX_LOG_ERR, cf->log, 0,
177177
"Mem calloc ngx_http_qrcode_loc_conf_t fail");
178178
return NGX_CONF_ERROR;
179179
}
@@ -199,7 +199,7 @@ ngx_http_qrcode_create_loc_conf(ngx_conf_t *cf)
199199
return qlcf;
200200
}
201201

202-
static char *
202+
static char *
203203
ngx_http_qrcode_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child) {
204204
ngx_http_qrcode_loc_conf_t *pre = parent;
205205
ngx_http_qrcode_loc_conf_t *qlcf = child;
@@ -290,16 +290,16 @@ ngx_http_qrcode_urlencode_txt(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
290290
static char *
291291
ngx_http_qrcode_gen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
292292
{
293-
ngx_http_core_loc_conf_t *core_conf =
293+
ngx_http_core_loc_conf_t *core_conf =
294294
ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
295295

296296
core_conf->handler = ngx_http_qrcode_handler;
297-
297+
298298
return NGX_CONF_OK;
299299
}
300300

301-
static ngx_int_t
302-
ngx_http_qrcode_handler(ngx_http_request_t* r)
301+
static ngx_int_t
302+
ngx_http_qrcode_handler(ngx_http_request_t* r)
303303
{
304304
ngx_http_qrcode_loc_conf_t *qlcf;
305305
ngx_int_t code_size;
@@ -309,7 +309,7 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
309309
int img_stream_len;
310310
u_char *encoded_txt;
311311
ngx_int_t rc;
312-
312+
313313
qlcf = ngx_http_get_module_loc_conf(r, ngx_http_qrcode_module);
314314

315315
/* compile args */
@@ -320,27 +320,27 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
320320
}
321321

322322
encoded_txt = ngx_pcalloc(r->pool, qlcf->txt.len + 1);
323-
ngx_sprintf(encoded_txt, "%V", &qlcf->txt);
323+
ngx_sprintf(encoded_txt, "%V", &qlcf->txt);
324324

325325
QRcode *code;
326-
code = QRcode_encodeString((char*)encoded_txt,
326+
code = QRcode_encodeString((char*)encoded_txt,
327327
qlcf->version, qlcf->level, qlcf->hint, qlcf->casesensitive);
328328

329329
if(code == NULL) {
330-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
330+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
331331
"Failed to encode content.exception raised by libqrencode: %s", strerror(errno));
332332
return NGX_HTTP_INTERNAL_SERVER_ERROR;
333333
}
334334

335-
code_size = qlcf->size / code->width;
335+
code_size = ((qlcf->size > 2048) ? 1024 : qlcf->size) / code->width;
336336
code_size = (code_size == 0) ? 1 : code_size;
337337
img_margin = qlcf->margin;
338338
img_width = code->width * code_size + 2 * img_margin;
339339

340340
gdImagePtr img;
341341
img = gdImageCreate(img_width, img_width);
342342

343-
fg_color = gdImageColorAllocate(img,
343+
fg_color = gdImageColorAllocate(img,
344344
qlcf->fg_color[0], qlcf->fg_color[1], qlcf->fg_color[2]);
345345

346346
bg_color = gdImageColorAllocate(img,
@@ -349,18 +349,18 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
349349
gdImageFill(img, 0, 0, bg_color);
350350

351351
u_char *p = code->data;
352-
for (y = 0; y < code->width; y++)
352+
for (y = 0; y < code->width; y++)
353353
{
354-
for (x = 0; x < code->width; x++)
354+
for (x = 0; x < code->width; x++)
355355
{
356356
if (*p & 1) {
357357
posx = x * code_size + img_margin;
358358
posy = y * code_size + img_margin;
359359

360-
gdImageFilledRectangle(img, posx, posy,
360+
gdImageFilledRectangle(img, posx, posy,
361361
posx + code_size, posy + code_size, fg_color);
362362
}
363-
p++;
363+
p++;
364364
}
365365
}
366366

@@ -380,15 +380,15 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
380380
buffer = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
381381

382382
if (buffer == NULL) {
383-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
383+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
384384
"Failed to allocate response buffer");
385385
return NGX_HTTP_INTERNAL_SERVER_ERROR;
386386
}
387387

388388
//set up the buffer chain
389389
ngx_chain_t out;
390390
buffer->pos = img_stream;
391-
buffer->last = img_stream + img_stream_len;
391+
buffer->last = img_stream + img_stream_len;
392392
buffer->memory = 1;
393393
buffer->last_buf = 1;
394394
out.buf = buffer;
@@ -398,22 +398,22 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
398398
}
399399

400400
static char *
401-
ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
401+
ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
402402
ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
403403
{
404404
ngx_http_qrcode_loc_conf_t *qlcf;
405405
ngx_http_qrcode_cmd_t *qr_cmd;
406406
ngx_str_t *raw_args;
407-
ngx_array_t **cmds_ptr;
407+
ngx_array_t **cmds_ptr;
408408
ngx_array_t **args_ptr;
409409
ngx_http_qrcode_arg_template_t *arg;
410410
ngx_http_script_compile_t sc;
411411
ngx_uint_t i, n;
412412

413413
qlcf = (ngx_http_qrcode_loc_conf_t *)conf;
414414
cmds_ptr = &qlcf->cmds;
415-
416-
if (*cmds_ptr == NULL)
415+
416+
if (*cmds_ptr == NULL)
417417
{
418418
*cmds_ptr = ngx_array_create(cf->pool, 1, sizeof(ngx_http_qrcode_cmd_t));
419419

@@ -440,7 +440,7 @@ ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
440440
raw_args = cf->args->elts;
441441

442442
// we skip the first arg and start from the second
443-
for (i = 1 ; i < cf->args->nelts; i++)
443+
for (i = 1 ; i < cf->args->nelts; i++)
444444
{
445445
arg = ngx_array_push(*args_ptr);
446446

@@ -471,4 +471,4 @@ ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
471471
} /* end for */
472472

473473
return NGX_CONF_OK;
474-
}
474+
}

0 commit comments

Comments
 (0)