Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/xps/xps-zip.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2024 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 #include "mupdf/fitz.h" | |
| 24 #include "xps-imp.h" | |
| 25 | |
| 26 #include <string.h> | |
| 27 | |
| 28 static void xps_init_document(fz_context *ctx, xps_document *doc); | |
| 29 | |
| 30 static xps_part * | |
| 31 xps_new_part(fz_context *ctx, xps_document *doc, char *name, fz_buffer *data) | |
| 32 { | |
| 33 xps_part *part = NULL; | |
| 34 | |
| 35 fz_var(part); | |
| 36 | |
| 37 fz_try(ctx) | |
| 38 { | |
| 39 part = fz_malloc_struct(ctx, xps_part); | |
| 40 part->name = fz_strdup(ctx, name); | |
| 41 part->data = data; /* take ownership of buffer */ | |
| 42 } | |
| 43 fz_catch(ctx) | |
| 44 { | |
| 45 fz_drop_buffer(ctx, data); | |
| 46 fz_free(ctx, part); | |
| 47 fz_rethrow(ctx); | |
| 48 } | |
| 49 | |
| 50 return part; | |
| 51 } | |
| 52 | |
| 53 void | |
| 54 xps_drop_part(fz_context *ctx, xps_document *doc, xps_part *part) | |
| 55 { | |
| 56 fz_free(ctx, part->name); | |
| 57 fz_drop_buffer(ctx, part->data); | |
| 58 fz_free(ctx, part); | |
| 59 } | |
| 60 | |
| 61 xps_part * | |
| 62 xps_read_part(fz_context *ctx, xps_document *doc, char *partname) | |
| 63 { | |
| 64 fz_archive *zip = doc->zip; | |
| 65 fz_buffer *buf = NULL; | |
| 66 fz_buffer *tmp = NULL; | |
| 67 char path[2048]; | |
| 68 int count; | |
| 69 char *name; | |
| 70 int seen_last; | |
| 71 | |
| 72 fz_var(buf); | |
| 73 fz_var(tmp); | |
| 74 | |
| 75 name = partname; | |
| 76 if (name[0] == '/') | |
| 77 name ++; | |
| 78 | |
| 79 fz_try(ctx) | |
| 80 { | |
| 81 /* All in one piece */ | |
| 82 if (fz_has_archive_entry(ctx, zip, name)) | |
| 83 { | |
| 84 buf = fz_read_archive_entry(ctx, zip, name); | |
| 85 } | |
| 86 | |
| 87 /* Assemble all the pieces */ | |
| 88 else | |
| 89 { | |
| 90 buf = fz_new_buffer(ctx, 512); | |
| 91 seen_last = 0; | |
| 92 for (count = 0; !seen_last; ++count) | |
| 93 { | |
| 94 fz_snprintf(path, sizeof path, "%s/[%d].piece", name, count); | |
| 95 if (fz_has_archive_entry(ctx, zip, path)) | |
| 96 { | |
| 97 tmp = fz_read_archive_entry(ctx, zip, path); | |
| 98 fz_append_buffer(ctx, buf, tmp); | |
| 99 fz_drop_buffer(ctx, tmp); | |
| 100 tmp = NULL; | |
| 101 } | |
| 102 else | |
| 103 { | |
| 104 fz_snprintf(path, sizeof path, "%s/[%d].last.piece", name, count); | |
| 105 if (fz_has_archive_entry(ctx, zip, path)) | |
| 106 { | |
| 107 tmp = fz_read_archive_entry(ctx, zip, path); | |
| 108 fz_append_buffer(ctx, buf, tmp); | |
| 109 fz_drop_buffer(ctx, tmp); | |
| 110 tmp = NULL; | |
| 111 seen_last = 1; | |
| 112 } | |
| 113 else | |
| 114 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find all pieces for part '%s'", partname); | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 } | |
| 120 fz_catch(ctx) | |
| 121 { | |
| 122 fz_drop_buffer(ctx, tmp); | |
| 123 fz_drop_buffer(ctx, buf); | |
| 124 fz_rethrow(ctx); | |
| 125 } | |
| 126 | |
| 127 return xps_new_part(ctx, doc, partname, buf); | |
| 128 } | |
| 129 | |
| 130 int | |
| 131 xps_has_part(fz_context *ctx, xps_document *doc, char *name) | |
| 132 { | |
| 133 char buf[2048]; | |
| 134 if (name[0] == '/') | |
| 135 name++; | |
| 136 if (fz_has_archive_entry(ctx, doc->zip, name)) | |
| 137 return 1; | |
| 138 fz_snprintf(buf, sizeof buf, "%s/[0].piece", name); | |
| 139 if (fz_has_archive_entry(ctx, doc->zip, buf)) | |
| 140 return 1; | |
| 141 fz_snprintf(buf, sizeof buf, "%s/[0].last.piece", name); | |
| 142 if (fz_has_archive_entry(ctx, doc->zip, buf)) | |
| 143 return 1; | |
| 144 return 0; | |
| 145 } | |
| 146 | |
| 147 fz_document * | |
| 148 xps_open_document_with_directory(fz_context *ctx, fz_archive *dir) | |
| 149 { | |
| 150 xps_document *doc; | |
| 151 | |
| 152 doc = fz_malloc_struct(ctx, xps_document); | |
| 153 xps_init_document(ctx, doc); | |
| 154 | |
| 155 fz_try(ctx) | |
| 156 { | |
| 157 doc->zip = fz_keep_archive(ctx, dir); | |
| 158 xps_read_page_list(ctx, doc); | |
| 159 } | |
| 160 fz_catch(ctx) | |
| 161 { | |
| 162 fz_drop_document(ctx, &doc->super); | |
| 163 fz_rethrow(ctx); | |
| 164 } | |
| 165 | |
| 166 return (fz_document*)doc; | |
| 167 } | |
| 168 | |
| 169 fz_document * | |
| 170 xps_open_document_with_stream(fz_context *ctx, fz_stream *file) | |
| 171 { | |
| 172 xps_document *doc; | |
| 173 | |
| 174 doc = fz_malloc_struct(ctx, xps_document); | |
| 175 xps_init_document(ctx, doc); | |
| 176 | |
| 177 fz_try(ctx) | |
| 178 { | |
| 179 doc->zip = fz_open_zip_archive_with_stream(ctx, file); | |
| 180 xps_read_page_list(ctx, doc); | |
| 181 } | |
| 182 fz_catch(ctx) | |
| 183 { | |
| 184 fz_drop_document(ctx, &doc->super); | |
| 185 fz_rethrow(ctx); | |
| 186 } | |
| 187 | |
| 188 return (fz_document*)doc; | |
| 189 } | |
| 190 | |
| 191 fz_document * | |
| 192 xps_open_document(fz_context *ctx, const char *filename) | |
| 193 { | |
| 194 fz_stream *file; | |
| 195 fz_document *doc = NULL; | |
| 196 | |
| 197 if (fz_is_directory(ctx, filename)) | |
| 198 { | |
| 199 fz_archive *dir = fz_open_directory(ctx, filename); | |
| 200 | |
| 201 fz_try(ctx) | |
| 202 doc = xps_open_document_with_directory(ctx, dir); | |
| 203 fz_always(ctx) | |
| 204 fz_drop_archive(ctx, dir); | |
| 205 fz_catch(ctx) | |
| 206 fz_rethrow(ctx); | |
| 207 | |
| 208 return doc; | |
| 209 } | |
| 210 | |
| 211 file = fz_open_file(ctx, filename); | |
| 212 | |
| 213 fz_try(ctx) | |
| 214 doc = xps_open_document_with_stream(ctx, file); | |
| 215 fz_always(ctx) | |
| 216 fz_drop_stream(ctx, file); | |
| 217 fz_catch(ctx) | |
| 218 fz_rethrow(ctx); | |
| 219 | |
| 220 return (fz_document*)doc; | |
| 221 } | |
| 222 | |
| 223 static void | |
| 224 xps_drop_document(fz_context *ctx, fz_document *doc_) | |
| 225 { | |
| 226 xps_document *doc = (xps_document*)doc_; | |
| 227 xps_font_cache *font, *next; | |
| 228 | |
| 229 if (doc->zip) | |
| 230 fz_drop_archive(ctx, doc->zip); | |
| 231 | |
| 232 font = doc->font_table; | |
| 233 while (font) | |
| 234 { | |
| 235 next = font->next; | |
| 236 fz_drop_font(ctx, font->font); | |
| 237 fz_free(ctx, font->name); | |
| 238 fz_free(ctx, font); | |
| 239 font = next; | |
| 240 } | |
| 241 | |
| 242 xps_drop_page_list(ctx, doc); | |
| 243 | |
| 244 fz_free(ctx, doc->start_part); | |
| 245 } | |
| 246 | |
| 247 static int | |
| 248 xps_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, size_t size) | |
| 249 { | |
| 250 if (!strcmp(key, FZ_META_FORMAT)) | |
| 251 return 1 + (int)fz_strlcpy(buf, "XPS", size); | |
| 252 return -1; | |
| 253 } | |
| 254 | |
| 255 static void | |
| 256 xps_init_document(fz_context *ctx, xps_document *doc) | |
| 257 { | |
| 258 doc->super.refs = 1; | |
| 259 doc->super.drop_document = xps_drop_document; | |
| 260 doc->super.load_outline = xps_load_outline; | |
| 261 doc->super.resolve_link_dest = xps_lookup_link_target; | |
| 262 doc->super.count_pages = xps_count_pages; | |
| 263 doc->super.load_page = xps_load_page; | |
| 264 doc->super.lookup_metadata = xps_lookup_metadata; | |
| 265 } |
