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 fd->bytesRemain -= rc;
00409 if (fd->stats != NULL)
00410 (void) rpmswExit(fdstat_op(fd, opx), rc);
00411 }
00412
00415
00416 static inline
00417 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00418
00419
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 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 break;
00441 case FDSTAT_SEEK:
00442 break;
00443 case FDSTAT_CLOSE:
00444 break;
00445 }
00446 }
00447 }
00448
00449
00452 static inline
00453 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00454
00455 {
00456 FDSANE(fd);
00457 fd->syserrno = syserrno;
00458
00459 fd->errcookie = errcookie;
00460
00461 }
00462
00465 static inline
00466 int fdGetRdTimeoutSecs(FD_t fd)
00467
00468 {
00469 FDSANE(fd);
00470 return fd->rd_timeoutsecs;
00471 }
00472
00475 static inline
00476 long int fdGetCpioPos(FD_t fd)
00477
00478 {
00479 FDSANE(fd);
00480 return fd->fd_cpioPos;
00481 }
00482
00485 static inline
00486 void fdSetCpioPos(FD_t fd, long int cpioPos)
00487
00488 {
00489 FDSANE(fd);
00490 fd->fd_cpioPos = cpioPos;
00491 }
00492
00495 static inline
00496 FD_t c2f( void * cookie)
00497
00498 {
00499
00500 FD_t fd = (FD_t) cookie;
00501
00502 FDSANE(fd);
00503 return fd;
00504 }
00505
00509 static inline
00510 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00511
00512
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 static inline
00528 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00529
00530
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 static inline
00548 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00549 void ** datap,
00550 size_t * lenp,
00551 int asAscii)
00552
00553
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
00572 if (i < 0) {
00573 if (datap) *datap = NULL;
00574 if (lenp) *lenp = 0;
00575 }
00576
00577
00578 fd->ndigests = imax;
00579 if (i < imax)
00580 fd->ndigests++;
00581 }
00582
00583
00586 static inline
00587 int fdFileno( void * cookie)
00588
00589 {
00590 FD_t fd;
00591 if (cookie == NULL) return -2;
00592 fd = c2f(cookie);
00593
00594 return fd->fps[0].fdno;
00595
00596 }
00597
00598
00606 int rpmioSlurp(const char * fn,
00607 const unsigned char ** bp, ssize_t * blenp)
00608
00609 ;
00610
00611 #ifdef __cplusplus
00612 }
00613 #endif
00614
00615 #endif