00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003
00009 #include <rpmio.h>
00010 #include <rpmurl.h>
00011
00012 #include <rpmpgp.h>
00013 #include <rpmsw.h>
00014 #include <base64.h>
00015
00016 #include "nss.h"
00017 #include "sechash.h"
00018 #include "keyhi.h"
00019 #include "cryptohi.h"
00020
00024 struct pgpDigParams_s {
00025
00026 const char * userid;
00027
00028 const byte * hash;
00029 const char * params[4];
00030 byte tag;
00031
00032 byte version;
00033 byte time[4];
00034 byte pubkey_algo;
00036 byte hash_algo;
00037 byte sigtype;
00038 byte hashlen;
00039 byte signhash16[2];
00040 byte signid[8];
00041 byte saved;
00042 #define PGPDIG_SAVED_TIME (1 << 0)
00043 #define PGPDIG_SAVED_ID (1 << 1)
00044
00045 };
00046
00050 struct pgpDig_s {
00051 struct pgpDigParams_s signature;
00052 struct pgpDigParams_s pubkey;
00053
00054 size_t nbytes;
00056
00057 DIGEST_CTX sha1ctx;
00058
00059 DIGEST_CTX hdrsha1ctx;
00060
00061 void * sha1;
00062 size_t sha1len;
00064
00065 DIGEST_CTX md5ctx;
00066
00067 DIGEST_CTX hdrmd5ctx;
00068
00069 void * md5;
00070 size_t md5len;
00072
00073 SECKEYPublicKey *dsa;
00074 SECItem *dsasig;
00075
00076
00077 SECKEYPublicKey *rsa;
00078 SECItem *rsasig;
00079 };
00080
00083 typedef struct _FDSTACK_s {
00084
00085 FDIO_t io;
00086
00087 void * fp;
00088 int fdno;
00089 } FDSTACK_t;
00090
00094 typedef enum fdOpX_e {
00095 FDSTAT_READ = 0,
00096 FDSTAT_WRITE = 1,
00097 FDSTAT_SEEK = 2,
00098 FDSTAT_CLOSE = 3,
00099 FDSTAT_DIGEST = 4,
00100 FDSTAT_MAX = 5
00101 } fdOpX;
00102
00106 typedef struct {
00107 struct rpmop_s ops[FDSTAT_MAX];
00108 } * FDSTAT_t;
00109
00112 typedef struct _FDDIGEST_s {
00113 pgpHashAlgo hashalgo;
00114 DIGEST_CTX hashctx;
00115 } * FDDIGEST_t;
00116
00120 struct _FD_s {
00121
00122 int nrefs;
00123 int flags;
00124 #define RPMIO_DEBUG_IO 0x40000000
00125 #define RPMIO_DEBUG_REFS 0x20000000
00126 int magic;
00127 #define FDMAGIC 0x04463138
00128 int nfps;
00129 FDSTACK_t fps[8];
00130 int urlType;
00131
00132
00133 void * url;
00134
00135 void * req;
00136
00137 int rd_timeoutsecs;
00138 ssize_t bytesRemain;
00139 ssize_t contentLength;
00140 int persist;
00141 int wr_chunked;
00142
00143 int syserrno;
00144
00145 const void *errcookie;
00146
00147 FDSTAT_t stats;
00148
00149 int ndigests;
00150 #define FDDIGEST_MAX 4
00151 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00152
00153 int ftpFileDoneNeeded;
00154 unsigned int firstFree;
00155 long int fileSize;
00156 long int fd_cpioPos;
00157 };
00158
00159
00160 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00161
00162
00163
00164 extern int _rpmio_debug;
00165
00166
00167
00168
00169 extern int _av_debug;
00170
00171
00172
00173
00174 extern int _ftp_debug;
00175
00176
00177
00178
00179 extern int _dav_debug;
00180
00181
00182 #define DBG(_f, _m, _x) \
00183 \
00184 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00185
00186
00187 #if defined(__LCLINT__XXX)
00188 #define DBGIO(_f, _x)
00189 #define DBGREFS(_f, _x)
00190 #else
00191 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00192 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00193 #endif
00194
00195 #ifdef __cplusplus
00196 extern "C" {
00197 #endif
00198
00201 int fdFgets(FD_t fd, char * buf, size_t len)
00202
00203 ;
00204
00207 FD_t ftpOpen(const char *url, int flags,
00208 mode_t mode, urlinfo *uret)
00209
00210 ;
00211
00214 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00215
00216 ;
00217
00220 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00221
00222 ;
00223
00226 int ufdClose( void * cookie)
00227
00228 ;
00229
00232 static inline
00233 FDIO_t fdGetIo(FD_t fd)
00234
00235 {
00236 FDSANE(fd);
00237
00238 return fd->fps[fd->nfps].io;
00239
00240 }
00241
00244
00245 static inline
00246 void fdSetIo(FD_t fd, FDIO_t io)
00247
00248 {
00249 FDSANE(fd);
00250
00251
00252 fd->fps[fd->nfps].io = io;
00253
00254
00255 }
00256
00257
00260 static inline
00261 FILE * fdGetFILE(FD_t fd)
00262
00263 {
00264 FDSANE(fd);
00265
00266
00267 return ((FILE *)fd->fps[fd->nfps].fp);
00268
00269
00270 }
00271
00274 static inline
00275 void * fdGetFp(FD_t fd)
00276
00277 {
00278 FDSANE(fd);
00279
00280 return fd->fps[fd->nfps].fp;
00281
00282 }
00283
00286
00287 static inline
00288 void fdSetFp(FD_t fd, void * fp)
00289
00290 {
00291 FDSANE(fd);
00292
00293
00294 fd->fps[fd->nfps].fp = fp;
00295
00296
00297 }
00298
00299
00302 static inline
00303 int fdGetFdno(FD_t fd)
00304
00305 {
00306 FDSANE(fd);
00307
00308 return fd->fps[fd->nfps].fdno;
00309
00310 }
00311
00314 static inline
00315 void fdSetFdno(FD_t fd, int fdno)
00316
00317 {
00318 FDSANE(fd);
00319
00320 fd->fps[fd->nfps].fdno = fdno;
00321
00322 }
00323
00326 static inline
00327 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00328
00329 {
00330 FDSANE(fd);
00331 fd->contentLength = fd->bytesRemain = contentLength;
00332 }
00333
00336 static inline
00337 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00338
00339 {
00340 FDSANE(fd);
00341 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00342 return;
00343 fd->nfps++;
00344 fdSetIo(fd, io);
00345 fdSetFp(fd, fp);
00346 fdSetFdno(fd, fdno);
00347 }
00348
00351 static inline
00352 void fdPop(FD_t fd)
00353
00354 {
00355 FDSANE(fd);
00356 if (fd->nfps < 0) return;
00357 fdSetIo(fd, NULL);
00358 fdSetFp(fd, NULL);
00359 fdSetFdno(fd, -1);
00360 fd->nfps--;
00361 }
00362
00365 static inline
00366 rpmop fdstat_op( FD_t fd, fdOpX opx)
00367
00368 {
00369 rpmop op = NULL;
00370
00371
00372 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00373 op = fd->stats->ops + opx;
00374
00375 return op;
00376 }
00377
00380 static inline
00381 void fdstat_enter( FD_t fd, int opx)
00382
00383
00384 {
00385 if (fd == NULL) return;
00386 if (fd->stats != NULL)
00387 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00388 }
00389
00392 static inline
00393 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00394
00395
00396 {
00397 if (fd == NULL) return;
00398 if (rc == -1)
00399 fd->syserrno = errno;
00400 else if (rc > 0 && fd->bytesRemain > 0)
00401 switch (opx) {
00402 case FDSTAT_READ:
00403 case FDSTAT_WRITE:
00404 fd->bytesRemain -= rc;
00405 break;
00406 default:
00407 break;
00408 }
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