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 void * req;
00159
00160 int rd_timeoutsecs;
00161 ssize_t bytesRemain;
00162 ssize_t contentLength;
00163 int persist;
00164 int wr_chunked;
00165
00166 int syserrno;
00167
00168 const void *errcookie;
00169
00170 FDSTAT_t stats;
00171
00172 int ndigests;
00173 #define FDDIGEST_MAX 4
00174 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00175
00176 int ftpFileDoneNeeded;
00177 unsigned int firstFree;
00178 long int fileSize;
00179 long int fd_cpioPos;
00180 };
00181
00182
00183 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00184
00185
00186
00187 extern int _rpmio_debug;
00188
00189
00190
00191
00192 extern int _av_debug;
00193
00194
00195
00196
00197 extern int _ftp_debug;
00198
00199
00200
00201
00202 extern int _dav_debug;
00203
00204
00205 #define DBG(_f, _m, _x) \
00206 \
00207 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00208
00209
00210 #if defined(__LCLINT__XXX)
00211 #define DBGIO(_f, _x)
00212 #define DBGREFS(_f, _x)
00213 #else
00214 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00215 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00216 #endif
00217
00218 #ifdef __cplusplus
00219 extern "C" {
00220 #endif
00221
00224 int fdFgets(FD_t fd, char * buf, size_t len)
00225
00226 ;
00227
00230 FD_t ftpOpen(const char *url, int flags,
00231 mode_t mode, urlinfo *uret)
00232
00233 ;
00234
00237 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00238
00239 ;
00240
00243 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00244
00245 ;
00246
00249 int ufdClose( void * cookie)
00250
00251 ;
00252
00255 static inline
00256 FDIO_t fdGetIo(FD_t fd)
00257
00258 {
00259 FDSANE(fd);
00260
00261 return fd->fps[fd->nfps].io;
00262
00263 }
00264
00267
00268 static inline
00269 void fdSetIo(FD_t fd, FDIO_t io)
00270
00271 {
00272 FDSANE(fd);
00273
00274
00275 fd->fps[fd->nfps].io = io;
00276
00277
00278 }
00279
00280
00283 static inline
00284 FILE * fdGetFILE(FD_t fd)
00285
00286 {
00287 FDSANE(fd);
00288
00289
00290 return ((FILE *)fd->fps[fd->nfps].fp);
00291
00292
00293 }
00294
00297 static inline
00298 void * fdGetFp(FD_t fd)
00299
00300 {
00301 FDSANE(fd);
00302
00303 return fd->fps[fd->nfps].fp;
00304
00305 }
00306
00309
00310 static inline
00311 void fdSetFp(FD_t fd, void * fp)
00312
00313 {
00314 FDSANE(fd);
00315
00316
00317 fd->fps[fd->nfps].fp = fp;
00318
00319
00320 }
00321
00322
00325 static inline
00326 int fdGetFdno(FD_t fd)
00327
00328 {
00329 FDSANE(fd);
00330
00331 return fd->fps[fd->nfps].fdno;
00332
00333 }
00334
00337 static inline
00338 void fdSetFdno(FD_t fd, int fdno)
00339
00340 {
00341 FDSANE(fd);
00342
00343 fd->fps[fd->nfps].fdno = fdno;
00344
00345 }
00346
00349 static inline
00350 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00351
00352 {
00353 FDSANE(fd);
00354 fd->contentLength = fd->bytesRemain = contentLength;
00355 }
00356
00359 static inline
00360 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00361
00362 {
00363 FDSANE(fd);
00364 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00365 return;
00366 fd->nfps++;
00367 fdSetIo(fd, io);
00368 fdSetFp(fd, fp);
00369 fdSetFdno(fd, fdno);
00370 }
00371
00374 static inline
00375 void fdPop(FD_t fd)
00376
00377 {
00378 FDSANE(fd);
00379 if (fd->nfps < 0) return;
00380 fdSetIo(fd, NULL);
00381 fdSetFp(fd, NULL);
00382 fdSetFdno(fd, -1);
00383 fd->nfps--;
00384 }
00385
00388 static inline
00389 rpmop fdstat_op( FD_t fd, fdOpX opx)
00390
00391 {
00392 rpmop op = NULL;
00393
00394
00395 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00396 op = fd->stats->ops + opx;
00397
00398 return op;
00399 }
00400
00403 static inline
00404 void fdstat_enter( FD_t fd, int opx)
00405
00406
00407 {
00408 if (fd == NULL) return;
00409 if (fd->stats != NULL)
00410 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00411 }
00412
00415 static inline
00416 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00417
00418
00419 {
00420 if (fd == NULL) return;
00421 if (rc == -1)
00422 fd->syserrno = errno;
00423 else if (rc > 0 && fd->bytesRemain > 0)
00424 switch (opx) {
00425 case FDSTAT_READ:
00426 case FDSTAT_WRITE:
00427 fd->bytesRemain -= rc;
00428 break;
00429 default:
00430 break;
00431 }
00432 if (fd->stats != NULL)
00433 (void) rpmswExit(fdstat_op(fd, opx), rc);
00434 }
00435
00438
00439 static inline
00440 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00441
00442
00443 {
00444 static int usec_scale = (1000*1000);
00445 int opx;
00446
00447 if (fd == NULL || fd->stats == NULL) return;
00448 for (opx = 0; opx < 4; opx++) {
00449 rpmop op = &fd->stats->ops[opx];
00450 if (op->count <= 0) continue;
00451 switch (opx) {
00452 case FDSTAT_READ:
00453 if (msg) fprintf(fp, "%s:", msg);
00454 fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00455 op->count, (long)op->bytes,
00456 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00457 break;
00458 case FDSTAT_WRITE:
00459 if (msg) fprintf(fp, "%s:", msg);
00460 fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00461 op->count, (long)op->bytes,
00462 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00463 break;
00464 case FDSTAT_SEEK:
00465 break;
00466 case FDSTAT_CLOSE:
00467 break;
00468 }
00469 }
00470 }
00471
00472
00475 static inline
00476 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00477
00478 {
00479 FDSANE(fd);
00480 fd->syserrno = syserrno;
00481
00482 fd->errcookie = errcookie;
00483
00484 }
00485
00488 static inline
00489 int fdGetRdTimeoutSecs(FD_t fd)
00490
00491 {
00492 FDSANE(fd);
00493 return fd->rd_timeoutsecs;
00494 }
00495
00498 static inline
00499 long int fdGetCpioPos(FD_t fd)
00500
00501 {
00502 FDSANE(fd);
00503 return fd->fd_cpioPos;
00504 }
00505
00508 static inline
00509 void fdSetCpioPos(FD_t fd, long int cpioPos)
00510
00511 {
00512 FDSANE(fd);
00513 fd->fd_cpioPos = cpioPos;
00514 }
00515
00518 static inline
00519 FD_t c2f( void * cookie)
00520
00521 {
00522
00523 FD_t fd = (FD_t) cookie;
00524
00525 FDSANE(fd);
00526 return fd;
00527 }
00528
00532 static inline
00533 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00534
00535
00536 {
00537 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00538 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00539 fd->ndigests++;
00540 fddig->hashalgo = hashalgo;
00541 fdstat_enter(fd, FDSTAT_DIGEST);
00542 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00543 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00544 }
00545 }
00546
00550 static inline
00551 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00552
00553
00554 {
00555 int i;
00556
00557 if (buf != NULL && buflen > 0)
00558 for (i = fd->ndigests - 1; i >= 0; i--) {
00559 FDDIGEST_t fddig = fd->digests + i;
00560 if (fddig->hashctx == NULL)
00561 continue;
00562 fdstat_enter(fd, FDSTAT_DIGEST);
00563 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00564 fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00565 }
00566 }
00567
00570 static inline
00571 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00572 void ** datap,
00573 size_t * lenp,
00574 int asAscii)
00575
00576
00577 {
00578 int imax = -1;
00579 int i;
00580
00581 for (i = fd->ndigests - 1; i >= 0; i--) {
00582 FDDIGEST_t fddig = fd->digests + i;
00583 if (fddig->hashctx == NULL)
00584 continue;
00585 if (i > imax) imax = i;
00586 if (fddig->hashalgo != hashalgo)
00587 continue;
00588 fdstat_enter(fd, FDSTAT_DIGEST);
00589 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00590 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00591 fddig->hashctx = NULL;
00592 break;
00593 }
00594
00595 if (i < 0) {
00596 if (datap) *datap = NULL;
00597 if (lenp) *lenp = 0;
00598 }
00599
00600
00601 fd->ndigests = imax;
00602 if (i < imax)
00603 fd->ndigests++;
00604 }
00605
00606
00609 static inline
00610 int fdFileno( void * cookie)
00611
00612 {
00613 FD_t fd;
00614 if (cookie == NULL) return -2;
00615 fd = c2f(cookie);
00616
00617 return fd->fps[0].fdno;
00618
00619 }
00620
00621
00629 int rpmioSlurp(const char * fn,
00630 const unsigned char ** bp, ssize_t * blenp)
00631
00632 ;
00633
00634 #ifdef __cplusplus
00635 }
00636 #endif
00637
00638 #endif