sfdcpzip.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder/***********************************************************************
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 *
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* Information and Software Systems Research *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* AT&T Research *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* Florham Park NJ *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova* Glenn Fowler <gsf@research.att.com> *
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova***********************************************************************/
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * sfio pzip discipline
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 */
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 */
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * pzip exception handler
8b054cade993ef373d564b2d74c9c5a2da48f8b7Kristina Sojakova * free on close
1a38107941725211e7c3f051f7a8f5e12199f03acmaedersfpzexcept(Sfio_t* sp, int op, void* val, Sfdisc_t* dp)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return (*((Pz_t**)val) = pz->pz) ? 1 : -1;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * sfio pzip discipline read
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakovasfpzread(Sfio_t* fp, Void_t* buf, size_t size, Sfdisc_t* dp)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * sfio pzip discipline write
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakovasfpzwrite(Sfio_t* fp, const Void_t* buf, size_t size, Sfdisc_t* dp)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova return pzwrite(pz->pz, pz->io, buf, size);
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * create and push the sfio pzip discipline
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * (flags&PZ_STAT) return
2fa2a7c86b9416f0e1607787e9416e274feb1143Christian Maeder * >0 is a pzip file
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova * 0 not a pzip file
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
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroedersfdcpzip(Sfio_t* sp, const char* path, unsigned long flags, Pzdisc_t* disc)
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova unsigned char* s;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova s = (unsigned char*)sfreserve(sp, PZ_GZ_MAGOFF + 2, 1);
097b7fb3f8f90e87120d30bf37a1d89fe0ddfaf0Kristina Sojakova r = m1 == PZ_MAGIC_1 && m2 == PZ_MAGIC_2 && s[2] > 0 && s[3] < 10 ||
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova s[PZ_GZ_MAGOFF] == PZ_GZ_MAGIC_1 && s[PZ_GZ_MAGOFF+1] == PZ_GZ_MAGIC_2;
b5702fcfbabcc2b13557bc96ed8376133420dc73Kristina Sojakova r = sfdcgzip(sp, (flags & PZ_CRC) ? 0 : SFGZ_NOCRC);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder else if (m1 == 'B' && m2 == 'Z' && s[2] == 'h' && s[3] >= '1' && s[3] <= '9')
2ddc9d39235393dca2e40203dde20284db4c3deeKristina Sojakova if (!(io = sfnew(NiL, NiL, SF_UNBOUND, sffileno(sp), (sfset(sp, 0, 0) & (SF_READ|SF_WRITE)))))
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);
9d770d1ea15092156d65e2a89b081eeeb8c6b153Kristina Sojakova if (!(pz->pz = pzopen(&pz->disc, (char*)io, flags)) || (sp->_file = open("/dev/null", 0)) < 0)