-
- base = lbx->offsets[index];
- len = lbx->offsets[index+1] - lbx->offsets[index];
-
- /* Seek to the beginning of the file. */
- if (lbx->foff != base) {
- int seekrc;
- long dist = (lbx->foff < base) ? (base - lbx->foff)
- : -(lbx->foff - base);
-
- seekrc = fseek(lbx->f, dist, SEEK_CUR);
- if (seekrc == -1 && lbx->foff < base) {
- if (skipdata(lbx->f, dist) == -1) {
- /* lbx_errno set by skipdata */
- return 0;
- }
- } else if (seekrc == -1) {
- lbx_errno = -errno;
+
+ lbx->last_file = NULL;
+ if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
+ lbx_errno = -errno;
+ return NULL;
+ }
+
+ state = malloc(sizeof *state);
+ if (!state) {
+ lbx_errno = -errno;
+ return NULL;
+ }
+
+ *state = (struct lbx_file_state) {
+ .base = lbx->offsets[fileno],
+ .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
+ .lbx = lbx,
+ };
+
+ lbx->last_file = state;
+ return state;
+}
+
+size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
+{
+ const struct lbx_file_ops *fops = f->lbx->fops;
+ size_t want = MIN(n, f->limit - f->offset);
+ size_t rc;
+
+ if (f != f->lbx->last_file) {
+ f->lbx->last_file = NULL;
+ if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)