~ubuntu-branches/debian/sid/libembperl-perl/sid

« back to all changes in this revision

Viewing changes to epio.c

  • Committer: Bazaar Package Importer
  • Author(s): Angus Lees
  • Date: 2004-02-15 14:23:39 UTC
  • Revision ID: james.westby@ubuntu.com-20040215142339-n21gqf7mx9tmyb8d
Tags: upstream-2.0b10
ImportĀ upstreamĀ versionĀ 2.0b10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*###################################################################################
 
2
#
 
3
#   Embperl - Copyright (c) 1997-2004 Gerald Richter / ECOS
 
4
#
 
5
#   You may distribute under the terms of either the GNU General Public
 
6
#   License or the Artistic License, as specified in the Perl README file.
 
7
#   For use with Apache httpd and mod_perl, see also Apache copyright.
 
8
#
 
9
#   THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 
10
#   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 
11
#   WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
12
#
 
13
#   $Id: epio.c,v 1.25 2004/01/23 06:50:55 richter Exp $
 
14
#
 
15
###################################################################################*/
 
16
 
 
17
 
 
18
#include "ep.h"
 
19
#include "epmacro.h"
 
20
#include "crypto/epcrypto.h"
 
21
 
 
22
 
 
23
#ifndef PerlIO
 
24
 
 
25
 
 
26
#define FILEIOTYPE "StdIO"
 
27
/* define same helper macros to let it run with plain perl 5.003 */
 
28
/* where no PerlIO is present */
 
29
 
 
30
#define PerlIO_stdinF stdin
 
31
#define PerlIO_stdoutF stdout
 
32
#define PerlIO_stderrF stderr
 
33
#define PerlIO_close fclose
 
34
#define PerlIO_open fopen
 
35
#define PerlIO_flush fflush
 
36
#define PerlIO_vprintf vfprintf
 
37
#define PerlIO_fileno fileno
 
38
#define PerlIO_tell ftell
 
39
#define PerlIO_seek fseek
 
40
 
 
41
#define PerlIO_read(f,buf,cnt) fread(buf,1,cnt,f)
 
42
#define PerlIO_write(f,buf,cnt) fwrite(buf,1,cnt,f)
 
43
 
 
44
#define PerlIO_putc(f,c) fputc(c,f)
 
45
 
 
46
#else
 
47
 
 
48
#define FILEIOTYPE "PerlIO"
 
49
 
 
50
#define PerlIO_stdinF PerlIO_stdin ()
 
51
#define PerlIO_stdoutF PerlIO_stdout ()
 
52
#define PerlIO_stderrF PerlIO_stderr ()
 
53
 
 
54
 
 
55
#endif
 
56
 
 
57
 
 
58
/* Some helper macros for tied handles, taken from mod_perl 2.0 :-) */
 
59
/*
 
60
 * bleedperl change #11639 switch tied handle magic
 
61
 * from living in the gv to the GvIOp(gv), so we have to deal
 
62
 * with both to support 5.6.x
 
63
 */
 
64
#if ((PERL_REVISION == 5) && (PERL_VERSION >= 7))
 
65
#   define TIEHANDLE_SV(handle) (SV*)GvIOp((SV*)handle)
 
66
#else
 
67
#   define TIEHANDLE_SV(handle) (SV*)handle
 
68
#endif
 
69
 
 
70
#define HANDLE_GV(name) gv_fetchpv(name, TRUE, SVt_PVIO)
 
71
 
 
72
 
 
73
 
 
74
#ifdef APACHE
 
75
#define DefaultLog "/tmp/embperl.log"
 
76
 
 
77
 
 
78
static request_rec * pAllocReq = NULL ;
 
79
#endif
 
80
 
 
81
 
 
82
 
 
83
/* -------------------------------------------------------------------------------------
 
84
*
 
85
* begin output transaction
 
86
*
 
87
-------------------------------------------------------------------------------------- */
 
88
 
 
89
struct tBuf *   oBegin (/*i/o*/ register req * r)
 
90
 
 
91
    {
 
92
    EPENTRY1N (oBegin, r -> Component.pOutput -> nMarker) ;
 
93
    
 
94
    r -> Component.pOutput -> nMarker++ ;
 
95
    
 
96
    return r -> Component.pOutput -> pLastBuf ;
 
97
    }
 
98
 
 
99
/* -------------------------------------------------------------------------------------
 
100
*
 
101
*  rollback output transaction (throw away all the output since corresponding begin)
 
102
*
 
103
-------------------------------------------------------------------------------------- */
 
104
 
 
105
void oRollbackOutput (/*i/o*/ register req * r,
 
106
                        struct tBuf *   pBuf) 
 
107
 
 
108
    {
 
109
    EPENTRY1N (oRollback, r -> Component.pOutput -> nMarker) ;
 
110
 
 
111
    if (pBuf == NULL)
 
112
        {
 
113
        if (r -> Component.pOutput -> pLastFreeBuf)
 
114
            r -> Component.pOutput -> pLastFreeBuf -> pNext = r -> Component.pOutput -> pFirstBuf ;
 
115
        else 
 
116
            r -> Component.pOutput -> pFreeBuf = r -> Component.pOutput -> pFirstBuf ;
 
117
        
 
118
        r -> Component.pOutput -> pLastFreeBuf = r -> Component.pOutput -> pLastBuf ;
 
119
        
 
120
        r -> Component.pOutput -> pFirstBuf   = NULL ;
 
121
        r -> Component.pOutput -> nMarker     = 0 ;
 
122
        }
 
123
    else
 
124
        {
 
125
        if (r -> Component.pOutput -> pLastBuf == pBuf || pBuf -> pNext == NULL)
 
126
            r -> Component.pOutput -> nMarker-- ;
 
127
        else
 
128
            {
 
129
            r -> Component.pOutput -> nMarker = pBuf -> pNext -> nMarker - 1 ;
 
130
            if (r -> Component.pOutput -> pLastFreeBuf)
 
131
                r -> Component.pOutput -> pLastFreeBuf -> pNext = pBuf -> pNext ;
 
132
            else
 
133
                r -> Component.pOutput -> pFreeBuf = pBuf -> pNext ;
 
134
            r -> Component.pOutput -> pLastFreeBuf = r -> Component.pOutput -> pLastBuf ;
 
135
            }
 
136
        pBuf -> pNext = NULL ;
 
137
        }
 
138
        
 
139
    r -> Component.pOutput -> pLastBuf = pBuf ;
 
140
 
 
141
    }
 
142
 
 
143
/* -------------------------------------------------------------------------------------
 
144
*
 
145
*  rollback output transaction  and errors(throw away all the output since corresponding
 
146
*  begin)
 
147
*
 
148
-------------------------------------------------------------------------------------- */
 
149
 
 
150
void oRollback (/*i/o*/ register req * r,
 
151
                        struct tBuf *   pBuf) 
 
