xref: /trunk/main/shell/source/win32/shlxthandler/ooofilt/stream_helper.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2010 by Sun Microsystems, Inc.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_shell.hxx"
30 
31 #if defined _MSC_VER
32 #pragma warning(push, 1)
33 #endif
34 #include <windows.h>
35 #if defined _MSC_VER
36 #pragma warning(pop)
37 #endif
38 
39 #include <stdio.h>
40 #include <objidl.h>
41 
42 /*#include <string.h>
43 #include <filter.h>
44 #include <filterr.h>
45 #include <ntquery.h>
46 #include "assert.h"
47 #include "propspec.hxx"
48 #ifdef __MINGW32__
49 #include <algorithm>
50 using ::std::min;
51 #endif
52 */
53 
54 #include "internal/stream_helper.hxx"
55 
56 extern "C" {
57     voidpf ZCALLBACK cb_sopen OF((voidpf opaque, const char * filename, int mode));
58     uLong ZCALLBACK cb_sread OF((voidpf opaque, voidpf stream, void* vuf, uLong size));
59     uLong ZCALLBACK cb_swrite OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
60     long ZCALLBACK cb_stell OF((voidpf opaque, voidpf stream));
61     long ZCALLBACK cb_sseek OF((voidpf opaque, voidpf stream, uLong offset, int origin));
62     int ZCALLBACK cb_sclose OF((voidpf opaque, voidpf stream));
63     int ZCALLBACK cb_serror OF((voidpf opaque, voidpf stream));
64 
65     void fill_stream_filefunc (zlib_filefunc_def* pzlib_filefunc_def);
66 }
67 
68 //-----------------------------
69 IStream* PrepareIStream( IStream* pStream, zlib_filefunc_def &zFileFunc )
70 {
71     // These next few lines work around the "Seek pointer" bug found on Vista.
72     char cBuf[20];
73     unsigned long nCount;
74     HRESULT hr;
75     ULARGE_INTEGER nNewPosition;
76     LARGE_INTEGER nMove;
77     nMove.QuadPart = 0;
78     hr = pStream->Seek( nMove, STREAM_SEEK_SET, &nNewPosition );
79     hr = pStream->Read( cBuf, 20, &nCount );
80 
81     fill_stream_filefunc( &zFileFunc );
82     zFileFunc.opaque = (void*)pStream;
83 
84     return pStream;
85 }
86 
87 extern "C" {
88 
89     // IStream callback
90     voidpf ZCALLBACK cb_sopen (voidpf opaque, const char* /*filename*/, int /*mode*/) {
91         return opaque;
92     }
93 
94     uLong ZCALLBACK cb_sread (voidpf /*opaque*/, voidpf stream, void* buf, uLong size) {
95         unsigned long newsize;
96         HRESULT hr;
97 
98         hr = ((IStream *)stream)->Read (buf, size, &newsize);
99         if (hr == S_OK){
100             return (unsigned long)newsize;
101         }
102         else {
103             return (uLong)0;
104         }
105     }
106 
107     long ZCALLBACK cb_sseek (voidpf /*opaque*/, voidpf stream, uLong offset, int origin) {
108         // IStream::Seek parameters
109         HRESULT hr;
110         LARGE_INTEGER Move;
111         DWORD dwOrigin;
112         Move.QuadPart = (__int64)offset;
113 
114         switch (origin) {
115             case SEEK_CUR:
116                 dwOrigin = STREAM_SEEK_CUR;
117                 break;
118             case SEEK_END:
119                 dwOrigin = STREAM_SEEK_END;
120                 break;
121             case SEEK_SET:
122                 dwOrigin = STREAM_SEEK_SET;
123                 break;
124             default:
125                 return -1;
126         }
127 
128         hr = ((IStream*)stream)->Seek (Move, dwOrigin, NULL);
129         if (hr == S_OK){
130             return 0;
131         }
132         else {
133             return -1;
134         }
135     }
136 
137     long ZCALLBACK cb_stell (voidpf /*opaque*/, voidpf stream) {
138         // IStream::Seek parameters
139         HRESULT hr;
140         LARGE_INTEGER Move;
141         ULARGE_INTEGER NewPosition;
142         Move.QuadPart = 0;
143         NewPosition.QuadPart = 0;
144 
145         hr = ((IStream*)stream)->Seek (Move, STREAM_SEEK_CUR, &NewPosition);
146         if (hr == S_OK){
147             return (long) NewPosition.QuadPart;
148         }
149         else {
150             return -1;
151         }
152     }
153 
154     int ZCALLBACK cb_sclose (voidpf /*opaque*/, voidpf /*stream*/) {
155         return 0;
156     }
157 
158     int ZCALLBACK cb_serror (voidpf /*opaque*/, voidpf /*stream*/) {
159         return 0;  //RJK - for now
160     }
161 
162     uLong ZCALLBACK cb_swrite (voidpf /*opaque*/, voidpf stream, const void* buf, uLong size) {
163         HRESULT hr;
164         unsigned long writecount;
165         hr = ((IStream*)stream)->Write (buf, size, &writecount);
166         if (hr == S_OK)
167             return (unsigned int)writecount;
168         else
169             return (uLong)0;
170     }
171 
172     void fill_stream_filefunc (zlib_filefunc_def* pzlib_filefunc_def) {
173         pzlib_filefunc_def->zopen_file = cb_sopen;
174         pzlib_filefunc_def->zread_file = cb_sread;
175         pzlib_filefunc_def->zwrite_file = cb_swrite;
176         pzlib_filefunc_def->ztell_file = cb_stell;
177         pzlib_filefunc_def->zseek_file = cb_sseek;
178         pzlib_filefunc_def->zclose_file = cb_sclose;
179         pzlib_filefunc_def->zerror_file = cb_serror;
180     }
181 }
182