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     size_t nbytes;              
00072 /*@only@*/ /*@null@*/
00073     DIGEST_CTX sha1ctx;         
00074 /*@only@*/ /*@null@*/
00075     DIGEST_CTX hdrsha1ctx;      
00076 /*@only@*/ /*@null@*/
00077     void * sha1;                
00078     size_t sha1len;             
00080 /*@only@*/ /*@null@*/
00081     DIGEST_CTX md5ctx;          
00082 /*@only@*/ /*@null@*/
00083     DIGEST_CTX hdrmd5ctx;       
00084 /*@only@*/ /*@null@*/
00085     void * md5;                 
00086     size_t md5len;              
00088     /* DSA parameters. */
00089     mpbarrett p;
00090     mpbarrett q;
00091     mpnumber g;
00092     mpnumber y;
00093     mpnumber hm;
00094     mpnumber r;
00095     mpnumber s;
00096 
00097     /* RSA parameters. */
00098     rsapk rsa_pk;
00099     mpnumber m;
00100     mpnumber c;
00101     mpnumber rsahm;
00102 };
00103 
00106 typedef struct _FDSTACK_s {
00107 /*@exposed@*/
00108     FDIO_t              io;
00109 /*@dependent@*/
00110     void *              fp;
00111     int                 fdno;
00112 } FDSTACK_t;
00113 
00117 typedef enum fdOpX_e {
00118     FDSTAT_READ         = 0,    
00119     FDSTAT_WRITE        = 1,    
00120     FDSTAT_SEEK         = 2,    
00121     FDSTAT_CLOSE        = 3,    
00122     FDSTAT_DIGEST       = 4,    
00123     FDSTAT_MAX          = 5
00124 } fdOpX;
00125 
00129 typedef /*@abstract@*/ struct {
00130     struct rpmop_s      ops[FDSTAT_MAX];        
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 /*@relnull@*/
00158 
00159     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00160     ssize_t     bytesRemain;    /* ufdio: */
00161     ssize_t     contentLength;  /* ufdio: */
00162     int         persist;        /* ufdio: */
00163     int         wr_chunked;     /* ufdio: */
00164 
00165     int         syserrno;       /* last system errno encountered */
00166 /*@observer@*/
00167     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00168 
00169     FDSTAT_t    stats;          /* I/O statistics */
00170 
00171     int         ndigests;
00172 #define FDDIGEST_MAX    4
00173     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00174 
00175     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00176     unsigned int firstFree;     /* fadio: */
00177     long int    fileSize;       /* fadio: */
00178     long int    fd_cpioPos;     /* cpio: */
00179 };
00180 /*@access FD_t@*/
00181 
00182 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00183 
00184 /*@-redecl@*/
00185 /*@unchecked@*/
00186 extern int _rpmio_debug;
00187 /*@=redecl@*/
00188 
00189 /*@-redecl@*/
00190 /*@unchecked@*/
00191 extern int _av_debug;
00192 /*@=redecl@*/
00193 
00194 /*@-redecl@*/
00195 /*@unchecked@*/
00196 extern int _ftp_debug;
00197 /*@=redecl@*/
00198 
00199 /*@-redecl@*/
00200 /*@unchecked@*/
00201 extern int _dav_debug;
00202 /*@=redecl@*/
00203 
00204 #define DBG(_f, _m, _x) \
00205     /*@-modfilesys@*/ \
00206     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00207     /*@=modfilesys@*/
00208 
00209 #if defined(__LCLINT__XXX)
00210 #define DBGIO(_f, _x)
00211 #define DBGREFS(_f, _x)
00212 #else
00213 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00214 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00215 #endif
00216 
00217 #ifdef __cplusplus
00218 extern "C" {
00219 #endif
00220 
00223 int fdFgets(FD_t fd, char * buf, size_t len)
00224         /*@globals errno, fileSystem @*/
00225         /*@modifies *buf, fd, errno, fileSystem @*/;
00226 
00229 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00230                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00231         /*@globals h_errno, fileSystem, internalState @*/
00232         /*@modifies *uret, fileSystem, internalState @*/;
00233 
00236 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00237         /*@globals fileSystem, internalState @*/
00238         /*@modifies data, fileSystem, internalState @*/;
00239 
00242 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00243         /*@globals h_errno, fileSystem, internalState @*/
00244         /*@modifies fileSystem, internalState @*/;
00245 
00248 int ufdClose( /*@only@*/ void * cookie)
00249         /*@globals fileSystem, internalState @*/
00250         /*@modifies cookie, fileSystem, internalState @*/;
00251 
00254 /*@unused@*/ static inline
00255 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00256         /*@*/
00257 {
00258     FDSANE(fd);
00259 /*@-boundsread@*/
00260     return fd->fps[fd->nfps].io;
00261 /*@=boundsread@*/
00262 }
00263 
00266 /*@-nullstate@*/ /* FIX: io may be NULL */
00267 /*@unused@*/ static inline
00268 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00269         /*@modifies fd @*/
00270 {
00271     FDSANE(fd);
00272 /*@-boundswrite@*/
00273     /*@-assignexpose@*/
00274     fd->fps[fd->nfps].io = io;
00275     /*@=assignexpose@*/
00276 /*@=boundswrite@*/
00277 }
00278 /*@=nullstate@*/
00279 
00282 /*@unused@*/ static inline
00283 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00284         /*@*/
00285 {
00286     FDSANE(fd);
00287 /*@-boundsread@*/
00288     /*@+voidabstract@*/
00289     return ((FILE *)fd->fps[fd->nfps].fp);
00290     /*@=voidabstract@*/
00291 /*@=boundsread@*/
00292 }
00293 
00296 /*@unused@*/ static inline
00297 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00298         /*@*/
00299 {
00300     FDSANE(fd);
00301 /*@-boundsread@*/
00302     return fd->fps[fd->nfps].fp;
00303 /*@=boundsread@*/
00304 }
00305 
00308 /*@-nullstate@*/ /* FIX: fp may be NULL */
00309 /*@unused@*/ static inline
00310 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00311         /*@modifies fd @*/
00312 {
00313     FDSANE(fd);
00314 /*@-boundswrite@*/
00315     /*@-assignexpose@*/
00316     fd->fps[fd->nfps].fp = fp;
00317     /*@=assignexpose@*/
00318 /*@=boundswrite@*/
00319 }
00320 /*@=nullstate@*/
00321 
00324 /*@unused@*/ static inline
00325 int fdGetFdno(FD_t fd)
00326         /*@*/
00327 {
00328     FDSANE(fd);
00329 /*@-boundsread@*/
00330     return fd->fps[fd->nfps].fdno;
00331 /*@=boundsread@*/
00332 }
00333 
00336 /*@unused@*/ static inline
00337 void fdSetFdno(FD_t fd, int fdno)
00338         /*@modifies fd @*/
00339 {
00340     FDSANE(fd);
00341 /*@-boundswrite@*/
00342     fd->fps[fd->nfps].fdno = fdno;
00343 /*@=boundswrite@*/
00344 }
00345 
00348 /*@unused@*/ static inline
00349 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00350         /*@modifies fd @*/
00351 {
00352     FDSANE(fd);
00353     fd->contentLength = fd->bytesRemain = contentLength;
00354 }
00355 
00358 /*@unused@*/ static inline
00359 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00360         /*@modifies fd @*/
00361 {
00362     FDSANE(fd);
00363     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00364         return;
00365     fd->nfps++;
00366     fdSetIo(fd, io);
00367     fdSetFp(fd, fp);
00368     fdSetFdno(fd, fdno);
00369 }
00370 
00373 /*@unused@*/ static inline
00374 void fdPop(FD_t fd)
00375         /*@modifies fd @*/
00376 {
00377     FDSANE(fd);
00378     if (fd->nfps < 0) return;
00379     fdSetIo(fd, NULL);
00380     fdSetFp(fd, NULL);
00381     fdSetFdno(fd, -1);
00382     fd->nfps--;
00383 }
00384 
00387 /*@unused@*/ static inline /*@null@*/
00388 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00389         /*@*/
00390 {
00391     rpmop op = NULL;
00392 
00393 /*@-boundsread@*/
00394     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00395         op = fd->stats->ops + opx;
00396 /*@=boundsread@*/
00397     return op;
00398 }
00399 
00402 /*@unused@*/ static inline
00403 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00404         /*@globals internalState @*/
00405         /*@modifies internalState @*/
00406 {
00407     if (fd == NULL) return;
00408     if (fd->stats != NULL)
00409         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00410 }
00411 
00414 /*@unused@*/ static inline
00415 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00416         /*@globals internalState @*/
00417         /*@modifies fd, internalState @*/
00418 {
00419     if (fd == NULL) return;
00420     if (rc == -1)
00421         fd->syserrno = errno;
00422     else if (rc > 0 && fd->bytesRemain > 0)
00423         switch (opx) {
00424         case FDSTAT_READ:
00425         case FDSTAT_WRITE:
00426         fd->bytesRemain -= rc;
00427             break;
00428         default:
00429             break;
00430         }
00431     if (fd->stats != NULL)
00432         (void) rpmswExit(fdstat_op(fd, opx), rc);
00433 }
00434 
00437 /*@-boundsread@*/
00438 /*@unused@*/ static inline
00439 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00440         /*@globals fileSystem @*/
00441         /*@modifies *fp, fileSystem @*/
00442 {
00443     static int usec_scale = (1000*1000);
00444     int opx;
00445 
00446     if (fd == NULL || fd->stats == NULL) return;
00447     for (opx = 0; opx < 4; opx++) {
00448         rpmop op = &fd->stats->ops[opx];
00449         if (op->count <= 0) continue;
00450         switch (opx) {
00451         case FDSTAT_READ:
00452             if (msg) fprintf(fp, "%s:", msg);
00453             fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00454                 op->count, (long)op->bytes,
00455                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00456             /*@switchbreak@*/ break;
00457         case FDSTAT_WRITE:
00458             if (msg) fprintf(fp, "%s:", msg);
00459             fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00460                 op->count, (long)op->bytes,
00461                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00462             /*@switchbreak@*/ break;
00463         case FDSTAT_SEEK:
00464             /*@switchbreak@*/ break;
00465         case FDSTAT_CLOSE:
00466             /*@switchbreak@*/ break;
00467         }
00468     }
00469 }
00470 /*@=boundsread@*/
00471 
00474 /*@unused@*/ static inline
00475 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00476         /*@modifies fd @*/
00477 {
00478     FDSANE(fd);
00479     fd->syserrno = syserrno;
00480     /*@-assignexpose@*/
00481     fd->errcookie = errcookie;
00482     /*@=assignexpose@*/
00483 }
00484 
00487 /*@unused@*/ static inline
00488 int fdGetRdTimeoutSecs(FD_t fd)
00489         /*@*/
00490 {
00491     FDSANE(fd);
00492     return fd->rd_timeoutsecs;
00493 }
00494 
00497 /*@unused@*/ static inline
00498 long int fdGetCpioPos(FD_t fd)
00499         /*@*/
00500 {
00501     FDSANE(fd);
00502     return fd->fd_cpioPos;
00503 }
00504 
00507 /*@unused@*/ static inline
00508 void fdSetCpioPos(FD_t fd, long int cpioPos)
00509         /*@modifies fd @*/
00510 {
00511     FDSANE(fd);
00512     fd->fd_cpioPos = cpioPos;
00513 }
00514 
00517 /*@mayexit@*/ /*@unused@*/ static inline
00518 FD_t c2f(/*@null@*/ void * cookie)
00519         /*@*/
00520 {
00521     /*@-castexpose@*/
00522     FD_t fd = (FD_t) cookie;
00523     /*@=castexpose@*/
00524     FDSANE(fd);
00525     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00526 }
00527 
00531 /*@unused@*/ static inline
00532 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00533         /*@globals internalState @*/
00534         /*@modifies fd, internalState @*/
00535 {
00536     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00537     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00538         fd->ndigests++;
00539         fddig->hashalgo = hashalgo;
00540         fdstat_enter(fd, FDSTAT_DIGEST);
00541         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00542         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00543     }
00544 }
00545 
00549 /*@unused@*/ static inline
00550 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00551         /*@globals internalState @*/
00552         /*@modifies fd, internalState @*/
00553 {
00554     int i;
00555 
00556     if (buf != NULL && buflen > 0)
00557     for (i = fd->ndigests - 1; i >= 0; i--) {
00558         FDDIGEST_t fddig = fd->digests + i;
00559         if (fddig->hashctx == NULL)
00560             continue;
00561         fdstat_enter(fd, FDSTAT_DIGEST);
00562         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00563         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00564     }
00565 }
00566 
00569 /*@unused@*/ static inline
00570 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00571                 /*@null@*/ /*@out@*/ void ** datap,
00572                 /*@null@*/ /*@out@*/ size_t * lenp,
00573                 int asAscii)
00574         /*@globals internalState @*/
00575         /*@modifies fd, *datap, *lenp, internalState @*/
00576 {
00577     int imax = -1;
00578     int i;
00579 
00580     for (i = fd->ndigests - 1; i >= 0; i--) {
00581         FDDIGEST_t fddig = fd->digests + i;
00582         if (fddig->hashctx == NULL)
00583             continue;
00584         if (i > imax) imax = i;
00585         if (fddig->hashalgo != hashalgo)
00586             continue;
00587         fdstat_enter(fd, FDSTAT_DIGEST);
00588         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00589         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00590         fddig->hashctx = NULL;
00591         break;
00592     }
00593 /*@-boundswrite@*/
00594     if (i < 0) {
00595         if (datap) *datap = NULL;
00596         if (lenp) *lenp = 0;
00597     }
00598 /*@=boundswrite@*/
00599 
00600     fd->ndigests = imax;
00601     if (i < imax)
00602         fd->ndigests++;         /* convert index to count */
00603 }
00604 
00605 /*@-shadow@*/
00608 /*@unused@*/ static inline
00609 int fdFileno(/*@null@*/ void * cookie)
00610         /*@*/
00611 {
00612     FD_t fd;
00613     if (cookie == NULL) return -2;
00614     fd = c2f(cookie);
00615 /*@-boundsread@*/
00616     return fd->fps[0].fdno;
00617 /*@=boundsread@*/
00618 }
00619 /*@=shadow@*/
00620 
00628 int rpmioSlurp(const char * fn,
00629                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00630         /*@globals h_errno, fileSystem, internalState @*/
00631         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00632 
00633 #ifdef __cplusplus
00634 }
00635 #endif
00636 
00637 #endif  /* H_RPMIO_INTERNAL */

Generated on Mon Mar 5 14:31:00 2007 for rpm by  doxygen 1.5.1