152
 
 
153
    {
 
154
    oRollbackOutput (r, pBuf) ;
 
155
    
 
156
    /* RollbackError (r) ; */
 
157
    }
 
158
 
 
159
/* ---------------------------------------------------------------------------- */
 
160
/*                                                                              */
 
161
/* commit output transaction (all the output since corresponding begin is vaild)*/
 
162
/*                                                                              */
 
163
/* ---------------------------------------------------------------------------- */
 
164
 
 
165
void oCommitToMem (/*i/o*/ register req * r,
 
166
                        struct tBuf *   pBuf,
 
167
                   char *          pOut) 
 
168
 
 
169
    {
 
170
    EPENTRY1N (oCommit, r -> Component.pOutput -> nMarker) ;
 
171
 
 
172
    
 
173
    if (pBuf == NULL)
 
174
        r -> Component.pOutput -> nMarker = 0 ;
 
175
    else
 
176
        if (r -> Component.pOutput -> pLastBuf == pBuf)
 
177
            r -> Component.pOutput -> nMarker-- ;
 
178
        else
 
179
            r -> Component.pOutput -> nMarker = pBuf -> pNext -> nMarker - 1 ;
 
180
    
 
181
    if (r -> Component.pOutput -> nMarker == 0)
 
182
        {
 
183
        if (pBuf == NULL)
 
184
            pBuf = r -> Component.pOutput -> pFirstBuf ;
 
185
        else
 
186
            pBuf = pBuf -> pNext ;
 
187
        
 
188
        if (pOut)
 
189
            {
 
190
            while (pBuf)
 
191
                {
 
192
                memmove (pOut, pBuf + 1, pBuf -> nSize) ;
 
193
                pOut += pBuf -> nSize ;
 
194
                pBuf = pBuf -> pNext ;
 
195
                }
 
196
            *pOut = '\0' ;                
 
197
            }
 
198
        else
 
199
            {
 
200
            while (pBuf)
 
201
                {
 
202
                owrite (r, pBuf + 1, pBuf -> nSize) ;
 
203
                pBuf = pBuf -> pNext ;
 
204
                }
 
205
            }
 
206
        }
 
207
 
 
208
    /* CommitError (r) ; */
 
209
    }
 
210
 
 
211
/* ---------------------------------------------------------------------------- */
 
212
/*                                                                              */
 
213
/* commit output transaction (all the output since corresponding begin is vaild)*/
 
214
/*                                                                              */
 
215
/* ---------------------------------------------------------------------------- */
 
216
 
 
217
void oCommit (/*i/o*/ register req *  r,
 
218
                      struct tBuf *   pBuf) 
 
219
 
 
220
    {
 
221
    EPENTRY1N (oCommit, r -> Component.pOutput -> nMarker) ;
 
222
 
 
223
    oCommitToMem (r, pBuf, NULL) ;
 
224
    }
 
225
 
 
226
/* ---------------------------------------------------------------------------- */
 
227
/*                                                                              */
 
228
/* write to a buffer                                                            */
 
229
/*                                                                              */
 
230
/* we will alloc a new buffer for every write                                   */
 
231
/* this is fast with ep_palloc                                                  */
 
232
/*                                                                              */
 
233
/* ---------------------------------------------------------------------------- */
 
234
 
 
235
 
 
236
static int bufwrite (/*i/o*/ register req * r,
 
237
                     /*in*/ const void * ptr, size_t size) 
 
238
 
 
239
 
 
240
    {
 
241
    struct tBuf * pBuf ;
 
242
 
 
243
    EPENTRY1N (bufwrite, r -> Component.pOutput -> nMarker) ;
 
244
 
 
245
    pBuf = (struct tBuf *)ep_palloc (r -> Component.pOutput -> pPool, size + sizeof (struct tBuf)) ;
 
246
 
 
247
    if (pBuf == NULL)
 
248
        return 0 ;
 
249
 
 
250
    memcpy (pBuf + 1,  ptr, size) ;
 
251
    pBuf -> pNext   = NULL ;
 
252
    pBuf -> nSize   = size ;
 
253
    pBuf -> nMarker = r -> Component.pOutput -> nMarker ;
 
254
 
 
255
    if (r -> Component.pOutput -> pLastBuf)
 
256
        {
 
257
        r -> Component.pOutput -> pLastBuf -> pNext = pBuf ;
 
258
        pBuf -> nCount    = r -> Component.pOutput -> pLastBuf -> nCount + size ;
 
259
        }
 
260
    else
 
261
        pBuf -> nCount    = size ;
 
262
        
 
263
    if (r -> Component.pOutput -> pFirstBuf == NULL)
 
264
        r -> Component.pOutput -> pFirstBuf = pBuf ;
 
265
    r -> Component.pOutput -> pLastBuf = pBuf ;
 
266
 
 
267
 
 
268
    return size ;
 
269
    }
 
270
 
 
271
#if 0
 
272
/* ---------------------------------------------------------------------------- */
 
273
/*                                                                              */
 
274
/* free buffers                                                                 */
 
275
/*                                                                              */
 
276
/* free all buffers                                                             */
 
277
/* note: this is not nessecary for apache palloc, because all buffers are freed */
 
278
/*       at the end of the request                                              */
 
279
/*                                                                              */
 
280
/* ---------------------------------------------------------------------------- */
 
281
 
 
282
 
 
283
static void buffree (/*i/o*/ register req * r)
 
284
 
 
285
    {
 
286
    struct tBuf * pNext = NULL ;
 
287
    struct tBuf * pBuf ;
 
288
 
 
289
#ifdef APACHE
 
290
    if ((r -> Component.Config.bDebug & dbgMem) == 0 && pAllocReq != NULL)
 
291
        {
 
292
        r -> Component.pOutput -> pFirstBuf    = NULL ;
 
293
        r -> Component.pOutput -> pLastBuf     = NULL ;
 
294
        r -> Component.pOutput -> pFreeBuf     = NULL ;
 
295
        r -> Component.pOutput -> pLastFreeBuf = NULL ;
 
296
        return ; /* no need for apache to free memory */
 
297
        }
 
298
#endif
 
299
        
 
300
    /* first walk thru the used buffers */
 
301
 
 
302
    pBuf = r -> Component.pOutput -> pFirstBuf ;
 
303
    while (pBuf)
 
304
        {
 
305
        pNext = pBuf -> pNext ;
 
306
        _free (r, pBuf) ;
 
307
        pBuf = pNext ;
 
308
        }
 
309
 
 
310
    r -> Component.pOutput -> pFirstBuf = NULL ;
 
311
    r -> Component.pOutput -> pLastBuf  = NULL ;
 
312
 
 
313
 
 
314
    /* now walk thru the unused buffers */
 
315
    
 
316
    pBuf = r -> Component.pOutput -> pFreeBuf ;
 
317
    while (pBuf)
 
318
        {
 
319
        pNext = pBuf -> pNext ;
 
320
        _free (r, pBuf) ;
 
321
        pBuf = pNext ;
 
322
        }
 
323
 
 
324
    r -> Component.pOutput -> pFreeBuf = NULL ;
 
325
    r -> Component.pOutput -> pLastFreeBuf  = NULL ;
 
326
    }
 
