+
+ return rc;
+}
+
+struct lbx_file_state *lbx_file_open(struct lbx *pub, unsigned fileno)
+{
+ struct lbx_priv *lbx = (struct lbx_priv *)pub;
+ struct lbx_file_state *state;
+
+ if (fileno >= lbx->pub.nfiles) {
+ lbx_error_raise(LBX_ENOENT);
+ return NULL;
+ }
+
+ lbx->last_file = NULL;
+ if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
+ return NULL;
+ }
+
+ state = malloc(sizeof *state);
+ if (!state) {
+ lbx_error_raise(LBX_ENOMEM);
+ 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)
+ return 0;
+ f->lbx->last_file = f;
+ }
+
+ rc = fops->read(buf, want, f->lbx->f);
+ f->offset += rc;
+
+ if (want < n || (rc < want && fops->eof(f->lbx->f)))
+ f->eof = 1;
+ return rc;