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

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         fd->bytesRemain -= rc;
00409     if (fd->stats != NULL)
00410         (void) rpmswExit(fdstat_op(fd, opx), rc);
00411 }
00412 
00415 /*@-boundsread@*/
00416 /*@unused@*/ static inline
00417 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00418         /*@globals fileSystem @*/
00419         /*@modifies *fp, fileSystem @*/
00420 {
00421     static int usec_scale = (1000*1000);
00422     int opx;
00423 
00424     if (fd == NULL || fd->stats == NULL) return;
00425     for (opx = 0; opx < 4; opx++) {
00426         rpmop op = &fd->stats->ops[opx];
00427         if (op->count <= 0) continue;
00428         switch (opx) {
00429         case FDSTAT_READ:
00430             if (msg) fprintf(fp, "%s:", msg);
00431             fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00432                 op->count, (long)op->bytes,
00433                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00434             /*@switchbreak@*/ break;
00435         case FDSTAT_WRITE:
00436             if (msg) fprintf(fp, "%s:", msg);
00437             fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00438                 op->count, (long)op->bytes,
00439                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00440             /*@switchbreak@*/ break;
00441         case FDSTAT_SEEK:
00442             /*@switchbreak@*/ break;
00443         case FDSTAT_CLOSE:
00444             /*@switchbreak@*/ break;
00445         }
00446     }
00447 }
00448 /*@=boundsread@*/
00449 
00452 /*@unused@*/ static inline
00453 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00454         /*@modifies fd @*/
00455 {
00456     FDSANE(fd);
00457     fd->syserrno = syserrno;
00458     /*@-assignexpose@*/
00459     fd->errcookie = errcookie;
00460     /*@=assignexpose@*/
00461 }
00462 
00465 /*@unused@*/ static inline
00466 int fdGetRdTimeoutSecs(FD_t fd)
00467         /*@*/
00468 {
00469     FDSANE(fd);
00470     return fd->rd_timeoutsecs;
00471 }
00472 
00475 /*@unused@*/ static inline
00476 long int fdGetCpioPos(FD_t fd)
00477         /*@*/
00478 {
00479     FDSANE(fd);
00480     return fd->fd_cpioPos;
00481 }
00482 
00485 /*@unused@*/ static inline
00486 void fdSetCpioPos(FD_t fd, long int cpioPos)
00487         /*@modifies fd @*/
00488 {
00489     FDSANE(fd);
00490     fd->fd_cpioPos = cpioPos;
00491 }
00492 
00495 /*@mayexit@*/ /*@unused@*/ static inline
00496 FD_t c2f(/*@null@*/ void * cookie)
00497         /*@*/
00498 {
00499     /*@-castexpose@*/
00500     FD_t fd = (FD_t) cookie;
00501     /*@=castexpose@*/
00502     FDSANE(fd);
00503     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00504 }
00505 
00509 /*@unused@*/ static inline
00510 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00511         /*@globals internalState @*/
00512         /*@modifies fd, internalState @*/
00513 {
00514     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00515     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00516         fd->ndigests++;
00517         fddig->hashalgo = hashalgo;
00518         fdstat_enter(fd, FDSTAT_DIGEST);
00519         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00520         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00521     }
00522 }
00523 
00527 /*@unused@*/ static inline
00528 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00529         /*@globals internalState @*/
00530         /*@modifies fd, internalState @*/
00531 {
00532     int i;
00533 
00534     if (buf != NULL && buflen > 0)
00535     for (i = fd->ndigests - 1; i >= 0; i--) {
00536         FDDIGEST_t fddig = fd->digests + i;
00537         if (fddig->hashctx == NULL)
00538             continue;
00539         fdstat_enter(fd, FDSTAT_DIGEST);
00540         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00541         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00542     }
00543 }
00544 
00547 /*@unused@*/ static inline
00548 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00549                 /*@null@*/ /*@out@*/ void ** datap,
00550                 /*@null@*/ /*@out@*/ size_t * lenp,
00551                 int asAscii)
00552         /*@globals internalState @*/
00553         /*@modifies fd, *datap, *lenp, internalState @*/
00554 {
00555     int imax = -1;
00556     int i;
00557 
00558     for (i = fd->ndigests - 1; i >= 0; i--) {
00559         FDDIGEST_t fddig = fd->digests + i;
00560         if (fddig->hashctx == NULL)
00561             continue;
00562         if (i > imax) imax = i;
00563         if (fddig->hashalgo != hashalgo)
00564             continue;
00565         fdstat_enter(fd, FDSTAT_DIGEST);
00566         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00567         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00568         fddig->hashctx = NULL;
00569         break;
00570     }
00571 /*@-boundswrite@*/
00572     if (i < 0) {
00573         if (datap) *datap = NULL;
00574         if (lenp) *lenp = 0;
00575     }
00576 /*@=boundswrite@*/
00577 
00578     fd->ndigests = imax;
00579     if (i < imax)
00580         fd->ndigests++;         /* convert index to count */
00581 }
00582 
00583 /*@-shadow@*/
00586 /*@unused@*/ static inline
00587 int fdFileno(/*@null@*/ void * cookie)
00588         /*@*/
00589 {
00590     FD_t fd;
00591     if (cookie == NULL) return -2;
00592     fd = c2f(cookie);
00593 /*@-boundsread@*/
00594     return fd->fps[0].fdno;
00595 /*@=boundsread@*/
00596 }
00597 /*@=shadow@*/
00598 
00606 int rpmioSlurp(const char * fn,
00607                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00608         /*@globals h_errno, fileSystem, internalState @*/
00609         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00610 
00611 #ifdef __cplusplus
00612 }
00613 #endif
00614 
00615 #endif  /* H_RPMIO_INTERNAL */

Generated on Wed Dec 28 16:43:13 2005 for rpm by  doxygen 1.3.9.1