Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

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     byte ** ppkts;
00071     int npkts;
00072     size_t nbytes;              
00074 /*@only@*/ /*@null@*/
00075     DIGEST_CTX sha1ctx;         
00076 /*@only@*/ /*@null@*/
00077     DIGEST_CTX hdrsha1ctx;      
00078 /*@only@*/ /*@null@*/
00079     void * sha1;                
00080     size_t sha1len;             
00082 /*@only@*/ /*@null@*/
00083     DIGEST_CTX md5ctx;          
00084 /*@only@*/ /*@null@*/
00085     DIGEST_CTX hdrmd5ctx;       
00086 /*@only@*/ /*@null@*/
00087     void * md5;                 
00088     size_t md5len;              
00090     /* DSA parameters. */
00091     mpbarrett p;
00092     mpbarrett q;
00093     mpnumber g;
00094     mpnumber y;
00095     mpnumber hm;
00096     mpnumber r;
00097     mpnumber s;
00098 
00099     /* RSA parameters. */
00100     rsapk rsa_pk;
00101     mpnumber m;
00102     mpnumber c;
00103     mpnumber rsahm;
00104 };
00105 
00108 typedef struct _FDSTACK_s {
00109 /*@exposed@*/
00110     FDIO_t              io;
00111 /*@dependent@*/
00112     void *              fp;
00113     int                 fdno;
00114 } FDSTACK_t;
00115 
00119 typedef enum fdOpX_e {
00120     FDSTAT_READ         = 0,    
00121     FDSTAT_WRITE        = 1,    
00122     FDSTAT_SEEK         = 2,    
00123     FDSTAT_CLOSE        = 3,    
00124     FDSTAT_DIGEST       = 4,    
00125     FDSTAT_MAX          = 5
00126 } fdOpX;
00127 
00131 typedef /*@abstract@*/ struct {
00132     struct rpmop_s      ops[FDSTAT_MAX];        
00133 } * FDSTAT_t;
00134 
00137 typedef struct _FDDIGEST_s {
00138     pgpHashAlgo         hashalgo;
00139     DIGEST_CTX          hashctx;
00140 } * FDDIGEST_t;
00141 
00145 struct _FD_s {
00146 /*@refs@*/
00147     int         nrefs;
00148     int         flags;
00149 #define RPMIO_DEBUG_IO          0x40000000
00150 #define RPMIO_DEBUG_REFS        0x20000000
00151     int         magic;
00152 #define FDMAGIC                 0x04463138
00153     int         nfps;
00154     FDSTACK_t   fps[8];
00155     int         urlType;        /* ufdio: */
00156 
00157 /*@dependent@*/
00158     void *      url;            /* ufdio: URL info */
00159 /*@relnull@*/
00160 
00161     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00162     ssize_t     bytesRemain;    /* ufdio: */
00163     ssize_t     contentLength;  /* ufdio: */
00164     int         persist;        /* ufdio: */
00165     int         wr_chunked;     /* ufdio: */
00166 
00167     int         syserrno;       /* last system errno encountered */
00168 /*@observer@*/
00169     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00170 
00171 /*null@*/
00172     const char *opath;          /* open(2) args. */
00173     int         oflags;
00174     mode_t      omode;
00175 
00176     FDSTAT_t    stats;          /* I/O statistics */
00177 
00178     int         ndigests;
00179 #define FDDIGEST_MAX    4
00180     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00181 
00182     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00183     unsigned long long  fd_cpioPos;     /* cpio: */
00184 };
00185 /*@access FD_t@*/
00186 
00187 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00188 
00189 /*@-redecl@*/
00190 /*@unchecked@*/
00191 extern int _rpmio_debug;
00192 /*@=redecl@*/
00193 
00194 /*@-redecl@*/
00195 /*@unchecked@*/
00196 extern int _av_debug;
00197 /*@=redecl@*/
00198 
00199 /*@-redecl@*/
00200 /*@unchecked@*/
00201 extern int _ftp_debug;
00202 /*@=redecl@*/
00203 
00204 /*@-redecl@*/
00205 /*@unchecked@*/
00206 extern int _dav_debug;
00207 /*@=redecl@*/
00208 
00209 #define DBG(_f, _m, _x) \
00210     /*@-modfilesys@*/ \
00211     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00212     /*@=modfilesys@*/
00213 
00214 #if defined(__LCLINT__XXX)
00215 #define DBGIO(_f, _x)
00216 #define DBGREFS(_f, _x)
00217 #else
00218 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00219 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00220 #endif
00221 
00222 #ifdef __cplusplus
00223 extern "C" {
00224 #endif
00225 
00228 int fdFgets(FD_t fd, char * buf, size_t len)
00229         /*@globals errno, fileSystem @*/
00230         /*@modifies *buf, fd, errno, fileSystem @*/;
00231 
00234 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00235                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00236         /*@globals h_errno, fileSystem, internalState @*/
00237         /*@modifies *uret, fileSystem, internalState @*/;
00238 
00241 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00242         /*@globals fileSystem, internalState @*/
00243         /*@modifies data, fileSystem, internalState @*/;
00244 
00247 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00248         /*@globals h_errno, fileSystem, internalState @*/
00249         /*@modifies fileSystem, internalState @*/;
00250 
00253 int ufdClose( /*@only@*/ void * cookie)
00254         /*@globals fileSystem, internalState @*/
00255         /*@modifies cookie, fileSystem, internalState @*/;
00256 
00259 /*@unused@*/ static inline
00260 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
00261         /*@modifies fd @*/
00262 {
00263     FDSANE(fd);
00264     if (fd->opath != NULL) {
00265         free((void *)fd->opath);
00266         fd->opath = NULL;
00267     }
00268     fd->opath = xstrdup(path);
00269     fd->oflags = flags;
00270     fd->omode = mode;
00271 }
00272 
00275 /*@unused@*/ static inline
00276 /*@null@*/ /*@observer@*/ const char * fdGetOPath(FD_t fd)
00277         /*@*/
00278 {
00279     FDSANE(fd);
00280     return fd->opath;
00281 }
00282 
00285 /*@unused@*/ static inline
00286 int fdGetOFlags(FD_t fd)
00287         /*@*/
00288 {
00289     FDSANE(fd);
00290     return fd->oflags;
00291 }
00292 
00295 /*@unused@*/ static inline
00296 mode_t fdGetOMode(FD_t fd)
00297         /*@*/
00298 {
00299     FDSANE(fd);
00300     return fd->omode;
00301 }
00302 
00305 /*@unused@*/ static inline
00306 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00307         /*@*/
00308 {
00309     FDSANE(fd);
00310 /*@-boundsread@*/
00311     return fd->fps[fd->nfps].io;
00312 /*@=boundsread@*/
00313 }
00314 
00317 /*@-nullstate@*/ /* FIX: io may be NULL */
00318 /*@unused@*/ static inline
00319 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00320         /*@modifies fd @*/
00321 {
00322     FDSANE(fd);
00323 /*@-boundswrite@*/
00324     /*@-assignexpose@*/
00325     fd->fps[fd->nfps].io = io;
00326     /*@=assignexpose@*/
00327 /*@=boundswrite@*/
00328 }
00329 /*@=nullstate@*/
00330 
00333 /*@unused@*/ static inline
00334 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00335         /*@*/
00336 {
00337     FDSANE(fd);
00338 /*@-boundsread@*/
00339     /*@+voidabstract@*/
00340     return ((FILE *)fd->fps[fd->nfps].fp);
00341     /*@=voidabstract@*/
00342 /*@=boundsread@*/
00343 }
00344 
00347 /*@unused@*/ static inline
00348 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00349         /*@*/
00350 {
00351     FDSANE(fd);
00352 /*@-boundsread@*/
00353     return fd->fps[fd->nfps].fp;
00354 /*@=boundsread@*/
00355 }
00356 
00359 /*@-nullstate@*/ /* FIX: fp may be NULL */
00360 /*@unused@*/ static inline
00361 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00362         /*@modifies fd @*/
00363 {
00364     FDSANE(fd);
00365 /*@-boundswrite@*/
00366     /*@-assignexpose@*/
00367     fd->fps[fd->nfps].fp = fp;
00368     /*@=assignexpose@*/
00369 /*@=boundswrite@*/
00370 }
00371 /*@=nullstate@*/
00372 
00375 /*@unused@*/ static inline
00376 int fdGetFdno(FD_t fd)
00377         /*@*/
00378 {
00379     FDSANE(fd);
00380 /*@-boundsread@*/
00381     return fd->fps[fd->nfps].fdno;
00382 /*@=boundsread@*/
00383 }
00384 
00387 /*@unused@*/ static inline
00388 void fdSetFdno(FD_t fd, int fdno)
00389         /*@modifies fd @*/
00390 {
00391     FDSANE(fd);
00392 /*@-boundswrite@*/
00393     fd->fps[fd->nfps].fdno = fdno;
00394 /*@=boundswrite@*/
00395 }
00396 
00399 /*@unused@*/ static inline
00400 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00401         /*@modifies fd @*/
00402 {
00403     FDSANE(fd);
00404     fd->contentLength = fd->bytesRemain = contentLength;
00405 }
00406 
00409 /*@unused@*/ static inline
00410 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00411         /*@modifies fd @*/
00412 {
00413     FDSANE(fd);
00414     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00415         return;
00416     fd->nfps++;
00417     fdSetIo(fd, io);
00418     fdSetFp(fd, fp);
00419     fdSetFdno(fd, fdno);
00420 }
00421 
00424 /*@unused@*/ static inline
00425 void fdPop(FD_t fd)
00426         /*@modifies fd @*/
00427 {
00428     FDSANE(fd);
00429     if (fd->nfps < 0) return;
00430     fdSetIo(fd, NULL);
00431     fdSetFp(fd, NULL);
00432     fdSetFdno(fd, -1);
00433     fd->nfps--;
00434 }
00435 
00438 /*@unused@*/ static inline /*@null@*/
00439 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00440         /*@*/
00441 {
00442     rpmop op = NULL;
00443 
00444 /*@-boundsread@*/
00445     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00446         op = fd->stats->ops + opx;
00447 /*@=boundsread@*/
00448     return op;
00449 }
00450 
00453 /*@unused@*/ static inline
00454 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00455         /*@globals internalState @*/
00456         /*@modifies internalState @*/
00457 {
00458     if (fd == NULL) return;
00459     if (fd->stats != NULL)
00460         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00461 }
00462 
00465 /*@unused@*/ static inline
00466 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00467         /*@globals internalState @*/
00468         /*@modifies fd, internalState @*/
00469 {
00470     if (fd == NULL) return;
00471     if (rc == -1)
00472         fd->syserrno = errno;
00473     else if (rc > 0 && fd->bytesRemain > 0)
00474         switch (opx) {
00475         case FDSTAT_READ:
00476         case FDSTAT_WRITE:
00477         fd->bytesRemain -= rc;
00478             break;
00479         default:
00480             break;
00481         }
00482     if (fd->stats != NULL)
00483         (void) rpmswExit(fdstat_op(fd, opx), rc);
00484 }
00485 
00488 /*@-boundsread@*/
00489 /*@unused@*/ static inline
00490 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00491         /*@globals fileSystem @*/
00492         /*@modifies *fp, fileSystem @*/
00493 {
00494     static int usec_scale = (1000*1000);
00495     int opx;
00496 
00497     if (fd == NULL || fd->stats == NULL) return;
00498     for (opx = 0; opx < 4; opx++) {
00499         rpmop op = &fd->stats->ops[opx];
00500         if (op->count <= 0) continue;
00501         switch (opx) {
00502         case FDSTAT_READ:
00503             if (msg) fprintf(fp, "%s:", msg);
00504             fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
00505                 op->count, (unsigned long)op->bytes,
00506                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00507             /*@switchbreak@*/ break;
00508         case FDSTAT_WRITE:
00509             if (msg) fprintf(fp, "%s:", msg);
00510             fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
00511                 op->count, (unsigned long)op->bytes,
00512                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00513             /*@switchbreak@*/ break;
00514         case FDSTAT_SEEK:
00515             /*@switchbreak@*/ break;
00516         case FDSTAT_CLOSE:
00517             /*@switchbreak@*/ break;
00518         }
00519     }
00520 }
00521 /*@=boundsread@*/
00522 
00525 /*@unused@*/ static inline
00526 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00527         /*@modifies fd @*/
00528 {
00529     FDSANE(fd);
00530     fd->syserrno = syserrno;
00531     /*@-assignexpose@*/
00532     fd->errcookie = errcookie;
00533     /*@=assignexpose@*/
00534 }
00535 
00538 /*@unused@*/ static inline
00539 int fdGetRdTimeoutSecs(FD_t fd)
00540         /*@*/
00541 {
00542     FDSANE(fd);
00543     return fd->rd_timeoutsecs;
00544 }
00545 
00548 /*@unused@*/ static inline
00549 unsigned long long fdGetCpioPos(FD_t fd)
00550         /*@*/
00551 {
00552     FDSANE(fd);
00553     return fd->fd_cpioPos;
00554 }
00555 
00558 /*@unused@*/ static inline
00559 void fdSetCpioPos(FD_t fd, long int cpioPos)
00560         /*@modifies fd @*/
00561 {
00562     FDSANE(fd);
00563     fd->fd_cpioPos = cpioPos;
00564 }
00565 
00568 /*@mayexit@*/ /*@unused@*/ static inline
00569 FD_t c2f(/*@null@*/ void * cookie)
00570         /*@*/
00571 {
00572     /*@-castexpose@*/
00573     FD_t fd = (FD_t) cookie;
00574     /*@=castexpose@*/
00575     FDSANE(fd);
00576     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00577 }
00578 
00582 /*@unused@*/ static inline
00583 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00584         /*@globals internalState @*/
00585         /*@modifies fd, internalState @*/
00586 {
00587     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00588     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00589         fd->ndigests++;
00590         fddig->hashalgo = hashalgo;
00591         fdstat_enter(fd, FDSTAT_DIGEST);
00592         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00593         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00594     }
00595 }
00596 
00600 /*@unused@*/ static inline
00601 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00602         /*@globals internalState @*/
00603         /*@modifies fd, internalState @*/
00604 {
00605     int i;
00606 
00607     if (buf != NULL && buflen > 0)
00608     for (i = fd->ndigests - 1; i >= 0; i--) {
00609         FDDIGEST_t fddig = fd->digests + i;
00610         if (fddig->hashctx == NULL)
00611             continue;
00612         fdstat_enter(fd, FDSTAT_DIGEST);
00613         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00614         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00615     }
00616 }
00617 
00620 /*@unused@*/ static inline
00621 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00622                 /*@null@*/ /*@out@*/ void ** datap,
00623                 /*@null@*/ /*@out@*/ size_t * lenp,
00624                 int asAscii)
00625         /*@globals internalState @*/
00626         /*@modifies fd, *datap, *lenp, internalState @*/
00627 {
00628     int imax = -1;
00629     int i;
00630 
00631     for (i = fd->ndigests - 1; i >= 0; i--) {
00632         FDDIGEST_t fddig = fd->digests + i;
00633         if (fddig->hashctx == NULL)
00634             continue;
00635         if (i > imax) imax = i;
00636         if (fddig->hashalgo != hashalgo)
00637             continue;
00638         fdstat_enter(fd, FDSTAT_DIGEST);
00639         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00640         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00641         fddig->hashctx = NULL;
00642         break;
00643     }
00644 /*@-boundswrite@*/
00645     if (i < 0) {
00646         if (datap) *datap = NULL;
00647         if (lenp) *lenp = 0;
00648     }
00649 /*@=boundswrite@*/
00650 
00651     fd->ndigests = imax;
00652     if (i < imax)
00653         fd->ndigests++;         /* convert index to count */
00654 }
00655 
00656 /*@-shadow@*/
00659 /*@unused@*/ static inline
00660 int fdFileno(/*@null@*/ void * cookie)
00661         /*@*/
00662 {
00663     FD_t fd;
00664     if (cookie == NULL) return -2;
00665     fd = c2f(cookie);
00666 /*@-boundsread@*/
00667     return fd->fps[0].fdno;
00668 /*@=boundsread@*/
00669 }
00670 /*@=shadow@*/
00671 
00679 int rpmioSlurp(const char * fn,
00680                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00681         /*@globals h_errno, fileSystem, internalState @*/
00682         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00683 
00684 #ifdef __cplusplus
00685 }
00686 #endif
00687 
00688 #endif  /* H_RPMIO_INTERNAL */

Generated on Mon Aug 3 15:15:36 2009 for rpm by  doxygen 1.4.4