327
#endif
 
328
 
 
329
/* ---------------------------------------------------------------------------- */
 
330
/*                                                                              */
 
331
/* get the length outputed to buffers so far                                    */
 
332
/*                                                                              */
 
333
/* ---------------------------------------------------------------------------- */
 
334
 
 
335
int GetContentLength (/*i/o*/ register req * r)
 
336
    {
 
337
    if (r -> Component.pOutput -> pLastBuf)
 
338
        return r -> Component.pOutput -> pLastBuf -> nCount ;
 
339
    else
 
340
        return 0 ;
 
341
    
 
342
    }
 
343
 
 
344
/* ---------------------------------------------------------------------------- */
 
345
/*                                                                              */
 
346
/* set the name of the input file and open it                                   */
 
347
/*                                                                              */
 
348
/* ---------------------------------------------------------------------------- */
 
349
 
 
350
 
 
351
int OpenInput (/*i/o*/ register req * r,
 
352
                        /*in*/ const char *  sFilename)
 
353
 
 
354
    {
 
355
    MAGIC *mg;
 
356
    GV *handle ;
 
357
    epTHX ;
 
358
 
 
359
#ifdef APACHE
 
360
    if (r -> pApacheReq)
 
361
        return ok ;
 
362
#endif
 
363
    
 
364
    handle = HANDLE_GV("STDIN") ;
 
365
    if (handle)
 
366
        {
 
367
        SV *iohandle = TIEHANDLE_SV(handle) ;
 
368
 
 
369
        if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 
 
370
            {
 
371
            r -> Component.ifdobj = mg->mg_obj ;
 
372
            if (r -> Component.Config.bDebug)
 
373
                {
 
374
                char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj)));
 
375
                lprintf (r -> pApp,  "[%d]Open TIED STDIN %s...\n", r -> pThread -> nPid, package) ;
 
376
                }
 
377
            return ok ;
 
378
            }
 
379
        }
 
380
 
 
381
    if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF)
 
382
        PerlIO_close (r -> Component.ifd) ;
 
383
 
 
384
    r -> Component.ifd = NULL ;
 
385
 
 
386
    if (sFilename == NULL || *sFilename == '\0')
 
387
        {
 
388
        /*
 
389
        GV * io = gv_fetchpv("STDIN", TRUE, SVt_PVIO) ;
 
390
        if (io == NULL || (r -> Component.ifd = IoIFP(io)) == NULL)
 
391
            {
 
392
            if (r -> Component.Config.bDebug)
 
393
                lprintf (r -> pApp,  "[%d]Cannot get Perl STDIN, open os stdin\n", r -> pThread -> nPid) ;
 
394
            r -> Component.ifd = PerlIO_stdinF ;
 
395
            }
 
396
        */
 
397
        
 
398
        r -> Component.ifd = PerlIO_stdinF ;
 
399
 
 
400
        return ok ;
 
401
        }
 
402
 
 
403
    if ((r -> Component.ifd = PerlIO_open (sFilename, "r")) == NULL)
 
404
        {
 
405
        strncpy (r -> errdat1, sFilename, sizeof (r -> errdat1) - 1) ;
 
406
        strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ; 
 
407
        return rcFileOpenErr ;
 
408
        }
 
409
 
 
410
    return ok ;
 
411
    }
 
412
 
 
413
 
 
414
/* ---------------------------------------------------------------------------- */
 
415
/*                                                                              */
 
416
/* close input file                                                             */
 
417
/*                                                                              */
 
418
/* ---------------------------------------------------------------------------- */
 
419
 
 
420
 
 
421
int CloseInput (/*i/o*/ register req * r)
 
422
 
 
423
    {
 
424
    epTHX ;
 
425
 
 
426
#if 0
 
427
    if (0) /* r -> Component.ifdobj) */
 
428
        {           
 
429
        dSP;
 
430
        ENTER;
 
431
        SAVETMPS;
 
432
        PUSHMARK(sp);
 
433
        XPUSHs(r -> Component.ifdobj);
 
434
        PUTBACK;
 
435
        perl_call_method ("CLOSE", G_VOID | G_EVAL) ; 
 
436
        SPAGAIN ;
 
437
        FREETMPS;
 
438
        LEAVE;
 
439
        r -> Component.ifdobj = NULL ;
 
440
        }
 
441
#endif
 
442
 
 
443
#ifdef APACHE
 
444
    if (r -> pApacheReq)
 
445
        return ok ;
 
446
#endif
 
447
 
 
448
    if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF)
 
449
        PerlIO_close (r -> Component.ifd) ;
 
450
 
 
451
    r -> Component.ifd = NULL ;
 
452
 
 
453
    return ok ;
 
454
    }
 
455
 
 
456
 
 
457
 
 
458
/* ---------------------------------------------------------------------------- */
 
459
/*                                                                              */
 
460
/* read block of data from input (web client)                                   */
 
461
/*                                                                              */
 
462
/* ---------------------------------------------------------------------------- */
 
463
 
 
464
 
 
465
int iread (/*i/o*/ register req * r,
 
466
           /*in*/ void * ptr, size_t size) 
 
467
 
 
468
    {
 
469
    char * p = (char *)ptr ; /* workaround for aix c complier */
 
470
    epTHX ;
 
471
    
 
472
    if (size == 0)
 
473
        return 0 ;
 
474
 
 
475
    if (r -> Component.ifdobj)
 
476
        {           
 
477
        int num ;
 
478
        int n ;
 
479
        SV * pBufSV ;
 
480
 
 
481
        dSP;
 
482
        ENTER;
 
483
        SAVETMPS;
 
484
        PUSHMARK(sp);
 
485
        XPUSHs(r -> Component.ifdobj);
 
486
        XPUSHs(sv_2mortal(pBufSV = NEWSV(0, 0)));
 
487
        PUTBACK;
 
488
        num = perl_call_method ("READ", G_SCALAR) ; 
 
489
        SPAGAIN;
 
490
        n = 0 ;
 
491
        if (num > 0)
 
492
            {
 
493
            STRLEN  n = POPu ;
 
494
            char * p ;
 
495
            STRLEN l ;
 
496
            if (n >= 0)
 
497
                {
 
498
                p = SvPV (pBufSV, l) ;
 
499
                if (l > size)
 
500
                    l = size ;
 
501
                if (l > n)
 
502
                    l = n ;
 
503
                memcpy (ptr, p, l) ;
 
504
                }
 
505
            }
 
506
        PUTBACK;
 
507
        FREETMPS;
 
508
        LEAVE;
 
509
        return n ;
 
510
        }
 
511
 
 
512
#if defined (APACHE)
 
513
    if (r -> pApacheReq)
 
514
        {
 
515
        ap_setup_client_block(r -> pApacheReq, REQUEST_CHUNKED_ERROR); 
 
516
        if(ap_should_client_block(r -> pApacheReq))
 
517
            {
 
518
            int c ;
 
519
            int n = 0 ;
 
520
            while (1)
 
521
                {
 
522
                c = ap_get_client_block(r -> pApacheReq, p, size); 
 
523
                if (c < 0 || c == 0)
 
524
                    return n ;
 
525
                n            += c ;
 
526
                p            += c ;
 
527
                size         -= c ;
 
528
                }
 
529
            }
 
530
        else
 
531
            return 0 ;
 
532
        } 
 
533
#endif
 
534
 
 
535
    return PerlIO_read (r -> Component.ifd, p, size) ;
 
536
    }
 
