00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003
00009 #include <rpmio.h>
00010 #include <rpmurl.h>
00011
00012 #include <beecrypt/types.h>
00013 #include <rpmpgp.h>
00014
00015
00016 #include <beecrypt/beecrypt.h>
00017 #include <beecrypt/base64.h>
00018 #include <beecrypt/dsa.h>
00019 #include <beecrypt/endianness.h>
00020 #include <beecrypt/md5.h>
00021 #include <beecrypt/mp32.h>
00022 #include <beecrypt/rsa.h>
00023 #include <beecrypt/rsapk.h>
00024 #include <beecrypt/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 mp32barrett p;
00081 mp32barrett q;
00082 mp32number g;
00083 mp32number y;
00084 mp32number hm;
00085 mp32number r;
00086 mp32number s;
00087
00088
00089 rsapk rsa_pk;
00090 mp32number m;
00091 mp32number c;
00092 mp32number rsahm;
00093 };
00094
00097 typedef struct _FDSTACK_s {
00098 FDIO_t io;
00099 void * fp;
00100 int fdno;
00101 } FDSTACK_t;
00102
00106 typedef struct {
00107 int count;
00108 off_t bytes;
00109 time_t msecs;
00110 } OPSTAT_t;
00111
00115 enum FDSTAT_e {
00116 FDSTAT_READ = 0,
00117 FDSTAT_WRITE = 1,
00118 FDSTAT_SEEK = 2,
00119 FDSTAT_CLOSE = 3
00120 };
00121
00125 typedef struct {
00126 struct timeval create;
00127 struct timeval begin;
00128 OPSTAT_t ops[4];
00129 } * FDSTAT_t;
00130
00133 typedef struct _FDDIGEST_s {
00134 pgpHashAlgo hashalgo;
00135 DIGEST_CTX hashctx;
00136 } * FDDIGEST_t;
00137
00141 struct _FD_s {
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 void * url;
00153 int rd_timeoutsecs;
00154 ssize_t bytesRemain;
00155 ssize_t contentLength;
00156 int persist;
00157 int wr_chunked;
00158
00159 int syserrno;
00160 const void *errcookie;
00161
00162 FDSTAT_t stats;
00163
00164 int ndigests;
00165 #define FDDIGEST_MAX 4
00166 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00167
00168 int ftpFileDoneNeeded;
00169 unsigned int firstFree;
00170 long int fileSize;
00171 long int fd_cpioPos;
00172 };
00173
00174
00175 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00176
00177
00178
00179 extern int _rpmio_debug;
00180
00181
00182
00183
00184 extern int _ftp_debug;
00185
00186
00187 #define DBG(_f, _m, _x) \
00188 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x
00189
00190 #if defined(__LCLINT__XXX)
00191 #define DBGIO(_f, _x)
00192 #define DBGREFS(_f, _x)
00193 #else
00194 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00195 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00196 #endif
00197
00198 #ifdef __cplusplus
00199 extern "C" {
00200 #endif
00201
00204 int fdFgets(FD_t fd, char * buf, size_t len)
00205
00206 ;
00207
00210 FD_t ftpOpen(const char *url, int flags,
00211 mode_t mode, urlinfo *uret)
00212
00213 ;
00214
00217 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00218
00219 ;
00220
00223 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00224
00225 ;
00226
00229 int ufdClose( void * cookie)
00230
00231 ;
00232
00235 static inline
00236 FDIO_t fdGetIo(FD_t fd)
00237
00238 {
00239 FDSANE(fd);
00240
00241 return fd->fps[fd->nfps].io;
00242
00243 }
00244
00247
00248 static inline
00249 void fdSetIo(FD_t fd, FDIO_t io)
00250
00251 {
00252 FDSANE(fd);
00253
00254
00255 fd->fps[fd->nfps].io = io;
00256
00257
00258 }
00259
00260
00263 static inline
00264 FILE * fdGetFILE(FD_t fd)
00265
00266 {
00267 FDSANE(fd);
00268
00269
00270 return ((FILE *)fd->fps[fd->nfps].fp);
00271
00272
00273 }
00274
00277 static inline
00278 void * fdGetFp(FD_t fd)
00279
00280 {
00281 FDSANE(fd);
00282
00283 return fd->fps[fd->nfps].fp;
00284
00285 }
00286
00289
00290 static inline
00291 void fdSetFp(FD_t fd, void * fp)
00292
00293 {
00294 FDSANE(fd);
00295
00296
00297 fd->fps[fd->nfps].fp = fp;
00298
00299
00300 }
00301
00302
00305 static inline
00306 int fdGetFdno(FD_t fd)
00307
00308 {
00309 FDSANE(fd);
00310
00311 return fd->fps[fd->nfps].fdno;
00312
00313 }
00314
00317 static inline
00318 void fdSetFdno(FD_t fd, int fdno)
00319
00320 {
00321 FDSANE(fd);
00322
00323 fd->fps[fd->nfps].fdno = fdno;
00324
00325 }
00326
00329 static inline
00330 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00331
00332 {
00333 FDSANE(fd);
00334 fd->contentLength = fd->bytesRemain = contentLength;
00335 }
00336
00339 static inline
00340 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00341
00342 {
00343 FDSANE(fd);
00344 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00345 return;
00346 fd->nfps++;
00347 fdSetIo(fd, io);
00348 fdSetFp(fd, fp);
00349 fdSetFdno(fd, fdno);
00350 }
00351
00354 static inline void fdPop(FD_t fd)
00355
00356 {
00357 FDSANE(fd);
00358 if (fd->nfps < 0) return;
00359 fdSetIo(fd, NULL);
00360 fdSetFp(fd, NULL);
00361 fdSetFdno(fd, -1);
00362 fd->nfps--;
00363 }
00364
00367 static inline void fdstat_enter( FD_t fd, int opx)
00368
00369 {
00370 if (fd == NULL || fd->stats == NULL) return;
00371
00372 fd->stats->ops[opx].count++;
00373
00374 (void) gettimeofday(&fd->stats->begin, NULL);
00375 }
00376
00379 static inline
00380 time_t tvsub( const struct timeval * etv,
00381 const struct timeval * btv)
00382
00383 {
00384 time_t secs, usecs;
00385 if (etv == NULL || btv == NULL) return 0;
00386 secs = etv->tv_sec - btv->tv_sec;
00387 for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
00388 secs++;
00389 return ((secs * 1000) + (usecs/1000));
00390 }
00391
00394 static inline
00395 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00396
00397 {
00398 struct timeval end;
00399 if (fd == NULL) return;
00400 if (rc == -1) fd->syserrno = errno;
00401 if (fd->stats == NULL) return;
00402
00403 (void) gettimeofday(&end, NULL);
00404 if (rc >= 0) {
00405 switch(opx) {
00406 case FDSTAT_SEEK:
00407 fd->stats->ops[opx].bytes = rc;
00408 break;
00409 default:
00410 fd->stats->ops[opx].bytes += rc;
00411 if (fd->bytesRemain > 0) fd->bytesRemain -= rc;
00412 break;
00413 }
00414 }
00415 fd->stats->ops[opx].msecs += tvsub(&end, &fd->stats->begin);
00416 fd->stats->begin = end;
00417
00418 }
00419
00422
00423 static inline
00424 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00425
00426
00427 {
00428 int opx;
00429 if (fd == NULL || fd->stats == NULL) return;
00430 for (opx = 0; opx < 4; opx++) {
00431 OPSTAT_t *ops = &fd->stats->ops[opx];
00432 if (ops->count <= 0) continue;
00433 switch (opx) {
00434 case FDSTAT_READ:
00435 if (msg) fprintf(fp, "%s:", msg);
00436 fprintf(fp, "%8d reads, %8ld total bytes in %d.%03d secs\n",
00437 ops->count, (long)ops->bytes,
00438 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00439 break;
00440 case FDSTAT_WRITE:
00441 if (msg) fprintf(fp, "%s:", msg);
00442 fprintf(fp, "%8d writes, %8ld total bytes in %d.%03d secs\n",
00443 ops->count, (long)ops->bytes,
00444 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00445 break;
00446 case FDSTAT_SEEK:
00447 break;
00448 case FDSTAT_CLOSE:
00449 break;
00450 }
00451 }
00452 }
00453
00454
00457 static inline
00458 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00459
00460 {
00461 FDSANE(fd);
00462 fd->syserrno = syserrno;
00463
00464 fd->errcookie = errcookie;
00465
00466 }
00467
00470 static inline
00471 int fdGetRdTimeoutSecs(FD_t fd)
00472
00473 {
00474 FDSANE(fd);
00475 return fd->rd_timeoutsecs;
00476 }
00477
00480 static inline
00481 long int fdGetCpioPos(FD_t fd)
00482
00483 {
00484 FDSANE(fd);
00485 return fd->fd_cpioPos;
00486 }
00487
00490 static inline
00491 void fdSetCpioPos(FD_t fd, long int cpioPos)
00492
00493 {
00494 FDSANE(fd);
00495 fd->fd_cpioPos = cpioPos;
00496 }
00497
00500 static inline
00501 FD_t c2f( void * cookie)
00502
00503 {
00504
00505 FD_t fd = (FD_t) cookie;
00506
00507 FDSANE(fd);
00508 return fd;
00509 }
00510
00514 static inline
00515 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00516
00517 {
00518 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00519 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00520 fd->ndigests++;
00521 fddig->hashalgo = hashalgo;
00522 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00523 }
00524 }
00525
00529 static inline
00530 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00531
00532 {
00533 int i;
00534
00535 if (buf != NULL && buflen > 0)
00536 for (i = fd->ndigests - 1; i >= 0; i--) {
00537 FDDIGEST_t fddig = fd->digests + i;
00538 if (fddig->hashctx == NULL)
00539 continue;
00540 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00541 }
00542 }
00543
00546 static inline
00547 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00548 void ** datap,
00549 size_t * lenp,
00550 int asAscii)
00551
00552 {
00553 int imax = -1;
00554 int i;
00555
00556 for (i = fd->ndigests - 1; i >= 0; i--) {
00557 FDDIGEST_t fddig = fd->digests + i;
00558 if (fddig->hashctx == NULL)
00559 continue;
00560 if (i > imax) imax = i;
00561 if (fddig->hashalgo != hashalgo)
00562 continue;
00563 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00564 fddig->hashctx = NULL;
00565 break;
00566 }
00567
00568 if (i < 0) {
00569 if (datap) *datap = NULL;
00570 if (lenp) *lenp = 0;
00571 }
00572
00573
00574 fd->ndigests = imax;
00575 if (i < imax)
00576 fd->ndigests++;
00577 }
00578
00579
00582 static inline
00583 int fdFileno( void * cookie)
00584
00585 {
00586 FD_t fd;
00587 if (cookie == NULL) return -2;
00588 fd = c2f(cookie);
00589
00590 return fd->fps[0].fdno;
00591
00592 }
00593
00594
00602 int rpmioSlurp(const char * fn,
00603 const unsigned char ** bp, ssize_t * blenp)
00604
00605 ;
00606
00607 #ifdef __cplusplus
00608 }
00609 #endif
00610
00611 #endif