diff options
author | Nickolay V. Shmyrev <nshmyrev@yandex.ru> | 2007-01-08 12:25:31 (GMT) |
---|---|---|
committer | Nickolay V. Shmyrev <nshmyrev@src.gnome.org> | 2007-01-08 12:25:31 (GMT) |
commit | 13a06349251874bd35d2f03c3fc93217cee749a2 (patch) | |
tree | a681279b008acb19f686ee265aaed2be0da8d9e8 /backend/impress/zip.c | |
parent | afb550ab779e00918d8fe24742abee3a81ebfe93 (diff) |
Reorganize source tree.
2007-01-08 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
* Makefile.am:
* backend/Makefile.am:
* backend/comics/Makefile.am:
* backend/djvu/Makefile.am:
* backend/dvi/Makefile.am:
* backend/ev-async-renderer.c:
* backend/ev-async-renderer.h:
* backend/ev-attachment.c:
* backend/ev-attachment.h:
* backend/ev-backend-marshal.c:
* backend/ev-document-factory.c:
* backend/ev-document-factory.h:
* backend/ev-document-find.c:
* backend/ev-document-find.h:
* backend/ev-document-fonts.c:
* backend/ev-document-fonts.h:
* backend/ev-document-images.c:
* backend/ev-document-images.h:
* backend/ev-document-info.h:
* backend/ev-document-links.c:
* backend/ev-document-links.h:
* backend/ev-document-misc.c:
* backend/ev-document-misc.h:
* backend/ev-document-security.c:
* backend/ev-document-security.h:
* backend/ev-document-thumbnails.c:
* backend/ev-document-thumbnails.h:
* backend/ev-document-transition.c:
* backend/ev-document-transition.h:
* backend/ev-document.c:
* backend/ev-document.h:
* backend/ev-file-exporter.c:
* backend/ev-file-exporter.h:
* backend/ev-image.c:
* backend/ev-image.h:
* backend/ev-link-action.c:
* backend/ev-link-action.h:
* backend/ev-link-dest.c:
* backend/ev-link-dest.h:
* backend/ev-link.c:
* backend/ev-link.h:
* backend/ev-render-context.c:
* backend/ev-render-context.h:
* backend/ev-selection.c:
* backend/ev-selection.h:
* backend/impress/Makefile.am:
* backend/pdf/Makefile.am:
* backend/pixbuf/Makefile.am:
* backend/ps/Makefile.am:
* backend/ps/ps-document.c: (push_pixbuf), (interpreter_failed),
(ps_document_widget_event), (setup_pixmap), (setup_page), (input),
(start_interpreter), (stop_interpreter), (document_load),
(ps_document_next_page), (render_page):
* backend/tiff/Makefile.am:
* comics/Makefile.am:
* comics/comics-document.c:
* comics/comics-document.h:
* configure.ac:
* cut-n-paste/zoom-control/ephy-zoom-control.c:
* djvu/Makefile.am:
* djvu/djvu-document-private.h:
* djvu/djvu-document.c:
* djvu/djvu-document.h:
* djvu/djvu-links.c:
* djvu/djvu-links.h:
* djvu/djvu-text-page.c:
* djvu/djvu-text-page.h:
* djvu/djvu-text.c:
* djvu/djvu-text.h:
* dvi/Makefile.am:
* dvi/dvi-document.c:
* dvi/dvi-document.h:
* dvi/fonts.c:
* dvi/fonts.h:
* dvi/mdvi-lib/Makefile.am:
* dvi/mdvi-lib/afmparse.c:
* dvi/mdvi-lib/afmparse.h:
* dvi/mdvi-lib/bitmap.c:
* dvi/mdvi-lib/bitmap.h:
* dvi/mdvi-lib/color.c:
* dvi/mdvi-lib/color.h:
* dvi/mdvi-lib/common.c:
* dvi/mdvi-lib/common.h:
* dvi/mdvi-lib/defaults.h:
* dvi/mdvi-lib/dvimisc.c:
* dvi/mdvi-lib/dviopcodes.h:
* dvi/mdvi-lib/dviread.c:
* dvi/mdvi-lib/files.c:
* dvi/mdvi-lib/font.c:
* dvi/mdvi-lib/fontmap.c:
* dvi/mdvi-lib/fontmap.h:
* dvi/mdvi-lib/fontsrch.c:
* dvi/mdvi-lib/gf.c:
* dvi/mdvi-lib/hash.c:
* dvi/mdvi-lib/hash.h:
* dvi/mdvi-lib/list.c:
* dvi/mdvi-lib/mdvi.h:
* dvi/mdvi-lib/pagesel.c:
* dvi/mdvi-lib/paper.c:
* dvi/mdvi-lib/paper.h:
* dvi/mdvi-lib/pk.c:
* dvi/mdvi-lib/private.h:
* dvi/mdvi-lib/setup.c:
* dvi/mdvi-lib/sp-epsf.c:
* dvi/mdvi-lib/special.c:
* dvi/mdvi-lib/sysdeps.h:
* dvi/mdvi-lib/t1.c:
* dvi/mdvi-lib/tfm.c:
* dvi/mdvi-lib/tfmfile.c:
* dvi/mdvi-lib/tt.c:
* dvi/mdvi-lib/util.c:
* dvi/mdvi-lib/vf.c:
* dvi/pixbuf-device.c:
* dvi/pixbuf-device.h:
* impress/Makefile.am:
* impress/common.h:
* impress/document.c:
* impress/f_oasis.c:
* impress/f_oo13.c:
* impress/iksemel.c:
* impress/iksemel.h:
* impress/imposter.h:
* impress/impress-document.c:
* impress/impress-document.h:
* impress/internal.h:
* impress/r_back.c:
* impress/r_draw.c:
* impress/r_geometry.c:
* impress/r_gradient.c:
* impress/r_style.c:
* impress/r_text.c:
* impress/render.c:
* impress/render.h:
* impress/zip.c:
* impress/zip.h:
* lib/Makefile.am:
* lib/ev-debug.c:
* lib/ev-debug.h:
* lib/ev-file-helpers.c:
* lib/ev-file-helpers.h:
* lib/ev-gui.c:
* lib/ev-gui.h:
* lib/ev-tooltip.c:
* lib/ev-tooltip.h:
* libdocument/Makefile.am:
* libdocument/ev-file-helpers.c:
* pdf/Makefile.am:
* pdf/ev-poppler.cc:
* pdf/ev-poppler.h:
* pixbuf/Makefile.am:
* pixbuf/pixbuf-document.c:
* pixbuf/pixbuf-document.h:
* properties/Makefile.am:
* ps/Makefile.am:
* ps/gsdefaults.c:
* ps/gsdefaults.h:
* ps/gsio.c:
* ps/gsio.h:
* ps/gstypes.h:
* ps/ps-document.c:
* ps/ps-document.h:
* ps/ps.c:
* ps/ps.h:
* shell/Makefile.am:
* shell/ev-application.h:
* shell/ev-sidebar-links.c:
* shell/ev-sidebar-links.h:
* shell/ev-utils.c: (ev_gui_sanitise_popup_position),
(ev_gui_menu_position_tree_selection):
* shell/ev-utils.h:
* shell/ev-view.c: (ev_view_finalize):
* shell/ev-window.c:
* shell/main.c: (main):
* thumbnailer/Makefile.am:
* tiff/Makefile.am:
* tiff/tiff-document.c:
* tiff/tiff-document.h:
* tiff/tiff2ps.c:
* tiff/tiff2ps.h:
Reorganize source tree.
svn path=/trunk/; revision=2197
Diffstat (limited to 'backend/impress/zip.c')
-rw-r--r-- | backend/impress/zip.c | 346 |
1 files changed, 346 insertions, 0 deletions
diff --git a/backend/impress/zip.c b/backend/impress/zip.c new file mode 100644 index 0000000..4b179b5 --- /dev/null +++ b/backend/impress/zip.c @@ -0,0 +1,346 @@ +/* imposter (OO.org Impress viewer) +** Copyright (C) 2003-2005 Gurer Ozen +** This code is free software; you can redistribute it and/or +** modify it under the terms of GNU General Public License. +*/ + +#include "common.h" +#include "zip.h" +#include <zlib.h> +#define _(x) x + +enum { + ZIP_OK = 0, + ZIP_NOMEM, + ZIP_NOSIG, + ZIP_BADZIP, + ZIP_NOMULTI, + ZIP_EOPEN, + ZIP_EREAD, + ZIP_NOFILE +}; + +struct zipfile { + struct zipfile *next; + char *name; + ulong crc; + ulong zip_size; + ulong real_size; + ulong pos; +}; + +struct zip_struct { + FILE *f; + struct zipfile *files; + ulong cd_pos; + ulong cd_size; + ulong cd_offset; + ulong head_size; + ulong rem_size; + ulong nr_files; +}; + +char * +zip_error (int err) +{ + char *ret; + + switch (err) { + case ZIP_OK: + ret = _("No error"); + break; + case ZIP_NOMEM: + ret = _("Not enough memory"); + break; + case ZIP_NOSIG: + ret = _("Cannot find zip signature"); + break; + case ZIP_BADZIP: + ret = _("Invalid zip file"); + break; + case ZIP_NOMULTI: + ret = _("Multi file zips are not supported"); + break; + case ZIP_EOPEN: + ret = _("Cannot open the file"); + break; + case ZIP_EREAD: + ret = _("Cannot read data from file"); + break; + case ZIP_NOFILE: + ret = _("Cannot find file in the zip archive"); + break; + default: + ret = _("Unknown error"); + break; + } + return ret; +} + +static int +find_cd (zip *z) +{ + FILE *f; + char *buf; + ulong size, pos, i, flag; + + f = z->f; + if (fseek (f, 0, SEEK_END) != 0) return 1; + size = ftell (f); + if (size < 0xffff) pos = 0; else pos = size - 0xffff; + buf = malloc (size - pos + 1); + if (!buf) return 1; + if (fseek (f, pos, SEEK_SET) != 0) { + free (buf); + return 1; + } + if (fread (buf, size - pos, 1, f) != 1) { + free (buf); + return 1; + } + flag = 0; + for (i = size - pos - 3; i > 0; i--) { + if (buf[i] == 0x50 && buf[i+1] == 0x4b && buf[i+2] == 0x05 && buf[i+3] == 0x06) { + z->cd_pos = i + pos; + flag = 1; + break; + } + } + free (buf); + if (flag != 1) return 1; + return 0; +} + +static unsigned long +get_long (unsigned char *buf) +{ + return buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24); +} + +static unsigned long +get_word (unsigned char *buf) +{ + return buf[0] + (buf[1] << 8); +} + +static int +list_files (zip *z) +{ + unsigned char buf[46]; + struct zipfile *zfile; + ulong pat, fn_size; + int nr = 0; + + pat = z->cd_offset; + while (nr < z->nr_files) { + fseek (z->f, pat + z->head_size, SEEK_SET); + + if (fread (buf, 46, 1, z->f) != 1) return ZIP_EREAD; + if (get_long (buf) != 0x02014b50) return ZIP_BADZIP; + + zfile = malloc (sizeof (struct zipfile)); + if (!zfile) return ZIP_NOMEM; + memset (zfile, 0, sizeof (struct zipfile)); + + zfile->crc = get_long (buf + 16); + zfile->zip_size = get_long (buf + 20); + zfile->real_size = get_long (buf + 24); + fn_size = get_word (buf + 28); + zfile->pos = get_long (buf + 42); + + zfile->name = malloc (fn_size + 1); + if (!zfile->name) { + free (zfile); + return ZIP_NOMEM; + } + fread (zfile->name, fn_size, 1, z->f); + zfile->name[fn_size] = '\0'; + + zfile->next = z->files; + z->files = zfile; + + pat += 0x2e + fn_size + get_word (buf + 30) + get_word (buf + 32); + nr++; + } + return ZIP_OK; +} + +zip * +zip_open (const char *fname, int *err) +{ + unsigned char buf[22]; + zip *z; + FILE *f; + + f = fopen (fname, "rb"); + if (NULL == f) { + *err = ZIP_EOPEN; + return NULL; + } + + z = malloc (sizeof (zip)); + memset (z, 0, sizeof (zip)); + z->f = f; + + if (find_cd (z)) { + zip_close (z); + *err = ZIP_NOSIG; + return NULL; + } + + fseek (f, z->cd_pos, SEEK_SET); + if (fread (buf, 22, 1, f) != 1) { + zip_close (z); + *err = ZIP_EREAD; + return NULL; + } + z->nr_files = get_word (buf + 10); + if (get_word (buf + 8) != z->nr_files) { + zip_close (z); + *err = ZIP_NOMULTI; + return NULL; + } + z->cd_size = get_long (buf + 12); + z->cd_offset = get_long (buf + 16); + z->rem_size = get_word (buf + 20); + z->head_size = z->cd_pos - (z->cd_offset + z->cd_size); + + *err = list_files (z); + if (*err != ZIP_OK) { + zip_close (z); + return NULL; + } + + *err = ZIP_OK; + return z; +} + +void +zip_close (zip *z) +{ + struct zipfile *zfile, *tmp; + + zfile = z->files; + while (zfile) { + tmp = zfile->next; + if (zfile->name) free (zfile->name); + free (zfile); + zfile = tmp; + } + z->files = NULL; + if (z->f) fclose (z->f); + z->f = NULL; +} + +static struct zipfile * +find_file (zip *z, const char *name) +{ + struct zipfile *zfile; + + zfile = z->files; + while (zfile) { + if (strcmp (zfile->name, name) == 0) return zfile; + zfile = zfile->next; + } + return NULL; +} + +static int +seek_file (zip *z, struct zipfile *zfile) +{ + unsigned char buf[30]; + + fseek (z->f, zfile->pos + z->head_size, SEEK_SET); + if (fread (buf, 30, 1, z->f) != 1) return ZIP_EREAD; + if (get_long (buf) != 0x04034b50) return ZIP_BADZIP; + fseek (z->f, get_word (buf + 26) + get_word (buf + 28), SEEK_CUR); + return ZIP_OK; +} + +iks * +zip_load_xml (zip *z, const char *name, int *err) +{ + iksparser *prs; + char *real_buf; + iks *x; + struct zipfile *zfile; + + *err = ZIP_OK; + + zfile = find_file (z, name); + if (!zfile) { + *err = ZIP_NOFILE; + return NULL; + } + + seek_file (z, zfile); + + real_buf = malloc (zfile->real_size + 1); + if (zfile->zip_size < zfile->real_size) { + char *zip_buf; + z_stream zs; + zs.zalloc = NULL; + zs.zfree = NULL; + zs.opaque = NULL; + zip_buf = malloc (zfile->zip_size); + fread (zip_buf, zfile->zip_size, 1, z->f); + zs.next_in = zip_buf; + zs.avail_in = zfile->zip_size; + zs.next_out = real_buf; + zs.avail_out = zfile->real_size; + inflateInit2 (&zs, -MAX_WBITS); + inflate (&zs, Z_FINISH); + inflateEnd (&zs); + free (zip_buf); + } else { + fread (real_buf, zfile->real_size, 1, z->f); + } + + real_buf[zfile->real_size] = '\0'; + prs = iks_dom_new (&x); + iks_parse (prs, real_buf, zfile->real_size, 1); + iks_parser_delete (prs); + free (real_buf); + return x; +} + +unsigned long zip_get_size (zip *z, const char *name) +{ + struct zipfile *zf; + + zf = find_file (z, name); + if (!zf) return 0; + return zf->real_size; +} + +int zip_load (zip *z, const char *name, char *buf) +{ + struct zipfile *zfile; + + zfile = find_file (z, name); + if (!zfile) return ZIP_NOFILE; + + seek_file (z, zfile); + + if (zfile->zip_size < zfile->real_size) { + char *zip_buf; + z_stream zs; + zs.zalloc = NULL; + zs.zfree = NULL; + zs.opaque = NULL; + zip_buf = malloc (zfile->zip_size); + fread (zip_buf, zfile->zip_size, 1, z->f); + zs.next_in = zip_buf; + zs.avail_in = zfile->zip_size; + zs.next_out = buf; + zs.avail_out = zfile->real_size; + inflateInit2 (&zs, -MAX_WBITS); + inflate (&zs, Z_FINISH); + inflateEnd (&zs); + free (zip_buf); + } else { + fread (buf, zfile->real_size, 1, z->f); + } + + return ZIP_OK; +} |