00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef lint
00034 static char copyright[] =
00035 "@(#) Copyright (c) 2002-2004\n\
00036 Netherlands Forensic Institute. All rights reserved.\n";
00037 #endif
00038
00039 #ifdef HAVE_CONFIG_H
00040 #include "config.h"
00041 #endif
00042
00043 #include <stdlib.h>
00044 #include <sys/types.h>
00045 #include <errno.h>
00046 #include <unistd.h>
00047
00048 #include "rdd.h"
00049 #include "reader.h"
00050
00051 typedef struct _RDD_FD_READER {
00052 int fd;
00053 } RDD_FD_READER;
00054
00055
00056
00057
00058 static int rdd_fd_read(RDD_READER *r, unsigned char *buf, unsigned nbyte,
00059 unsigned *nread);
00060 static int rdd_fd_tell(RDD_READER *r, rdd_count_t *pos);
00061 static int rdd_fd_seek(RDD_READER *r, rdd_count_t pos);
00062 static int rdd_fd_close(RDD_READER *r, int recurse);
00063
00064 static RDD_READ_OPS fd_read_ops = {
00065 rdd_fd_read,
00066 rdd_fd_tell,
00067 rdd_fd_seek,
00068 rdd_fd_close
00069 };
00070
00071 int
00072 rdd_open_fd_reader(RDD_READER **self, int fd)
00073 {
00074 RDD_READER *r = 0;
00075 RDD_FD_READER *state = 0;
00076 int rc = RDD_OK;
00077
00078 rc = rdd_new_reader(&r, &fd_read_ops, sizeof(RDD_FD_READER));
00079 if (rc != RDD_OK) {
00080 return rc;
00081 }
00082
00083 state = (RDD_FD_READER *) r->state;
00084 state->fd = fd;
00085
00086 *self = r;
00087 return RDD_OK;
00088 }
00089
00090 static int
00091 rdd_fd_read(RDD_READER *self, unsigned char *buf, unsigned nbyte,
00092 unsigned *nread)
00093 {
00094 RDD_FD_READER *state = self->state;
00095 unsigned char *next = buf;
00096 int n;
00097
00098 while (nbyte > 0) {
00099 n = read(state->fd, next, nbyte);
00100 if (n < 0) {
00101 #if defined(RDD_SIGNALS)
00102 if (errno == EINTR) continue;
00103 #endif
00104 return RDD_EREAD;
00105 } else if (n == 0) {
00106 break;
00107 }
00108 nbyte -= n;
00109 next += n;
00110 }
00111
00112 *nread = next - buf;
00113 return RDD_OK;
00114 }
00115
00116 static int
00117 rdd_fd_tell(RDD_READER *self, rdd_count_t *pos)
00118 {
00119 RDD_FD_READER *state = self->state;
00120 off_t offset;
00121
00122 if ((offset = lseek(state->fd, (off_t) 0, SEEK_CUR)) == (off_t) -1) {
00123 return RDD_ETELL;
00124 }
00125
00126 *pos = (rdd_count_t) offset;
00127 return RDD_OK;
00128 }
00129
00130 static int
00131 rdd_fd_seek(RDD_READER *self, rdd_count_t pos)
00132 {
00133 RDD_FD_READER *state = self->state;
00134
00135 if ((lseek(state->fd, (off_t) pos, SEEK_SET)) == (off_t) -1) {
00136 return RDD_ESEEK;
00137 }
00138 return RDD_OK;
00139 }
00140
00141 static int
00142 rdd_fd_close(RDD_READER *self, int recurse )
00143 {
00144 if (self == 0) {
00145 return RDD_BADARG;
00146 }
00147 int rc = RDD_OK;
00148
00149 RDD_FD_READER *state = self->state;
00150
00151 if (close(state->fd) < 0) {
00152 rc = RDD_ECLOSE;
00153 }
00154
00155 return rc;
00156 }