537
 
 
538
 
 
539
/* ---------------------------------------------------------------------------- */
 
540
/*                                                                              */
 
541
/* read line of data from input (web client)                                    */
 
542
/*                                                                              */
 
543
/* ---------------------------------------------------------------------------- */
 
544
 
 
545
 
 
546
char * igets (/*i/o*/ register req * r,
 
547
                        /*in*/ char * s,   int    size) 
 
548
 
 
549
    {
 
550
#if defined (APACHE)
 
551
    if (r -> pApacheReq)
 
552
        return NULL ;
 
553
#endif
 
554
 
 
555
#ifdef PerlIO
 
556
        {
 
557
        /*
 
558
        FILE * f = PerlIO_exportFILE (r -> Component.ifd, 0) ;
 
559
        char * p = fgets (s, size, f) ;
 
560
        PerlIO_releaseFILE (r -> Component.ifd, f) ;
 
561
        return p ;
 
562
        */
 
563
        return NULL ;
 
564
        }
 
565
#else
 
566
    return fgets (s, size, r -> Component.ifd) ;
 
567
#endif
 
568
    }
 
569
 
 
570
/* ---------------------------------------------------------------------------- */
 
571
/*                                                                              */
 
572
/* read HTML File into pBuf                                                     */
 
573
/*                                                                              */
 
574
/* ---------------------------------------------------------------------------- */
 
575
 
 
576
int ReadHTML (/*i/o*/ register req * r,
 
577
              /*in*/    char *    sInputfile,
 
578
              /*in*/    size_t *  nFileSize,
 
579
              /*out*/   SV   * *  ppBuf)
 
580
 
 
581
    {              
 
582
    SV   * pBufSV ;
 
583
    char * pData ;
 
584
#ifdef PerlIO
 
585
    PerlIO * ifd ;
 
586
#else
 
587
    FILE *   ifd ;
 
588
#endif
 
589
    epTHX ;
 
590
    
 
591
    if (r -> Component.Config.bDebug)
 
592
        lprintf (r -> pApp,  "[%d]Reading %s as input using %s (%d Bytes)...\n", r -> pThread -> nPid, sInputfile, FILEIOTYPE, *nFileSize) ;
 
593
 
 
594
#ifdef WIN32
 
595
    if ((ifd = PerlIO_open (sInputfile, "rb")) == NULL)
 
596
#else
 
597
    if ((ifd = PerlIO_open (sInputfile, "r")) == NULL)
 
598
#endif        
 
599
        {
 
600
        strncpy (r -> errdat1, sInputfile, sizeof (r -> errdat1) - 1) ;
 
601
        strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ; 
 
602
        return rcFileOpenErr ;
 
603
        }
 
604
 
 
605
    if ((long)*nFileSize < 0)
 
606
        return rcFileOpenErr ;
 
607
 
 
608
 
 
609
    pBufSV = sv_2mortal (newSV(*nFileSize + 1)) ;
 
610
    pData = SvPVX(pBufSV) ;
 
611
 
 
612
#if EPC_ENABLE
 
613
    
 
614
    if (*nFileSize)
 
615
        {
 
616
        int rc ;
 
617
        char * syntax ;
 
618
 
 
619
#ifndef EP2
 
620
        syntax = (r -> Component.pTokenTable && strcmp ((char *)r -> Component.pTokenTable, "Text") == 0)?"Text":"Embperl" ;
 
621
#else
 
622
        syntax = r -> Component.Config.sSyntax ;
 
623
#endif
 
624
 
 
625
        if ((rc = do_crypt_file (ifd, NULL, pData, *nFileSize, 0, syntax, EPC_HEADER)) <= 0)
 
626
            {
 
627
            if (rc < -1 || !EPC_UNENCYRPTED)
 
628
                {
 
629
                sprintf (r -> errdat1, "%d", rc) ;
 
630
                return rcCryptoWrongHeader + -rc - 1;
 
631
                }
 
632
 
 
633
            PerlIO_seek (ifd, 0, SEEK_SET) ;
 
634
            *nFileSize = PerlIO_read (ifd, pData, *nFileSize) ;
 
635
            }
 
636
        else
 
637
            *nFileSize = rc ;
 
638
        }
 
639
#else
 
640
    
 
641
    if (*nFileSize)
 
642
        *nFileSize = PerlIO_read (ifd, pData, *nFileSize) ;
 
643
 
 
644
#endif
 
645
 
 
646
    PerlIO_close (ifd) ;
 
647
    
 
648
    pData [*nFileSize] = '\0' ;
 
649
    SvCUR_set (pBufSV, *nFileSize) ;
 
650
    SvTEMP_off (pBufSV) ;
 
651
    SvPOK_on   (pBufSV) ;
 
652
 
 
653
    *ppBuf = pBufSV ;
 
654
 
 
655
    return ok ;
 
656
    }
 
657
 
 
658
 
 
659
/* ---------------------------------------------------------------------------- */
 
660
/*                                                                              */
 
661
/* set the name of the output file and open it                                  */
 
662
/*                                                                              */
 
663
/* ---------------------------------------------------------------------------- */
 
664
 
 
665
 
 
666
 
 
667
int OpenOutput (/*i/o*/ register req * r,
 
668
                        /*in*/ const char *  sFilename)
 
