00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003
00009 #include <rpmio.h>
00010 #include <rpmurl.h>
00011
00012 #include <beecrypt.api.h>
00013 #include <rpmpgp.h>
00014
00015
00016 #include <beecrypt.h>
00017 #include <base64.h>
00018 #include <dsa.h>
00019 #include <endianness.h>
00020 #include <md5.h>
00021 #include <mp.h>
00022 #include <rsa.h>
00023 #include <rsapk.h>
00024 #include <sha1.h>
00025
00029 struct pgpDigParams_s {
00030
00031 const char * userid;
00032
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
00062 DIGEST_CTX sha1ctx;
00063
00064 DIGEST_CTX hdrsha1ctx;
00065
00066 void * sha1;
00067 size_t sha1len;
00069
00070 DIGEST_CTX md5ctx;
00071 #ifdef NOTYET
00072
00073 DIGEST_CTX hdrmd5ctx;
00074 #endif
00075
00076 void * md5;
00077 size_t md5len;
00079
00080 mpbarrett p;
00081 mpbarrett q;
00082 mpnumber g;
00083 mpnumber y;
00084 mpnumber hm;
00085 mpnumber r;
00086 mpnumber s;
00087
00088
00089 rsapk rsa_pk;
00090 mpnumber m;
00091 mpnumber c;
00092 mpnumber rsahm;
00093 };
00094
00097 typedef struct _FDSTACK_s {
00098
00099 FDIO_t io;
00100
00101 void * fp;
00102 int fdno;
00103 } FDSTACK_t;
00104
00108 typedef struct {
00109 int count;
00110 off_t bytes;
00111 time_t msecs;
00112 } OPSTAT_t;
00113
00117 enum FDSTAT_e {
00118 FDSTAT_READ = 0,
00119 FDSTAT_WRITE = 1,
00120 FDSTAT_SEEK = 2,
00121 FDSTAT_CLOSE = 3
00122 };
00123
00127 typedef struct {
00128 struct timeval create;
00129 struct timeval begin;
00130 OPSTAT_t ops[4];
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 int rd_timeoutsecs;
00158 ssize_t bytesRemain;
00159 ssize_t contentLength;
00160 int persist;
00161 int wr_chunked;
00162
00163 int syserrno;
00164
00165 const void *errcookie;
00166
00167 FDSTAT_t stats;
00168
00169 int ndigests;
00170 #define FDDIGEST_MAX 4
00171 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00172
00173 int ftpFileDoneNeeded;
00174 unsigned int firstFree;
00175 long int fileSize;
00176 long int fd_cpioPos;
00177 };
00178
00179
00180 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00181
00182
00183
00184 extern int _rpmio_debug;
00185
00186
00187
00188
00189 extern int _ftp_debug;
00190
00191
00192 #define DBG(_f, _m, _x) \
00193 \
00194 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00195
00196
00197 #if defined(__LCLINT__XXX)
00198 #define DBGIO(_f, _x)
00199 #define DBGREFS(_f, _x)
00200 #else
00201 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00202 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00203 #endif
00204
00205 #ifdef __cplusplus
00206 extern "C" {
00207 #endif
00208
00211 int fdFgets(FD_t fd, char * buf, size_t len)
00212
00213 ;
00214
00217 FD_t ftpOpen(const char *url, int flags,
00218 mode_t mode, urlinfo *uret)
00219
00220 ;
00221
00224 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00225
00226 ;
00227
00230 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00231
00232 ;
00233
00236 int ufdClose( void * cookie)
00237
00238 ;
00239
00242 static inline
00243 FDIO_t fdGetIo(FD_t fd)
00244
00245 {
00246 FDSANE(fd);
00247
00248 return fd->fps[fd->nfps].io;
00249
00250 }
00251
00254
00255 static inline
00256 void fdSetIo(FD_t fd, FDIO_t io)
00257
00258 {
00259 FDSANE(fd);
00260
00261
00262 fd->fps[fd->nfps].io = io;
00263
00264
00265 }
00266
00267
00270 static inline
00271 FILE * fdGetFILE(FD_t fd)
00272
00273 {
00274 FDSANE(fd);
00275
00276
00277 return ((FILE *)fd->fps[fd->nfps].fp);
00278
00279
00280 }
00281
00284 static inline
00285 void * fdGetFp(FD_t fd)
00286
00287 {
00288 FDSANE(fd);
00289
00290 return fd->fps[fd->nfps].fp;
00291
00292 }
00293
00296
00297 static inline
00298 void fdSetFp(FD_t fd, void * fp)
00299
00300 {
00301 FDSANE(fd);
00302
00303
00304 fd->fps[fd->nfps].fp = fp;
00305
00306
00307 }
00308
00309
00312 static inline
00313 int fdGetFdno(FD_t fd)
00314
00315 {
00316 FDSANE(fd);
00317
00318 return fd->fps[fd->nfps].fdno;
00319
00320 }
00321
00324 static inline
00325 void fdSetFdno(FD_t fd, int fdno)
00326
00327 {
00328 FDSANE(fd);
00329
00330 fd->fps[fd->nfps].fdno = fdno;
00331
00332 }
00333
00336 static inline
00337 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00338
00339 {
00340 FDSANE(fd);
00341 fd->contentLength = fd->bytesRemain = contentLength;
00342 }
00343
00346 static inline
00347 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00348
00349 {
00350 FDSANE(fd);
00351 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00352 return;
00353 fd->nfps++;
00354 fdSetIo(fd, io);
00355 fdSetFp(fd, fp);
00356 fdSetFdno(fd, fdno);
00357 }
00358
00361 static inline void fdPop(FD_t fd)
00362
00363 {
00364 FDSANE(fd);
00365 if (fd->nfps < 0) return;
00366 fdSetIo(fd, NULL);
00367 fdSetFp(fd, NULL);
00368 fdSetFdno(fd, -1);
00369 fd->nfps--;
00370 }
00371
00374 static inline void fdstat_enter( FD_t fd, int opx)
00375
00376 {
00377 if (fd == NULL || fd->stats == NULL) return;
00378
00379 fd->stats->ops[opx].count++;
00380
00381 (void) gettimeofday(&fd->stats->begin, NULL);
00382 }
00383
00386 static inline
00387 time_t tvsub( const struct timeval * etv,
00388 const struct timeval * btv)
00389
00390 {
00391 time_t secs, usecs;
00392 if (etv == NULL || btv == NULL) return 0;
00393 secs = etv->tv_sec - btv->tv_sec;
00394 for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
00395 secs++;
00396 return ((secs * 1000) + (usecs/1000));
00397 }
00398
00401 static inline
00402 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00403
00404 {
00405 struct timeval end;
00406 if (fd == NULL) return;
00407 if (rc == -1) fd->syserrno = errno;
00408 if (fd->stats == NULL) return;
00409
00410 (void) gettimeofday(&end, NULL);
00411 if (rc >= 0) {
00412 switch(opx) {
00413 case FDSTAT_SEEK:
00414 fd->stats->ops[opx].bytes = rc;
00415 break;
00416 default:
00417 fd->stats->ops[opx].bytes += rc;
00418 if (fd->bytesRemain > 0) fd->bytesRemain -= rc;
00419 break;
00420 }
00421 }
00422 fd->stats->ops[opx].msecs += tvsub(&end, &fd->stats->begin);
00423 fd->stats->begin = end;
00424
00425 }
00426
00429
00430 static inline
00431 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00432
00433
00434 {
00435 int opx;
00436 if (fd == NULL || fd->stats == NULL) return;
00437 for (opx = 0; opx < 4; opx++) {
00438 OPSTAT_t *ops = &fd->stats->ops[opx];
00439 if (ops->count <= 0) continue;
00440 switch (opx) {
00441 case FDSTAT_READ:
00442 if (msg) fprintf(fp, "%s:", msg);
00443 fprintf(fp, "%8d reads, %8ld total bytes in %d.%03d secs\n",
00444 ops->count, (long)ops->bytes,
00445 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00446 break;
00447 case FDSTAT_WRITE:
00448 if (msg) fprintf(fp, "%s:", msg);
00449 fprintf(fp, "%8d writes, %8ld total bytes in %d.%03d secs\n",
00450 ops->count, (long)ops->bytes,
00451 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00452 break;
00453 case FDSTAT_SEEK:
00454 break;
00455 case FDSTAT_CLOSE:
00456 break;
00457 }
00458 }
00459 }
00460
00461
00464 static inline
00465 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00466
00467 {
00468 FDSANE(fd);
00469 fd->syserrno = syserrno;
00470
00471 fd->errcookie = errcookie;
00472
00473 }
00474
00477 static inline
00478 int fdGetRdTimeoutSecs(FD_t fd)
00479
00480 {
00481 FDSANE(fd);
00482 return fd->rd_timeoutsecs;
00483 }
00484
00487 static inline
00488 long int fdGetCpioPos(FD_t fd)
00489
00490 {
00491 FDSANE(fd);
00492 return fd->fd_cpioPos;
00493 }
00494
00497 static inline
00498 void fdSetCpioPos(FD_t fd, long int cpioPos)
00499
00500 {
00501 FDSANE(fd);
00502 fd->fd_cpioPos = cpioPos;
00503 }
00504
00507 static inline
00508 FD_t c2f( void * cookie)
00509
00510 {
00511
00512 FD_t fd = (FD_t) cookie;
00513
00514 FDSANE(fd);
00515 return fd;
00516 }
00517
00521 static inline
00522 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00523
00524 {
00525 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00526 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00527 fd->ndigests++;
00528 fddig->hashalgo = hashalgo;
00529 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00530 }
00531 }
00532
00536 static inline
00537 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
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 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00548 }
00549 }
00550
00553 static inline
00554 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00555 void ** datap,
00556 size_t * lenp,
00557 int asAscii)
00558
00559 {
00560 int imax = -1;
00561 int i;
00562
00563 for (i = fd->ndigests - 1; i >= 0; i--) {
00564 FDDIGEST_t fddig = fd->digests + i;
00565 if (fddig->hashctx == NULL)
00566 continue;
00567 if (i > imax) imax = i;
00568 if (fddig->hashalgo != hashalgo)
00569 continue;
00570 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00571 fddig->hashctx = NULL;
00572 break;
00573 }
00574
00575 if (i < 0) {
00576 if (datap) *datap = NULL;
00577 if (lenp) *lenp = 0;
00578 }
00579
00580
00581 fd->ndigests = imax;
00582 if (i < imax)
00583 fd->ndigests++;
00584 }
00585
00586
00589 static inline
00590 int fdFileno( void * cookie)
00591
00592 {
00593 FD_t fd;
00594 if (cookie == NULL) return -2;
00595 fd = c2f(cookie);
00596
00597 return fd->fps[0].fdno;
00598
00599 }
00600
00601
00609 int rpmioSlurp(const char * fn,
00610 const unsigned char ** bp, ssize_t * blenp)
00611
00612 ;
00613
00614 #ifdef __cplusplus
00615 }
00616 #endif
00617
00618 #endif