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 #include <rpmsw.h>
00015 
00016 /* Drag in the beecrypt includes. */
00017 #include <beecrypt.h>
00018 #include <base64.h>
00019 #include <dsa.h>
00020 #include <endianness.h>
00021 #include <md5.h>
00022 #include <mp.h>
00023 #include <rsa.h>
00024 #include <rsapk.h>
00025 #include <sha1.h>
00026 
00030 struct pgpDigParams_s {
00031 /*@only@*/ /*@null@*/
00032     const char * userid;
00033 /*@only@*/ /*@null@*/
00034     const byte * hash;
00035     const char * params[4];
00036     byte tag;
00037 
00038     byte version;               
00039     byte time[4];               
00040     byte pubkey_algo;           
00042     byte hash_algo;
00043     byte sigtype;
00044     byte hashlen;
00045     byte signhash16[2];
00046     byte signid[8];
00047     byte saved;
00048 #define PGPDIG_SAVED_TIME       (1 << 0)
00049 #define PGPDIG_SAVED_ID         (1 << 1)
00050 
00051 };
00052 
00056 struct pgpDig_s {
00057     struct pgpDigParams_s signature;
00058     struct pgpDigParams_s pubkey;
00059 
00060     size_t nbytes;              
00062 /*@only@*/ /*@null@*/
00063     DIGEST_CTX sha1ctx;         
00064 /*@only@*/ /*@null@*/
00065     DIGEST_CTX hdrsha1ctx;      
00066 /*@only@*/ /*@null@*/
00067     void * sha1;                
00068     size_t sha1len;             
00070 /*@only@*/ /*@null@*/
00071     DIGEST_CTX md5ctx;          
00072 #ifdef  NOTYET
00073 /*@only@*/ /*@null@*/
00074     DIGEST_CTX hdrmd5ctx;       
00075 #endif
00076 /*@only@*/ /*@null@*/
00077     void * md5;                 
00078     size_t md5len;              
00080     /* DSA parameters. */
00081     mpbarrett p;
00082     mpbarrett q;
00083     mpnumber g;
00084     mpnumber y;
00085     mpnumber hm;
00086     mpnumber r;
00087     mpnumber s;
00088 
00089     /* RSA parameters. */
00090     rsapk rsa_pk;
00091     mpnumber m;
00092     mpnumber c;
00093     mpnumber rsahm;
00094 };
00095 
00098 typedef struct _FDSTACK_s {
00099 /*@exposed@*/
00100     FDIO_t              io;
00101 /*@dependent@*/
00102     void *              fp;
00103     int                 fdno;
00104 } FDSTACK_t;
00105 
00109 typedef enum fdOpX_e {
00110     FDSTAT_READ         = 0,    
00111     FDSTAT_WRITE        = 1,    
00112     FDSTAT_SEEK         = 2,    
00113     FDSTAT_CLOSE        = 3,    
00114     FDSTAT_DIGEST       = 4,    
00115     FDSTAT_MAX          = 5
00116 } fdOpX;
00117 
00121 typedef /*@abstract@*/ struct {
00122     struct rpmop_s      ops[FDSTAT_MAX];        
00123 } * FDSTAT_t;
00124 
00127 typedef struct _FDDIGEST_s {
00128     pgpHashAlgo         hashalgo;
00129     DIGEST_CTX          hashctx;
00130 } * FDDIGEST_t;
00131 
00135 struct _FD_s {
00136 /*@refs@*/
00137     int         nrefs;
00138     int         flags;
00139 #define RPMIO_DEBUG_IO          0x40000000
00140 #define RPMIO_DEBUG_REFS        0x20000000
00141     int         magic;
00142 #define FDMAGIC                 0x04463138
00143     int         nfps;
00144     FDSTACK_t   fps[8];
00145     int         urlType;        /* ufdio: */
00146 
00147 /*@dependent@*/
00148     void *      url;            /* ufdio: URL info */
00149     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00150     ssize_t     bytesRemain;    /* ufdio: */
00151     ssize_t     contentLength;  /* ufdio: */
00152     int         persist;        /* ufdio: */
00153     int         wr_chunked;     /* ufdio: */
00154 
00155     int         syserrno;       /* last system errno encountered */
00156 /*@observer@*/
00157     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00158 
00159     FDSTAT_t    stats;          /* I/O statistics */
00160 
00161     int         ndigests;
00162 #define FDDIGEST_MAX    4
00163     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00164 
00165     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00166     unsigned int firstFree;     /* fadio: */
00167     long int    fileSize;       /* fadio: */
00168     long int    fd_cpioPos;     /* cpio: */
00169 };
00170 /*@access FD_t@*/
00171 
00172 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00173 
00174 /*@-redecl@*/
00175 /*@unchecked@*/
00176 extern int _rpmio_debug;
00177 /*@=redecl@*/
00178 
00179 /*@-redecl@*/
00180 /*@unchecked@*/
00181 extern int _ftp_debug;
00182 /*@=redecl@*/
00183 
00184 #define DBG(_f, _m, _x) \
00185     /*@-modfilesys@*/ \
00186     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00187     /*@=modfilesys@*/
00188 
00189 #if defined(__LCLINT__XXX)
00190 #define DBGIO(_f, _x)
00191 #define DBGREFS(_f, _x)
00192 #else
00193 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00194 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00195 #endif
00196 
00197 #ifdef __cplusplus
00198 extern "C" {
00199 #endif
00200 
00203 int fdFgets(FD_t fd, char * buf, size_t len)
00204         /*@globals errno, fileSystem @*/
00205         /*@modifies *buf, fd, errno, fileSystem @*/;
00206 
00209 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00210                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00211         /*@globals h_errno, fileSystem, internalState @*/
00212         /*@modifies *uret, fileSystem, internalState @*/;
00213 
00216 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00217         /*@globals fileSystem, internalState @*/
00218         /*@modifies data, fileSystem, internalState @*/;
00219 
00222 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00223         /*@globals h_errno, fileSystem, internalState @*/
00224         /*@modifies fileSystem, internalState @*/;
00225 
00228 int ufdClose( /*@only@*/ void * cookie)
00229         /*@globals fileSystem, internalState @*/
00230         /*@modifies cookie, fileSystem, internalState @*/;
00231 
00234 /*@unused@*/ static inline
00235 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00236         /*@*/
00237 {
00238     FDSANE(fd);
00239 /*@-boundsread@*/
00240     return fd->fps[fd->nfps].io;
00241 /*@=boundsread@*/
00242 }
00243 
00246 /*@-nullstate@*/ /* FIX: io may be NULL */
00247 /*@unused@*/ static inline
00248 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00249         /*@modifies fd @*/
00250 {
00251     FDSANE(fd);
00252 /*@-boundswrite@*/
00253     /*@-assignexpose@*/
00254     fd->fps[fd->nfps].io = io;
00255     /*@=assignexpose@*/
00256 /*@=boundswrite@*/
00257 }
00258 /*@=nullstate@*/
00259 
00262 /*@unused@*/ static inline
00263 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00264         /*@*/
00265 {
00266     FDSANE(fd);
00267 /*@-boundsread@*/
00268     /*@+voidabstract@*/
00269     return ((FILE *)fd->fps[fd->nfps].fp);
00270     /*@=voidabstract@*/
00271 /*@=boundsread@*/
00272 }
00273 
00276 /*@unused@*/ static inline
00277 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00278         /*@*/
00279 {
00280     FDSANE(fd);
00281 /*@-boundsread@*/
00282     return fd->fps[fd->nfps].fp;
00283 /*@=boundsread@*/
00284 }
00285 
00288 /*@-nullstate@*/ /* FIX: fp may be NULL */
00289 /*@unused@*/ static inline
00290 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00291         /*@modifies fd @*/
00292 {
00293     FDSANE(fd);
00294 /*@-boundswrite@*/
00295     /*@-assignexpose@*/
00296     fd->fps[fd->nfps].fp = fp;
00297     /*@=assignexpose@*/
00298 /*@=boundswrite@*/
00299 }
00300 /*@=nullstate@*/
00301 
00304 /*@unused@*/ static inline
00305 int fdGetFdno(FD_t fd)
00306         /*@*/
00307 {
00308     FDSANE(fd);
00309 /*@-boundsread@*/
00310     return fd->fps[fd->nfps].fdno;
00311 /*@=boundsread@*/
00312 }
00313 
00316 /*@unused@*/ static inline
00317 void fdSetFdno(FD_t fd, int fdno)
00318         /*@modifies fd @*/
00319 {
00320     FDSANE(fd);
00321 /*@-boundswrite@*/
00322     fd->fps[fd->nfps].fdno = fdno;
00323 /*@=boundswrite@*/
00324 }
00325 
00328 /*@unused@*/ static inline
00329 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00330         /*@modifies fd @*/
00331 {
00332     FDSANE(fd);
00333     fd->contentLength = fd->bytesRemain = contentLength;
00334 }
00335 
00338 /*@unused@*/ static inline
00339 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00340         /*@modifies fd @*/
00341 {
00342     FDSANE(fd);
00343     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00344         return;
00345     fd->nfps++;
00346     fdSetIo(fd, io);
00347     fdSetFp(fd, fp);
00348     fdSetFdno(fd, fdno);
00349 }
00350 
00353 /*@unused@*/ static inline
00354 void fdPop(FD_t fd)
00355         /*@modifies fd @*/
00356 {
00357     FDSANE(fd);
00358     if (fd->nfps < 0) return;
00359     fdSetIo(fd, NULL);
00360     fdSetFp(fd, NULL);
00361     fdSetFdno(fd, -1);
00362     fd->nfps--;
00363 }
00364 
00367 /*@unused@*/ static inline /*@null@*/
00368 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00369         /*@*/
00370 {
00371     rpmop op = NULL;
00372 
00373 /*@-boundsread@*/
00374     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00375         op = fd->stats->ops + opx;
00376 /*@=boundsread@*/
00377     return op;
00378 }
00379 
00382 /*@unused@*/ static inline
00383 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00384         /*@globals internalState @*/
00385         /*@modifies internalState @*/
00386 {
00387     if (fd == NULL) return;
00388     if (fd->stats != NULL)
00389         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00390 }
00391 
00394 /*@unused@*/ static inline
00395 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00396         /*@globals internalState @*/
00397         /*@modifies fd, internalState @*/
00398 {
00399     if (fd == NULL) return;
00400     if (rc == -1)
00401         fd->syserrno = errno;
00402     else if (rc > 0 && fd->bytesRemain > 0)
00403         fd->bytesRemain -= rc;
00404     if (fd->stats != NULL)
00405         (void) rpmswExit(fdstat_op(fd, opx), rc);
00406 }
00407 
00410 /*@-boundsread@*/
00411 /*@unused@*/ static inline
00412 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00413         /*@globals fileSystem @*/
00414         /*@modifies *fp, fileSystem @*/
00415 {
00416     static int usec_scale = (1000*1000);
00417     int opx;
00418 
00419     if (fd == NULL || fd->stats == NULL) return;
00420     for (opx = 0; opx < 4; opx++) {
00421         rpmop op = &fd->stats->ops[opx];
00422         if (op->count <= 0) continue;
00423         switch (opx) {
00424         case FDSTAT_READ:
00425             if (msg) fprintf(fp, "%s:", msg);
00426             fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00427                 op->count, (long)op->bytes,
00428                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00429             /*@switchbreak@*/ break;
00430         case FDSTAT_WRITE:
00431             if (msg) fprintf(fp, "%s:", msg);
00432             fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00433                 op->count, (long)op->bytes,
00434                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00435             /*@switchbreak@*/ break;
00436         case FDSTAT_SEEK:
00437             /*@switchbreak@*/ break;
00438         case FDSTAT_CLOSE:
00439             /*@switchbreak@*/ break;
00440         }
00441     }
00442 }
00443 /*@=boundsread@*/
00444 
00447 /*@unused@*/ static inline
00448 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00449         /*@modifies fd @*/
00450 {
00451     FDSANE(fd);
00452     fd->syserrno = syserrno;
00453     /*@-assignexpose@*/
00454     fd->errcookie = errcookie;
00455     /*@=assignexpose@*/
00456 }
00457 
00460 /*@unused@*/ static inline
00461 int fdGetRdTimeoutSecs(FD_t fd)
00462         /*@*/
00463 {
00464     FDSANE(fd);
00465     return fd->rd_timeoutsecs;
00466 }
00467 
00470 /*@unused@*/ static inline
00471 long int fdGetCpioPos(FD_t fd)
00472         /*@*/
00473 {
00474     FDSANE(fd);
00475     return fd->fd_cpioPos;
00476 }
00477 
00480 /*@unused@*/ static inline
00481 void fdSetCpioPos(FD_t fd, long int cpioPos)
00482         /*@modifies fd @*/
00483 {
00484     FDSANE(fd);
00485     fd->fd_cpioPos = cpioPos;
00486 }
00487 
00490 /*@mayexit@*/ /*@unused@*/ static inline
00491 FD_t c2f(/*@null@*/ void * cookie)
00492         /*@*/
00493 {
00494     /*@-castexpose@*/
00495     FD_t fd = (FD_t) cookie;
00496     /*@=castexpose@*/
00497     FDSANE(fd);
00498     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00499 }
00500 
00504 /*@unused@*/ static inline
00505 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00506         /*@globals internalState @*/
00507         /*@modifies fd, internalState @*/
00508 {
00509     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00510     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00511         fd->ndigests++;
00512         fddig->hashalgo = hashalgo;
00513         fdstat_enter(fd, FDSTAT_DIGEST);
00514         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00515         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00516     }
00517 }
00518 
00522 /*@unused@*/ static inline
00523 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00524         /*@globals internalState @*/
00525         /*@modifies fd, internalState @*/
00526 {
00527     int i;
00528 
00529     if (buf != NULL && buflen > 0)
00530     for (i = fd->ndigests - 1; i >= 0; i--) {
00531         FDDIGEST_t fddig = fd->digests + i;
00532         if (fddig->hashctx == NULL)
00533             continue;
00534         fdstat_enter(fd, FDSTAT_DIGEST);
00535         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00536         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00537     }
00538 }
00539 
00542 /*@unused@*/ static inline
00543 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00544                 /*@null@*/ /*@out@*/ void ** datap,
00545                 /*@null@*/ /*@out@*/ size_t * lenp,
00546                 int asAscii)
00547         /*@globals internalState @*/
00548         /*@modifies fd, *datap, *lenp, internalState @*/
00549 {
00550     int imax = -1;
00551     int i;
00552 
00553     for (i = fd->ndigests - 1; i >= 0; i--) {
00554         FDDIGEST_t fddig = fd->digests + i;
00555         if (fddig->hashctx == NULL)
00556             continue;
00557         if (i > imax) imax = i;
00558         if (fddig->hashalgo != hashalgo)
00559             continue;
00560         fdstat_enter(fd, FDSTAT_DIGEST);
00561         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00562         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00563         fddig->hashctx = NULL;
00564         break;
00565     }
00566 /*@-boundswrite@*/
00567     if (i < 0) {
00568         if (datap) *datap = NULL;
00569         if (lenp) *lenp = 0;
00570     }
00571 /*@=boundswrite@*/
00572 
00573     fd->ndigests = imax;
00574     if (i < imax)
00575         fd->ndigests++;         /* convert index to count */
00576 }
00577 
00578 /*@-shadow@*/
00581 /*@unused@*/ static inline
00582 int fdFileno(/*@null@*/ void * cookie)
00583         /*@*/
00584 {
00585     FD_t fd;
00586     if (cookie == NULL) return -2;
00587     fd = c2f(cookie);
00588 /*@-boundsread@*/
00589     return fd->fps[0].fdno;
00590 /*@=boundsread@*/
00591 }
00592 /*@=shadow@*/
00593 
00601 int rpmioSlurp(const char * fn,
00602                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00603         /*@globals h_errno, fileSystem, internalState @*/
00604         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00605 
00606 #ifdef __cplusplus
00607 }
00608 #endif
00609 
00610 #endif  /* H_RPMIO_INTERNAL */

Generated on Fri Aug 20 04:04:21 2004 for rpm by doxygen 1.3.5