669
 
 
670
    {
 
671
    MAGIC *mg;
 
672
    GV *handle ;
 
673
    epTHX ;
 
674
    
 
675
    r -> Component.pOutput -> pFirstBuf = NULL ; 
 
676
    r -> Component.pOutput -> pLastBuf  = NULL ; 
 
677
    r -> Component.pOutput -> nMarker   = 0 ;
 
678
    r -> Component.pOutput -> pMemBuf   = NULL ;
 
679
    r -> Component.pOutput -> nMemBufSize = 0 ;
 
680
    r -> Component.pOutput -> pFreeBuf     = NULL ;
 
681
    r -> Component.pOutput -> pLastFreeBuf = NULL ;
 
682
 
 
683
 
 
684
    
 
685
    if (r -> Component.pOutput -> ofd && r -> Component.pOutput -> ofd != PerlIO_stdoutF)
 
686
        PerlIO_close (r -> Component.pOutput -> ofd) ;
 
687
 
 
688
    r -> Component.pOutput -> ofd = NULL ;
 
689
 
 
690
    if (sFilename == NULL || *sFilename == '\0')
 
691
        {
 
692
#if defined (APACHE)
 
693
        if (r -> pApacheReq)
 
694
            {
 
695
            if (r -> Component.Config.bDebug)
 
696
                lprintf (r -> pApp,  "[%d]Using APACHE for output...\n", r -> pThread -> nPid) ;
 
697
            return ok ;
 
698
            }
 
699
#endif
 
700
 
 
701
        handle = HANDLE_GV("STDOUT") ;
 
702
        if (handle)
 
703
            {
 
704
            SV *iohandle = TIEHANDLE_SV(handle) ;
 
705
 
 
706
            if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 
 
707
                {
 
708
                r -> Component.pOutput -> ofdobj = mg->mg_obj ;
 
709
                if (r -> Component.Config.bDebug)
 
710
                    {
 
711
                    char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj)));
 
712
                    lprintf (r -> pApp,  "[%d]Open TIED STDOUT %s for output...\n", r -> pThread -> nPid, package) ;
 
713
                    }
 
714
                return ok ;
 
715
                }
 
716
            }
 
717
        
 
718
        r -> Component.pOutput -> ofd = PerlIO_stdoutF ;
 
719
        
 
720
        if (r -> Component.Config.bDebug)
 
721
            {
 
722
#ifdef APACHE
 
723
             if (r -> pApacheReq)
 
724
                lprintf (r -> pApp,  "[%d]Open STDOUT to Apache for output...\n", r -> pThread -> nPid) ;
 
725
             else
 
726
#endif
 
727
             lprintf (r -> pApp,  "[%d]Open STDOUT for output...\n", r -> pThread -> nPid) ;
 
728
            }
 
729
        return ok ;
 
730
        }
 
731
 
 
732
    if (r -> Component.Config.bDebug)
 
733
        lprintf (r -> pApp,  "[%d]Open %s for output...\n", r -> pThread -> nPid, sFilename) ;
 
734
 
 
735
#ifdef WIN32
 
736
    if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "wb")) == NULL)
 
737
#else
 
738
    if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "w")) == NULL)
 
739
#endif        
 
740
        {
 
741
        strncpy (r -> errdat1, sFilename, sizeof (r -> errdat1) - 1) ;
 
742
        strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ; 
 
743
        return rcFileOpenErr ;
 
744
        }
 
745
 
 
746
    return ok ;
 
747
    }
 
748
 
 
749
 
 
750
/* ---------------------------------------------------------------------------- */
 
751
/*                                                                              */
 
752
/* close the output file                                                        */
 
753
/*                                                                              */
 
754
/* ---------------------------------------------------------------------------- */
 
755
 
 
756
 
 
757
int CloseOutput (/*in*/ tReq *             r,
 
758
                        tComponentOutput * pOutput)
 
759
 
 
760
    {
 
761
    epTHX ;
 
762
    
 
763
    if (!pOutput)
 
764
        return ok ;
 
765
 
 
766
#if 0
 
767
    if (0) /* r -> Component.pOutput -> ofdobj) */
 
768
        {           
 
769
        dSP;
 
770
        ENTER;
 
771
        SAVETMPS;
 
772
        PUSHMARK(sp);
 
773
        XPUSHs(pOutput -> ofdobj);
 
774
        PUTBACK;
 
775
        perl_call_method ("CLOSE", G_VOID | G_EVAL) ; 
 
776
        SPAGAIN ;
 
777
        FREETMPS;
 
778
        LEAVE;
 
779
        pOutput -> ofdobj = NULL ;
 
780
        }
 
781
#endif
 
782
 
 
783
    if (pOutput -> ofd && pOutput -> ofd != PerlIO_stdoutF)
 
784
        PerlIO_close (pOutput -> ofd) ;
 
785
 
 
786
    pOutput -> ofd = NULL ;
 
787
 
 
788
    return ok ;
 
789
    }
 
790
 
 
791
 
 
792
/* ---------------------------------------------------------------------------- */
 
793
/*                                                                              */
 
794
/* set output to memory buffer                                                  */
 
795
/*                                                                              */
 
796
/* ---------------------------------------------------------------------------- */
 
797
 
 
798
 
 
799
 
 
800
void OutputToMemBuf (/*i/o*/ register req * r,
 
801
                        /*in*/ char *  pBuf,
 
802
                     /*in*/ size_t  nBufSize)
 
803
 
 
804
    {
 
805
    if (pBuf == NULL)
 
806
        pBuf = ep_palloc (r -> Component.pOutput -> pPool, nBufSize) ;
 
807
 
 
808
    *pBuf = '\0' ;
 
809
    r -> Component.pOutput -> pMemBuf    = pBuf ;
 
810
    r -> Component.pOutput -> pMemBufPtr      = pBuf ;
 
811
    r -> Component.pOutput -> nMemBufSize     = nBufSize ;
 
812
    r -> Component.pOutput -> nMemBufSizeFree = nBufSize ;
 
813
    }
 
814
 
 
815
 
 
816
/* ---------------------------------------------------------------------------- */
 
817
/*                                                                              */
 
818
/* set output to standard                                                       */
 
819
/*                                                                              */
 
820
/* ---------------------------------------------------------------------------- */
 
821
 
 
822
 
 
823
char * OutputToStd (/*i/o*/ register req * r)
 
824
 
 
825
    {
 
826
    char * p = r -> Component.pOutput -> pMemBuf ;
 
827
    r -> Component.pOutput -> pMemBuf         = NULL ;
 
828
    r -> Component.pOutput -> nMemBufSize     = 0 ;
 
829
    r -> Component.pOutput -> nMemBufSizeFree = 0 ;
 
830
    return p ;
 
831
    }
 
832
 
 
833
 
 
834
 
 
835
/* ---------------------------------------------------------------------------- */
 
836
/*                                                                              */
 
837
/* puts to output (web client)                                                  */
 
838
/*                                                                              */
 
839
/* ---------------------------------------------------------------------------- */
 
840
 
 
841
int oputs (/*i/o*/ register req * r,
 
842
                        /*in*/ const char *  str) 
 
843
 
 
844
    {
 
845
    return owrite (r, str, strlen (str)) ;
 
846
    }
 
847
 
 
848
 
 
849
/* ---------------------------------------------------------------------------- */
 
850
/*                                                                              */
 
851
/* write block of data to output (web client)                                   */
 
852
/*                                                                              */
 
853
/* ---------------------------------------------------------------------------- */
 
854
 
 
855
int owrite (/*i/o*/ register req * r,
 
856
            /*in*/ const void * ptr, size_t size) 
 
