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
00035 struct pgpDigParams_s {
00036
00037 const char * userid;
00038
00039 const byte * hash;
00040 const char * params[4];
00041 byte tag;
00042
00043 byte version;
00044 byte time[4];
00045 byte pubkey_algo;
00047 byte hash_algo;
00048 byte sigtype;
00049 byte hashlen;
00050 byte signhash16[2];
00051 byte signid[8];
00052 byte saved;
00053 #define PGPDIG_SAVED_TIME (1 << 0)
00054 #define PGPDIG_SAVED_ID (1 << 1)
00055
00056 };
00057
00061 struct pgpDig_s {
00062 struct pgpDigParams_s signature;
00063 struct pgpDigParams_s pubkey;
00064
00065 size_t nbytes;
00067
00068 DIGEST_CTX sha1ctx;
00069
00070 DIGEST_CTX hdrsha1ctx;
00071
00072 void * sha1;
00073 size_t sha1len;
00075
00076 DIGEST_CTX md5ctx;
00077 #ifdef NOTYET
00078
00079 DIGEST_CTX hdrmd5ctx;
00080 #endif
00081
00082 void * md5;
00083 size_t md5len;
00085
00086 mpbarrett p;
00087 mpbarrett q;
00088 mpnumber g;
00089 mpnumber y;
00090 mpnumber hm;
00091 mpnumber r;
00092 mpnumber s;
00093
00094
00095 rsapk rsa_pk;
00096 mpnumber m;
00097 mpnumber c;
00098 mpnumber rsahm;
00099 };
00100
00103 typedef struct _FDSTACK_s {
00104
00105 FDIO_t io;
00106
00107 void * fp;
00108 int fdno;
00109 } FDSTACK_t;
00110
00114 typedef enum fdOpX_e {
00115 FDSTAT_READ = 0,
00116 FDSTAT_WRITE = 1,
00117 FDSTAT_SEEK = 2,
00118 FDSTAT_CLOSE = 3,
00119 FDSTAT_DIGEST = 4,
00120 FDSTAT_MAX = 5
00121 } fdOpX;
00122
00126 typedef struct {
00127 struct rpmop_s ops[FDSTAT_MAX];
00128 } * FDSTAT_t;
00129
00132 typedef struct _FDDIGEST_s {
00133 pgpHashAlgo hashalgo;
00134 DIGEST_CTX hashctx;
00135 } * FDDIGEST_t;
00136
00140 struct _FD_s {
00141
00142 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;
00151
00152
00153 void * url;
00154 int rd_timeoutsecs;
00155 ssize_t bytesRemain;
00156 ssize_t contentLength;
00157 int persist;
00158 int wr_chunked;
00159
00160 int syserrno;
00161
00162 const void *errcookie;
00163
00164 FDSTAT_t stats;
00165
00166 int ndigests;
00167 #define FDDIGEST_MAX 4
00168 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00169
00170 int ftpFileDoneNeeded;
00171 unsigned int firstFree;
00172 long int fileSize;
00173 long int fd_cpioPos;
00174 };
00175
00176
00177 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00178
00179
00180
00181 extern int _rpmio_debug;
00182
00183
00184
00185
00186 extern int _ftp_debug;
00187
00188
00189 #define DBG(_f, _m, _x) \
00190 \
00191 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00192
00193
00194 #if defined(__LCLINT__XXX)
00195 #define DBGIO(_f, _x)
00196 #define DBGREFS(_f, _x)
00197 #else
00198 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00199 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00200 #endif
00201
00202 #ifdef __cplusplus
00203 extern "C" {
00204 #endif
00205
00208 int fdFgets(FD_t fd, char * buf, size_t len)
00209
00210 ;
00211
00214 FD_t ftpOpen(const char *url, int flags,
00215 mode_t mode, urlinfo *uret)
00216
00217 ;
00218
00221 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00222
00223 ;
00224
00227 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00228
00229 ;
00230
00233 int ufdClose( void * cookie)
00234
00235 ;
00236
00239 static inline
00240 FDIO_t fdGetIo(FD_t fd)
00241
00242 {
00243 FDSANE(fd);
00244
00245 return fd->fps[fd->nfps].io;
00246
00247 }
00248
00251
00252 static inline
00253 void fdSetIo(FD_t fd, FDIO_t io)
00254
00255 {
00256 FDSANE(fd);
00257
00258
00259 fd->fps[fd->nfps].io = io;
00260
00261
00262 }
00263
00264
00267 static inline
00268 FILE * fdGetFILE(FD_t fd)
00269
00270 {
00271 FDSANE(fd);
00272
00273
00274 return ((FILE *)fd->fps[fd->nfps].fp);
00275
00276
00277 }
00278
00281 static inline
00282 void * fdGetFp(FD_t fd)
00283
00284 {
00285 FDSANE(fd);
00286
00287 return fd->fps[fd->nfps].fp;
00288
00289 }
00290
00293
00294 static inline
00295 void fdSetFp(FD_t fd, void * fp)
00296
00297 {
00298 FDSANE(fd);
00299
00300
00301 fd->fps[fd->nfps].fp = fp;
00302
00303
00304 }
00305
00306
00309 static inline
00310 int fdGetFdno(FD_t fd)
00311
00312 {
00313 FDSANE(fd);
00314
00315 return fd->fps[fd->nfps].fdno;
00316
00317 }
00318
00321 static inline
00322 void fdSetFdno(FD_t fd, int fdno)
00323
00324 {
00325 FDSANE(fd);
00326
00327 fd->fps[fd->nfps].fdno = fdno;
00328
00329 }
00330
00333 static inline
00334 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00335
00336 {
00337 FDSANE(fd);
00338 fd->contentLength = fd->bytesRemain = contentLength;
00339 }
00340
00343 static inline
00344 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00345
00346 {
00347 FDSANE(fd);
00348 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00349 return;
00350 fd->nfps++;
00351 fdSetIo(fd, io);
00352 fdSetFp(fd, fp);
00353 fdSetFdno(fd, fdno);
00354 }
00355
00358 static inline
00359 void fdPop(FD_t fd)
00360
00361 {
00362 FDSANE(fd);
00363 if (fd->nfps < 0) return;
00364 fdSetIo(fd, NULL);
00365 fdSetFp(fd, NULL);
00366 fdSetFdno(fd, -1);
00367 fd->nfps--;
00368 }
00369
00372 static inline
00373 rpmop fdstat_op( FD_t fd, fdOpX opx)
00374
00375 {
00376 rpmop op = NULL;
00377
00378
00379 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00380 op = fd->stats->ops + opx;
00381
00382 return op;
00383 }
00384
00387 static inline
00388 void fdstat_enter( FD_t fd, int opx)
00389
00390
00391 {
00392 if (fd == NULL) return;
00393 if (fd->stats != NULL)
00394 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00395 }
00396
00399 static inline
00400 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00401
00402
00403 {
00404 if (fd == NULL) return;
00405 if (rc == -1)
00406 fd->syserrno = errno;
00407 else if (rc > 0 && fd->bytesRemain > 0) {
00408 switch (opx) {
00409 case FDSTAT_READ:
00410 case FDSTAT_WRITE:
00411 fd->bytesRemain -= rc;
00412 break;
00413 default:
00414 break;
00415 }
00416 }
00417 if (fd->stats != NULL)
00418 (void) rpmswExit(fdstat_op(fd, opx), rc);
00419 }
00420
00423
00424 static inline
00425 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00426
00427
00428 {
00429 static int usec_scale = (1000*1000);
00430 int opx;
00431
00432 if (fd == NULL || fd->stats == NULL) return;
00433 for (opx = 0; opx < 4; opx++) {
00434 rpmop op = &fd->stats->ops[opx];
00435 if (op->count <= 0) continue;
00436 switch (opx) {
00437 case FDSTAT_READ:
00438 if (msg) fprintf(fp, "%s:", msg);
00439 fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00440 op->count, (long)op->bytes,
00441 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00442 break;
00443 case FDSTAT_WRITE:
00444 if (msg) fprintf(fp, "%s:", msg);
00445 fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00446 op->count, (long)op->bytes,
00447 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00448 break;
00449 case FDSTAT_SEEK:
00450 break;
00451 case FDSTAT_CLOSE:
00452 break;
00453 }
00454 }
00455 }
00456
00457
00460 static inline
00461 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00462
00463 {
00464 FDSANE(fd);
00465 fd->syserrno = syserrno;
00466
00467 fd->errcookie = errcookie;
00468
00469 }
00470
00473 static inline
00474 int fdGetRdTimeoutSecs(FD_t fd)
00475
00476 {
00477 FDSANE(fd);
00478 return fd->rd_timeoutsecs;
00479 }
00480
00483 static inline
00484 long int fdGetCpioPos(FD_t fd)
00485
00486 {
00487 FDSANE(fd);
00488 return fd->fd_cpioPos;
00489 }
00490
00493 static inline
00494 void fdSetCpioPos(FD_t fd, long int cpioPos)
00495
00496 {
00497 FDSANE(fd);
00498 fd->fd_cpioPos = cpioPos;
00499 }
00500
00503 static inline
00504 FD_t c2f( void * cookie)
00505
00506 {
00507
00508 FD_t fd = (FD_t) cookie;
00509
00510 FDSANE(fd);
00511 return fd;
00512 }
00513
00517 static inline
00518 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00519
00520
00521 {
00522 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00523 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00524 fd->ndigests++;
00525 fddig->hashalgo = hashalgo;
00526 fdstat_enter(fd, FDSTAT_DIGEST);
00527 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00528 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00529 }
00530 }
00531
00535 static inline
00536 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00537
00538
00539 {
00540 int i;
00541
00542 if (buf != NULL && buflen > 0)
00543 for (i = fd->ndigests - 1; i >= 0; i--) {
00544 FDDIGEST_t fddig = fd->digests + i;
00545 if (fddig->hashctx == NULL)
00546 continue;
00547 fdstat_enter(fd, FDSTAT_DIGEST);
00548 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00549 fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00550 }
00551 }
00552
00555 static inline
00556 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00557 void ** datap,
00558 size_t * lenp,
00559 int asAscii)
00560
00561
00562 {
00563 int imax = -1;
00564 int i;
00565
00566 for (i = fd->ndigests - 1; i >= 0; i--) {
00567 FDDIGEST_t fddig = fd->digests + i;
00568 if (fddig->hashctx == NULL)
00569 continue;
00570 if (i > imax) imax = i;
00571 if (fddig->hashalgo != hashalgo)
00572 continue;
00573 fdstat_enter(fd, FDSTAT_DIGEST);
00574 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00575 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00576 fddig->hashctx = NULL;
00577 break;
00578 }
00579
00580 if (i < 0) {
00581 if (datap) *datap = NULL;
00582 if (lenp) *lenp = 0;
00583 }
00584
00585
00586 fd->ndigests = imax;
00587 if (i < imax)
00588 fd->ndigests++;
00589 }
00590
00591
00594 static inline
00595 int fdFileno( void * cookie)
00596
00597 {
00598 FD_t fd;
00599 if (cookie == NULL) return -2;
00600 fd = c2f(cookie);
00601
00602 return fd->fps[0].fdno;
00603
00604 }
00605
00606
00614 int rpmioSlurp(const char * fn,
00615 const unsigned char ** bp, ssize_t * blenp)
00616
00617 ;
00618
00619 #ifdef __cplusplus
00620 }
00621 #endif
00622
00623 #endif