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 #include <rpmsw.h>
00015
00016
00017 #include <beecrypt.h>
00018 #include <base64.h>
00019 #include <dsa.h>
00020 #include <endianness.h>
00021 #include <md5.h>
00022 #include <mp.h>
00023 #include <rsa.h>
00024 #include <rsapk.h>
00025 #include <sha1.h>
00026
00030 struct pgpDigParams_s {
00031
00032 const char * userid;
00033
00034 const byte * hash;
00035 const char * params[4];
00036 byte tag;
00037
00038 byte version;
00039 byte time[4];
00040 byte pubkey_algo;
00042 byte hash_algo;
00043 byte sigtype;
00044 byte hashlen;
00045 byte signhash16[2];
00046 byte signid[8];
00047 byte saved;
00048 #define PGPDIG_SAVED_TIME (1 << 0)
00049 #define PGPDIG_SAVED_ID (1 << 1)
00050
00051 };
00052
00056 struct pgpDig_s {
00057 struct pgpDigParams_s signature;
00058 struct pgpDigParams_s pubkey;
00059
00060 size_t nbytes;
00062
00063 DIGEST_CTX sha1ctx;
00064
00065 DIGEST_CTX hdrsha1ctx;
00066
00067 void * sha1;
00068 size_t sha1len;
00070
00071 DIGEST_CTX md5ctx;
00072 #ifdef NOTYET
00073
00074 DIGEST_CTX hdrmd5ctx;
00075 #endif
00076
00077 void * md5;
00078 size_t md5len;
00080
00081 mpbarrett p;
00082 mpbarrett q;
00083 mpnumber g;
00084 mpnumber y;
00085 mpnumber hm;
00086 mpnumber r;
00087 mpnumber s;
00088
00089
00090 rsapk rsa_pk;
00091 mpnumber m;
00092 mpnumber c;
00093 mpnumber rsahm;
00094 };
00095
00098 typedef struct _FDSTACK_s {
00099
00100 FDIO_t io;
00101
00102 void * fp;
00103 int fdno;
00104 } FDSTACK_t;
00105
00109 typedef enum fdOpX_e {
00110 FDSTAT_READ = 0,
00111 FDSTAT_WRITE = 1,
00112 FDSTAT_SEEK = 2,
00113 FDSTAT_CLOSE = 3,
00114 FDSTAT_DIGEST = 4,
00115 FDSTAT_MAX = 5
00116 } fdOpX;
00117
00121 typedef struct {
00122 struct rpmop_s ops[FDSTAT_MAX];
00123 } * FDSTAT_t;
00124
00127 typedef struct _FDDIGEST_s {
00128 pgpHashAlgo hashalgo;
00129 DIGEST_CTX hashctx;
00130 } * FDDIGEST_t;
00131
00135 struct _FD_s {
00136
00137 int nrefs;
00138 int flags;
00139 #define RPMIO_DEBUG_IO 0x40000000
00140 #define RPMIO_DEBUG_REFS 0x20000000
00141 int magic;
00142 #define FDMAGIC 0x04463138
00143 int nfps;
00144 FDSTACK_t fps[8];
00145 int urlType;
00146
00147
00148 void * url;
00149 int rd_timeoutsecs;
00150 ssize_t bytesRemain;
00151 ssize_t contentLength;
00152 int persist;
00153 int wr_chunked;
00154
00155 int syserrno;
00156
00157 const void *errcookie;
00158
00159 FDSTAT_t stats;
00160
00161 int ndigests;
00162 #define FDDIGEST_MAX 4
00163 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00164
00165 int ftpFileDoneNeeded;
00166 unsigned int firstFree;
00167 long int fileSize;
00168 long int fd_cpioPos;
00169 };
00170
00171
00172 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00173
00174
00175
00176 extern int _rpmio_debug;
00177
00178
00179
00180
00181 extern int _ftp_debug;
00182
00183
00184 #define DBG(_f, _m, _x) \
00185 \
00186 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00187
00188
00189 #if defined(__LCLINT__XXX)
00190 #define DBGIO(_f, _x)
00191 #define DBGREFS(_f, _x)
00192 #else
00193 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00194 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00195 #endif
00196
00197 #ifdef __cplusplus
00198 extern "C" {
00199 #endif
00200
00203 int fdFgets(FD_t fd, char * buf, size_t len)
00204
00205 ;
00206
00209 FD_t ftpOpen(const char *url, int flags,
00210 mode_t mode, urlinfo *uret)
00211
00212 ;
00213
00216 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00217
00218 ;
00219
00222 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00223
00224 ;
00225
00228 int ufdClose( void * cookie)
00229
00230 ;
00231
00234 static inline
00235 FDIO_t fdGetIo(FD_t fd)
00236
00237 {
00238 FDSANE(fd);
00239
00240 return fd->fps[fd->nfps].io;
00241
00242 }
00243
00246
00247 static inline
00248 void fdSetIo(FD_t fd, FDIO_t io)
00249
00250 {
00251 FDSANE(fd);
00252
00253
00254 fd->fps[fd->nfps].io = io;
00255
00256
00257 }
00258
00259
00262 static inline
00263 FILE * fdGetFILE(FD_t fd)
00264
00265 {
00266 FDSANE(fd);
00267
00268
00269 return ((FILE *)fd->fps[fd->nfps].fp);
00270
00271
00272 }
00273
00276 static inline
00277 void * fdGetFp(FD_t fd)
00278
00279 {
00280 FDSANE(fd);
00281
00282 return fd->fps[fd->nfps].fp;
00283
00284 }
00285
00288
00289 static inline
00290 void fdSetFp(FD_t fd, void * fp)
00291
00292 {
00293 FDSANE(fd);
00294
00295
00296 fd->fps[fd->nfps].fp = fp;
00297
00298
00299 }
00300
00301
00304 static inline
00305 int fdGetFdno(FD_t fd)
00306
00307 {
00308 FDSANE(fd);
00309
00310 return fd->fps[fd->nfps].fdno;
00311
00312 }
00313
00316 static inline
00317 void fdSetFdno(FD_t fd, int fdno)
00318
00319 {
00320 FDSANE(fd);
00321
00322 fd->fps[fd->nfps].fdno = fdno;
00323
00324 }
00325
00328 static inline
00329 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00330
00331 {
00332 FDSANE(fd);
00333 fd->contentLength = fd->bytesRemain = contentLength;
00334 }
00335
00338 static inline
00339 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00340
00341 {
00342 FDSANE(fd);
00343 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00344 return;
00345 fd->nfps++;
00346 fdSetIo(fd, io);
00347 fdSetFp(fd, fp);
00348 fdSetFdno(fd, fdno);
00349 }
00350
00353 static inline
00354 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
00368 rpmop fdstat_op( FD_t fd, fdOpX opx)
00369
00370 {
00371 rpmop op = NULL;
00372
00373
00374 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00375 op = fd->stats->ops + opx;
00376
00377 return op;
00378 }
00379
00382 static inline
00383 void fdstat_enter( FD_t fd, int opx)
00384
00385
00386 {
00387 if (fd == NULL) return;
00388 if (fd->stats != NULL)
00389 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00390 }
00391
00394 static inline
00395 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00396
00397
00398 {
00399 if (fd == NULL) return;
00400 if (rc == -1)
00401 fd->syserrno = errno;
00402 else if (rc > 0 && fd->bytesRemain > 0)
00403 fd->bytesRemain -= rc;
00404 if (fd->stats != NULL)
00405 (void) rpmswExit(fdstat_op(fd, opx), rc);
00406 }
00407
00410
00411 static inline
00412 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00413
00414
00415 {
00416 static int usec_scale = (1000*1000);
00417 int opx;
00418
00419 if (fd == NULL || fd->stats == NULL) return;
00420 for (opx = 0; opx < 4; opx++) {
00421 rpmop op = &fd->stats->ops[opx];
00422 if (op->count <= 0) continue;
00423 switch (opx) {
00424 case FDSTAT_READ:
00425 if (msg) fprintf(fp, "%s:", msg);
00426 fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
00427 op->count, (long)op->bytes,
00428 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00429 break;
00430 case FDSTAT_WRITE:
00431 if (msg) fprintf(fp, "%s:", msg);
00432 fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
00433 op->count, (long)op->bytes,
00434 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00435 break;
00436 case FDSTAT_SEEK:
00437 break;
00438 case FDSTAT_CLOSE:
00439 break;
00440 }
00441 }
00442 }
00443
00444
00447 static inline
00448 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00449
00450 {
00451 FDSANE(fd);
00452 fd->syserrno = syserrno;
00453
00454 fd->errcookie = errcookie;
00455
00456 }
00457
00460 static inline
00461 int fdGetRdTimeoutSecs(FD_t fd)
00462
00463 {
00464 FDSANE(fd);
00465 return fd->rd_timeoutsecs;
00466 }
00467
00470 static inline
00471 long int fdGetCpioPos(FD_t fd)
00472
00473 {
00474 FDSANE(fd);
00475 return fd->fd_cpioPos;
00476 }
00477
00480 static inline
00481 void fdSetCpioPos(FD_t fd, long int cpioPos)
00482
00483 {
00484 FDSANE(fd);
00485 fd->fd_cpioPos = cpioPos;
00486 }
00487
00490 static inline
00491 FD_t c2f( void * cookie)
00492
00493 {
00494
00495 FD_t fd = (FD_t) cookie;
00496
00497 FDSANE(fd);
00498 return fd;
00499 }
00500
00504 static inline
00505 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00506
00507
00508 {
00509 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00510 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00511 fd->ndigests++;
00512 fddig->hashalgo = hashalgo;
00513 fdstat_enter(fd, FDSTAT_DIGEST);
00514 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00515 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00516 }
00517 }
00518
00522 static inline
00523 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00524
00525
00526 {
00527 int i;
00528
00529 if (buf != NULL && buflen > 0)
00530 for (i = fd->ndigests - 1; i >= 0; i--) {
00531 FDDIGEST_t fddig = fd->digests + i;
00532 if (fddig->hashctx == NULL)
00533 continue;
00534 fdstat_enter(fd, FDSTAT_DIGEST);
00535 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00536 fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00537 }
00538 }
00539
00542 static inline
00543 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00544 void ** datap,
00545 size_t * lenp,
00546 int asAscii)
00547
00548
00549 {
00550 int imax = -1;
00551 int i;
00552
00553 for (i = fd->ndigests - 1; i >= 0; i--) {
00554 FDDIGEST_t fddig = fd->digests + i;
00555 if (fddig->hashctx == NULL)
00556 continue;
00557 if (i > imax) imax = i;
00558 if (fddig->hashalgo != hashalgo)
00559 continue;
00560 fdstat_enter(fd, FDSTAT_DIGEST);
00561 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00562 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00563 fddig->hashctx = NULL;
00564 break;
00565 }
00566
00567 if (i < 0) {
00568 if (datap) *datap = NULL;
00569 if (lenp) *lenp = 0;
00570 }
00571
00572
00573 fd->ndigests = imax;
00574 if (i < imax)
00575 fd->ndigests++;
00576 }
00577
00578
00581 static inline
00582 int fdFileno( void * cookie)
00583
00584 {
00585 FD_t fd;
00586 if (cookie == NULL) return -2;
00587 fd = c2f(cookie);
00588
00589 return fd->fps[0].fdno;
00590
00591 }
00592
00593
00601 int rpmioSlurp(const char * fn,
00602 const unsigned char ** bp, ssize_t * blenp)
00603
00604 ;
00605
00606 #ifdef __cplusplus
00607 }
00608 #endif
00609
00610 #endif