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 #include <beecrypt.api.h>
00013 #include <rpmpgp.h>
00014 
00015 /* Drag in the beecrypt includes. */
00016 #include <beecrypt.h>
00017 #include <base64.h>
00018 #include <dsa.h>
00019 #include <endianness.h>
00020 #include <md5.h>
00021 #include <mp.h>
00022 #include <rsa.h>
00023 #include <rsapk.h>
00024 #include <sha1.h>
00025 
00029 struct pgpDigParams_s {
00030 /*@only@*/ /*@null@*/
00031     const char * userid;
00032 /*@only@*/ /*@null@*/
00033     const byte * hash;
00034     const char * params[4];
00035     byte tag;
00036 
00037     byte version;               
00038     byte time[4];               
00039     byte pubkey_algo;           
00041     byte hash_algo;
00042     byte sigtype;
00043     byte hashlen;
00044     byte signhash16[2];
00045     byte signid[8];
00046     byte saved;
00047 #define PGPDIG_SAVED_TIME       (1 << 0)
00048 #define PGPDIG_SAVED_ID         (1 << 1)
00049 
00050 };
00051 
00055 struct pgpDig_s {
00056     struct pgpDigParams_s signature;
00057     struct pgpDigParams_s pubkey;
00058 
00059     size_t nbytes;              
00061 /*@only@*/ /*@null@*/
00062     DIGEST_CTX sha1ctx;         
00063 /*@only@*/ /*@null@*/
00064     DIGEST_CTX hdrsha1ctx;      
00065 /*@only@*/ /*@null@*/
00066     void * sha1;                
00067     size_t sha1len;             
00069 /*@only@*/ /*@null@*/
00070     DIGEST_CTX md5ctx;          
00071 #ifdef  NOTYET
00072 /*@only@*/ /*@null@*/
00073     DIGEST_CTX hdrmd5ctx;       
00074 #endif
00075 /*@only@*/ /*@null@*/
00076     void * md5;                 
00077     size_t md5len;              
00079     /* DSA parameters. */
00080     mpbarrett p;
00081     mpbarrett q;
00082     mpnumber g;
00083     mpnumber y;
00084     mpnumber hm;
00085     mpnumber r;
00086     mpnumber s;
00087 
00088     /* RSA parameters. */
00089     rsapk rsa_pk;
00090     mpnumber m;
00091     mpnumber c;
00092     mpnumber rsahm;
00093 };
00094 
00097 typedef struct _FDSTACK_s {
00098 /*@exposed@*/
00099     FDIO_t              io;
00100 /*@dependent@*/
00101     void *              fp;
00102     int                 fdno;
00103 } FDSTACK_t;
00104 
00108 typedef struct {
00109     int                 count;  
00110     off_t               bytes;  
00111     time_t              msecs;  
00112 } OPSTAT_t;
00113 
00117 enum FDSTAT_e {
00118     FDSTAT_READ         = 0,    
00119     FDSTAT_WRITE        = 1,    
00120     FDSTAT_SEEK         = 2,    
00121     FDSTAT_CLOSE        = 3     
00122 };
00123 
00127 typedef /*@abstract@*/ struct {
00128     struct timeval      create; 
00129     struct timeval      begin;  
00130     OPSTAT_t            ops[4]; 
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     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00158     ssize_t     bytesRemain;    /* ufdio: */
00159     ssize_t     contentLength;  /* ufdio: */
00160     int         persist;        /* ufdio: */
00161     int         wr_chunked;     /* ufdio: */
00162 
00163     int         syserrno;       /* last system errno encountered */
00164 /*@observer@*/
00165     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00166 
00167     FDSTAT_t    stats;          /* I/O statistics */
00168 
00169     int         ndigests;
00170 #define FDDIGEST_MAX    4
00171     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00172 
00173     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00174     unsigned int firstFree;     /* fadio: */
00175     long int    fileSize;       /* fadio: */
00176     long int    fd_cpioPos;     /* cpio: */
00177 };
00178 /*@access FD_t@*/
00179 
00180 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00181 
00182 /*@-redecl@*/
00183 /*@unchecked@*/
00184 extern int _rpmio_debug;
00185 /*@=redecl@*/
00186 
00187 /*@-redecl@*/
00188 /*@unchecked@*/
00189 extern int _ftp_debug;
00190 /*@=redecl@*/
00191 
00192 #define DBG(_f, _m, _x) \
00193     /*@-modfilesys@*/ \
00194     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00195     /*@=modfilesys@*/
00196 
00197 #if defined(__LCLINT__XXX)
00198 #define DBGIO(_f, _x)
00199 #define DBGREFS(_f, _x)
00200 #else
00201 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00202 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00203 #endif
00204 
00205 #ifdef __cplusplus
00206 extern "C" {
00207 #endif
00208 
00211 int fdFgets(FD_t fd, char * buf, size_t len)
00212         /*@globals errno, fileSystem @*/
00213         /*@modifies *buf, fd, errno, fileSystem @*/;
00214 
00217 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00218                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00219         /*@globals fileSystem, internalState @*/
00220         /*@modifies *uret, fileSystem, internalState @*/;
00221 
00224 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00225         /*@globals fileSystem, internalState @*/
00226         /*@modifies data, fileSystem, internalState @*/;
00227 
00230 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00231         /*@globals fileSystem, internalState @*/
00232         /*@modifies fileSystem, internalState @*/;
00233 
00236 int ufdClose( /*@only@*/ void * cookie)
00237         /*@globals fileSystem, internalState @*/
00238         /*@modifies cookie, fileSystem, internalState @*/;
00239 
00242 /*@unused@*/ static inline
00243 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00244         /*@*/
00245 {
00246     FDSANE(fd);
00247 /*@-boundsread@*/
00248     return fd->fps[fd->nfps].io;
00249 /*@=boundsread@*/
00250 }
00251 
00254 /*@-nullstate@*/ /* FIX: io may be NULL */
00255 /*@unused@*/ static inline
00256 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00257         /*@modifies fd @*/
00258 {
00259     FDSANE(fd);
00260 /*@-boundswrite@*/
00261     /*@-assignexpose@*/
00262     fd->fps[fd->nfps].io = io;
00263     /*@=assignexpose@*/
00264 /*@=boundswrite@*/
00265 }
00266 /*@=nullstate@*/
00267 
00270 /*@unused@*/ static inline
00271 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00272         /*@*/
00273 {
00274     FDSANE(fd);
00275 /*@-boundsread@*/
00276     /*@+voidabstract@*/
00277     return ((FILE *)fd->fps[fd->nfps].fp);
00278     /*@=voidabstract@*/
00279 /*@=boundsread@*/
00280 }
00281 
00284 /*@unused@*/ static inline
00285 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00286         /*@*/
00287 {
00288     FDSANE(fd);
00289 /*@-boundsread@*/
00290     return fd->fps[fd->nfps].fp;
00291 /*@=boundsread@*/
00292 }
00293 
00296 /*@-nullstate@*/ /* FIX: fp may be NULL */
00297 /*@unused@*/ static inline
00298 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00299         /*@modifies fd @*/
00300 {
00301     FDSANE(fd);
00302 /*@-boundswrite@*/
00303     /*@-assignexpose@*/
00304     fd->fps[fd->nfps].fp = fp;
00305     /*@=assignexpose@*/
00306 /*@=boundswrite@*/
00307 }
00308 /*@=nullstate@*/
00309 
00312 /*@unused@*/ static inline
00313 int fdGetFdno(FD_t fd)
00314         /*@*/
00315 {
00316     FDSANE(fd);
00317 /*@-boundsread@*/
00318     return fd->fps[fd->nfps].fdno;
00319 /*@=boundsread@*/
00320 }
00321 
00324 /*@unused@*/ static inline
00325 void fdSetFdno(FD_t fd, int fdno)
00326         /*@modifies fd @*/
00327 {
00328     FDSANE(fd);
00329 /*@-boundswrite@*/
00330     fd->fps[fd->nfps].fdno = fdno;
00331 /*@=boundswrite@*/
00332 }
00333 
00336 /*@unused@*/ static inline
00337 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00338         /*@modifies fd @*/
00339 {
00340     FDSANE(fd);
00341     fd->contentLength = fd->bytesRemain = contentLength;
00342 }
00343 
00346 /*@unused@*/ static inline
00347 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00348         /*@modifies fd @*/
00349 {
00350     FDSANE(fd);
00351     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00352         return;
00353     fd->nfps++;
00354     fdSetIo(fd, io);
00355     fdSetFp(fd, fp);
00356     fdSetFdno(fd, fdno);
00357 }
00358 
00361 /*@unused@*/ static inline void fdPop(FD_t fd)
00362         /*@modifies fd @*/
00363 {
00364     FDSANE(fd);
00365     if (fd->nfps < 0) return;
00366     fdSetIo(fd, NULL);
00367     fdSetFp(fd, NULL);
00368     fdSetFdno(fd, -1);
00369     fd->nfps--;
00370 }
00371 
00374 /*@unused@*/ static inline void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00375         /*@modifies fd @*/
00376 {
00377     if (fd == NULL || fd->stats == NULL) return;
00378 /*@-boundswrite@*/
00379     fd->stats->ops[opx].count++;
00380 /*@=boundswrite@*/
00381     (void) gettimeofday(&fd->stats->begin, NULL);
00382 }
00383 
00386 /*@unused@*/ static inline
00387 time_t tvsub(/*@null@*/ const struct timeval * etv,
00388                 /*@null@*/ const struct timeval * btv)
00389         /*@*/
00390 {
00391     time_t secs, usecs;
00392     if (etv == NULL  || btv == NULL) return 0;
00393     secs = etv->tv_sec - btv->tv_sec;
00394     for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
00395         secs++;
00396     return ((secs * 1000) + (usecs/1000));
00397 }
00398 
00401 /*@unused@*/ static inline
00402 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00403         /*@modifies fd @*/
00404 {
00405     struct timeval end;
00406     if (fd == NULL) return;
00407     if (rc == -1) fd->syserrno = errno;
00408     if (fd->stats == NULL) return;
00409 /*@-boundswrite@*/
00410     (void) gettimeofday(&end, NULL);
00411     if (rc >= 0) {
00412         switch(opx) {
00413         case FDSTAT_SEEK:
00414             fd->stats->ops[opx].bytes = rc;
00415             break;
00416         default:
00417             fd->stats->ops[opx].bytes += rc;
00418             if (fd->bytesRemain > 0) fd->bytesRemain -= rc;
00419             break;
00420         }
00421     }
00422     fd->stats->ops[opx].msecs += tvsub(&end, &fd->stats->begin);
00423     fd->stats->begin = end;     /* structure assignment */
00424 /*@=boundswrite@*/
00425 }
00426 
00429 /*@-boundsread@*/
00430 /*@unused@*/ static inline
00431 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00432         /*@globals fileSystem @*/
00433         /*@modifies *fp, fileSystem @*/
00434 {
00435     int opx;
00436     if (fd == NULL || fd->stats == NULL) return;
00437     for (opx = 0; opx < 4; opx++) {
00438         OPSTAT_t *ops = &fd->stats->ops[opx];
00439         if (ops->count <= 0) continue;
00440         switch (opx) {
00441         case FDSTAT_READ:
00442             if (msg) fprintf(fp, "%s:", msg);
00443             fprintf(fp, "%8d reads, %8ld total bytes in %d.%03d secs\n",
00444                 ops->count, (long)ops->bytes,
00445                 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00446             /*@switchbreak@*/ break;
00447         case FDSTAT_WRITE:
00448             if (msg) fprintf(fp, "%s:", msg);
00449             fprintf(fp, "%8d writes, %8ld total bytes in %d.%03d secs\n",
00450                 ops->count, (long)ops->bytes,
00451                 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00452             /*@switchbreak@*/ break;
00453         case FDSTAT_SEEK:
00454             /*@switchbreak@*/ break;
00455         case FDSTAT_CLOSE:
00456             /*@switchbreak@*/ break;
00457         }
00458     }
00459 }
00460 /*@=boundsread@*/
00461 
00464 /*@unused@*/ static inline
00465 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00466         /*@modifies fd @*/
00467 {
00468     FDSANE(fd);
00469     fd->syserrno = syserrno;
00470     /*@-assignexpose@*/
00471     fd->errcookie = errcookie;
00472     /*@=assignexpose@*/
00473 }
00474 
00477 /*@unused@*/ static inline
00478 int fdGetRdTimeoutSecs(FD_t fd)
00479         /*@*/
00480 {
00481     FDSANE(fd);
00482     return fd->rd_timeoutsecs;
00483 }
00484 
00487 /*@unused@*/ static inline
00488 long int fdGetCpioPos(FD_t fd)
00489         /*@*/
00490 {
00491     FDSANE(fd);
00492     return fd->fd_cpioPos;
00493 }
00494 
00497 /*@unused@*/ static inline
00498 void fdSetCpioPos(FD_t fd, long int cpioPos)
00499         /*@modifies fd @*/
00500 {
00501     FDSANE(fd);
00502     fd->fd_cpioPos = cpioPos;
00503 }
00504 
00507 /*@mayexit@*/ /*@unused@*/ static inline
00508 FD_t c2f(/*@null@*/ void * cookie)
00509         /*@*/
00510 {
00511     /*@-castexpose@*/
00512     FD_t fd = (FD_t) cookie;
00513     /*@=castexpose@*/
00514     FDSANE(fd);
00515     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00516 }
00517 
00521 /*@unused@*/ static inline
00522 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00523         /*@modifies fd @*/
00524 {
00525     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00526     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00527         fd->ndigests++;
00528         fddig->hashalgo = hashalgo;
00529         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00530     }
00531 }
00532 
00536 /*@unused@*/ static inline
00537 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00538         /*@modifies fd @*/
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         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00548     }
00549 }
00550 
00553 /*@unused@*/ static inline
00554 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00555                 /*@null@*/ /*@out@*/ void ** datap,
00556                 /*@null@*/ /*@out@*/ size_t * lenp,
00557                 int asAscii)
00558         /*@modifies fd, *datap, *lenp @*/
00559 {
00560     int imax = -1;
00561     int i;
00562 
00563     for (i = fd->ndigests - 1; i >= 0; i--) {
00564         FDDIGEST_t fddig = fd->digests + i;
00565         if (fddig->hashctx == NULL)
00566             continue;
00567         if (i > imax) imax = i;
00568         if (fddig->hashalgo != hashalgo)
00569             continue;
00570         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00571         fddig->hashctx = NULL;
00572         break;
00573     }
00574 /*@-boundswrite@*/
00575     if (i < 0) {
00576         if (datap) *datap = NULL;
00577         if (lenp) *lenp = 0;
00578     }
00579 /*@=boundswrite@*/
00580 
00581     fd->ndigests = imax;
00582     if (i < imax)
00583         fd->ndigests++;         /* convert index to count */
00584 }
00585 
00586 /*@-shadow@*/
00589 /*@unused@*/ static inline
00590 int fdFileno(/*@null@*/ void * cookie)
00591         /*@*/
00592 {
00593     FD_t fd;
00594     if (cookie == NULL) return -2;
00595     fd = c2f(cookie);
00596 /*@-boundsread@*/
00597     return fd->fps[0].fdno;
00598 /*@=boundsread@*/
00599 }
00600 /*@=shadow@*/
00601 
00609 int rpmioSlurp(const char * fn,
00610                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00611         /*@globals fileSystem, internalState @*/
00612         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00613 
00614 #ifdef __cplusplus
00615 }
00616 #endif
00617 
00618 #endif  /* H_RPMIO_INTERNAL */

Generated on Sun Oct 26 13:02:03 2003 for rpm by doxygen1.2.18