857
 
 
858
    {
 
859
    size_t n = size ;
 
860
    epTHX ;
 
861
 
 
862
    if (n == 0 || r -> Component.pOutput -> bDisableOutput)
 
863
        return 0 ;
 
864
 
 
865
    if (r -> Component.pOutput -> pMemBuf)
 
866
        {
 
867
        char * p ;
 
868
        size_t s = r -> Component.pOutput -> nMemBufSize ;
 
869
        if (n >= r -> Component.pOutput -> nMemBufSizeFree)
 
870
            {
 
871
            size_t oldsize = s ;
 
872
            if (s < n)
 
873
                s = n + r -> Component.pOutput -> nMemBufSize ;
 
874
            
 
875
            r -> Component.pOutput -> nMemBufSize      += s ;
 
876
            r -> Component.pOutput -> nMemBufSizeFree  += s ;
 
877
            /*lprintf (r -> pApp,  "[%d]MEM:  Realloc pMemBuf, nMemSize = %d\n", nPid, nMemBufSize) ; */
 
878
            
 
879
            p = ep_palloc (r -> Component.pOutput -> pPool, r -> Component.pOutput -> nMemBufSize) ;
 
880
            if (p == NULL)
 
881
                {
 
882
                r -> Component.pOutput -> nMemBufSize      -= s ;
 
883
                r -> Component.pOutput -> nMemBufSizeFree  -= s ;
 
884
                return 0 ;
 
885
                }
 
886
            memcpy (p, r -> Component.pOutput -> pMemBuf, oldsize) ;
 
887
            r -> Component.pOutput -> pMemBufPtr = p + (r -> Component.pOutput -> pMemBufPtr - r -> Component.pOutput -> pMemBuf) ;
 
888
            r -> Component.pOutput -> pMemBuf = p ;
 
889
            }
 
890
                
 
891
        memcpy (r -> Component.pOutput -> pMemBufPtr, ptr, n) ;
 
892
        r -> Component.pOutput -> pMemBufPtr += n ;
 
893
        *(r -> Component.pOutput -> pMemBufPtr) = '\0' ;
 
894
        r -> Component.pOutput -> nMemBufSizeFree -= n ;
 
895
        return n ;
 
896
        }
 
897
 
 
898
    
 
899
    if (r -> Component.pOutput -> nMarker)
 
900
        return bufwrite (r, ptr, n) ;
 
901
 
 
902
    if (r -> Component.pOutput -> ofdobj)
 
903
        {           
 
904
        dSP;
 
905
        ENTER;
 
906
        SAVETMPS;
 
907
        PUSHMARK(sp);
 
908
        XPUSHs(r -> Component.pOutput -> ofdobj);
 
909
        XPUSHs(sv_2mortal(newSVpv((char *)ptr,size)));
 
910
        PUTBACK;
 
911
        perl_call_method ("PRINT", G_SCALAR) ; 
 
912
        SPAGAIN ;
 
913
        FREETMPS;
 
914
        LEAVE;
 
915
        return size ;
 
916
        }
 
917
 
 
918
 
 
919
#if defined (APACHE)
 
920
    if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
 
921
        {
 
922
        if (n > 0)
 
923
            {
 
924
            n = ap_rwrite (ptr, n, r -> pApacheReq) ;
 
925
            if (r -> Component.Config.bDebug & dbgFlushOutput)
 
926
                ap_rflush (r -> pApacheReq) ;
 
927
            return n ;
 
928
            }
 
929
        else
 
930
            return 0 ;
 
931
        }
 
932
#endif
 
933
    if (n > 0 && r -> Component.pOutput -> ofd)
 
934
        {
 
935
        n = PerlIO_write (r -> Component.pOutput -> ofd, (void *)ptr, size) ;
 
936
 
 
937
        if (r -> Component.Config.bDebug & dbgFlushOutput)
 
938
            PerlIO_flush (r -> Component.pOutput -> ofd) ;
 
939
        }
 
940
 
 
941
    return n ;
 
942
    }
 
943
 
 
944
 
 
945
 
 
946
/* ---------------------------------------------------------------------------- */
 
947
/*                                                                              */
 
948
/* write one char to output (web client)                                        */
 
949
/*                                                                              */
 
950
/* ---------------------------------------------------------------------------- */
 
951
 
 
952
 
 
953
void oputc (/*i/o*/ register req * r,
 
954
                        /*in*/ char c)
 
955
 
 
956
    {
 
957
    epTHX ;
 
958
    
 
959
    if (r -> Component.pOutput -> nMarker || r -> Component.pOutput -> pMemBuf || r -> Component.pOutput -> ofdobj)
 
960
        {
 
961
        owrite (r, &c, 1) ;
 
962
        return ;
 
963
        }
 
964
 
 
965
#if defined (APACHE)
 
966
    if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
 
967
        {
 
968
        ap_rputc (c, r -> pApacheReq) ;
 
969
        if (r -> Component.Config.bDebug & dbgFlushOutput)
 
970
            ap_rflush (r -> pApacheReq) ;
 
971
        return ;
 
972
        }
 
973
#endif
 
974
    PerlIO_putc (r -> Component.pOutput -> ofd, c) ;
 
975
 
 
976
    if (r -> Component.Config.bDebug & dbgFlushOutput)
 
977
        PerlIO_flush (r -> Component.pOutput -> ofd) ;
 
978
    }
 
979
 
 
980
 
 
981
 
 
982
/* ---------------------------------------------------------------------------- */
 
983
/*                                                                              */
 
984
/* log file open                                                                */
 
985
/*                                                                              */
 
986
/* ---------------------------------------------------------------------------- */
 
987
 
 
988
 
 
989
int OpenLog    (/*i/o*/ tApp *       a)
 
990
 
 
991
    {
 
992
    epaTHX ;
 
993
    
 
994
    if (a -> lfd)
 
995
        return ok ; /*already open */
 
996
 
 
997
    if (a -> lfd && a -> lfd != PerlIO_stdoutF)
 
998
        PerlIO_close (a -> lfd) ;  /* close old logfile */
 
999
   
 
1000
    a -> lfd = NULL ;
 
1001
 
 
1002
    
 
1003
    if (a -> Config.bDebug == 0)
 
1004
        return ok ; /* never write to logfile if debugging is disabled */           
 
1005
    
 
1006
    if (!a -> Config.sLog && a -> Config.sLog[0] == '\0')
 
1007
        {
 
1008
        a -> lfd = PerlIO_stdoutF ;
 
1009
        return ok ;
 
1010
        }
 
1011
 
 
1012
    if ((a -> lfd = PerlIO_open (a -> Config.sLog, "a")) == NULL)
 
1013
        {
 
1014
        tReq * r = a -> pThread -> pCurrReq ;        
 
1015
        if (r)
 
1016
            {
 
1017
            strncpy (r -> errdat1, a -> Config.sLog, sizeof (r -> errdat1) - 1) ;
 
1018
            strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ; 
 
1019
            }
 
1020
        return rcLogFileOpenErr ;
 
1021
        }
 
1022
 
 
1023
    return ok ;
 
1024
    }
 
1025
 
 
1026
/* ---------------------------------------------------------------------------- */
 
