1 #ifndef H_RPMIO_INTERNAL
2 #define H_RPMIO_INTERNAL
5 * \file rpmio/rpmio_internal.h
12 #include <beecrypt/types.h>
16 /* Drag in the beecrypt includes. */
17 #include <beecrypt/beecrypt.h>
18 #include <beecrypt/base64.h>
19 #include <beecrypt/dsa.h>
20 #include <beecrypt/endianness.h>
21 #include <beecrypt/md5.h>
22 #include <beecrypt/mp32.h>
23 #include <beecrypt/rsa.h>
24 #include <beecrypt/rsapk.h>
25 #include <beecrypt/sha1.h>
28 * Values parsed from OpenPGP signature/pubkey packet(s).
30 struct pgpDigParams_s {
35 const char * params[4];
38 byte version; /*!< version number. */
39 byte time[4]; /*!< time that the key was created. */
40 byte pubkey_algo; /*!< public key algorithm. */
48 #define PGPDIG_SAVED_TIME (1 << 0)
49 #define PGPDIG_SAVED_ID (1 << 1)
54 * Container for values parsed from an OpenPGP signature and public key.
57 struct pgpDigParams_s signature;
58 struct pgpDigParams_s pubkey;
60 size_t nbytes; /*!< No. bytes of plain text. */
63 DIGEST_CTX sha1ctx; /*!< (dsa) sha1 hash context. */
65 DIGEST_CTX hdrsha1ctx; /*!< (dsa) header sha1 hash context. */
67 void * sha1; /*!< (dsa) V3 signature hash. */
68 size_t sha1len; /*!< (dsa) V3 signature hash length. */
71 DIGEST_CTX md5ctx; /*!< (rsa) md5 hash context. */
74 DIGEST_CTX hdrmd5ctx; /*!< (rsa) header md5 hash context. */
77 void * md5; /*!< (rsa) V3 signature hash. */
78 size_t md5len; /*!< (rsa) V3 signature hash length. */
98 typedef struct _FDSTACK_s {
100 /*@dependent@*/ void * fp;
105 * Identify per-desciptor I/O operation statistics.
108 FDSTAT_READ = 0, /*!< Read statistics index. */
109 FDSTAT_WRITE = 1, /*!< Write statistics index. */
110 FDSTAT_SEEK = 2, /*!< Seek statistics index. */
111 FDSTAT_CLOSE = 3, /*!< Close statistics index */
112 FDSTAT_DIGEST = 4, /*!< Digest statistics index. */
117 * Cumulative statistics for a descriptor.
119 typedef /*@abstract@*/ struct {
120 struct rpmop_s ops[FDSTAT_MAX]; /*!< Cumulative statistics. */
125 typedef struct _FDDIGEST_s {
126 pgpHashAlgo hashalgo;
131 * The FD_t File Handle data structure.
137 #define RPMIO_DEBUG_IO 0x40000000
138 #define RPMIO_DEBUG_REFS 0x20000000
140 #define FDMAGIC 0x04463138
143 int urlType; /* ufdio: */
146 void * url; /* ufdio: URL info */
147 int rd_timeoutsecs; /* ufdRead: per FD_t timer */
148 ssize_t bytesRemain; /* ufdio: */
149 ssize_t contentLength; /* ufdio: */
150 int persist; /* ufdio: */
151 int wr_chunked; /* ufdio: */
153 int syserrno; /* last system errno encountered */
155 const void *errcookie; /* gzdio/bzdio/ufdio: */
157 FDSTAT_t stats; /* I/O statistics */
160 #define FDDIGEST_MAX 4
161 struct _FDDIGEST_s digests[FDDIGEST_MAX];
163 int ftpFileDoneNeeded; /* ufdio: (FTP) */
164 unsigned int firstFree; /* fadio: */
165 long int fileSize; /* fadio: */
166 long int fd_cpioPos; /* cpio: */
170 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
174 extern int _rpmio_debug;
179 extern int _ftp_debug;
182 #define DBG(_f, _m, _x) \
183 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x
185 #if defined(__LCLINT__XXX)
186 #define DBGIO(_f, _x)
187 #define DBGREFS(_f, _x)
189 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
190 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
199 int fdFgets(FD_t fd, char * buf, size_t len)
200 /*@globals errno, fileSystem @*/
201 /*@modifies *buf, fd, errno, fileSystem @*/;
205 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
206 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
207 /*@globals fileSystem, internalState @*/
208 /*@modifies *uret, fileSystem, internalState @*/;
212 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
213 /*@globals fileSystem, internalState @*/
214 /*@modifies data, fileSystem, internalState @*/;
218 int ftpCmd(const char * cmd, const char * url, const char * arg2)
219 /*@globals fileSystem, internalState @*/
220 /*@modifies fileSystem, internalState @*/;
224 int ufdClose( /*@only@*/ void * cookie)
225 /*@globals fileSystem, internalState @*/
226 /*@modifies cookie, fileSystem, internalState @*/;
230 /*@unused@*/ static inline
231 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
236 return fd->fps[fd->nfps].io;
242 /*@-nullstate@*/ /* FIX: io may be NULL */
243 /*@unused@*/ static inline
244 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
250 fd->fps[fd->nfps].io = io;
258 /*@unused@*/ static inline
259 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
265 return ((FILE *)fd->fps[fd->nfps].fp);
272 /*@unused@*/ static inline
273 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
278 return fd->fps[fd->nfps].fp;
284 /*@-nullstate@*/ /* FIX: fp may be NULL */
285 /*@unused@*/ static inline
286 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
292 fd->fps[fd->nfps].fp = fp;
300 /*@unused@*/ static inline
301 int fdGetFdno(FD_t fd)
306 return fd->fps[fd->nfps].fdno;
312 /*@unused@*/ static inline
313 void fdSetFdno(FD_t fd, int fdno)
318 fd->fps[fd->nfps].fdno = fdno;
324 /*@unused@*/ static inline
325 void fdSetContentLength(FD_t fd, ssize_t contentLength)
329 fd->contentLength = fd->bytesRemain = contentLength;
334 /*@unused@*/ static inline
335 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
339 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
349 /*@unused@*/ static inline void fdPop(FD_t fd)
353 if (fd->nfps < 0) return;
362 /*@unused@*/ static inline
363 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
364 /*@globals internalState @*/
365 /*@modifies fd, internalState @*/
367 if (fd == NULL) return;
369 if (fd->stats != NULL)
370 (void) rpmswEnter(&fd->stats->ops[opx], 0);
376 /*@unused@*/ static inline
377 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
378 /*@globals internalState @*/
379 /*@modifies fd, internalState @*/
381 if (fd == NULL) return;
383 fd->syserrno = errno;
384 else if (rc > 0 && fd->bytesRemain > 0)
385 fd->bytesRemain -= rc;
387 if (fd->stats != NULL)
388 (void) rpmswExit(&fd->stats->ops[opx], rc);
395 /*@unused@*/ static inline
396 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
397 /*@globals fileSystem @*/
398 /*@modifies *fp, fileSystem @*/
400 static int usec_scale = (1000*1000);
403 if (fd == NULL || fd->stats == NULL) return;
404 for (opx = 0; opx < 4; opx++) {
405 rpmop op = &fd->stats->ops[opx];
406 if (op->count <= 0) continue;
409 if (msg) fprintf(fp, "%s:", msg);
410 fprintf(fp, "%8d reads, %8ld total bytes in %d.%06d secs\n",
411 op->count, (long)op->bytes,
412 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
413 /*@switchbreak@*/ break;
415 if (msg) fprintf(fp, "%s:", msg);
416 fprintf(fp, "%8d writes, %8ld total bytes in %d.%06d secs\n",
417 op->count, (long)op->bytes,
418 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
419 /*@switchbreak@*/ break;
421 /*@switchbreak@*/ break;
423 /*@switchbreak@*/ break;
431 /*@unused@*/ static inline
432 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
436 fd->syserrno = syserrno;
438 fd->errcookie = errcookie;
444 /*@unused@*/ static inline
445 int fdGetRdTimeoutSecs(FD_t fd)
449 return fd->rd_timeoutsecs;
454 /*@unused@*/ static inline
455 long int fdGetCpioPos(FD_t fd)
459 return fd->fd_cpioPos;
464 /*@unused@*/ static inline
465 void fdSetCpioPos(FD_t fd, long int cpioPos)
469 fd->fd_cpioPos = cpioPos;
474 /*@mayexit@*/ /*@unused@*/ static inline
475 FD_t c2f(/*@null@*/ void * cookie)
479 FD_t fd = (FD_t) cookie;
482 /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
486 * Attach digest to fd.
488 /*@unused@*/ static inline
489 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
492 FDDIGEST_t fddig = fd->digests + fd->ndigests;
493 if (fddig != (fd->digests + FDDIGEST_MAX)) {
495 fddig->hashalgo = hashalgo;
496 fdstat_enter(fd, FDSTAT_DIGEST);
497 fddig->hashctx = rpmDigestInit(hashalgo, flags);
498 fdstat_exit(fd, FDSTAT_DIGEST, 0);
503 * Update digest(s) attached to fd.
505 /*@unused@*/ static inline
506 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
511 if (buf != NULL && buflen > 0)
512 for (i = fd->ndigests - 1; i >= 0; i--) {
513 FDDIGEST_t fddig = fd->digests + i;
514 if (fddig->hashctx == NULL)
516 fdstat_enter(fd, FDSTAT_DIGEST);
517 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
518 fdstat_exit(fd, FDSTAT_DIGEST, buflen);
524 /*@unused@*/ static inline
525 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
526 /*@null@*/ /*@out@*/ void ** datap,
527 /*@null@*/ /*@out@*/ size_t * lenp,
529 /*@modifies fd, *datap, *lenp @*/
534 for (i = fd->ndigests - 1; i >= 0; i--) {
535 FDDIGEST_t fddig = fd->digests + i;
536 if (fddig->hashctx == NULL)
538 if (i > imax) imax = i;
539 if (fddig->hashalgo != hashalgo)
541 fdstat_enter(fd, FDSTAT_DIGEST);
542 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
543 fdstat_exit(fd, FDSTAT_DIGEST, 0);
544 fddig->hashctx = NULL;
549 if (datap) *datap = NULL;
556 fd->ndigests++; /* convert index to count */
562 /*@unused@*/ static inline
563 int fdFileno(/*@null@*/ void * cookie)
567 if (cookie == NULL) return -2;
570 return fd->fps[0].fdno;
576 * Read an entire file into a buffer.
577 * @param fn file name to read
578 * @retval *bp (malloc'd) buffer address
579 * @retval *blenp (malloc'd) buffer length
580 * @return 0 on success
582 int rpmioSlurp(const char * fn,
583 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
584 /*@globals fileSystem, internalState @*/
585 /*@modifies *bp, *blenp, fileSystem, internalState @*/;
591 #endif /* H_RPMIO_INTERNAL */