1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26
27 #if defined _MSC_VER
28 #pragma warning(push, 1)
29 #endif
30 #include <windows.h>
31 #if defined _MSC_VER
32 #pragma warning(pop)
33 #endif
34
35 #include <stdio.h>
36 #include <objidl.h>
37
38 /*#include <string.h>
39 #include <filter.h>
40 #include <filterr.h>
41 #include <ntquery.h>
42 #include "assert.h"
43 #include "propspec.hxx"
44 #ifdef __MINGW32__
45 #include <algorithm>
46 using ::std::min;
47 #endif
48 */
49
50 #include "internal/stream_helper.hxx"
51
52 extern "C" {
53 voidpf ZCALLBACK cb_sopen OF((voidpf opaque, const char * filename, int mode));
54 uLong ZCALLBACK cb_sread OF((voidpf opaque, voidpf stream, void* vuf, uLong size));
55 uLong ZCALLBACK cb_swrite OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
56 long ZCALLBACK cb_stell OF((voidpf opaque, voidpf stream));
57 long ZCALLBACK cb_sseek OF((voidpf opaque, voidpf stream, uLong offset, int origin));
58 int ZCALLBACK cb_sclose OF((voidpf opaque, voidpf stream));
59 int ZCALLBACK cb_serror OF((voidpf opaque, voidpf stream));
60
61 void fill_stream_filefunc (zlib_filefunc_def* pzlib_filefunc_def);
62 }
63
64 //-----------------------------
PrepareIStream(IStream * pStream,zlib_filefunc_def & zFileFunc)65 IStream* PrepareIStream( IStream* pStream, zlib_filefunc_def &zFileFunc )
66 {
67 // These next few lines work around the "Seek pointer" bug found on Vista.
68 char cBuf[20];
69 unsigned long nCount;
70 HRESULT hr;
71 ULARGE_INTEGER nNewPosition;
72 LARGE_INTEGER nMove;
73 nMove.QuadPart = 0;
74 hr = pStream->Seek( nMove, STREAM_SEEK_SET, &nNewPosition );
75 hr = pStream->Read( cBuf, 20, &nCount );
76
77 fill_stream_filefunc( &zFileFunc );
78 zFileFunc.opaque = (void*)pStream;
79
80 return pStream;
81 }
82
83 extern "C" {
84
85 // IStream callback
cb_sopen(voidpf opaque,const char *,int)86 voidpf ZCALLBACK cb_sopen (voidpf opaque, const char* /*filename*/, int /*mode*/) {
87 return opaque;
88 }
89
cb_sread(voidpf,voidpf stream,void * buf,uLong size)90 uLong ZCALLBACK cb_sread (voidpf /*opaque*/, voidpf stream, void* buf, uLong size) {
91 unsigned long newsize;
92 HRESULT hr;
93
94 hr = ((IStream *)stream)->Read (buf, size, &newsize);
95 if (hr == S_OK){
96 return (unsigned long)newsize;
97 }
98 else {
99 return (uLong)0;
100 }
101 }
102
cb_sseek(voidpf,voidpf stream,uLong offset,int origin)103 long ZCALLBACK cb_sseek (voidpf /*opaque*/, voidpf stream, uLong offset, int origin) {
104 // IStream::Seek parameters
105 HRESULT hr;
106 LARGE_INTEGER Move;
107 DWORD dwOrigin;
108 Move.QuadPart = (__int64)offset;
109
110 switch (origin) {
111 case SEEK_CUR:
112 dwOrigin = STREAM_SEEK_CUR;
113 break;
114 case SEEK_END:
115 dwOrigin = STREAM_SEEK_END;
116 break;
117 case SEEK_SET:
118 dwOrigin = STREAM_SEEK_SET;
119 break;
120 default:
121 return -1;
122 }
123
124 hr = ((IStream*)stream)->Seek (Move, dwOrigin, NULL);
125 if (hr == S_OK){
126 return 0;
127 }
128 else {
129 return -1;
130 }
131 }
132
cb_stell(voidpf,voidpf stream)133 long ZCALLBACK cb_stell (voidpf /*opaque*/, voidpf stream) {
134 // IStream::Seek parameters
135 HRESULT hr;
136 LARGE_INTEGER Move;
137 ULARGE_INTEGER NewPosition;
138 Move.QuadPart = 0;
139 NewPosition.QuadPart = 0;
140
141 hr = ((IStream*)stream)->Seek (Move, STREAM_SEEK_CUR, &NewPosition);
142 if (hr == S_OK){
143 return (long) NewPosition.QuadPart;
144 }
145 else {
146 return -1;
147 }
148 }
149
cb_sclose(voidpf,voidpf)150 int ZCALLBACK cb_sclose (voidpf /*opaque*/, voidpf /*stream*/) {
151 return 0;
152 }
153
cb_serror(voidpf,voidpf)154 int ZCALLBACK cb_serror (voidpf /*opaque*/, voidpf /*stream*/) {
155 return 0; //RJK - for now
156 }
157
cb_swrite(voidpf,voidpf stream,const void * buf,uLong size)158 uLong ZCALLBACK cb_swrite (voidpf /*opaque*/, voidpf stream, const void* buf, uLong size) {
159 HRESULT hr;
160 unsigned long writecount;
161 hr = ((IStream*)stream)->Write (buf, size, &writecount);
162 if (hr == S_OK)
163 return (unsigned int)writecount;
164 else
165 return (uLong)0;
166 }
167
fill_stream_filefunc(zlib_filefunc_def * pzlib_filefunc_def)168 void fill_stream_filefunc (zlib_filefunc_def* pzlib_filefunc_def) {
169 pzlib_filefunc_def->zopen_file = cb_sopen;
170 pzlib_filefunc_def->zread_file = cb_sread;
171 pzlib_filefunc_def->zwrite_file = cb_swrite;
172 pzlib_filefunc_def->ztell_file = cb_stell;
173 pzlib_filefunc_def->zseek_file = cb_sseek;
174 pzlib_filefunc_def->zclose_file = cb_sclose;
175 pzlib_filefunc_def->zerror_file = cb_serror;
176 }
177 }
178