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 <rpmpgp.h>
00013 #include <rpmsw.h>
00014 #include <base64.h>
00015 
00016 #include "nss.h"
00017 #include "sechash.h"
00018 #include "keyhi.h"
00019 #include "cryptohi.h"
00020 
00024 struct pgpDigParams_s {
00025 /*@only@*/ /*@null@*/
00026     const char * userid;
00027 /*@only@*/ /*@null@*/
00028     const byte * hash;
00029     const char * params[4];
00030     byte tag;
00031 
00032     byte version;               
00033     byte time[4];               
00034     byte pubkey_algo;           
00036     byte hash_algo;
00037     byte sigtype;
00038     byte hashlen;
00039     byte signhash16[2];
00040     byte signid[8];
00041     byte saved;
00042 #define PGPDIG_SAVED_TIME       (1 << 0)
00043 #define PGPDIG_SAVED_ID         (1 << 1)
00044 
00045 };
00046 
00050 struct pgpDig_s {
00051     struct pgpDigParams_s signature;
00052     struct pgpDigParams_s pubkey;
00053 
00054     size_t nbytes;              
00056 /*@only@*/ /*@null@*/
00057     DIGEST_CTX sha1ctx;         
00058 /*@only@*/ /*@null@*/
00059     DIGEST_CTX hdrsha1ctx;      
00060 /*@only@*/ /*@null@*/
00061     void * sha1;                
00062     size_t sha1len;             
00064 /*@only@*/ /*@null@*/
00065     DIGEST_CTX md5ctx;          
00066 /*@only@*/ /*@null@*/
00067     DIGEST_CTX hdrmd5ctx;       
00068 /*@only@*/ /*@null@*/
00069     void * md5;                 
00070     size_t md5len;              
00072     /* DSA parameters */
00073     SECKEYPublicKey *dsa;
00074     SECItem *dsasig;
00075 
00076     /* RSA parameters */
00077     SECKEYPublicKey *rsa;
00078     SECItem *rsasig;
00079 };
00080 
00083 typedef struct _FDSTACK_s {
00084 /*@exposed@*/
00085     FDIO_t              io;
00086 /*@dependent@*/
00087     void *              fp;
00088     int                 fdno;
00089 } FDSTACK_t;
00090 
00094 typedef enum fdOpX_e {
00095     FDSTAT_READ         = 0,    
00096     FDSTAT_WRITE        = 1,    
00097     FDSTAT_SEEK         = 2,    
00098     FDSTAT_CLOSE        = 3,    
00099     FDSTAT_DIGEST       = 4,    
00100     FDSTAT_MAX          = 5
00101 } fdOpX;
00102 
00106 typedef /*@abstract@*/ struct {
00107     struct rpmop_s      ops[FDSTAT_MAX];        
00108 } * FDSTAT_t;
00109 
00112 typedef struct _FDDIGEST_s {
00113     pgpHashAlgo         hashalgo;
00114     DIGEST_CTX          hashctx;
00115 } * FDDIGEST_t;
00116 
00120 struct _FD_s {
00121 /*@refs@*/
00122     int         nrefs;
00123     int         flags;
00124 #define RPMIO_DEBUG_IO          0x40000000
00125 #define RPMIO_DEBUG_REFS        0x20000000
00126     int         magic;
00127 #define FDMAGIC                 0x04463138
00128     int         nfps;
00129     FDSTACK_t   fps[8];
00130     int         urlType;        /* ufdio: */
00131 
00132 /*@dependent@*/
00133     void *      url;            /* ufdio: URL info */
00134 /*@relnull@*/
00135     void *      req;            /* ufdio: HTTP request */
00136 
00137     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00138     ssize_t     bytesRemain;    /* ufdio: */
00139     ssize_t     contentLength;  /* ufdio: */
00140     int         persist;        /* ufdio: */
00141     int         wr_chunked;     /* ufdio: */
00142 
00143     int         syserrno;       /* last system errno encountered */
00144 /*@observer@*/
00145     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00146 
00147     FDSTAT_t    stats;          /* I/O statistics */
00148 
00149     int         ndigests;
00150 #define FDDIGEST_MAX    4
00151     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00152 
00153     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00154     unsigned int firstFree;     /* fadio: */
00155     long int    fileSize;       /* fadio: */
00156     long int    fd_cpioPos;     /* cpio: */
00157 };
00158 /*@access FD_t@*/
00159 
00160 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00161 
00162 /*@-redecl@*/
00163 /*@unchecked@*/
00164 extern int _rpmio_debug;
00165 /*@=redecl@*/
00166 
00167 /*@-redecl@*/
00168 /*@unchecked@*/
00169 extern int _av_debug;
00170 /*@=redecl@*/
00171 
00172 /*@-redecl@*/
00173 /*@unchecked@*/
00174 extern int _ftp_debug;
00175 /*@=redecl@*/
00176 
00177 /*@-redecl@*/
00178 /*@unchecked@*/
00179 extern int _dav_debug;
00180 /*@=redecl@*/
00181 
00182 #define DBG(_f, _m, _x) \
00183     /*@-modfilesys@*/ \
00184     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00185     /*@=modfilesys@*/
00186 
00187 #if defined(__LCLINT__XXX)
00188 #define DBGIO(_f, _x)
00189 #define DBGREFS(_f, _x)
00190 #else
00191 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00192 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00193 #endif
00194 
00195 #ifdef __cplusplus
00196 extern "C" {
00197 #endif
00198 
00201 int fdFgets(FD_t fd, char * buf, size_t len)
00202         /*@globals errno, fileSystem @*/
00203         /*@modifies *buf, fd, errno, fileSystem @*/;
00204 
00207 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00208                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00209         /*@globals h_errno, fileSystem, internalState @*/
00210         /*@modifies *uret, fileSystem, internalState @*/;
00211 
00214 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00215         /*@globals fileSystem, internalState @*/
00216         /*@modifies data, fileSystem, internalState @*/;
00217 
00220 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00221         /*@globals h_errno, fileSystem, internalState @*/
00222         /*@modifies fileSystem, internalState @*/;
00223 
00226 int ufdClose( /*@only@*/ void * cookie)
00227         /*@globals fileSystem, internalState @*/
00228         /*@modifies cookie, fileSystem, internalState @*/;
00229 
00232 /*@unused@*/ static inline
00233 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00234         /*@*/
00235 {
00236     FDSANE(fd);
00237 /*@-boundsread@*/
00238     return fd->fps[fd->nfps].io;
00239 /*@=boundsread@*/
00240 }
00241 
00244 /*@-nullstate@*/ /* FIX: io may be NULL */
00245 /*@unused@*/ static inline
00246 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00247         /*@modifies fd @*/
00248 {
00249     FDSANE(fd);
00250 /*@-boundswrite@*/
00251     /*@-assignexpose@*/
00252     fd->fps[fd->nfps].io = io;
00253     /*@=assignexpose@*/
00254 /*@=boundswrite@*/
00255 }
00256 /*@=nullstate@*/
00257 
00260 /*@unused@*/ static inline
00261 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00262         /*@*/
00263 {
00264     FDSANE(fd);
00265 /*@-boundsread@*/
00266     /*@+voidabstract@*/
00267     return ((FILE *)fd->fps[fd->nfps].fp);
00268     /*@=voidabstract@*/
00269 /*@=boundsread@*/
00270 }
00271 
00274 /*@unused@*/ static inline
00275 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00276         /*@*/
00277 {
00278     FDSANE(fd);
00279 /*@-boundsread@*/
00280     return fd->fps[fd->nfps].fp;
00281 /*@=boundsread@*/
00282 }
00283 
00286 /*@-nullstate@*/ /* FIX: fp may be NULL */
00287 /*@unused@*/ static inline
00288 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00289         /*@modifies fd @*/
00290 {
00291     FDSANE(fd);
00292 /*@-boundswrite@*/
00293     /*@-assignexpose@*/
00294     fd->fps[fd->nfps].fp = fp;
00295     /*@=assignexpose@*/
00296 /*@=boundswrite@*/
00297 }
00298 /*@=nullstate@*/
00299 
00302 /*@unused@*/ static inline
00303 int fdGetFdno(FD_t fd)
00304         /*@*/
00305 {
00306     FDSANE(fd);
00307 /*@-boundsread@*/
00308     return fd->fps[fd->nfps].fdno;
00309 /*@=boundsread@*/
00310 }
00311 
00314 /*@unused@*/ static inline
00315 void fdSetFdno(FD_t fd, int fdno)
00316         /*@modifies fd @*/
00317 {
00318     FDSANE(fd);
00319 /*@-boundswrite@*/
00320     fd->fps[fd->nfps].fdno = fdno;
00321 /*@=boundswrite@*/
00322 }
00323 
00326 /*@unused@*/ static inline
00327 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00328         /*@modifies fd @*/
00329 {
00330     FDSANE(fd);
00331     fd->contentLength = fd->bytesRemain = contentLength;
00332 }
00333 
00336 /*@unused@*/ static inline
00337 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00338         /*@modifies fd @*/
00339 {
00340     FDSANE(fd);
00341     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00342         return;
00343     fd->nfps++;
00344     fdSetIo(fd, io);
00345     fdSetFp(fd, fp);
00346     fdSetFdno(fd, fdno);
00347 }
00348 
00351 /*@unused@*/ static inline
00352 void fdPop(FD_t fd)
00353         /*@modifies fd @*/
00354 {
00355     FDSANE(fd);
00356     if (fd->nfps < 0) return;
00357     fdSetIo(fd, NULL);
00358     fdSetFp(fd, NULL);
00359     fdSetFdno(fd, -1);
00360     fd->nfps--;
00361 }
00362 
00365 /*@unused@*/ static inline /*@null@*/
00366 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00367         /*@*/
00368 {
00369     rpmop op = NULL;
00370 
00371 /*@-boundsread@*/
00372     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00373         op = fd->stats->ops + opx;
00374 /*@=boundsread@*/
00375     return op;
00376 }
00377 
00380 /*@unused@*/ static inline
00381 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00382         /*@globals internalState @*/
00383         /*@modifies internalState @*/
00384 {
00385     if (fd == NULL) return;
00386     if (fd->stats != NULL)
00387         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00388 }
00389 
00392 /*@unused@*/ static inline
00393 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00394         /*@globals internalState @*/
00395         /*@modifies fd, internalState @*/
00396 {
00397     if (fd == NULL) return;
00398     if (rc == -1)
00399         fd->syserrno = errno;
00400     else if (rc > 0 && fd->bytesRemain > 0)
00401         switch (opx) {
00402         case FDSTAT_READ:
00403         case FDSTAT_WRITE:
00404         fd->bytesRemain -= rc;
00405             break;
00406         default:
00407             break;
00408         }
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 Sat Sep 5 17:48:09 2009 for rpm by  doxygen 1.4.7