1027
/*                                                                              */
 
1028
/* return the handle of the log file                                            */
 
1029
/*                                                                              */
 
1030
/* ---------------------------------------------------------------------------- */
 
1031
 
 
1032
 
 
1033
int GetLogHandle (/*i/o*/ tApp *       a)
 
1034
 
 
1035
    {
 
1036
    epaTHX  ;
 
1037
 
 
1038
    if (a -> lfd)
 
1039
        return PerlIO_fileno (a -> lfd) ;
 
1040
 
 
1041
    return 0 ;
 
1042
    }
 
1043
 
 
1044
 
 
1045
/* ---------------------------------------------------------------------------- */
 
1046
/*                                                                              */
 
1047
/* return the current posittion of the log file                                 */
 
1048
/*                                                                              */
 
1049
/* ---------------------------------------------------------------------------- */
 
1050
 
 
1051
 
 
1052
long GetLogFilePos (/*i/o*/ tApp *       a)
 
1053
 
 
1054
    {
 
1055
    epaTHX  ;
 
1056
 
 
1057
    if (a -> lfd)
 
1058
        return PerlIO_tell (a -> lfd) ;
 
1059
 
 
1060
    return 0 ;
 
1061
    }
 
1062
 
 
1063
 
 
1064
/* ---------------------------------------------------------------------------- */
 
1065
/*                                                                              */
 
1066
/* close the log file                                                           */
 
1067
/*                                                                              */
 
1068
/* ---------------------------------------------------------------------------- */
 
1069
 
 
1070
 
 
1071
int CloseLog (/*i/o*/ tApp *       a)
 
1072
 
 
1073
    {
 
1074
    epaTHX  ;
 
1075
 
 
1076
    if (a -> lfd && a -> lfd != PerlIO_stdoutF)
 
1077
        PerlIO_close (a -> lfd) ;
 
1078
 
 
1079
    a -> lfd = NULL ;
 
1080
 
 
1081
    return ok ;
 
1082
    }
 
1083
 
 
1084
 
 
1085
 
 
1086
/* ---------------------------------------------------------------------------- */
 
1087
/*                                                                              */
 
1088
/* flush the log file                                                           */
 
1089
/*                                                                              */
 
1090
/* ---------------------------------------------------------------------------- */
 
1091
 
 
1092
 
 
1093
int FlushLog (/*i/o*/ tApp *       a)
 
1094
 
 
1095
    {
 
1096
    epaTHX  ;
 
1097
 
 
1098
    if (a -> lfd != NULL)
 
1099
        PerlIO_flush (a -> lfd) ;
 
1100
 
 
1101
    return ok ;
 
1102
    }
 
1103
 
 
1104
 
 
1105
 
 
1106
/* ---------------------------------------------------------------------------- */
 
1107
/*                                                                              */
 
1108
/* printf to log file                                                           */
 
1109
/*                                                                              */
 
1110
/* ---------------------------------------------------------------------------- */
 
1111
 
 
1112
int lprintf (/*i/o*/ tApp *       a,
 
1113
             /*in*/ const char *  sFormat,
 
1114
             /*in*/ ...) 
 
1115
 
 
1116
    {
 
1117
    va_list  ap ;
 
1118
    int      n ;
 
1119
    epaTHX  ;
 
1120
 
 
1121
    if (a -> lfd == NULL)
 
1122
        return 0 ;
 
1123
    
 
1124
    va_start (ap, sFormat) ;
 
1125
    
 
1126
        {
 
1127
        n = PerlIO_vprintf (a -> lfd, sFormat, ap) ;
 
1128
        if (a -> Config.bDebug & dbgFlushLog)
 
1129
            PerlIO_flush (a -> lfd) ;
 
1130
        }
 
1131
 
 
1132
 
 
1133
    va_end (ap) ;
 
1134
 
 
1135
    return n ;
 
1136
    }
 
1137
 
 
1138
 
 
1139
/* ---------------------------------------------------------------------------- */
 
1140
/*                                                                              */
 
1141
/* write block of data to log file                                              */
 
1142
/*                                                                              */
 
1143
/* ---------------------------------------------------------------------------- */
 
1144
 
 
1145
 
 
1146
int lwrite (/*i/o*/ tApp *       a,
 
1147
            /*in*/  const void * ptr, 
 
1148
            /*in*/  size_t size) 
 
1149
 
 
1150
    {
 
1151
    int n ;
 
1152
    epaTHX  ;
 
1153
 
 
1154
    if (a -> lfd == NULL)
 
1155
        return 0 ;
 
1156
    
 
1157
    n = PerlIO_write (a -> lfd, (void *)ptr, size) ;
 
1158
 
 
1159
    if (a -> Config.bDebug & dbgFlushLog)
 
1160
        PerlIO_flush (a -> lfd) ;
 
1161
 
 
1162
    return n ;
 
1163
    }
 
1164
 
 
1165
 
 
1166
 
 
1167
 
 
1168
/* ---------------------------------------------------------------------------- */
 
1169
/*                                                                              */
 
1170
/* Memory Allocation                                                            */
 
1171
/*                                                                              */
 
1172
/* ---------------------------------------------------------------------------- */
 
1173
 
 
1174
 
 
1175
void _free (/*i/o*/ register req * r,
 
1176
                        void * p)
 
