~spuul/nginx/trunk

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/src/ngx_http_lua_clfactory.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-02-15 03:05:42 UTC
  • mfrom: (4.3.10 sid)
  • Revision ID: package-import@ubuntu.com-20140215030542-71ubtowl24vf7nfn
Tags: 1.4.5-1ubuntu1
* Resynchronise with Debian (LP: #1280511).  Remaining changes:
  - debian/patches/ubuntu-branding.patch:
    + Add Ubuntu branding to server_tokens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#include "ngx_http_lua_clfactory.h"
16
16
 
17
17
 
 
18
#define CLFACTORY_BEGIN_CODE "return function() "
 
19
#define CLFACTORY_BEGIN_SIZE (sizeof(CLFACTORY_BEGIN_CODE) - 1)
 
20
 
 
21
#define CLFACTORY_END_CODE "\nend"
 
22
#define CLFACTORY_END_SIZE (sizeof(CLFACTORY_END_CODE) - 1)
 
23
 
 
24
 
18
25
/*
19
26
 * taken from chaoslawful:
20
27
 * Lua bytecode header        Luajit bytecode header
216
223
 * ---------------------
217
224
*/
218
225
 
219
 
/* bytecode for luajit */
220
 
#define    LJ_LITTLE_ENDIAN_CODE_STRIPPED                               \
221
 
    "\x14\x03\x00\x01\x00\x01\x00\x03"                                  \
222
 
    "\x31\x00\x00\x00\x30\x00\x00\x80\x48\x00\x02\x00"                  \
223
 
    "\x00\x00"
224
 
#define    LJ_BIG_ENDIAN_CODE_STRIPPED                                  \
225
 
    "\x14\x03\x00\x01\x00\x01\x00\x03"                                  \
226
 
    "\x00\x00\x00\x31\x80\x00\x00\x30\x00\x02\x00\x48"                  \
227
 
    "\x00\x00"
228
 
#define    LJ_LITTLE_ENDIAN_CODE                                        \
229
 
    "\x15\x03\x00\x01\x00\x01\x00\x03\x00"                              \
230
 
    "\x31\x00\x00\x00\x30\x00\x00\x80\x48\x00\x02\x00"                  \
231
 
    "\x00\x00"
232
 
#define    LJ_BIG_ENDIAN_CODE                                           \
233
 
    "\x15\x03\x00\x01\x00\x01\x00\x03\x00"                              \
234
 
    "\x00\x00\x00\x31\x80\x00\x00\x30\x00\x02\x00\x48"                  \
 
226
/* bytecode for luajit 2.0 */
 
227
 
 
228
#define    LJ20_LITTLE_ENDIAN_CODE_STRIPPED                             \
 
229
    "\x14\x03\x00\x01\x00\x01\x00\x03"                                  \
 
230
    "\x31\x00\x00\x00\x30\x00\x00\x80\x48\x00\x02\x00"                  \
 
231
    "\x00\x00"
 
232
 
 
233
#define    LJ20_BIG_ENDIAN_CODE_STRIPPED                                \
 
234
    "\x14\x03\x00\x01\x00\x01\x00\x03"                                  \
 
235
    "\x00\x00\x00\x31\x80\x00\x00\x30\x00\x02\x00\x48"                  \
 
236
    "\x00\x00"
 
237
 
 
238
#define    LJ20_LITTLE_ENDIAN_CODE                                      \
 
239
    "\x15\x03\x00\x01\x00\x01\x00\x03\x00"                              \
 
240
    "\x31\x00\x00\x00\x30\x00\x00\x80\x48\x00\x02\x00"                  \
 
241
    "\x00\x00"
 
242
 
 
243
#define    LJ20_BIG_ENDIAN_CODE                                         \
 
244
    "\x15\x03\x00\x01\x00\x01\x00\x03\x00"                              \
 
245
    "\x00\x00\x00\x31\x80\x00\x00\x30\x00\x02\x00\x48"                  \
 
246
    "\x00\x00"
 
247
 
 
248
/* bytecode for luajit 2.1 */
 
249
 
 
250
#define    LJ21_LITTLE_ENDIAN_CODE_STRIPPED                                  \
 
251
    "\x14\x03\x00\x01\x00\x01\x00\x03"                                       \
 
252
    "\x33\x00\x00\x00\x32\x00\x00\x80\x4c\x00\x02\x00"                       \
 
253
    "\x00\x00"
 
254
 
 
255
#define    LJ21_BIG_ENDIAN_CODE_STRIPPED                                     \
 
256
    "\x14\x03\x00\x01\x00\x01\x00\x03"                                       \
 
257
    "\x00\x00\x00\x33\x80\x00\x00\x32\x00\x02\x00\x4c"                       \
 
258
    "\x00\x00"
 
259
 
 
260
#define    LJ21_LITTLE_ENDIAN_CODE                                           \
 
261
    "\x15\x03\x00\x01\x00\x01\x00\x03\x00"                                   \
 
262
    "\x33\x00\x00\x00\x32\x00\x00\x80\x4c\x00\x02\x00"                       \
 
263
    "\x00\x00"
 
264
 
 
265
#define    LJ21_BIG_ENDIAN_CODE                                              \
 
266
    "\x15\x03\x00\x01\x00\x01\x00\x03\x00"                                   \
 
267
    "\x00\x00\x00\x33\x80\x00\x00\x32\x00\x02\x00\x4c"                       \
235
268
    "\x00\x00"
236
269
 
237
270
#define    LJ_CODE_LEN              23
239
272
#define    LJ_HEADERSIZE            5
240
273
#define    LJ_BCDUMP_F_BE           0x01
241
274
#define    LJ_BCDUMP_F_STRIP        0x02
242
 
#define    LJ_BCDUMP_VERSION        1
 
275
#define    LJ21_BCDUMP_VERSION        2
 
276
#define    LJ20_BCDUMP_VERSION        1
243
277
#define    LJ_SIGNATURE             "\x1b\x4c\x4a"
244
278
 
245
279
 
247
281
    NGX_LUA_TEXT_FILE,
248
282
    NGX_LUA_BT_LUA,
249
283
    NGX_LUA_BT_LJ
250
 
} clfactory_file_type_e;
 
