rpmio/rpmio_internal.h

Go to the documentation of this file.
00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003 
00009 #include <rpmio.h>
00010 #include <rpmurl.h>
00011 
00012 #if HAVE_BEECRYPT_API_H
00013 #include <api.h>
00014 #else
00015 #include <beecrypt.api.h>
00016 #endif
00017 
00018 #include <rpmpgp.h>
00019 #include <rpmsw.h>
00020 
00021 /* Drag in the beecrypt includes. */
00022 #include <beecrypt.h>
00023 #include <base64.h>
00024 #include <dsa.h>
00025 #include <endianness.h>
00026 #include <md5.h>
00027 #include <mp.h>
00028 #include <rsa.h>
00029 #include <rsapk.h>
00030 #include <sha1.h>
00031 #if HAVE_BEECRYPT_API_H
00032 #include <sha256.h>
00033 #include <sha384.h>
00034 #include <sha512.h>
00035 #endif
00036 
00040 struct pgpDigParams_s {
00041 /*@only@*/ /*@null@*/
00042     const char * userid;
00043 /*@only@*/ /*@null@*/
00044     const byte * hash;
00045     const char * params[4];
00046     byte tag;
00047 
00048     byte version;               
00049     byte time[4];               
00050     byte pubkey_algo;           
00052     byte hash_algo;
00053     byte sigtype;
00054     byte hashlen;
00055     byte signhash16[2];
00056     byte signid[8];
00057     byte saved;
00058 #define PGPDIG_SAVED_TIME       (1 << 0)
00059 #define PGPDIG_SAVED_ID         (1 << 1)
00060 
00061 };
00062 
00066 struct pgpDig_s {
00067     struct pgpDigParams_s signature;
00068     struct pgpDigParams_s pubkey;
00069 
00070     size_t nbytes;              
00072 /*@only@*/ /*@null@*/
00073     DIGEST_CTX sha1ctx;         
00074 /*@only@*/ /*@null@*/
00075     DIGEST_CTX hdrsha1ctx;      
00076 /*@only@*/ /*@null@*/
00077     void * sha1;                
00078     size_t sha1len;             
00080 /*@only@*/ /*@null@*/
00081     DIGEST_CTX md5ctx;          
00082 /*@only@*/ /*@null@*/
00083     DIGEST_CTX hdrmd5ctx;       
00084 /*@only@*/ /*@null@*/
00085     void * md5;                 
00086     size_t md5len;              
00088     /* DSA parameters. */
00089     mpbarrett p;
00090     mpbarrett q;
00091     mpnumber g;
00092     mpnumber y;
00093     mpnumber hm;
00094     mpnumber r;
00095     mpnumber s;
00096 
00097     /* RSA parameters. */
00098     rsapk rsa_pk;
00099     mpnumber m;
00100     mpnumber c;
00101     mpnumber rsahm;
00102 };
00103 
00106 typedef struct _FDSTACK_s {
00107 /*@exposed@*/
00108     FDIO_t              io;
00109 /*@dependent@*/
00110     void *              fp;
00111     int                 fdno;
00112 } FDSTACK_t;
00113 
00117 typedef enum fdOpX_e {
00118     FDSTAT_READ         = 0,    
00119     FDSTAT_WRITE        = 1,    
00120     FDSTAT_SEEK         = 2,    
00121     FDSTAT_CLOSE        = 3,    
00122     FDSTAT_DIGEST       = 4,    
00123     FDSTAT_MAX          = 5
00124 } fdOpX;
00125 
00129 typedef /*@abstract@*/ struct {
00130     struct rpmop_s      ops[FDSTAT_MAX];        
00131 } * FDSTAT_t;
00132 
00135 typedef struct _FDDIGEST_s {
00136     pgpHashAlgo         hashalgo;
00137     DIGEST_CTX          hashctx;
00138 } * FDDIGEST_t;
00139 
00143 struct _FD_s {
00144 /*@refs@*/
00145     int         nrefs;
00146     int         flags;
00147 #define RPMIO_DEBUG_IO          0x40000000
00148 #define RPMIO_DEBUG_REFS        0x20000000
00149     int         magic;
00150 #define FDMAGIC                 0x04463138
00151     int         nfps;
00152     FDSTACK_t   fps[8];
00153     int         urlType;        /* ufdio: */
00154 
00155 /*@dependent@*/
00156     void *      url;            /* ufdio: URL info */
00157 /*@relnull@*/
00158     void *      req;            /* ufdio: HTTP request */
00159 
00160     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00161     ssize_t     bytesRemain;    /* ufdio: */
00162     ssize_t     contentLength;  /* ufdio: */
00163     int         persist;        /* ufdio: */
00164     int         wr_chunked;     /* ufdio: */
00165 
00166     int         syserrno;       /* last system errno encountered */
00167 /*@observer@*/
00168     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00169 
00170     FDSTAT_t    stats;          /* I/O statistics */
00171 
00172     int         ndigests;
00173 #define FDDIGEST_MAX    4
00174     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00175 
00176     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00177     unsigned int firstFree;     /* fadio: */
00178     long int    fileSize;       /* fadio: */
00179     long int    fd_cpioPos;     /* cpio: */
00180 };
00181 /*@access FD_t@*/
00182 
00183 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00184 
00185 /*@-redecl@*/
00186 /*@unchecked@*/
00187 extern int _rpmio_debug;
00188 /*@=redecl@*/
00189 
00190 /*@-redecl@*/
00191 /*@unchecked@*/
00192 extern int _av_debug;
00193 /*@=redecl@*/
00194 
00195 /*@-redecl@*/
00196 /*@unchecked@*/
00197 extern int _ftp_debug;
00198 /*@=redecl@*/
00199 
00200 /*@-redecl@*/
00201 /*@unchecked@*/
00202 extern int _dav_debug;
00203 /*@=redecl@*/
00204 
00205 #define DBG(_f, _m, _x) \
00206     /*@-modfilesys@*/ \
00207     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00208     /*@=modfilesys@*/
00209 
00210 #if defined(__LCLINT__XXX)
00211 #define DBGIO(_f, _x)
00212 #define DBGREFS(_f, _x)
00213 #else
00214 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00215 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00216 #endif
00217 
00218 #ifdef __cplusplus
00219 extern "C" {
00220 #endif
00221 
00224 int fdFgets(FD_t fd, char * buf, size_t len)
00225         /*@globals errno, fileSystem @*/
00226         /*@modifies *buf, fd, errno, fileSystem @*/;
00227 
00230 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00231                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00232         /*@globals h_errno, fileSystem, internalState @*/
00233         /*@modifies *uret, fileSystem, internalState @*/;
00234 
00237 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00238         /*@globals fileSystem, internalState @*/
00239         /*@modifies data, fileSystem, internalState @*/;
00240 
00243 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00244         /*@globals h_errno, fileSystem, internalState @*/
00245         /*@modifies fileSystem, internalState @*/;
00246 
00249 int ufdClose( /*@only@*/ void * cookie)
00250         /*@globals fileSystem, internalState @*/
00251         /*@modifies cookie, fileSystem, internalState @*/;
00252 
00255 /*@unused@*/ static inline
00256 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00257         /*@*/
00258 {
00259     FDSANE(fd);
00260 /*@-boundsread@*/
00261     return fd->fps[fd->nfps].io;
00262 /*@=boundsread@*/
00263 }
00264 
00267 /*@-nullstate@*/ /* FIX: io may be NULL */
00268 /*@unused@*/ static inline
00269 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00270         /*@modifies fd @*/
00271 {
00272     FDSANE(fd);
00273 /*@-boundswrite@*/
00274     /*@-assignexpose@*/
00275     fd->fps[fd->nfps].io = io;
00276     /*@=assignexpose@*/
00277 /*@=boundswrite@*/
00278 }
00279 /*@=nullstate@*/
00280 
00283 /*@unused@*/ static inline
00284 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00285         /*@*/
00286 {
00287     FDSANE(fd);
00288 /*@-boundsread@*/
00289     /*@+voidabstract@*/
00290     return ((FILE *)fd->fps[fd->nfps].fp);
00291     /*@=voidabstract@*/
00292 /*@=boundsread@*/
00293 }
00294 
00297 /*@unused@*/ static inline
00298 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00299         /*@*/
00300 {
00301     FDSANE(fd);
00302 /*@-boundsread@*/
00303     return fd->fps[fd->nfps].fp;
00304 /*@=boundsread@*/
00305 }
00306 
00309 /*@-nullstate@*/ /* FIX: fp may be NULL */
00310 /*@unused@*/ static inline
00311 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00312         /*@modifies fd @*/
00313 {
00314     FDSANE(fd);
00315 /*@-boundswrite@*/
00316     /*@-assignexpose@*/
00317     fd->fps[fd->nfps].fp = fp;
00318     /*@=assignexpose@*/
00319 /*@=boundswrite@*/
00320 }
00321 /*@=nullstate@*/
00322 
00325 /*@unused@*/ static inline
00326 int fdGetFdno(FD_t fd)
00327         /*@*/
00328 {
00329     FDSANE(fd);
00330 /*@-boundsread@*/
00331     return fd->fps[fd->nfps].fdno;
00332 /*@=boundsread@*/
00333 }
00334 
00337 /*@unused@*/ static inline
00338 void fdSetFdno(FD_t fd, int fdno)
00339         /*@modifies fd @*/
00340 {
00341     FDSANE(fd);
00342 /*@-boundswrite@*/
00343     fd->fps[fd->nfps].fdno = fdno;
00344 /*@=boundswrite@*/
00345 }
00346 
00349 /*@unused@*/ static inline
00350 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00351         /*@modifies fd @*/
00352 {
00353     FDSANE(fd);
00354     fd->contentLength = fd->bytesRemain = contentLength;
00355 }
00356 
00359 /*@unused@*/ static inline
00360 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00361         /*@modifies fd @*/
00362 {
00363     FDSANE(fd);
00364     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00365         return;
00366     fd->nfps++;
00367     fdSetIo(fd, io);
00368     fdSetFp(fd, fp);
00369     fdSetFdno(fd, fdno);
00370 }
00371 
00374 /*@unused@*/ static inline
00375 void fdPop(FD_t fd)
00376         /*@modifies fd @*/
00377 {
00378     FDSANE(fd);
00379     if (fd->nfps < 0) return;
00380     fdSetIo(fd, NULL);
00381     fdSetFp(fd, NULL);
00382     fdSetFdno(fd, -1);
00383     fd->nfps--;
00384 }
00385 
00388 /*@unused@*/ static inline /*@null@*/
00389 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00390         /*@*/
00391 {
00392     rpmop op = NULL;
00393 
00394 /*@-boundsread@*/
00395     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00396         op = fd->stats->ops + opx;
00397 /*@=boundsread@*/
00398     return op;
00399 }
00400 
00403 /*@unused@*/ static inline
00404 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00405         /*@globals internalState @*/
00406         /*@modifies internalState @*/
00407 {
00408     if (fd == NULL) return;
00409     if (fd->stats != NULL)
00410         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00411 }
00412 
00415 /*@unused@*/ static inline
00416 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00417         /*@globals internalState @*/
00418         /*@modifies fd, internalState @*/
00419 {
00420     if (fd == NULL) return;
00421     if (rc == -1)
00422         fd->syserrno = errno;
00423     else if (rc > 0 && fd->bytesRemain > 0)
00424         switch (opx) {
00425         case FDSTAT_READ:
00426         case FDSTAT_WRITE:
00427         fd->bytesRemain -= rc;
00428             break;
00429         default:
00430             break;
00431         }
00432     if (fd->stats != NULL)
00433         (void) rpmswExit(fdstat_op(fd, opx), rc);
00434 }
00435 
00438 /*@-boundsread@*/
00439 /*@unused@*/ static inline
00440 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00441         /*@globals fileSystem @*/
00442         /*@modifies *fp, fileSystem @*/
00443 {
00444     static int usec_scale = (1000*1000);
00445     int opx;
00446 
00447     if (fd == NULL || fd->stats == NULL) return;
00448     for (opx = 0; opx < 4; opx++) {
00449         rpmop op = &fd->stats->ops[opx];
00450         if (op->count <= 0) continue;
00451         switch (opx) {
00452         case FDSTAT_READ:
00453             if (msg) fprintf(fp, "%s:", msg);
00454             fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00455                 op->count, (long)op->bytes,
00456                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00457             /*@switchbreak@*/ break;
00458         case FDSTAT_WRITE:
00459             if (msg) fprintf(fp, "%s:", msg);
00460             fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00461                 op->count, (long)op->bytes,
00462                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00463             /*@switchbreak@*/ break;
00464         case FDSTAT_SEEK:
00465             /*@switchbreak@*/ break;
00466         case FDSTAT_CLOSE:
00467             /*@switchbreak@*/ break;
00468         }
00469     }
00470 }
00471 /*@=boundsread@*/
00472 
00475 /*@unused@*/ static inline
00476 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00477         /*@modifies fd @*/
00478 {
00479     FDSANE(fd);
00480     fd->syserrno = syserrno;
00481     /*@-assignexpose@*/
00482     fd->errcookie = errcookie;
00483     /*@=assignexpose@*/
00484 }
00485 
00488 /*@unused@*/ static inline
00489 int fdGetRdTimeoutSecs(FD_t fd)
00490         /*@*/
00491 {
00492     FDSANE(fd);
00493     return fd->rd_timeoutsecs;
00494 }
00495 
00498 /*@unused@*/ static inline
00499 long int fdGetCpioPos(FD_t fd)
00500         /*@*/
00501 {
00502     FDSANE(fd);
00503     return fd->fd_cpioPos;
00504 }
00505 
00508 /*@unused@*/ static inline
00509 void fdSetCpioPos(FD_t fd, long int cpioPos)
00510         /*@modifies fd @*/
00511 {
00512     FDSANE(fd);
00513     fd->fd_cpioPos = cpioPos;
00514 }
00515 
00518 /*@mayexit@*/ /*@unused@*/ static inline
00519 FD_t c2f(/*@null@*/ void * cookie)
00520         /*@*/
00521 {
00522     /*@-castexpose@*/
00523     FD_t fd = (FD_t) cookie;
00524     /*@=castexpose@*/
00525     FDSANE(fd);
00526     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00527 }
00528 
00532 /*@unused@*/ static inline
00533 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00534         /*@globals internalState @*/
00535         /*@modifies fd, internalState @*/
00536 {
00537     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00538     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00539         fd->ndigests++;
00540         fddig->hashalgo = hashalgo;
00541         fdstat_enter(fd, FDSTAT_DIGEST);
00542         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00543         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00544     }
00545 }
00546 
00550 /*@unused@*/ static inline
00551 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00552         /*@globals internalState @*/
00553         /*@modifies fd, internalState @*/
00554 {
00555     int i;
00556 
00557     if (buf != NULL && buflen > 0)
00558     for (i = fd->ndigests - 1; i >= 0; i--) {
00559         FDDIGEST_t fddig = fd->digests + i;
00560         if (fddig->hashctx == NULL)
00561             continue;
00562         fdstat_enter(fd, FDSTAT_DIGEST);
00563         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00564         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00565     }
00566 }
00567 
00570 /*@unused@*/ static inline
00571 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00572                 /*@null@*/ /*@out@*/ void ** datap,
00573                 /*@null@*/ /*@out@*/ size_t * lenp,
00574                 int asAscii)
00575         /*@globals internalState @*/
00576         /*@modifies fd, *datap, *lenp, internalState @*/
00577 {
00578     int imax = -1;
00579     int i;
00580 
00581     for (i = fd->ndigests - 1; i >= 0; i--) {
00582         FDDIGEST_t fddig = fd->digests + i;
00583         if (fddig->hashctx == NULL)
00584             continue;
00585         if (i > imax) imax = i;
00586         if (fddig->hashalgo != hashalgo)
00587             continue;
00588         fdstat_enter(fd, FDSTAT_DIGEST);
00589         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00590         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00591         fddig->hashctx = NULL;
00592         break;
00593     }
00594 /*@-boundswrite@*/
00595     if (i < 0) {
00596         if (datap) *datap = NULL;
00597         if (lenp) *lenp = 0;
00598     }
00599 /*@=boundswrite@*/
00600 
00601     fd->ndigests = imax;
00602     if (i < imax)
00603         fd->ndigests++;         /* convert index to count */
00604 }
00605 
00606 /*@-shadow@*/
00609 /*@unused@*/ static inline
00610 int fdFileno(/*@null@*/ void * cookie)
00611         /*@*/
00612 {
00613     FD_t fd;
00614     if (cookie == NULL) return -2;
00615     fd = c2f(cookie);
00616 /*@-boundsread@*/
00617     return fd->fps[0].fdno;
00618 /*@=boundsread@*/
00619 }
00620 /*@=shadow@*/
00621 
00629 int rpmioSlurp(const char * fn,
00630                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00631         /*@globals h_errno, fileSystem, internalState @*/
00632         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00633 
00634 #ifdef __cplusplus
00635 }
00636 #endif
00637 
00638 #endif  /* H_RPMIO_INTERNAL */

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2