1177
 
 
1178
    {
 
1179
#ifdef APACHE
 
1180
    if (pAllocReq && !(r -> Component.Config.bDebug & dbgMem))
 
1181
        return ;
 
1182
#endif
 
1183
 
 
1184
 
 
1185
#ifdef ALLOCSIZE
 
1186
    if (r -> Component.Config.bDebug & dbgMem)
 
1187
        {
 
1188
        size_t size ;
 
1189
        size_t * ps ;
 
1190
 
 
1191
        /* we do it a bit complicted so it compiles also on aix */
 
1192
        ps = (size_t *)p ;
 
1193
        ps-- ;
 
1194
        size = *ps ;
 
1195
        p = ps ;
 
1196
        r -> nAllocSize -= size ;
 
1197
        lprintf (r -> pApp,  "[%d]MEM:  Free %d Bytes at %08x  Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
 
1198
        }
 
1199
#endif
 
1200
 
 
1201
#ifdef APACHE
 
1202
    if (r -> pApacheReq == NULL)
 
1203
#endif
 
1204
        {
 
1205
        epTHX ;
 
1206
        free (p) ;
 
1207
        }
 
1208
    }
 
1209
 
 
1210
void * _malloc (/*i/o*/ register req * r, 
 
1211
                        size_t  size)
 
1212
 
 
1213
    {
 
1214
    void * p ;
 
1215
 
 
1216
#ifdef APACHE
 
1217
    pAllocReq = r -> pApacheReq  ;
 
1218
 
 
1219
    if (r -> pApacheReq)
 
1220
        {
 
1221
        p = apr_palloc (r -> pApacheReq -> pool, size + sizeof (size)) ;
 
1222
        }
 
1223
    else
 
1224
#endif
 
1225
        {
 
1226
        epTHX ;
 
1227
        
 
1228
        p = malloc (size + sizeof (size)) ;
 
1229
        }
 
1230
 
 
1231
#ifdef ALLOCSIZE
 
1232
    if (r -> Component.Config.bDebug & dbgMem)
 
1233
        {
 
1234
        size_t * ps ;
 
1235
        /* we do it a bit complicted so it compiles also on aix */
 
1236
        ps = (size_t *)p ;
 
1237
        *ps = size ;
 
1238
        p = ps + 1 ;
 
1239
 
 
1240
        r -> nAllocSize += size ;
 
1241
        lprintf (r -> pApp,  "[%d]MEM:  Alloc %d Bytes at %08x   Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
 
1242
        }
 
1243
#endif
 
1244
 
 
1245
    return p ;
 
1246
    }
 
1247
 
 
1248
void * _realloc (/*i/o*/ register req * r,  void * ptr, size_t oldsize, size_t  size)
 
1249
 
 
1250
    {
 
1251
    void * p ;
 
1252
    
 
1253
#ifdef APACHE
 
1254
    if (r -> pApacheReq)
 
1255
        {
 
1256
        p = apr_palloc (r -> pApacheReq -> pool, size + sizeof (size)) ;
 
1257
        if (p == NULL)
 
1258
            return NULL ;
 
1259
        
 
1260
#ifdef ALLOCSIZE
 
1261
        if (r -> Component.Config.bDebug & dbgMem)
 
1262
            {
 
1263
            size_t * ps ;
 
1264
            size_t sizeold ;
 
1265
            /* we do it a bit complicted so it compiles also on aix */
 
1266
            ps = (size_t *)p ;
 
1267
            *ps = size ;
 
1268
            p = ps + 1;
 
1269
        
 
1270
            ps = (size_t *)ptr ;
 
1271
            ps-- ;
 
1272
            sizeold = *ps ;
 
1273
            r -> nAllocSize += size - sizeold ;
 
1274
 
 
1275
            lprintf (r -> pApp,  "[%d]MEM:  ReAlloc %d Bytes at %08x   Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
 
1276
            }
 
1277
#endif
 
1278
 
 
1279
        memcpy (p, ptr, oldsize) ; 
 
1280
        }
 
1281
    else
 
1282
#endif
 
1283
#ifdef ALLOCSIZE
 
1284
        if (r -> Component.Config.bDebug & dbgMem)
 
1285
            {
 
1286
            size_t * ps ;
 
1287
            ps = (size_t *)ptr ;
 
1288
            ps-- ;
 
1289
            r -> nAllocSize -= *ps ;
 
1290
        
 
1291
            p = realloc (ps, size + sizeof (size)) ;
 
1292
            if (p == NULL)
 
1293
                return NULL ;
 
1294
 
 
1295
            /* we do it a bit complicted so it compiles also on aix */
 
1296
            ps = (size_t *)p ;
 
1297
            *ps = size ;
 
1298
            p = ps + 1;
 
1299
            r -> nAllocSize += size ;
 
1300
            lprintf (r -> pApp,  "[%d]MEM:  ReAlloc %d Bytes at %08x   Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
 
1301
            }
 
1302
        else
 
1303
#endif
 
1304
            {
 
1305
            epTHX ;
 
1306
            p = realloc (ptr, size + sizeof (size)) ;
 
1307
            }
 
1308
 
 
1309
    return p ;
 
1310
    }
 
1311
 
 
1312
 
 
1313
char * _memstrcat (/*i/o*/ register req * r,
 
1314
                        const char *s, ...) 
 
1315
 
 
1316
    {
 
1317
    va_list ap ;
 
1318
    char *  p ;
 
1319
    char *  str ;
 
1320
    char *  sp ;
 
1321
    int     l ;
 
1322
    int     sum ;
 
1323
 
 
1324
    EPENTRY(_memstrcat) ;
 
1325
 
 
1326
    va_start(ap, s) ;
 
1327
 
 
1328
    p = (char *)s ;
 
1329
    sum = 0 ;
 
1330
    while (p)
 
1331
        {
 
1332
        sum += strlen (p) ;
 
1333
        lprintf (r -> pApp,  "sum = %d p = %s\n", sum, p) ;
 
1334
        p = va_arg (ap, char *) ;
 
1335
        }
 
1336
    sum++ ;
 
1337
 
 
1338
    va_end (ap) ;
 
1339
 
 
1340
    sp = str = _malloc (r, sum+1) ;
 
1341
 
 
1342
    va_start(ap, s) ;
 
1343
 
 
1344
    p = (char *)s ;
 
1345
    while (p)
 
1346
        {
 
1347
        l = strlen (p) ;
 
1348
        lprintf (r -> pApp,  "l = %d p = %s\n", l, p) ;
 
1349
        memcpy (str, p, l) ;
 
1350
        str += l ;
 
1351
        p = va_arg (ap, char *) ;
 
1352
        }
 
1353
    *str = '\0' ;
 
1354
 
 
1355
    va_end (ap) ;
 
1356
 
 
1357
 
 
1358
    return sp ;
 
1359
    }
 
1360
 
 
1361
 
 
1362
 
 
1363
char * _ep_strdup (/*i/o*/ register req * r,
 
1364
                 /*in*/  const char * str)
 
1365
 
 
1366
    {
 
1367
    char * p ;        
 
1368
    int    len = strlen (str) ;
 
1369
 
 
1370
    p = (char *)_malloc (r, len + 1) ;
 
1371
 
 
1372
    if (p)
 
1373
        strcpy (p, str) ;
 
1374
 
 
1375
    return p ;
 
1376
    }
 
1377
    
 
1378
 
 
1379
 
 
1380
char * _ep_strndup (/*i/o*/ register req * r,
 
1381
                  /*in*/  const char *   str,
 
1382
                  /*in*/  int            len)
 
1383
 
 
1384
    {
 
1385
    char * p ;        
 
1386
 
 
1387
    p = (char *)_malloc (r, len + 1) ;
 
1388
 
 
1389
    if (p)
 
1390
        {
 
1391
        strncpy (p, str, len) ;
 
1392
 
 
1393
        p[len] = '\0' ;
 
1394
        }
 
1395
 
 
1396
    return p ;
 
1397
    }
 
1398
    
 
1399
char * _ep_memdup (/*i/o*/ register req * r,
 
1400
                  /*in*/  const char *   str,
 
1401
                  /*in*/  int            len)
 
1402
 
 
1403
    {
 
1404
    char * p ;        
 
1405
 
 
1406
    p = (char *)_malloc (r, len + 1) ;
 
1407
 
 
1408
    if (p)
 
1409
        {
 
1410
        memcpy (p, str, len) ;
 
1411
 
 
1412
        p[len] = '\0' ;
 
1413
        }
 
1414
 
 
1415
    return p ;
 
1416
    }
 
1417