284
} ngx_http_lua_clfactory_file_type_e;
251
285
 
252
286
 
253
287
typedef struct {
254
 
    clfactory_file_type_e file_type;
 
288
    ngx_http_lua_clfactory_file_type_e file_type;
 
289
 
255
290
    int         sent_begin;
256
291
    int         sent_end;
257
292
    int         extraline;
268
303
        char    str[MAX_END_CODE_SIZE];
269
304
    }           end_code;
270
305
    char        buff[LUAL_BUFFERSIZE];
271
 
} clfactory_file_ctx_t;
 
306
} ngx_http_lua_clfactory_file_ctx_t;
272
307
 
273
308
 
274
309
typedef struct {
276
311
    int         sent_end;
277
312
    const char *s;
278
313
    size_t      size;
279
 
} clfactory_buffer_ctx_t;
280
 
 
281
 
 
282
 
static const char *clfactory_getF(lua_State *L, void *ud, size_t *size);
283
 
static int clfactory_errfile(lua_State *L, const char *what, int fname_index);
284
 
static const char *clfactory_getS(lua_State *L, void *ud, size_t *size);
 
314
} ngx_http_lua_clfactory_buffer_ctx_t;
 
315
 
 
316
 
 
317
static const char *ngx_http_lua_clfactory_getF(lua_State *L, void *ud,
 
318
    size_t *size);
 
319
static int ngx_http_lua_clfactory_errfile(lua_State *L, const char *what,
 
320
    int fname_index);
 
321
static const char *ngx_http_lua_clfactory_getS(lua_State *L, void *ud,
 
322
    size_t *size);
 
