sfdcpzip.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder/***********************************************************************
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova* *
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner* This software is part of the ast package *
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova* Copyright (c) 1998-2011 AT&T Intellectual Property *
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova* and is licensed under the *
94e2e03f6efde106de095ef4ea0ec87f74955a31Kristina Sojakova* Eclipse Public License, Version 1.0 *
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu* by AT&T Intellectual Property *
211c5fb252e0a776baad9a4857ab198659289a4aKristina Sojakova* *
94e2e03f6efde106de095ef4ea0ec87f74955a31Kristina Sojakova* A copy of the License is available at *
211c5fb252e0a776baad9a4857ab198659289a4aKristina Sojakova* http://www.eclipse.org/org/documents/epl-v10.html *
211c5fb252e0a776baad9a4857ab198659289a4aKristina Sojakova* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova* *
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova* Information and Software Systems Research *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* AT&T Research *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* Florham Park NJ *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* Glenn Fowler <gsf@research.att.com> *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova***********************************************************************/
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova#pragma prototyped
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova/*
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * sfio pzip discipline
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova#include "pzlib.h"
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova#include <sfdcbzip.h>
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova#define GZ_MAGIC_1 0x1f /* 1st gzip magic char */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova#define GZ_MAGIC_2 0x8b /* 2nd gzip magic char */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova#define LZ_MAGIC_2 0x9d /* 2nd lzw magic char */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova#define PZ_GZ_MAGOFF 10 /* compressed magic offset */
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova#define PZ_GZ_MAGIC_1 0x92 /* 1st compressed magic char */
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova#define PZ_GZ_MAGIC_2 0x17 /* 2nd compressed magic char */
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakovatypedef struct
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova{
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova Sfdisc_t sfdisc; /* sfio discipline */
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova Pzdisc_t disc; /* pzip discipline */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova Pz_t* pz; /* pz handle */
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova Sfio_t* io; /* real pzwrite stream */
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova} Sfpzip_t;
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova
2c47bb55d963ff37dbae4a0a7701274fddb95fc8Christian Maeder/*
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * pzip exception handler
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova * free on close
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova */
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova
2c47bb55d963ff37dbae4a0a7701274fddb95fc8Christian Maederstatic int
1a38107941725211e7c3f051f7a8f5e12199f03acmaedersfpzexcept(Sfio_t* sp, int op, void* val, Sfdisc_t* dp)
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova{
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova register Sfpzip_t* pz = (Sfpzip_t*)dp;
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova int r;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova NoP(sp);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova switch (op)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder case SF_ATEXIT:
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova sfdisc(sp, SF_POPDISC);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return 0;
4e3744376d584470e1342cbac9ac27032f2045c3Christian Maeder case SF_CLOSING:
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova case SF_DPOP:
4e3744376d584470e1342cbac9ac27032f2045c3Christian Maeder case SF_FINAL:
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder if (pz->pz)
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova {
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova pz->pz->flags &= ~PZ_STREAM;
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova r = pzclose(pz->pz);
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova pz->pz = 0;
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova }
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova else
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova r = 0;
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova if (op != SF_CLOSING)
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova free(dp);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return r;
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova case SF_DBUFFER:
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return 1;
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova case SF_SYNC:
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova return val ? 0 : pzsync(pz->pz);
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova case SFPZ_HANDLE:
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return (*((Pz_t**)val) = pz->pz) ? 1 : -1;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova }
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return 0;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova}
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova/*
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * sfio pzip discipline read
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakovastatic ssize_t
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakovasfpzread(Sfio_t* fp, Void_t* buf, size_t size, Sfdisc_t* dp)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova{
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova register Sfpzip_t* pz = (Sfpzip_t*)dp;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return pzread(pz->pz, buf, size);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova}
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova/*
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * sfio pzip discipline write
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakovastatic ssize_t
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakovasfpzwrite(Sfio_t* fp, const Void_t* buf, size_t size, Sfdisc_t* dp)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova{
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova register Sfpzip_t* pz = (Sfpzip_t*)dp;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return pzwrite(pz->pz, pz->io, buf, size);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova}
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova/*
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * create and push the sfio pzip discipline
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * (flags&PZ_STAT) return
2fa2a7c86b9416f0e1607787e9416e274feb1143Christian Maeder * >0 is a pzip file
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * 0 not a pzip file
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * <0 error
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder * otherwise flags have pzopen() semantics and return
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * >0 discipline pushed (one or more of { pzip gzip lzw })
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * 0 discipline not needed
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * <0 error
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
4cfef84218e908a7db4a0bba0927be1397886315Kristina Sojakovaint
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroedersfdcpzip(Sfio_t* sp, const char* path, unsigned long flags, Pzdisc_t* disc)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova{
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova Sfio_t* io;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova Sfpzip_t* pz;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova Pz_t* oz;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (flags & PZ_HANDLE)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova oz = (Pz_t*)sp;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova sp = oz->io;
4cfef84218e908a7db4a0bba0927be1397886315Kristina Sojakova }
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova else
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder oz = 0;
4cfef84218e908a7db4a0bba0927be1397886315Kristina Sojakova if (sfset(sp, 0, 0) & SF_WRITE)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (flags & PZ_STAT)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return -1;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova }
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova else if (!(flags & PZ_FORCE))
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova unsigned char* s;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova int r;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova int m1;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova int m2;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (!(r = sfset(sp, 0, 0) & SF_SHARE))
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova sfset(sp, SF_SHARE, 1);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova s = (unsigned char*)sfreserve(sp, PZ_GZ_MAGOFF + 2, 1);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (!r)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova sfset(sp, SF_SHARE, 0);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (!s)
4cfef84218e908a7db4a0bba0927be1397886315Kristina Sojakova return -1;
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova m1 = s[0];
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova m2 = s[1];
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova r = m1 == PZ_MAGIC_1 && m2 == PZ_MAGIC_2 && s[2] > 0 && s[3] < 10 ||
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova m1 == GZ_MAGIC_1 && m2 == GZ_MAGIC_2 &&
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova s[PZ_GZ_MAGOFF] == PZ_GZ_MAGIC_1 && s[PZ_GZ_MAGOFF+1] == PZ_GZ_MAGIC_2;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova sfread(sp, s, 0);
4cfef84218e908a7db4a0bba0927be1397886315Kristina Sojakova if (flags & PZ_STAT)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return r;
4cfef84218e908a7db4a0bba0927be1397886315Kristina Sojakova if (!r)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (!(flags & PZ_NOGZIP))
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (m1 == GZ_MAGIC_1)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (m2 == GZ_MAGIC_2)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova r = sfdcgzip(sp, (flags & PZ_CRC) ? 0 : SFGZ_NOCRC);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova else if (m2 == LZ_MAGIC_2)
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder r = sfdclzw(sp, 0);
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder else if (m1 == 'B' && m2 == 'Z' && s[2] == 'h' && s[3] >= '1' && s[3] <= '9')
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder r = sfdcbzip(sp, 0);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder }
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return r;
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova }
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova sfsync(sp);
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova }
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova if (!(io = sfnew(NiL, NiL, SF_UNBOUND, sffileno(sp), (sfset(sp, 0, 0) & (SF_READ|SF_WRITE)))))
4e3744376d584470e1342cbac9ac27032f2045c3Christian Maeder return -1;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (!(pz = newof(0, Sfpzip_t, 1, 0)))
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova io->_file = -1;
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova sfclose(io);
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova return -1;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova }
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova pz->disc.version = PZ_VERSION;
4e3744376d584470e1342cbac9ac27032f2045c3Christian Maeder flags &= ~(PZ_READ|PZ_WRITE|PZ_STAT|PZ_STREAM|PZ_INTERNAL);
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder flags |= PZ_PUSHED|PZ_STREAM|((sfset(sp, 0, 0) & SF_READ) ? PZ_READ : PZ_WRITE);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (oz && (oz->flags & PZ_WRITE))
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova flags |= PZ_DELAY;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova if (disc)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova {
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova pz->disc.errorf = disc->errorf;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->disc.window = disc->window;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->disc.options = disc->options;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->disc.partition = disc->partition;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova if (disc->splitf)
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova flags |= PZ_ACCEPT;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova }
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova if (!(pz->pz = pzopen(&pz->disc, (char*)io, flags)) || (sp->_file = open("/dev/null", 0)) < 0)
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova {
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder io->_file = -1;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova sfclose(io);
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder free(pz);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return -1;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if (path)
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->pz->path = path;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->sfdisc.exceptf = sfpzexcept;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova if (flags & PZ_WRITE)
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder {
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->sfdisc.writef = sfpzwrite;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova pz->io = io;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova }
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova else
887a1999374d1fb3a534e602a8d322de6ef4c8e8Kristina Sojakova pz->sfdisc.readf = sfpzread;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova sfset(sp, SF_SHARE|SF_PUBLIC, 0);
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova if (sfdisc(sp, &pz->sfdisc) != &pz->sfdisc)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder {
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova close(sp->_file);
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder sp->_file = io->_file;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova sfseek(sp, sftell(io), SEEK_SET);
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder io->_file = -1;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder pzclose(pz->pz);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder free(pz);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return -1;
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if (oz)
fc08da86ea2ef76a631faca30ca30b8ed112d864Christian Maeder oz->flags |= pz->pz->flags & PZ_INTERNAL;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova return 1;
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova}
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova