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 
00035 struct pgpDigParams_s {
00036 /*@only@*/ /*@null@*/
00037     const char * userid;
00038 /*@only@*/ /*@null@*/
00039     const byte * hash;
00040     const char * params[4];
00041     byte tag;
00042 
00043     byte version;               
00044     byte time[4];               
00045     byte pubkey_algo;           
00047     byte hash_algo;
00048     byte sigtype;
00049     byte hashlen;
00050     byte signhash16[2];
00051     byte signid[8];
00052     byte saved;
00053 #define PGPDIG_SAVED_TIME       (1 << 0)
00054 #define PGPDIG_SAVED_ID         (1 << 1)
00055 
00056 };
00057 
00061 struct pgpDig_s {
00062     struct pgpDigParams_s signature;
00063     struct pgpDigParams_s pubkey;
00064 
00065     size_t nbytes;              
00067 /*@only@*/ /*@null@*/
00068     DIGEST_CTX sha1ctx;         
00069 /*@only@*/ /*@null@*/
00070     DIGEST_CTX hdrsha1ctx;      
00071 /*@only@*/ /*@null@*/
00072     void * sha1;                
00073     size_t sha1len;             
00075 /*@only@*/ /*@null@*/
00076     DIGEST_CTX md5ctx;          
00077 #ifdef  NOTYET
00078 /*@only@*/ /*@null@*/
00079     DIGEST_CTX hdrmd5ctx;       
00080 #endif
00081 /*@only@*/ /*@null@*/
00082     void * md5;                 
00083     size_t md5len;              
00085     /* DSA parameters. */
00086     mpbarrett p;
00087     mpbarrett q;
00088     mpnumber g;
00089     mpnumber y;
00090     mpnumber hm;
00091     mpnumber r;
00092     mpnumber s;
00093 
00094     /* RSA parameters. */
00095     rsapk rsa_pk;
00096     mpnumber m;
00097     mpnumber c;
00098     mpnumber rsahm;
00099 };
00100 
00103 typedef struct _FDSTACK_s {
00104 /*@exposed@*/
00105     FDIO_t              io;
00106 /*@dependent@*/
00107     void *              fp;
00108     int                 fdno;
00109 } FDSTACK_t;
00110 
00114 typedef enum fdOpX_e {
00115     FDSTAT_READ         = 0,    
00116     FDSTAT_WRITE        = 1,    
00117     FDSTAT_SEEK         = 2,    
00118     FDSTAT_CLOSE        = 3,    
00119     FDSTAT_DIGEST       = 4,    
00120     FDSTAT_MAX          = 5
00121 } fdOpX;
00122 
00126 typedef /*@abstract@*/ struct {
00127     struct rpmop_s      ops[FDSTAT_MAX];        
00128 } * FDSTAT_t;
00129 
00132 typedef struct _FDDIGEST_s {
00133     pgpHashAlgo         hashalgo;
00134     DIGEST_CTX          hashctx;
00135 } * FDDIGEST_t;
00136 
00140 struct _FD_s {
00141 /*@refs@*/
00142     int         nrefs;
00143     int         flags;
00144 #define RPMIO_DEBUG_IO          0x40000000
00145 #define RPMIO_DEBUG_REFS        0x20000000
00146     int         magic;
00147 #define FDMAGIC                 0x04463138
00148     int         nfps;
00149     FDSTACK_t   fps[8];
00150     int         urlType;        /* ufdio: */
00151 
00152 /*@dependent@*/
00153     void *      url;            /* ufdio: URL info */
00154     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00155     ssize_t     bytesRemain;    /* ufdio: */
00156     ssize_t     contentLength;  /* ufdio: */
00157     int         persist;        /* ufdio: */
00158     int         wr_chunked;     /* ufdio: */
00159 
00160     int         syserrno;       /* last system errno encountered */
00161 /*@observer@*/
00162     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00163 
00164     FDSTAT_t    stats;          /* I/O statistics */
00165 
00166     int         ndigests;
00167 #define FDDIGEST_MAX    4
00168     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00169 
00170     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00171     unsigned int firstFree;     /* fadio: */
00172     long int    fileSize;       /* fadio: */
00173     long int    fd_cpioPos;     /* cpio: */
00174 };
00175 /*@access FD_t@*/
00176 
00177 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00178 
00179 /*@-redecl@*/
00180 /*@unchecked@*/
00181 extern int _rpmio_debug;
00182 /*@=redecl@*/
00183 
00184 /*@-redecl@*/
00185 /*@unchecked@*/
00186 extern int _ftp_debug;
00187 /*@=redecl@*/
00188 
00189 #define DBG(_f, _m, _x) \
00190     /*@-modfilesys@*/ \
00191     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00192     /*@=modfilesys@*/
00193 
00194 #if defined(__LCLINT__XXX)
00195 #define DBGIO(_f, _x)
00196 #define DBGREFS(_f, _x)
00197 #else
00198 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00199 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00200 #endif
00201 
00202 #ifdef __cplusplus
00203 extern "C" {
00204 #endif
00205 
00208 int fdFgets(FD_t fd, char * buf, size_t len)
00209         /*@globals errno, fileSystem @*/
00210         /*@modifies *buf, fd, errno, fileSystem @*/;
00211 
00214 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00215                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00216         /*@globals h_errno, fileSystem, internalState @*/
00217         /*@modifies *uret, fileSystem, internalState @*/;
00218 
00221 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00222         /*@globals fileSystem, internalState @*/
00223         /*@modifies data, fileSystem, internalState @*/;
00224 
00227 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00228         /*@globals h_errno, fileSystem, internalState @*/
00229         /*@modifies fileSystem, internalState @*/;
00230 
00233 int ufdClose( /*@only@*/ void * cookie)
00234         /*@globals fileSystem, internalState @*/
00235         /*@modifies cookie, fileSystem, internalState @*/;
00236 
00239 /*@unused@*/ static inline
00240 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00241         /*@*/
00242 {
00243     FDSANE(fd);
00244 /*@-boundsread@*/
00245     return fd->fps[fd->nfps].io;
00246 /*@=boundsread@*/
00247 }
00248 
00251 /*@-nullstate@*/ /* FIX: io may be NULL */
00252 /*@unused@*/ static inline
00253 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00254         /*@modifies fd @*/
00255 {
00256     FDSANE(fd);
00257 /*@-boundswrite@*/
00258     /*@-assignexpose@*/
00259     fd->fps[fd->nfps].io = io;
00260     /*@=assignexpose@*/
00261 /*@=boundswrite@*/
00262 }
00263 /*@=nullstate@*/
00264 
00267 /*@unused@*/ static inline
00268 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00269         /*@*/
00270 {
00271     FDSANE(fd);
00272 /*@-boundsread@*/
00273     /*@+voidabstract@*/
00274     return ((FILE *)fd->fps[fd->nfps].fp);
00275     /*@=voidabstract@*/
00276 /*@=boundsread@*/
00277 }
00278 
00281 /*@unused@*/ static inline
00282 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00283         /*@*/
00284 {
00285     FDSANE(fd);
00286 /*@-boundsread@*/
00287     return fd->fps[fd->nfps].fp;
00288 /*@=boundsread@*/
00289 }
00290 
00293 /*@-nullstate@*/ /* FIX: fp may be NULL */
00294 /*@unused@*/ static inline
00295 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00296         /*@modifies fd @*/
00297 {
00298     FDSANE(fd);
00299 /*@-boundswrite@*/
00300     /*@-assignexpose@*/
00301     fd->fps[fd->nfps].fp = fp;
00302     /*@=assignexpose@*/
00303 /*@=boundswrite@*/
00304 }
00305 /*@=nullstate@*/
00306 
00309 /*@unused@*/ static inline
00310 int fdGetFdno(FD_t fd)
00311         /*@*/
00312 {
00313     FDSANE(fd);
00314 /*@-boundsread@*/
00315     return fd->fps[fd->nfps].fdno;
00316 /*@=boundsread@*/
00317 }
00318 
00321 /*@unused@*/ static inline
00322 void fdSetFdno(FD_t fd, int fdno)
00323         /*@modifies fd @*/
00324 {
00325     FDSANE(fd);
00326 /*@-boundswrite@*/
00327     fd->fps[fd->nfps].fdno = fdno;
00328 /*@=boundswrite@*/
00329 }
00330 
00333 /*@unused@*/ static inline
00334 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00335         /*@modifies fd @*/
00336 {
00337     FDSANE(fd);
00338     fd->contentLength = fd->bytesRemain = contentLength;
00339 }
00340 
00343 /*@unused@*/ static inline
00344 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00345         /*@modifies fd @*/
00346 {
00347     FDSANE(fd);
00348     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00349         return;
00350     fd->nfps++;
00351     fdSetIo(fd, io);
00352     fdSetFp(fd, fp);
00353     fdSetFdno(fd, fdno);
00354 }
00355 
00358 /*@unused@*/ static inline
00359 void fdPop(FD_t fd)
00360         /*@modifies fd @*/
00361 {
00362     FDSANE(fd);
00363     if (fd->nfps < 0) return;
00364     fdSetIo(fd, NULL);
00365     fdSetFp(fd, NULL);
00366     fdSetFdno(fd, -1);
00367     fd->nfps--;
00368 }
00369 
00372 /*@unused@*/ static inline /*@null@*/
00373 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00374         /*@*/
00375 {
00376     rpmop op = NULL;
00377 
00378 /*@-boundsread@*/
00379     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00380         op = fd->stats->ops + opx;
00381 /*@=boundsread@*/
00382     return op;
00383 }
00384 
00387 /*@unused@*/ static inline
00388 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00389         /*@globals internalState @*/
00390         /*@modifies internalState @*/
00391 {
00392     if (fd == NULL) return;
00393     if (fd->stats != NULL)
00394         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00395 }
00396 
00399 /*@unused@*/ static inline
00400 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00401         /*@globals internalState @*/
00402         /*@modifies fd, internalState @*/
00403 {
00404     if (fd == NULL) return;
00405     if (rc == -1)
00406         fd->syserrno = errno;
00407     else if (rc > 0 && fd->bytesRemain > 0) {
00408         switch (opx) {
00409         case FDSTAT_READ:
00410         case FDSTAT_WRITE:
00411             fd->bytesRemain -= rc;
00412             /*@switchbreak@*/ break;
00413         default:
00414             /*@switchbreak@*/ break;
00415         }
00416     }
00417     if (fd->stats != NULL)
00418         (void) rpmswExit(fdstat_op(fd, opx), rc);
00419 }
00420 
00423 /*@-boundsread@*/
00424 /*@unused@*/ static inline
00425 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00426         /*@globals fileSystem @*/
00427         /*@modifies *fp, fileSystem @*/
00428 {
00429     static int usec_scale = (1000*1000);
00430     int opx;
00431 
00432     if (fd == NULL || fd->stats == NULL) return;
00433     for (opx = 0; opx < 4; opx++) {
00434         rpmop op = &fd->stats->ops[opx];
00435         if (op->count <= 0) continue;
00436         switch (opx) {
00437         case FDSTAT_READ:
00438             if (msg) fprintf(fp, "%s:", msg);
00439             fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00440                 op->count, (long)op->bytes,
00441                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00442             /*@switchbreak@*/ break;
00443         case FDSTAT_WRITE:
00444             if (msg) fprintf(fp, "%s:", msg);
00445             fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00446                 op->count, (long)op->bytes,
00447                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00448             /*@switchbreak@*/ break;
00449         case FDSTAT_SEEK:
00450             /*@switchbreak@*/ break;
00451         case FDSTAT_CLOSE:
00452             /*@switchbreak@*/ break;
00453         }
00454     }
00455 }
00456 /*@=boundsread@*/
00457 
00460 /*@unused@*/ static inline
00461 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00462         /*@modifies fd @*/
00463 {
00464     FDSANE(fd);
00465     fd->syserrno = syserrno;
00466     /*@-assignexpose@*/
00467     fd->errcookie = errcookie;
00468     /*@=assignexpose@*/
00469 }
00470 
00473 /*@unused@*/ static inline
00474 int fdGetRdTimeoutSecs(FD_t fd)
00475         /*@*/
00476 {
00477     FDSANE(fd);
00478     return fd->rd_timeoutsecs;
00479 }
00480 
00483 /*@unused@*/ static inline
00484 long int fdGetCpioPos(FD_t fd)
00485         /*@*/
00486 {
00487     FDSANE(fd);
00488     return fd->fd_cpioPos;
00489 }
00490 
00493 /*@unused@*/ static inline
00494 void fdSetCpioPos(FD_t fd, long int cpioPos)
00495         /*@modifies fd @*/
00496 {
00497     FDSANE(fd);
00498     fd->fd_cpioPos = cpioPos;
00499 }
00500 
00503 /*@mayexit@*/ /*@unused@*/ static inline
00504 FD_t c2f(/*@null@*/ void * cookie)
00505         /*@*/
00506 {
00507     /*@-castexpose@*/
00508     FD_t fd = (FD_t) cookie;
00509     /*@=castexpose@*/
00510     FDSANE(fd);
00511     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00512 }
00513 
00517 /*@unused@*/ static inline
00518 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00519         /*@globals internalState @*/
00520         /*@modifies fd, internalState @*/
00521 {
00522     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00523     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00524         fd->ndigests++;
00525         fddig->hashalgo = hashalgo;
00526         fdstat_enter(fd, FDSTAT_DIGEST);
00527         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00528         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00529     }
00530 }
00531 
00535 /*@unused@*/ static inline
00536 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00537         /*@globals internalState @*/
00538         /*@modifies fd, internalState @*/
00539 {
00540     int i;
00541 
00542     if (buf != NULL && buflen > 0)
00543     for (i = fd->ndigests - 1; i >= 0; i--) {
00544         FDDIGEST_t fddig = fd->digests + i;
00545         if (fddig->hashctx == NULL)
00546             continue;
00547         fdstat_enter(fd, FDSTAT_DIGEST);
00548         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00549         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00550     }
00551 }
00552 
00555 /*@unused@*/ static inline
00556 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00557                 /*@null@*/ /*@out@*/ void ** datap,
00558                 /*@null@*/ /*@out@*/ size_t * lenp,
00559                 int asAscii)
00560         /*@globals internalState @*/
00561         /*@modifies fd, *datap, *lenp, internalState @*/
00562 {
00563     int imax = -1;
00564     int i;
00565 
00566     for (i = fd->ndigests - 1; i >= 0; i--) {
00567         FDDIGEST_t fddig = fd->digests + i;
00568         if (fddig->hashctx == NULL)
00569             continue;
00570         if (i > imax) imax = i;
00571         if (fddig->hashalgo != hashalgo)
00572             continue;
00573         fdstat_enter(fd, FDSTAT_DIGEST);
00574         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00575         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00576         fddig->hashctx = NULL;
00577         break;
00578     }
00579 /*@-boundswrite@*/
00580     if (i < 0) {
00581         if (datap) *datap = NULL;
00582         if (lenp) *lenp = 0;
00583     }
00584 /*@=boundswrite@*/
00585 
00586     fd->ndigests = imax;
00587     if (i < imax)
00588         fd->ndigests++;         /* convert index to count */
00589 }
00590 
00591 /*@-shadow@*/
00594 /*@unused@*/ static inline
00595 int fdFileno(/*@null@*/ void * cookie)
00596         /*@*/
00597 {
00598     FD_t fd;
00599     if (cookie == NULL) return -2;
00600     fd = c2f(cookie);
00601 /*@-boundsread@*/
00602     return fd->fps[0].fdno;
00603 /*@=boundsread@*/
00604 }
00605 /*@=shadow@*/
00606 
00614 int rpmioSlurp(const char * fn,
00615                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00616         /*@globals h_errno, fileSystem, internalState @*/
00617         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00618 
00619 #ifdef __cplusplus
00620 }
00621 #endif
00622 
00623 #endif  /* H_RPMIO_INTERNAL */

Generated on Sun Oct 2 12:29:41 2005 for rpm by doxygen 1.3.5