323
static long ngx_http_lua_clfactory_file_size(FILE *f);
285
324
 
286
325
 
287
326
int
288
 
ngx_http_lua_clfactory_bytecode_prepare(lua_State *L, clfactory_file_ctx_t *lf,
289
 
    int fname_index)
 
327
ngx_http_lua_clfactory_bytecode_prepare(lua_State *L,
 
328
    ngx_http_lua_clfactory_file_ctx_t *lf, int fname_index)
290
329
{
291
330
    int                 x = 1, size_of_int, size_of_size_t, little_endian,
292
331
                        size_of_inst, version, stripped;
293
332
    static int          num_of_inst = 3, num_of_inter_func = 1;
294
333
    const char         *filename, *emsg, *serr, *bytecode;
295
334
    size_t              size, bytecode_len;
296
 
    ngx_file_info_t     fi;
 
335
    long                fsize;
297
336
 
298
337
    serr = NULL;
299
338
 
310
349
 
311
350
        version = *(lf->begin_code.str + 3);
312
351
 
 
352
        dd("version: %d", (int) version);
 
353
 
313
354
        if (ngx_memcmp(lf->begin_code.str, LJ_SIGNATURE,
314
 
                       sizeof(LJ_SIGNATURE) - 1)
315
 
            || version != LJ_BCDUMP_VERSION)
 
355
                       sizeof(LJ_SIGNATURE) - 1))
