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/types.h>
    00013 #include <rpmpgp.h>
    00014
    00015 /* Drag in the beecrypt includes. */
    00016 #include <beecrypt/beecrypt.h>
    00017 #include <beecrypt/base64.h>
    00018 #include <beecrypt/dsa.h>
    00019 #include <beecrypt/endianness.h>
    00020 #include <beecrypt/md5.h>
    00021 #include <beecrypt/mp32.h>
    00022 #include <beecrypt/rsa.h>
    00023 #include <beecrypt/rsapk.h>
    00024 #include <beecrypt/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     mp32barrett p;
    00081     mp32barrett q;
    00082     mp32number g;
    00083     mp32number y;
    00084     mp32number hm;
    00085     mp32number r;
    00086     mp32number s;
    00087
    00088     /* RSA parameters. */
    00089     rsapk rsa_pk;
    00090     mp32number m;
    00091     mp32number c;
    00092     mp32number rsahm;
    00093 };
    00094
    00097 typedef struct _FDSTACK_s {
    00098     FDIO_t              io;
    00099 /*@dependent@*/ void *  fp;
    00100     int                 fdno;
    00101 } FDSTACK_t;
    00102
    00106 typedef struct {
    00107     int                 count;
    00108     off_t               bytes;
    00109     time_t              msecs;
    00110 } OPSTAT_t;
    00111
    00115 enum FDSTAT_e {
    00116     FDSTAT_READ         = 0,
    00117     FDSTAT_WRITE        = 1,
    00118     FDSTAT_SEEK         = 2,
    00119     FDSTAT_CLOSE        = 3
    00120 };
    00121
    00125 typedef /*@abstract@*/ struct {
    00126     struct timeval      create;
    00127     struct timeval      begin;
    00128     OPSTAT_t            ops[4];
    00129 } * FDSTAT_t;
    00130
    00133 typedef struct _FDDIGEST_s {
    00134     pgpHashAlgo         hashalgo;
    00135     DIGEST_CTX          hashctx;
    00136 } * FDDIGEST_t;
    00137
    00141 struct _FD_s {
    00142 /*@refs@*/ 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@*/ void *  url;    /* ufdio: URL info */
    00153     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
    00154     ssize_t     bytesRemain;    /* ufdio: */
    00155     ssize_t     contentLength;  /* ufdio: */
    00156     int         persist;        /* ufdio: */
    00157     int         wr_chunked;     /* ufdio: */
    00158
    00159     int         syserrno;       /* last system errno encountered */
    00160 /*@observer@*/ const void *errcookie;   /* gzdio/bzdio/ufdio: */
    00161
    00162     FDSTAT_t    stats;          /* I/O statistics */
    00163
    00164     int         ndigests;
    00165 #define FDDIGEST_MAX    4
    00166     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
    00167
    00168     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
    00169     unsigned int firstFree;     /* fadio: */
    00170     long int    fileSize;       /* fadio: */
    00171     long int    fd_cpioPos;     /* cpio: */
    00172 };
    00173 /*@access FD_t@*/
    00174
    00175 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
    00176
    00177 /*@-redecl@*/
    00178 /*@unchecked@*/
    00179 extern int _rpmio_debug;
    00180 /*@=redecl@*/
    00181
    00182 /*@-redecl@*/
    00183 /*@unchecked@*/
    00184 extern int _ftp_debug;
    00185 /*@=redecl@*/
    00186
    00187 #define DBG(_f, _m, _x) \
    00188     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x
    00189
    00190 #if defined(__LCLINT__XXX)
    00191 #define DBGIO(_f, _x)
    00192 #define DBGREFS(_f, _x)
    00193 #else
    00194 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
    00195 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
    00196 #endif
    00197
    00198 #ifdef __cplusplus
    00199 extern "C" {
    00200 #endif
    00201
    00204 int fdFgets(FD_t fd, char * buf, size_t len)
    00205         /*@globals errno, fileSystem @*/
    00206         /*@modifies *buf, fd, errno, fileSystem @*/;
    00207
    00210 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
    00211                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
    00212         /*@globals fileSystem, internalState @*/
    00213         /*@modifies *uret, fileSystem, internalState @*/;
    00214
    00217 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
    00218         /*@globals fileSystem, internalState @*/
    00219         /*@modifies data, fileSystem, internalState @*/;
    00220
    00223 int ftpCmd(const char * cmd, const char * url, const char * arg2)
    00224         /*@globals fileSystem, internalState @*/
    00225         /*@modifies fileSystem, internalState @*/;
    00226
    00229 int ufdClose( /*@only@*/ void * cookie)
    00230         /*@globals fileSystem, internalState @*/
    00231         /*@modifies cookie, fileSystem, internalState @*/;
    00232
    00235 /*@unused@*/ static inline
    00236 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
    00237         /*@*/
    00238 {
    00239     FDSANE(fd);
    00240 /*@-boundsread@*/
    00241     return fd->fps[fd->nfps].io;
    00242 /*@=boundsread@*/
    00243 }
    00244
    00247 /*@-nullstate@*/ /* FIX: io may be NULL */
    00248 /*@unused@*/ static inline
    00249 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
    00250         /*@modifies fd @*/
    00251 {
    00252     FDSANE(fd);
    00253 /*@-boundswrite@*/
    00254     /*@-assignexpose@*/
    00255     fd->fps[fd->nfps].io = io;
    00256     /*@=assignexpose@*/
    00257 /*@=boundswrite@*/
    00258 }
    00259 /*@=nullstate@*/
    00260
    00263 /*@unused@*/ static inline
    00264 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
    00265         /*@*/
    00266 {
    00267     FDSANE(fd);
    00268 /*@-boundsread@*/
    00269     /*@+voidabstract@*/
    00270     return ((FILE *)fd->fps[fd->nfps].fp);
    00271     /*@=voidabstract@*/
    00272 /*@=boundsread@*/
    00273 }
    00274
    00277 /*@unused@*/ static inline
    00278 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
    00279         /*@*/
    00280 {
    00281     FDSANE(fd);
    00282 /*@-boundsread@*/
    00283     return fd->fps[fd->nfps].fp;
    00284 /*@=boundsread@*/
    00285 }
    00286
    00289 /*@-nullstate@*/ /* FIX: fp may be NULL */
    00290 /*@unused@*/ static inline
    00291 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
    00292         /*@modifies fd @*/
    00293 {
    00294     FDSANE(fd);
    00295 /*@-boundswrite@*/
    00296     /*@-assignexpose@*/
    00297     fd->fps[fd->nfps].fp = fp;
    00298     /*@=assignexpose@*/
    00299 /*@=boundswrite@*/
    00300 }
    00301 /*@=nullstate@*/
    00302
    00305 /*@unused@*/ static inline
    00306 int fdGetFdno(FD_t fd)
    00307         /*@*/
    00308 {
    00309     FDSANE(fd);
    00310 /*@-boundsread@*/
    00311     return fd->fps[fd->nfps].fdno;
    00312 /*@=boundsread@*/
    00313 }
    00314
    00317 /*@unused@*/ static inline
    00318 void fdSetFdno(FD_t fd, int fdno)
    00319         /*@modifies fd @*/
    00320 {
    00321     FDSANE(fd);
    00322 /*@-boundswrite@*/
    00323     fd->fps[fd->nfps].fdno = fdno;
    00324 /*@=boundswrite@*/
    00325 }
    00326
    00329 /*@unused@*/ static inline
    00330 void fdSetContentLength(FD_t fd, ssize_t contentLength)
    00331         /*@modifies fd @*/
    00332 {
    00333     FDSANE(fd);
    00334     fd->contentLength = fd->bytesRemain = contentLength;
    00335 }
    00336
    00339 /*@unused@*/ static inline
    00340 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
    00341         /*@modifies fd @*/
    00342 {
    00343     FDSANE(fd);
    00344     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
    00345         return;
    00346     fd->nfps++;
    00347     fdSetIo(fd, io);
    00348     fdSetFp(fd, fp);
    00349     fdSetFdno(fd, fdno);
    00350 }
    00351
    00354 /*@unused@*/ static inline 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 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
    00368         /*@modifies fd @*/
    00369 {
    00370     if (fd == NULL || fd->stats == NULL) return;
    00371 /*@-boundswrite@*/
    00372     fd->stats->ops[opx].count++;
    00373 /*@=boundswrite@*/
    00374     (void) gettimeofday(&fd->stats->begin, NULL);
    00375 }
    00376
    00379 /*@unused@*/ static inline
    00380 time_t tvsub(/*@null@*/ const struct timeval * etv,
    00381                 /*@null@*/ const struct timeval * btv)
    00382         /*@*/
    00383 {
    00384     time_t secs, usecs;
    00385     if (etv == NULL  || btv == NULL) return 0;
    00386     secs = etv->tv_sec - btv->tv_sec;
    00387     for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
    00388         secs++;
    00389     return ((secs * 1000) + (usecs/1000));
    00390 }
    00391
    00394 /*@unused@*/ static inline
    00395 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
    00396         /*@modifies fd @*/
    00397 {
    00398     struct timeval end;
    00399     if (fd == NULL) return;
    00400     if (rc == -1) fd->syserrno = errno;
    00401     if (fd->stats == NULL) return;
    00402 /*@-boundswrite@*/
    00403     (void) gettimeofday(&end, NULL);
    00404     if (rc >= 0) {
    00405         switch(opx) {
    00406         case FDSTAT_SEEK:
    00407             fd->stats->ops[opx].bytes = rc;
    00408             break;
    00409         default:
    00410             fd->stats->ops[opx].bytes += rc;
    00411             if (fd->bytesRemain > 0) fd->bytesRemain -= rc;
    00412             break;
    00413         }
    00414     }
    00415     fd->stats->ops[opx].msecs += tvsub(&end, &fd->stats->begin);
    00416     fd->stats->begin = end;     /* structure assignment */
    00417 /*@=boundswrite@*/
    00418 }
    00419
    00422 /*@-boundsread@*/
    00423 /*@unused@*/ static inline
    00424 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
    00425         /*@globals fileSystem @*/
    00426         /*@modifies *fp, fileSystem @*/
    00427 {
    00428     int opx;
    00429     if (fd == NULL || fd->stats == NULL) return;
    00430     for (opx = 0; opx < 4; opx++) {
    00431         OPSTAT_t *ops = &fd->stats->ops[opx];
    00432         if (ops->count <= 0) continue;
    00433         switch (opx) {
    00434         case FDSTAT_READ:
    00435             if (msg) fprintf(fp, "%s:", msg);
    00436             fprintf(fp, "%8d reads, %8ld total bytes in %d.%03d secs\n",
    00437                 ops->count, (long)ops->bytes,
    00438                 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
    00439             /*@switchbreak@*/ break;
    00440         case FDSTAT_WRITE:
    00441             if (msg) fprintf(fp, "%s:", msg);
    00442             fprintf(fp, "%8d writes, %8ld total bytes in %d.%03d secs\n",
    00443                 ops->count, (long)ops->bytes,
    00444                 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
    00445             /*@switchbreak@*/ break;
    00446         case FDSTAT_SEEK:
    00447             /*@switchbreak@*/ break;
    00448         case FDSTAT_CLOSE:
    00449             /*@switchbreak@*/ break;
    00450         }
    00451     }
    00452 }
    00453 /*@=boundsread@*/
    00454
    00457 /*@unused@*/ static inline
    00458 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
    00459         /*@modifies fd @*/
    00460 {
    00461     FDSANE(fd);
    00462     fd->syserrno = syserrno;
    00463     /*@-assignexpose@*/
    00464     fd->errcookie = errcookie;
    00465     /*@=assignexpose@*/
    00466 }
    00467
    00470 /*@unused@*/ static inline
    00471 int fdGetRdTimeoutSecs(FD_t fd)
    00472         /*@*/
    00473 {
    00474     FDSANE(fd);
    00475     return fd->rd_timeoutsecs;
    00476 }
    00477
    00480 /*@unused@*/ static inline
    00481 long int fdGetCpioPos(FD_t fd)
    00482         /*@*/
    00483 {
    00484     FDSANE(fd);
    00485     return fd->fd_cpioPos;
    00486 }
    00487
    00490 /*@unused@*/ static inline
    00491 void fdSetCpioPos(FD_t fd, long int cpioPos)
    00492         /*@modifies fd @*/
    00493 {
    00494     FDSANE(fd);
    00495     fd->fd_cpioPos = cpioPos;
    00496 }
    00497
    00500 /*@mayexit@*/ /*@unused@*/ static inline
    00501 FD_t c2f(/*@null@*/ void * cookie)
    00502         /*@*/
    00503 {
    00504     /*@-castexpose@*/
    00505     FD_t fd = (FD_t) cookie;
    00506     /*@=castexpose@*/
    00507     FDSANE(fd);
    00508     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
    00509 }
    00510
    00514 /*@unused@*/ static inline
    00515 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
    00516         /*@modifies fd @*/
    00517 {
    00518     FDDIGEST_t fddig = fd->digests + fd->ndigests;
    00519     if (fddig != (fd->digests + FDDIGEST_MAX)) {
    00520         fd->ndigests++;
    00521         fddig->hashalgo = hashalgo;
    00522         fddig->hashctx = rpmDigestInit(hashalgo, flags);
    00523     }
    00524 }
    00525
    00529 /*@unused@*/ static inline
    00530 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
    00531         /*@modifies fd @*/
    00532 {
    00533     int i;
    00534
    00535     if (buf != NULL && buflen > 0)
    00536     for (i = fd->ndigests - 1; i >= 0; i--) {
    00537         FDDIGEST_t fddig = fd->digests + i;
    00538         if (fddig->hashctx == NULL)
    00539             continue;
    00540         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
    00541     }
    00542 }
    00543
    00546 /*@unused@*/ static inline
    00547 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
    00548                 /*@null@*/ /*@out@*/ void ** datap,
    00549                 /*@null@*/ /*@out@*/ size_t * lenp,
    00550                 int asAscii)
    00551         /*@modifies fd, *datap, *lenp @*/
    00552 {
    00553     int imax = -1;
    00554     int i;
    00555
    00556     for (i = fd->ndigests - 1; i >= 0; i--) {
    00557         FDDIGEST_t fddig = fd->digests + i;
    00558         if (fddig->hashctx == NULL)
    00559             continue;
    00560         if (i > imax) imax = i;
    00561         if (fddig->hashalgo != hashalgo)
    00562             continue;
    00563         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
    00564         fddig->hashctx = NULL;
    00565         break;
    00566     }
    00567 /*@-boundswrite@*/
    00568     if (i < 0) {
    00569         if (datap) *datap = NULL;
    00570         if (lenp) *lenp = 0;
    00571     }
    00572 /*@=boundswrite@*/
    00573
    00574     fd->ndigests = imax;
    00575     if (i < imax)
    00576         fd->ndigests++;         /* convert index to count */
    00577 }
    00578
    00579 /*@-shadow@*/
    00582 /*@unused@*/ static inline
    00583 int fdFileno(/*@null@*/ void * cookie)
    00584         /*@*/
    00585 {
    00586     FD_t fd;
    00587     if (cookie == NULL) return -2;
    00588     fd = c2f(cookie);
    00589 /*@-boundsread@*/
    00590     return fd->fps[0].fdno;
    00591 /*@=boundsread@*/
    00592 }
    00593 /*@=shadow@*/
    00594
    00602 int rpmioSlurp(const char * fn,
    00603                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
    00604         /*@globals fileSystem, internalState @*/
    00605         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
    00606
    00607 #ifdef __cplusplus
    00608 }
    00609 #endif
    00610
    00611 #endif  /* H_RPMIO_INTERNAL */