/* * Read and interleave split parts from a ZIP file. */ static xps_part * xps_read_zip_part(xps_context *ctx, char *partname) { char buf[2048]; xps_entry *ent; xps_part *part; int count, size, offset, i; char *name; name = partname; if (name[0] == '/') name ++; /* All in one piece */ ent = xps_find_zip_entry(ctx, name); if (ent) { part = xps_new_part(ctx, partname, ent->usize); xps_read_zip_entry(ctx, ent, part->data); return part; } /* Count the number of pieces and their total size */ count = 0; size = 0; while (1) { sprintf(buf, "%s/[%d].piece", name, count); ent = xps_find_zip_entry(ctx, buf); if (!ent) { sprintf(buf, "%s/[%d].last.piece", name, count); ent = xps_find_zip_entry(ctx, buf); } if (!ent) break; count ++; size += ent->usize; } /* Inflate the pieces */ if (count) { part = xps_new_part(ctx, partname, size); offset = 0; for (i = 0; i < count; i++) { if (i < count - 1) sprintf(buf, "%s/[%d].piece", name, i); else sprintf(buf, "%s/[%d].last.piece", name, i); ent = xps_find_zip_entry(ctx, buf); xps_read_zip_entry(ctx, ent, part->data + offset); offset += ent->usize; } return part; } return NULL; }
/* * Read and interleave split parts from a ZIP file. */ xps_part * xps_read_part(fz_context *ctx, xps_document *doc, char *partname) { fz_archive *zip = doc->zip; fz_buffer *buf, *tmp; char path[2048]; int count; char *name; int seen_last; name = partname; if (name[0] == '/') name ++; /* All in one piece */ if (fz_has_archive_entry(ctx, zip, name)) { buf = fz_read_archive_entry(ctx, zip, name); } /* Assemble all the pieces */ else { buf = fz_new_buffer(ctx, 512); seen_last = 0; for (count = 0; !seen_last; ++count) { fz_snprintf(path, sizeof path, "%s/[%d].piece", name, count); if (fz_has_archive_entry(ctx, zip, path)) { tmp = fz_read_archive_entry(ctx, zip, path); fz_append_buffer(ctx, buf, tmp); fz_drop_buffer(ctx, tmp); } else { fz_snprintf(path, sizeof path, "%s/[%d].last.piece", name, count); if (fz_has_archive_entry(ctx, zip, path)) { tmp = fz_read_archive_entry(ctx, zip, path); fz_append_buffer(ctx, buf, tmp); fz_drop_buffer(ctx, tmp); seen_last = 1; } else { fz_drop_buffer(ctx, buf); fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find all pieces for part '%s'", partname); } } } } return xps_new_part(ctx, doc, partname, buf); }
/* * Read and interleave split parts from a ZIP file. */ xps_part * xps_read_part(fz_context *ctx, xps_document *doc, char *partname) { fz_archive *zip = doc->zip; fz_buffer *buf, *tmp; char path[2048]; unsigned char *data; int size; int count; char *name; int seen_last; name = partname; if (name[0] == '/') name ++; /* All in one piece */ if (fz_has_archive_entry(ctx, zip, name)) { buf = fz_read_archive_entry(ctx, zip, name); } /* Assemble all the pieces */ else { buf = fz_new_buffer(ctx, 512); seen_last = 0; for (count = 0; !seen_last; ++count) { sprintf(path, "%s/[%d].piece", name, count); if (fz_has_archive_entry(ctx, zip, path)) { tmp = fz_read_archive_entry(ctx, zip, path); fz_append_buffer(ctx, buf, tmp); fz_drop_buffer(ctx, tmp); } else { sprintf(path, "%s/[%d].last.piece", name, count); if (fz_has_archive_entry(ctx, zip, path)) { tmp = fz_read_archive_entry(ctx, zip, path); fz_append_buffer(ctx, buf, tmp); fz_drop_buffer(ctx, tmp); seen_last = 1; } else { fz_drop_buffer(ctx, buf); fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find all pieces for part '%s'", partname); } } } } fz_write_buffer_byte(ctx, buf, 0); /* zero-terminate */ /* take over the data */ data = buf->data; /* size doesn't include the added zero-terminator */ size = buf->len - 1; fz_free(ctx, buf); return xps_new_part(ctx, doc, partname, data, size); }
/* * Read and interleave split parts from files in the directory. */ static xps_part * xps_read_dir_part(xps_context *ctx, char *name) { char buf[2048]; xps_part *part; FILE *file; int count, size, offset, i, n; fz_strlcpy(buf, ctx->directory, sizeof buf); fz_strlcat(buf, name, sizeof buf); /* All in one piece */ file = fopen(buf, "rb"); if (file) { fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); part = xps_new_part(ctx, name, size); fread(part->data, 1, size, file); fclose(file); return part; } /* Count the number of pieces and their total size */ count = 0; size = 0; while (1) { sprintf(buf, "%s%s/[%d].piece", ctx->directory, name, count); file = fopen(buf, "rb"); if (!file) { sprintf(buf, "%s%s/[%d].last.piece", ctx->directory, name, count); file = fopen(buf, "rb"); } if (!file) break; count ++; fseek(file, 0, SEEK_END); size += ftell(file); fclose(file); } /* Inflate the pieces */ if (count) { part = xps_new_part(ctx, name, size); offset = 0; for (i = 0; i < count; i++) { if (i < count - 1) sprintf(buf, "%s%s/[%d].piece", ctx->directory, name, i); else sprintf(buf, "%s%s/[%d].last.piece", ctx->directory, name, i); file = fopen(buf, "rb"); n = fread(part->data + offset, 1, size - offset, file); offset += n; fclose(file); } return part; } return NULL; }