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
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
00042 const char * userid;
00043
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
00073 DIGEST_CTX sha1ctx;
00074
00075 DIGEST_CTX hdrsha1ctx;
00076
00077 void * sha1;
00078 size_t sha1len;
00080
00081 DIGEST_CTX md5ctx;
00082
00083 DIGEST_CTX hdrmd5ctx;
00084
00085 void * md5;
00086 size_t md5len;
00088
00089 mpbarrett p;
00090 mpbarrett q;
00091 mpnumber g;
00092 mpnumber y;
00093 mpnumber hm;
00094 mpnumber r;
00095 mpnumber s;
00096
00097
00098 rsapk rsa_pk;
00099 mpnumber m;
00100 mpnumber c;
00101 mpnumber rsahm;
00102 };
00103
00106 typedef struct _FDSTACK_s {
00107
00108 FDIO_t io;
00109
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 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
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;
00154
00155
00156 void * url;
00157
00158
00159 int rd_timeoutsecs;
00160 ssize_t bytesRemain;
00161 ssize_t contentLength;
00162 int persist;
00163 int wr_chunked;
00164
00165 int syserrno;
00166
00167 const void *errcookie;
00168
00169 FDSTAT_t stats;
00170
00171 int ndigests;
00172 #define FDDIGEST_MAX 4
00173 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00174
00175 int ftpFileDoneNeeded;
00176 unsigned int firstFree;
00177 long int fileSize;
00178 long int fd_cpioPos;
00179 };
00180
00181
00182 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00183
00184
00185
00186 extern int _rpmio_debug;
00187
00188
00189
00190
00191 extern int _av_debug;
00192
00193
00194
00195
00196 extern int _ftp_debug;
00197
00198
00199
00200
00201 extern int _dav_debug;
00202
00203
00204 #define DBG(_f, _m, _x) \
00205 \
00206 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00207
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
00225 ;
00226
00229 FD_t ftpOpen(const char *url, int flags,
00230 mode_t mode, urlinfo *uret)
00231
00232 ;
00233
00236 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00237
00238 ;
00239
00242 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00243
00244 ;
00245
00248 int ufdClose( void * cookie)
00249
00250 ;
00251
00254 static inline
00255 FDIO_t fdGetIo(FD_t fd)
00256
00257 {
00258 FDSANE(fd);
00259
00260 return fd->fps[fd->nfps].io;
00261
00262 }
00263
00266
00267 static inline
00268 void fdSetIo(FD_t fd, FDIO_t io)
00269
00270 {
00271 FDSANE(fd);
00272
00273
00274 fd->fps[fd->nfps].io = io;
00275
00276
00277 }
00278
00279
00282 static inline
00283 FILE * fdGetFILE(FD_t fd)
00284
00285 {
00286 FDSANE(fd);
00287
00288
00289 return ((FILE *)fd->fps[fd->nfps].fp);
00290
00291
00292 }
00293
00296 static inline
00297 void * fdGetFp(FD_t fd)
00298
00299 {
00300 FDSANE(fd);
00301
00302 return fd->fps[fd->nfps].fp;
00303
00304 }
00305
00308
00309 static inline
00310 void fdSetFp(FD_t fd, void * fp)
00311
00312 {
00313 FDSANE(fd);
00314
00315
00316 fd->fps[fd->nfps].fp = fp;
00317
00318
00319 }
00320
00321
00324 static inline
00325 int fdGetFdno(FD_t fd)
00326
00327 {
00328 FDSANE(fd);
00329
00330 return fd->fps[fd->nfps].fdno;
00331
00332 }
00333
00336 static inline
00337 void fdSetFdno(FD_t fd, int fdno)
00338
00339 {
00340 FDSANE(fd);
00341
00342 fd->fps[fd->nfps].fdno = fdno;
00343
00344 }
00345
00348 static inline
00349 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00350
00351 {
00352 FDSANE(fd);
00353 fd->contentLength = fd->bytesRemain = contentLength;
00354 }
00355
00358 static inline
00359 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00360
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 static inline
00374 void fdPop(FD_t fd)
00375
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 static inline
00388 rpmop fdstat_op( FD_t fd, fdOpX opx)
00389
00390 {
00391 rpmop op = NULL;
00392
00393
00394 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00395 op = fd->stats->ops + opx;
00396
00397 return op;
00398 }
00399
00402 static inline
00403 void fdstat_enter( FD_t fd, int opx)
00404
00405
00406 {
00407 if (fd == NULL) return;
00408 if (fd->stats != NULL)
00409 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00410 }
00411
00414 static inline
00415 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00416
00417
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
00438 static inline
00439 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00440
00441
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 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 break;
00463 case FDSTAT_SEEK:
00464 break;
00465 case FDSTAT_CLOSE:
00466 break;
00467 }
00468 }
00469 }
00470
00471
00474 static inline
00475 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00476
00477 {
00478 FDSANE(fd);
00479 fd->syserrno = syserrno;
00480
00481 fd->errcookie = errcookie;
00482
00483 }
00484
00487 static inline
00488 int fdGetRdTimeoutSecs(FD_t fd)
00489
00490 {
00491 FDSANE(fd);
00492 return fd->rd_timeoutsecs;
00493 }
00494
00497 static inline
00498 long int fdGetCpioPos(FD_t fd)
00499
00500 {
00501 FDSANE(fd);
00502 return fd->fd_cpioPos;
00503 }
00504
00507 static inline
00508 void fdSetCpioPos(FD_t fd, long int cpioPos)
00509
00510 {
00511 FDSANE(fd);
00512 fd->fd_cpioPos = cpioPos;
00513 }
00514
00517 static inline
00518 FD_t c2f( void * cookie)
00519
00520 {
00521
00522 FD_t fd = (FD_t) cookie;
00523
00524 FDSANE(fd);
00525 return fd;
00526 }
00527
00531 static inline
00532 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00533
00534
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 static inline
00550 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00551
00552
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 static inline
00570 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00571 void ** datap,
00572 size_t * lenp,
00573 int asAscii)
00574
00575
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
00594 if (i < 0) {
00595 if (datap) *datap = NULL;
00596 if (lenp) *lenp = 0;
00597 }
00598
00599
00600 fd->ndigests = imax;
00601 if (i < imax)
00602 fd->ndigests++;
00603 }
00604
00605
00608 static inline
00609 int fdFileno( void * cookie)
00610
00611 {
00612 FD_t fd;
00613 if (cookie == NULL) return -2;
00614 fd = c2f(cookie);
00615
00616 return fd->fps[0].fdno;
00617
00618 }
00619
00620
00628 int rpmioSlurp(const char * fn,
00629 const unsigned char ** bp, ssize_t * blenp)
00630
00631 ;
00632
00633 #ifdef __cplusplus
00634 }
00635 #endif
00636
00637 #endif