316
356
        {
317
357
            emsg = "bad byte-code header";
318
358
            goto error;
333
373
        little_endian = !((*(lf->begin_code.str + 4)) & LJ_BCDUMP_F_BE);
334
374
        stripped = (*(lf->begin_code.str + 4)) & LJ_BCDUMP_F_STRIP;
335
375
 
336
 
        if (stripped) {
337
 
            if (little_endian) {
338
 
                lf->end_code.ptr = LJ_LITTLE_ENDIAN_CODE_STRIPPED;
339
 
 
340
 
            } else {
341
 
                lf->end_code.ptr = LJ_BIG_ENDIAN_CODE_STRIPPED;
342
 
            }
343
 
 
344
 
            lf->end_code_len = LJ_CODE_LEN_STRIPPED;
 
376
        dd("stripped: %d", (int) stripped);
 
377
 
 
378
        if (version == LJ21_BCDUMP_VERSION) {
 
379
            if (stripped) {
 
380
                if (little_endian) {
 
381
                    lf->end_code.ptr = LJ21_LITTLE_ENDIAN_CODE_STRIPPED;
 
382
 
 
383
                } else {
 
384
                    lf->end_code.ptr = LJ21_BIG_ENDIAN_CODE_STRIPPED;
 
385
                }
 
386
 
 
387
                lf->end_code_len = LJ_CODE_LEN_STRIPPED;
 
388
 
 
389
            } else {
 
390
                if (little_endian) {
 
391
                    lf->end_code.ptr = LJ21_LITTLE_ENDIAN_CODE;
 
392
 
 
393
                } else {
 
394
                    lf->end_code.ptr = LJ21_BIG_ENDIAN_CODE;
 
395
                }
 
396
 
 
397
                lf->end_code_len = LJ_CODE_LEN;
 
398
            }
 
399
 
 
400
        } else if (version == LJ20_BCDUMP_VERSION) {
 
401
            if (stripped) {
 
402
                if (little_endian) {
 
403
                    lf->end_code.ptr = LJ20_LITTLE_ENDIAN_CODE_STRIPPED;
 
404
 
 
405
                } else {
 
406
                    lf->end_code.ptr = LJ20_BIG_ENDIAN_CODE_STRIPPED;
 
407
                }
 
408
 
 
409
                lf->end_code_len = LJ_CODE_LEN_STRIPPED;
 
410
 
 
411
            } else {
 
412
                if (little_endian) {
 
413
                    lf->end_code.ptr = LJ20_LITTLE_ENDIAN_CODE;
 
414
 
 
415
                } else {
 
416
                    lf->end_code.ptr = LJ20_BIG_ENDIAN_CODE;
 
417
                }
 
418
 
 
419
                lf->end_code_len = LJ_CODE_LEN;
 
420
            }
345
421
 
346
422
        } else {
347
 
            if (little_endian) {
348
 
                lf->end_code.ptr = LJ_LITTLE_ENDIAN_CODE;
349
 
 
350
 
            } else {
351
 
                lf->end_code.ptr = LJ_BIG_ENDIAN_CODE;
352
 
            }
353
 
 
354
 
            lf->end_code_len = LJ_CODE_LEN;
 
423
            emsg = "bytecode format version unsupported";
 
424
            goto error;
355
425
        }
356
426
 
357
 
        if (ngx_fd_info(fileno(lf->f), &fi) == NGX_FILE_ERROR) {
 
427
        fsize = ngx_http_lua_clfactory_file_size(lf->f);
 
428
        if (fsize < 0) {
358
429
            serr = strerror(errno);
359
 
            emsg = "cannot fstat";
 
430
            emsg = "cannot fseek/ftell";
360
431
            goto error;
361
432
        }
362
433
 
363
 
        lf->rest_len = ngx_file_size(&fi) - LJ_HEADERSIZE;
 
434
        lf->rest_len = fsize - LJ_HEADERSIZE;
364
435
 
365
436
#if defined(DDEBUG) && (DDEBUG)
366
437
        {
504
575
 
505
576
error:
506
577
 
507
 
    if (lf->f != stdin) {
508
 
        fclose(lf->f);  /* close file (even in case of errors) */
509
 
    }
 
578
    fclose(lf->f);  /* close file (even in case of errors) */
510
579
 
511
580
    filename = lua_tostring(L, fname_index) + 1;
512
581
 
528
597
{
529
598
    int                         c, status, readstatus;
530
599
    ngx_flag_t                  sharp;
531
 
    clfactory_file_ctx_t        lf;
 
600
 
 
601
    ngx_http_lua_clfactory_file_ctx_t        lf;
532
602
 
533
603
    /* index of filename on the stack */
534
604
    int                         fname_index;
544
614
    lf.end_code.ptr = CLFACTORY_END_CODE;
545
615
    lf.end_code_len = CLFACTORY_END_SIZE;
546
616
 
547
 
    if (filename == NULL) {
548
 
        lua_pushliteral(L, "=stdin");
549
 
        lf.f = stdin;
550
 
 
551
 
    } else {
552
 
        lua_pushfstring(L, "@%s", filename);
553
 
        lf.f = fopen(filename, "r");
554
 
 
555
 
        if (lf.f == NULL) {
556
 
            return clfactory_errfile(L, "open", fname_index);
557
 
        }
 
617
    lua_pushfstring(L, "@%s", filename);
 
618
 
 
619
    lf.f = fopen(filename, "r");
 
620
    if (lf.f == NULL) {
 
621
        return ngx_http_lua_clfactory_errfile(L, "open", fname_index);
558
622
    }
559
623
 
560
624
    c = getc(lf.f);
577
641
        lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
578
642
 
579
643
        if (lf.f == NULL) {
580
 
            return clfactory_errfile(L, "reopen", fname_index);
 
644
            return ngx_http_lua_clfactory_errfile(L, "reopen", fname_index);
581
645
        }
582
646
 
583
647
        /* check whether lib jit exists */
630
694
    }
631
695
 
632
696
    lf.sent_begin = lf.sent_end = 0;
633
 
    status = lua_load(L, clfactory_getF, &lf, lua_tostring(L, -1));
 
697
    status = lua_load(L, ngx_http_lua_clfactory_getF, &lf,
 
698
                      lua_tostring(L, -1));
634
699
 
635
700
    readstatus = ferror(lf.f);
636
701
 
640
705
 
641
706
    if (readstatus) {
642
707
        lua_settop(L, fname_index);  /* ignore results from `lua_load' */
643
 
        return clfactory_errfile(L, "read", fname_index);
 
708
        return ngx_http_lua_clfactory_errfile(L, "read", fname_index);
644
709
    }
645
710
 
646
711
    lua_remove(L, fname_index);
650
715
 
651
716
 
652
717
int
653
 
ngx_http_lua_clfactory_loadstring(lua_State *L, const char *s)
654
 
{
655
 
    return ngx_http_lua_clfactory_loadbuffer(L, s, strlen(s), s);
656
 
}
657
 
 
658
 
 
659
 
int
660
718
ngx_http_lua_clfactory_loadbuffer(lua_State *L, const char *buff,
661
 
        size_t size, const char *name)
 
719
    size_t size, const char *name)
662
720
{
663
 
    clfactory_buffer_ctx_t ls;
 
721
    ngx_http_lua_clfactory_buffer_ctx_t     ls;
664
722
 
665
723
    ls.s = buff;
666
724
    ls.size = size;
667
725
    ls.sent_begin = 0;
668
726
    ls.sent_end = 0;
669
727
 
670
 
    return lua_load(L, clfactory_getS, &ls, name);
 
728
    return lua_load(L, ngx_http_lua_clfactory_getS, &ls, name);
671
729
}
672
730
 
673
731
 
674
732
static const char *
675
 
clfactory_getF(lua_State *L, void *ud, size_t *size)
 
733
ngx_http_lua_clfactory_getF(lua_State *L, void *ud, size_t *size)
676
734
{
677
735
    char                        *buf;
678
736
    size_t                       num;
679
 
    clfactory_file_ctx_t        *lf;
680
 
 
681
 
    lf = (clfactory_file_ctx_t *) ud;
 
737
 
 
738
    ngx_http_lua_clfactory_file_ctx_t        *lf;
 
739
 
 
740
    lf = (ngx_http_lua_clfactory_file_ctx_t *) ud;
682
741
 
683
742
    if (lf->extraline) {
684
743
        lf->extraline = 0;
743
802
 
744
803
 
745
804
static int
746
 
clfactory_errfile(lua_State *L, const char *what, int fname_index)
 
805
ngx_http_lua_clfactory_errfile(lua_State *L, const char *what, int fname_index)
747
806
{
748
807
    const char      *serr;
749
808
    const char      *filename;
765
824
 
766
825
 
767
826
static const char *
768
 
clfactory_getS(lua_State *L, void *ud, size_t *size)
 
827
ngx_http_lua_clfactory_getS(lua_State *L, void *ud, size_t *size)
769
828
{
770
 
    clfactory_buffer_ctx_t      *ls = ud;
 
829
    ngx_http_lua_clfactory_buffer_ctx_t      *ls = ud;
771
830
 
772
831
    if (ls->sent_begin == 0) {
773
832
        ls->sent_begin = 1;
792
851
    return ls->s;
793
852
}
794
853
 
 
854
 
 
855
static long
 
856
ngx_http_lua_clfactory_file_size(FILE *f)
 
857
{
 
858
    long              cur_pos, len;
 
859
 
 
860
    cur_pos = ftell(f);
 
861
    if (cur_pos == -1) {
 
862
        return -1;
 
863
    }
 
864
 
 
865
    if (fseek(f, 0, SEEK_END) != 0) {
 
866
        return -1;
 
867
    }
 
868
 
 
869
    len = ftell(f);
 
870
    if (len == -1) {
 
871
        return -1;
 
872
    }
 
873
 
 
874
    if (fseek(f, cur_pos, SEEK_SET) != 0) {
 
875
        return -1;
 
876
    }
 
877
 
 
878
    return len;
 
879
}
 
880
 
 
881
 
795
882
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */