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     void *      req;            /* ufdio: HTTP request */
00161 
00162     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00163     ssize_t     bytesRemain;    /* ufdio: */
00164     ssize_t     contentLength;  /* ufdio: */
00165     int         persist;        /* ufdio: */
00166     int         wr_chunked;     /* ufdio: */
00167 
00168     int         syserrno;       /* last system errno encountered */
00169 /*@observer@*/
00170     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00171 
00172 /*null@*/
00173     const char *opath;          /* open(2) args. */
00174     int         oflags;
00175     mode_t      omode;
00176 
00177     FDSTAT_t    stats;          /* I/O statistics */
00178 
00179     int         ndigests;
00180 #define FDDIGEST_MAX    4
00181     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00182 
00183     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00184     unsigned long long  fd_cpioPos;     /* cpio: */
00185 };
00186 /*@access FD_t@*/
00187 
00188 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00189 
00190 /*@-redecl@*/
00191 /*@unchecked@*/
00192 extern int _rpmio_debug;
00193 /*@=redecl@*/
00194 
00195 /*@-redecl@*/
00196 /*@unchecked@*/
00197 extern int _av_debug;
00198 /*@=redecl@*/
00199 
00200 /*@-redecl@*/
00201 /*@unchecked@*/
00202 extern int _ftp_debug;
00203 /*@=redecl@*/
00204 
00205 /*@-redecl@*/
00206 /*@unchecked@*/
00207 extern int _dav_debug;
00208 /*@=redecl@*/
00209 
00210 #define DBG(_f, _m, _x) \
00211     /*@-modfilesys@*/ \
00212     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00213     /*@=modfilesys@*/
00214 
00215 #if defined(__LCLINT__XXX)
00216 #define DBGIO(_f, _x)
00217 #define DBGREFS(_f, _x)
00218 #else
00219 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00220 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00221 #endif
00222 
00223 #ifdef __cplusplus
00224 extern "C" {
00225 #endif
00226 
00229 int fdFgets(FD_t fd, char * buf, size_t len)
00230         /*@globals errno, fileSystem @*/
00231         /*@modifies *buf, fd, errno, fileSystem @*/;
00232 
00235 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00236                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00237         /*@globals h_errno, fileSystem, internalState @*/
00238         /*@modifies *uret, fileSystem, internalState @*/;
00239 
00242 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00243         /*@globals fileSystem, internalState @*/
00244         /*@modifies data, fileSystem, internalState @*/;
00245 
00248 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00249         /*@globals h_errno, fileSystem, internalState @*/
00250         /*@modifies fileSystem, internalState @*/;
00251 
00254 int ufdClose( /*@only@*/ void * cookie)
00255         /*@globals fileSystem, internalState @*/
00256         /*@modifies cookie, fileSystem, internalState @*/;
00257 
00260 /*@unused@*/ static inline
00261 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
00262         /*@modifies fd @*/
00263 {
00264     FDSANE(fd);
00265     if (fd->opath != NULL) {
00266         free((void *)fd->opath);
00267         fd->opath = NULL;
00268     }
00269     fd->opath = xstrdup(path);
00270     fd->oflags = flags;
00271     fd->omode = mode;
00272 }
00273 
00276 /*@unused@*/ static inline
00277 /*@null@*/ /*@observer@*/ const char * fdGetOPath(FD_t fd)
00278         /*@*/
00279 {
00280     FDSANE(fd);
00281     return fd->opath;
00282 }
00283 
00286 /*@unused@*/ static inline
00287 int fdGetOFlags(FD_t fd)
00288         /*@*/
00289 {
00290     FDSANE(fd);
00291     return fd->oflags;
00292 }
00293 
00296 /*@unused@*/ static inline
00297 mode_t fdGetOMode(FD_t fd)
00298         /*@*/
00299 {
00300     FDSANE(fd);
00301     return fd->omode;
00302 }
00303 
00306 /*@unused@*/ static inline
00307 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00308         /*@*/
00309 {
00310     FDSANE(fd);
00311 /*@-boundsread@*/
00312     return fd->fps[fd->nfps].io;
00313 /*@=boundsread@*/
00314 }
00315 
00318 /*@-nullstate@*/ /* FIX: io may be NULL */
00319 /*@unused@*/ static inline
00320 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00321         /*@modifies fd @*/
00322 {
00323     FDSANE(fd);
00324 /*@-boundswrite@*/
00325     /*@-assignexpose@*/
00326     fd->fps[fd->nfps].io = io;
00327     /*@=assignexpose@*/
00328 /*@=boundswrite@*/
00329 }
00330 /*@=nullstate@*/
00331 
00334 /*@unused@*/ static inline
00335 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00336         /*@*/
00337 {
00338     FDSANE(fd);
00339 /*@-boundsread@*/
00340     /*@+voidabstract@*/
00341     return ((FILE *)fd->fps[fd->nfps].fp);
00342     /*@=voidabstract@*/
00343 /*@=boundsread@*/
00344 }
00345 
00348 /*@unused@*/ static inline
00349 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00350         /*@*/
00351 {
00352     FDSANE(fd);
00353 /*@-boundsread@*/
00354     return fd->fps[fd->nfps].fp;
00355 /*@=boundsread@*/
00356 }
00357 
00360 /*@-nullstate@*/ /* FIX: fp may be NULL */
00361 /*@unused@*/ static inline
00362 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00363         /*@modifies fd @*/
00364 {
00365     FDSANE(fd);
00366 /*@-boundswrite@*/
00367     /*@-assignexpose@*/
00368     fd->fps[fd->nfps].fp = fp;
00369     /*@=assignexpose@*/
00370 /*@=boundswrite@*/
00371 }
00372 /*@=nullstate@*/
00373 
00376 /*@unused@*/ static inline
00377 int fdGetFdno(FD_t fd)
00378         /*@*/
00379 {
00380     FDSANE(fd);
00381 /*@-boundsread@*/
00382     return fd->fps[fd->nfps].fdno;
00383 /*@=boundsread@*/
00384 }
00385 
00388 /*@unused@*/ static inline
00389 void fdSetFdno(FD_t fd, int fdno)
00390         /*@modifies fd @*/
00391 {
00392     FDSANE(fd);
00393 /*@-boundswrite@*/
00394     fd->fps[fd->nfps].fdno = fdno;
00395 /*@=boundswrite@*/
00396 }
00397 
00400 /*@unused@*/ static inline
00401 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00402         /*@modifies fd @*/
00403 {
00404     FDSANE(fd);
00405     fd->contentLength = fd->bytesRemain = contentLength;
00406 }
00407 
00410 /*@unused@*/ static inline
00411 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00412         /*@modifies fd @*/
00413 {
00414     FDSANE(fd);
00415     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00416         return;
00417     fd->nfps++;
00418     fdSetIo(fd, io);
00419     fdSetFp(fd, fp);
00420     fdSetFdno(fd, fdno);
00421 }
00422 
00425 /*@unused@*/ static inline
00426 void fdPop(FD_t fd)
00427         /*@modifies fd @*/
00428 {
00429     FDSANE(fd);
00430     if (fd->nfps < 0) return;
00431     fdSetIo(fd, NULL);
00432     fdSetFp(fd, NULL);
00433     fdSetFdno(fd, -1);
00434     fd->nfps--;
00435 }
00436 
00439 /*@unused@*/ static inline /*@null@*/
00440 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00441         /*@*/
00442 {
00443     rpmop op = NULL;
00444 
00445 /*@-boundsread@*/
00446     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00447         op = fd->stats->ops + opx;
00448 /*@=boundsread@*/
00449     return op;
00450 }
00451 
00454 /*@unused@*/ static inline
00455 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00456         /*@globals internalState @*/
00457         /*@modifies internalState @*/
00458 {
00459     if (fd == NULL) return;
00460     if (fd->stats != NULL)
00461         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00462 }
00463 
00466 /*@unused@*/ static inline
00467 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00468         /*@globals internalState @*/
00469         /*@modifies fd, internalState @*/
00470 {
00471     if (fd == NULL) return;
00472     if (rc == -1)
00473         fd->syserrno = errno;
00474     else if (rc > 0 && fd->bytesRemain > 0)
00475         switch (opx) {
00476         case FDSTAT_READ:
00477         case FDSTAT_WRITE:
00478         fd->bytesRemain -= rc;
00479             break;
00480         default:
00481             break;
00482         }
00483     if (fd->stats != NULL)
00484         (void) rpmswExit(fdstat_op(fd, opx), rc);
00485 }
00486 
00489 /*@-boundsread@*/
00490 /*@unused@*/ static inline
00491 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00492         /*@globals fileSystem @*/
00493         /*@modifies *fp, fileSystem @*/
00494 {
00495     static int usec_scale = (1000*1000);
00496     int opx;
00497 
00498     if (fd == NULL || fd->stats == NULL) return;
00499     for (opx = 0; opx < 4; opx++) {
00500         rpmop op = &fd->stats->ops[opx];
00501         if (op->count <= 0) continue;
00502         switch (opx) {
00503         case FDSTAT_READ:
00504             if (msg) fprintf(fp, "%s:", msg);
00505             fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
00506                 op->count, (unsigned long)op->bytes,
00507                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00508             /*@switchbreak@*/ break;
00509         case FDSTAT_WRITE:
00510             if (msg) fprintf(fp, "%s:", msg);
00511             fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
00512                 op->count, (unsigned long)op->bytes,
00513                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00514             /*@switchbreak@*/ break;
00515         case FDSTAT_SEEK:
00516             /*@switchbreak@*/ break;
00517         case FDSTAT_CLOSE:
00518             /*@switchbreak@*/ break;
00519         }
00520     }
00521 }
00522 /*@=boundsread@*/
00523 
00526 /*@unused@*/ static inline
00527 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00528         /*@modifies fd @*/
00529 {
00530     FDSANE(fd);
00531     fd->syserrno = syserrno;
00532     /*@-assignexpose@*/
00533     fd->errcookie = errcookie;
00534     /*@=assignexpose@*/
00535 }
00536 
00539 /*@unused@*/ static inline
00540 int fdGetRdTimeoutSecs(FD_t fd)
00541         /*@*/
00542 {
00543     FDSANE(fd);
00544     return fd->rd_timeoutsecs;
00545 }
00546 
00549 /*@unused@*/ static inline
00550 unsigned long long fdGetCpioPos(FD_t fd)
00551         /*@*/
00552 {
00553     FDSANE(fd);
00554     return fd->fd_cpioPos;
00555 }
00556 
00559 /*@unused@*/ static inline
00560 void fdSetCpioPos(FD_t fd, long int cpioPos)
00561         /*@modifies fd @*/
00562 {
00563     FDSANE(fd);
00564     fd->fd_cpioPos = cpioPos;
00565 }
00566 
00569 /*@mayexit@*/ /*@unused@*/ static inline
00570 FD_t c2f(/*@null@*/ void * cookie)
00571         /*@*/
00572 {
00573     /*@-castexpose@*/
00574     FD_t fd = (FD_t) cookie;
00575     /*@=castexpose@*/
00576     FDSANE(fd);
00577     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00578 }
00579 
00583 /*@unused@*/ static inline
00584 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00585         /*@globals internalState @*/
00586         /*@modifies fd, internalState @*/
00587 {
00588     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00589     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00590         fd->ndigests++;
00591         fddig->hashalgo = hashalgo;
00592         fdstat_enter(fd, FDSTAT_DIGEST);
00593         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00594         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00595     }
00596 }
00597 
00601 /*@unused@*/ static inline
00602 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00603         /*@globals internalState @*/
00604         /*@modifies fd, internalState @*/
00605 {
00606     int i;
00607 
00608     if (buf != NULL && buflen > 0)
00609     for (i = fd->ndigests - 1; i >= 0; i--) {
00610         FDDIGEST_t fddig = fd->digests + i;
00611         if (fddig->hashctx == NULL)
00612             continue;
00613         fdstat_enter(fd, FDSTAT_DIGEST);
00614         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00615         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00616     }
00617 }
00618 
00621 /*@unused@*/ static inline
00622 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00623                 /*@null@*/ /*@out@*/ void * datap,
00624                 /*@null@*/ /*@out@*/ size_t * lenp,
00625                 int asAscii)
00626         /*@globals internalState @*/
00627         /*@modifies fd, *datap, *lenp, internalState @*/
00628 {
00629     int imax = -1;
00630     int i;
00631 
00632     for (i = fd->ndigests - 1; i >= 0; i--) {
00633         FDDIGEST_t fddig = fd->digests + i;
00634         if (fddig->hashctx == NULL)
00635             continue;
00636         if (i > imax) imax = i;
00637         if (fddig->hashalgo != hashalgo)
00638             continue;
00639         fdstat_enter(fd, FDSTAT_DIGEST);
00640         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00641         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00642         fddig->hashctx = NULL;
00643         break;
00644     }
00645 /*@-boundswrite@*/
00646     if (i < 0) {
00647         if (datap) *(void **)datap = NULL;
00648         if (lenp) *lenp = 0;
00649     }
00650 /*@=boundswrite@*/
00651 
00652     fd->ndigests = imax;
00653     if (i < imax)
00654         fd->ndigests++;         /* convert index to count */
00655 }
00656 
00657 /*@-shadow@*/
00660 /*@unused@*/ static inline
00661 int fdFileno(/*@null@*/ void * cookie)
00662         /*@*/
00663 {
00664     FD_t fd;
00665     if (cookie == NULL) return -2;
00666     fd = c2f(cookie);
00667 /*@-boundsread@*/
00668     return fd->fps[0].fdno;
00669 /*@=boundsread@*/
00670 }
00671 /*@=shadow@*/
00672 
00680 int rpmioSlurp(const char * fn,
00681                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00682         /*@globals h_errno, fileSystem, internalState @*/
00683         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00684 
00685 #ifdef __cplusplus
00686 }
00687 #endif
00688 
00689 #endif  /* H_RPMIO_INTERNAL */

Generated on Fri Sep 7 01:07:26 2007 for rpm by  doxygen 1.5.1