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 USE_INTERNAL_BEECRYPT
00013 #if HAVE_BEECRYPT_API_H
00014 #include <api.h>
00015 #else
00016 #include <beecrypt.api.h>
00017 #endif
00018 #else /* use system beecrypt */
00019 #if HAVE_BEECRYPT_API_H
00020 #include <beecrypt/api.h>
00021 #else
00022 #include <beecrypt/beecrypt.api.h>
00023 #endif
00024 #endif
00025 
00026 #include <rpmpgp.h>
00027 #include <rpmsw.h>
00028 
00029 /* Drag in the beecrypt includes. */
00030 #if USE_INTERNAL_BEECRYPT
00031 #include <beecrypt.h>
00032 #include <base64.h>
00033 #include <dsa.h>
00034 #include <endianness.h>
00035 #include <md5.h>
00036 #include <mp.h>
00037 #include <rsa.h>
00038 #include <rsapk.h>
00039 #include <sha1.h>
00040 #if HAVE_BEECRYPT_API_H
00041 #include <sha256.h>
00042 #include <sha384.h>
00043 #include <sha512.h>
00044 #endif
00045 #else /* use system beecrypt */
00046 #include <beecrypt/beecrypt.h>
00047 #include <beecrypt/base64.h>
00048 #include <beecrypt/dsa.h>
00049 #include <beecrypt/endianness.h>
00050 #include <beecrypt/md5.h>
00051 #include <beecrypt/mp.h>
00052 #include <beecrypt/rsa.h>
00053 #include <beecrypt/rsapk.h>
00054 #include <beecrypt/sha1.h>
00055 #if HAVE_BEECRYPT_API_H
00056 #include <beecrypt/sha256.h>
00057 #include <beecrypt/sha384.h>
00058 #include <beecrypt/sha512.h>
00059 #endif
00060 #endif
00061 
00065 struct pgpDigParams_s {
00066 /*@only@*/ /*@null@*/
00067     const char * userid;
00068 /*@only@*/ /*@null@*/
00069     const byte * hash;
00070     const char * params[4];
00071     byte tag;
00072 
00073     byte version;               
00074     byte time[4];               
00075     byte pubkey_algo;           
00077     byte hash_algo;
00078     byte sigtype;
00079     byte hashlen;
00080     byte signhash16[2];
00081     byte signid[8];
00082     byte saved;
00083 #define PGPDIG_SAVED_TIME       (1 << 0)
00084 #define PGPDIG_SAVED_ID         (1 << 1)
00085 
00086 };
00087 
00091 struct pgpDig_s {
00092     struct pgpDigParams_s signature;
00093     struct pgpDigParams_s pubkey;
00094 
00095     byte ** ppkts;
00096     int npkts;
00097     size_t nbytes;              
00099 /*@only@*/ /*@null@*/
00100     DIGEST_CTX sha1ctx;         
00101 /*@only@*/ /*@null@*/
00102     DIGEST_CTX hdrsha1ctx;      
00103 /*@only@*/ /*@null@*/
00104     void * sha1;                
00105     size_t sha1len;             
00107 /*@only@*/ /*@null@*/
00108     DIGEST_CTX md5ctx;          
00109 /*@only@*/ /*@null@*/
00110     DIGEST_CTX hdrmd5ctx;       
00111 /*@only@*/ /*@null@*/
00112     void * md5;                 
00113     size_t md5len;              
00115     /* DSA parameters. */
00116     mpbarrett p;
00117     mpbarrett q;
00118     mpnumber g;
00119     mpnumber y;
00120     mpnumber hm;
00121     mpnumber r;
00122     mpnumber s;
00123 
00124     /* RSA parameters. */
00125     rsapk rsa_pk;
00126     mpnumber m;
00127     mpnumber c;
00128     mpnumber rsahm;
00129 };
00130 
00133 typedef struct _FDSTACK_s {
00134 /*@exposed@*/
00135     FDIO_t              io;
00136 /*@dependent@*/
00137     void *              fp;
00138     int                 fdno;
00139 } FDSTACK_t;
00140 
00144 typedef enum fdOpX_e {
00145     FDSTAT_READ         = 0,    
00146     FDSTAT_WRITE        = 1,    
00147     FDSTAT_SEEK         = 2,    
00148     FDSTAT_CLOSE        = 3,    
00149     FDSTAT_DIGEST       = 4,    
00150     FDSTAT_MAX          = 5
00151 } fdOpX;
00152 
00156 typedef /*@abstract@*/ struct {
00157     struct rpmop_s      ops[FDSTAT_MAX];        
00158 } * FDSTAT_t;
00159 
00162 typedef struct _FDDIGEST_s {
00163     pgpHashAlgo         hashalgo;
00164     DIGEST_CTX          hashctx;
00165 } * FDDIGEST_t;
00166 
00170 struct _FD_s {
00171 /*@refs@*/
00172     int         nrefs;
00173     int         flags;
00174 #define RPMIO_DEBUG_IO          0x40000000
00175 #define RPMIO_DEBUG_REFS        0x20000000
00176     int         magic;
00177 #define FDMAGIC                 0x04463138
00178     int         nfps;
00179     FDSTACK_t   fps[8];
00180     int         urlType;        /* ufdio: */
00181 
00182 /*@dependent@*/
00183     void *      url;            /* ufdio: URL info */
00184 /*@relnull@*/
00185     void *      req;            /* ufdio: HTTP request */
00186 
00187     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00188     ssize_t     bytesRemain;    /* ufdio: */
00189     ssize_t     contentLength;  /* ufdio: */
00190     int         persist;        /* ufdio: */
00191     int         wr_chunked;     /* ufdio: */
00192 
00193     int         syserrno;       /* last system errno encountered */
00194 /*@observer@*/
00195     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00196 
00197 /*null@*/
00198     const char *opath;          /* open(2) args. */
00199     int         oflags;
00200     mode_t      omode;
00201 
00202     FDSTAT_t    stats;          /* I/O statistics */
00203 
00204     int         ndigests;
00205 #define FDDIGEST_MAX    4
00206     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00207 
00208     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00209     unsigned long long  fd_cpioPos;     /* cpio: */
00210 };
00211 /*@access FD_t@*/
00212 
00213 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00214 
00215 /*@-redecl@*/
00216 /*@unchecked@*/
00217 extern int _rpmio_debug;
00218 /*@=redecl@*/
00219 
00220 /*@-redecl@*/
00221 /*@unchecked@*/
00222 extern int _av_debug;
00223 /*@=redecl@*/
00224 
00225 /*@-redecl@*/
00226 /*@unchecked@*/
00227 extern int _ftp_debug;
00228 /*@=redecl@*/
00229 
00230 /*@-redecl@*/
00231 /*@unchecked@*/
00232 extern int _dav_debug;
00233 /*@=redecl@*/
00234 
00235 #define DBG(_f, _m, _x) \
00236     /*@-modfilesys@*/ \
00237     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00238     /*@=modfilesys@*/
00239 
00240 #if defined(__LCLINT__XXX)
00241 #define DBGIO(_f, _x)
00242 #define DBGREFS(_f, _x)
00243 #else
00244 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00245 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00246 #endif
00247 
00248 #ifdef __cplusplus
00249 extern "C" {
00250 #endif
00251 
00254 int fdFgets(FD_t fd, char * buf, size_t len)
00255         /*@globals errno, fileSystem @*/
00256         /*@modifies *buf, fd, errno, fileSystem @*/;
00257 
00260 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00261                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00262         /*@globals h_errno, fileSystem, internalState @*/
00263         /*@modifies *uret, fileSystem, internalState @*/;
00264 
00267 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00268         /*@globals fileSystem, internalState @*/
00269         /*@modifies data, fileSystem, internalState @*/;
00270 
00273 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00274         /*@globals h_errno, fileSystem, internalState @*/
00275         /*@modifies fileSystem, internalState @*/;
00276 
00279 int ufdClose( /*@only@*/ void * cookie)
00280         /*@globals fileSystem, internalState @*/
00281         /*@modifies cookie, fileSystem, internalState @*/;
00282 
00285 /*@unused@*/ static inline
00286 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
00287         /*@modifies fd @*/
00288 {
00289     FDSANE(fd);
00290     if (fd->opath != NULL) {
00291         free((void *)fd->opath);
00292         fd->opath = NULL;
00293     }
00294     fd->opath = xstrdup(path);
00295     fd->oflags = flags;
00296     fd->omode = mode;
00297 }
00298 
00301 /*@unused@*/ static inline
00302 /*@null@*/ /*@observer@*/ const char * fdGetOPath(FD_t fd)
00303         /*@*/
00304 {
00305     FDSANE(fd);
00306     return fd->opath;
00307 }
00308 
00311 /*@unused@*/ static inline
00312 int fdGetOFlags(FD_t fd)
00313         /*@*/
00314 {
00315     FDSANE(fd);
00316     return fd->oflags;
00317 }
00318 
00321 /*@unused@*/ static inline
00322 mode_t fdGetOMode(FD_t fd)
00323         /*@*/
00324 {
00325     FDSANE(fd);
00326     return fd->omode;
00327 }
00328 
00331 /*@unused@*/ static inline
00332 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00333         /*@*/
00334 {
00335     FDSANE(fd);
00336 /*@-boundsread@*/
00337     return fd->fps[fd->nfps].io;
00338 /*@=boundsread@*/
00339 }
00340 
00343 /*@-nullstate@*/ /* FIX: io may be NULL */
00344 /*@unused@*/ static inline
00345 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00346         /*@modifies fd @*/
00347 {
00348     FDSANE(fd);
00349 /*@-boundswrite@*/
00350     /*@-assignexpose@*/
00351     fd->fps[fd->nfps].io = io;
00352     /*@=assignexpose@*/
00353 /*@=boundswrite@*/
00354 }
00355 /*@=nullstate@*/
00356 
00359 /*@unused@*/ static inline
00360 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00361         /*@*/
00362 {
00363     FDSANE(fd);
00364 /*@-boundsread@*/
00365     /*@+voidabstract@*/
00366     return ((FILE *)fd->fps[fd->nfps].fp);
00367     /*@=voidabstract@*/
00368 /*@=boundsread@*/
00369 }
00370 
00373 /*@unused@*/ static inline
00374 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00375         /*@*/
00376 {
00377     FDSANE(fd);
00378 /*@-boundsread@*/
00379     return fd->fps[fd->nfps].fp;
00380 /*@=boundsread@*/
00381 }
00382 
00385 /*@-nullstate@*/ /* FIX: fp may be NULL */
00386 /*@unused@*/ static inline
00387 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00388         /*@modifies fd @*/
00389 {
00390     FDSANE(fd);
00391 /*@-boundswrite@*/
00392     /*@-assignexpose@*/
00393     fd->fps[fd->nfps].fp = fp;
00394     /*@=assignexpose@*/
00395 /*@=boundswrite@*/
00396 }
00397 /*@=nullstate@*/
00398 
00401 /*@unused@*/ static inline
00402 int fdGetFdno(FD_t fd)
00403         /*@*/
00404 {
00405     FDSANE(fd);
00406 /*@-boundsread@*/
00407     return fd->fps[fd->nfps].fdno;
00408 /*@=boundsread@*/
00409 }
00410 
00413 /*@unused@*/ static inline
00414 void fdSetFdno(FD_t fd, int fdno)
00415         /*@modifies fd @*/
00416 {
00417     FDSANE(fd);
00418 /*@-boundswrite@*/
00419     fd->fps[fd->nfps].fdno = fdno;
00420 /*@=boundswrite@*/
00421 }
00422 
00425 /*@unused@*/ static inline
00426 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00427         /*@modifies fd @*/
00428 {
00429     FDSANE(fd);
00430     fd->contentLength = fd->bytesRemain = contentLength;
00431 }
00432 
00435 /*@unused@*/ static inline
00436 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00437         /*@modifies fd @*/
00438 {
00439     FDSANE(fd);
00440     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00441         return;
00442     fd->nfps++;
00443     fdSetIo(fd, io);
00444     fdSetFp(fd, fp);
00445     fdSetFdno(fd, fdno);
00446 }
00447 
00450 /*@unused@*/ static inline
00451 void fdPop(FD_t fd)
00452         /*@modifies fd @*/
00453 {
00454     FDSANE(fd);
00455     if (fd->nfps < 0) return;
00456     fdSetIo(fd, NULL);
00457     fdSetFp(fd, NULL);
00458     fdSetFdno(fd, -1);
00459     fd->nfps--;
00460 }
00461 
00464 /*@unused@*/ static inline /*@null@*/
00465 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00466         /*@*/
00467 {
00468     rpmop op = NULL;
00469 
00470 /*@-boundsread@*/
00471     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00472         op = fd->stats->ops + opx;
00473 /*@=boundsread@*/
00474     return op;
00475 }
00476 
00479 /*@unused@*/ static inline
00480 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00481         /*@globals internalState @*/
00482         /*@modifies internalState @*/
00483 {
00484     if (fd == NULL) return;
00485     if (fd->stats != NULL)
00486         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00487 }
00488 
00491 /*@unused@*/ static inline
00492 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00493         /*@globals internalState @*/
00494         /*@modifies fd, internalState @*/
00495 {
00496     if (fd == NULL) return;
00497     if (rc == -1)
00498         fd->syserrno = errno;
00499     else if (rc > 0 && fd->bytesRemain > 0)
00500         switch (opx) {
00501         case FDSTAT_READ:
00502         case FDSTAT_WRITE:
00503         fd->bytesRemain -= rc;
00504             break;
00505         default:
00506             break;
00507         }
00508     if (fd->stats != NULL)
00509         (void) rpmswExit(fdstat_op(fd, opx), rc);
00510 }
00511 
00514 /*@-boundsread@*/
00515 /*@unused@*/ static inline
00516 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00517         /*@globals fileSystem @*/
00518         /*@modifies *fp, fileSystem @*/
00519 {
00520     static int usec_scale = (1000*1000);
00521     int opx;
00522 
00523     if (fd == NULL || fd->stats == NULL) return;
00524     for (opx = 0; opx < 4; opx++) {
00525         rpmop op = &fd->stats->ops[opx];
00526         if (op->count <= 0) continue;
00527         switch (opx) {
00528         case FDSTAT_READ:
00529             if (msg) fprintf(fp, "%s:", msg);
00530             fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
00531                 op->count, (unsigned long)op->bytes,
00532                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00533             /*@switchbreak@*/ break;
00534         case FDSTAT_WRITE:
00535             if (msg) fprintf(fp, "%s:", msg);
00536             fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
00537                 op->count, (unsigned long)op->bytes,
00538                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00539             /*@switchbreak@*/ break;
00540         case FDSTAT_SEEK:
00541             /*@switchbreak@*/ break;
00542         case FDSTAT_CLOSE:
00543             /*@switchbreak@*/ break;
00544         }
00545     }
00546 }
00547 /*@=boundsread@*/
00548 
00551 /*@unused@*/ static inline
00552 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00553         /*@modifies fd @*/
00554 {
00555     FDSANE(fd);
00556     fd->syserrno = syserrno;
00557     /*@-assignexpose@*/
00558     fd->errcookie = errcookie;
00559     /*@=assignexpose@*/
00560 }
00561 
00564 /*@unused@*/ static inline
00565 int fdGetRdTimeoutSecs(FD_t fd)
00566         /*@*/
00567 {
00568     FDSANE(fd);
00569     return fd->rd_timeoutsecs;
00570 }
00571 
00574 /*@unused@*/ static inline
00575 unsigned long long fdGetCpioPos(FD_t fd)
00576         /*@*/
00577 {
00578     FDSANE(fd);
00579     return fd->fd_cpioPos;
00580 }
00581 
00584 /*@unused@*/ static inline
00585 void fdSetCpioPos(FD_t fd, long int cpioPos)
00586         /*@modifies fd @*/
00587 {
00588     FDSANE(fd);
00589     fd->fd_cpioPos = cpioPos;
00590 }
00591 
00594 /*@mayexit@*/ /*@unused@*/ static inline
00595 FD_t c2f(/*@null@*/ void * cookie)
00596         /*@*/
00597 {
00598     /*@-castexpose@*/
00599     FD_t fd = (FD_t) cookie;
00600     /*@=castexpose@*/
00601     FDSANE(fd);
00602     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00603 }
00604 
00608 /*@unused@*/ static inline
00609 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00610         /*@globals internalState @*/
00611         /*@modifies fd, internalState @*/
00612 {
00613     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00614     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00615         fd->ndigests++;
00616         fddig->hashalgo = hashalgo;
00617         fdstat_enter(fd, FDSTAT_DIGEST);
00618         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00619         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00620     }
00621 }
00622 
00626 /*@unused@*/ static inline
00627 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00628         /*@globals internalState @*/
00629         /*@modifies fd, internalState @*/
00630 {
00631     int i;
00632 
00633     if (buf != NULL && buflen > 0)
00634     for (i = fd->ndigests - 1; i >= 0; i--) {
00635         FDDIGEST_t fddig = fd->digests + i;
00636         if (fddig->hashctx == NULL)
00637             continue;
00638         fdstat_enter(fd, FDSTAT_DIGEST);
00639         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00640         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00641     }
00642 }
00643 
00646 /*@unused@*/ static inline
00647 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00648                 /*@null@*/ /*@out@*/ void * datap,
00649                 /*@null@*/ /*@out@*/ size_t * lenp,
00650                 int asAscii)
00651         /*@globals internalState @*/
00652         /*@modifies fd, *datap, *lenp, internalState @*/
00653 {
00654     int imax = -1;
00655     int i;
00656 
00657     for (i = fd->ndigests - 1; i >= 0; i--) {
00658         FDDIGEST_t fddig = fd->digests + i;
00659         if (fddig->hashctx == NULL)
00660             continue;
00661         if (i > imax) imax = i;
00662         if (fddig->hashalgo != hashalgo)
00663             continue;
00664         fdstat_enter(fd, FDSTAT_DIGEST);
00665         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00666         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00667         fddig->hashctx = NULL;
00668         break;
00669     }
00670 /*@-boundswrite@*/
00671     if (i < 0) {
00672         if (datap) *(void **)datap = NULL;
00673         if (lenp) *lenp = 0;
00674     }
00675 /*@=boundswrite@*/
00676 
00677     fd->ndigests = imax;
00678     if (i < imax)
00679         fd->ndigests++;         /* convert index to count */
00680 }
00681 
00682 /*@-shadow@*/
00685 /*@unused@*/ static inline
00686 int fdFileno(/*@null@*/ void * cookie)
00687         /*@*/
00688 {
00689     FD_t fd;
00690     if (cookie == NULL) return -2;
00691     fd = c2f(cookie);
00692 /*@-boundsread@*/
00693     return fd->fps[0].fdno;
00694 /*@=boundsread@*/
00695 }
00696 /*@=shadow@*/
00697 
00705 int rpmioSlurp(const char * fn,
00706                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00707         /*@globals h_errno, fileSystem, internalState @*/
00708         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00709 
00710 #ifdef __cplusplus
00711 }
00712 #endif
00713 
00714 #endif  /* H_RPMIO_INTERNAL */

Generated on Fri Aug 31 10:38:35 2007 for rpm by  doxygen 1.5.1