summaryrefslogtreecommitdiff
path: root/shell/source/win32/shlxthandler/ooofilt/stream_helper.cxx
blob: e12f95a88e50bcc7c17bd1501bbb2f2533233f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2010 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_shell.hxx"

#if defined _MSC_VER
#pragma warning(push, 1)
#endif
#include <windows.h>
#if defined _MSC_VER
#pragma warning(pop)
#endif

#include <stdio.h>
#include <objidl.h>

/*#include <string.h>
#include <filter.h>
#include <filterr.h>
#include <ntquery.h>
#include "assert.h"
#include "propspec.hxx"
#ifdef __MINGW32__
#include <algorithm>
using ::std::min;
#endif
*/

#include "internal/stream_helper.hxx"

extern "C" {
    voidpf ZCALLBACK cb_sopen OF((voidpf opaque, const char * filename, int mode));
    uLong ZCALLBACK cb_sread OF((voidpf opaque, voidpf stream, void* vuf, uLong size));
    uLong ZCALLBACK cb_swrite OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
    long ZCALLBACK cb_stell OF((voidpf opaque, voidpf stream));
    long ZCALLBACK cb_sseek OF((voidpf opaque, voidpf stream, uLong offset, int origin));
    int ZCALLBACK cb_sclose OF((voidpf opaque, voidpf stream));
    int ZCALLBACK cb_serror OF((voidpf opaque, voidpf stream));

    void fill_stream_filefunc (zlib_filefunc_def* pzlib_filefunc_def);
}

//-----------------------------
IStream* PrepareIStream( IStream* pStream, zlib_filefunc_def &zFileFunc )
{
    // These next few lines work around the "Seek pointer" bug found on Vista.
    char cBuf[20];
    unsigned long nCount;
    HRESULT hr;
    ULARGE_INTEGER nNewPosition;
    LARGE_INTEGER nMove;
    nMove.QuadPart = 0;
    hr = pStream->Seek( nMove, STREAM_SEEK_SET, &nNewPosition );
    hr = pStream->Read( cBuf, 20, &nCount );

    fill_stream_filefunc( &zFileFunc );
    zFileFunc.opaque = (void*)pStream;

    return pStream;
}

extern "C" {

    // IStream callback
    voidpf ZCALLBACK cb_sopen (voidpf opaque, const char* /*filename*/, int /*mode*/) {
        return opaque;
    }

    uLong ZCALLBACK cb_sread (voidpf /*opaque*/, voidpf stream, void* buf, uLong size) {
        unsigned long newsize;
        HRESULT hr;

        hr = ((IStream *)stream)->Read (buf, size, &newsize);
        if (hr == S_OK){
            return (unsigned long)newsize;
        }
        else {
            return (uLong)0;
        }
    }

    long ZCALLBACK cb_sseek (voidpf /*opaque*/, voidpf stream, uLong offset, int origin) {
        // IStream::Seek parameters
        HRESULT hr;
        LARGE_INTEGER Move;
        DWORD dwOrigin;
        Move.QuadPart = (__int64)offset;

        switch (origin) {
            case SEEK_CUR:
                dwOrigin = STREAM_SEEK_CUR;
                break;
            case SEEK_END:
                dwOrigin = STREAM_SEEK_END;
                break;
            case SEEK_SET:
                dwOrigin = STREAM_SEEK_SET;
                break;
            default:
                return -1;
        }

        hr = ((IStream*)stream)->Seek (Move, dwOrigin, NULL);
        if (hr == S_OK){
            return 0;
        }
        else {
            return -1;
        }
    }

    long ZCALLBACK cb_stell (voidpf /*opaque*/, voidpf stream) {
        // IStream::Seek parameters
        HRESULT hr;
        LARGE_INTEGER Move;
        ULARGE_INTEGER NewPosition;
        Move.QuadPart = 0;
        NewPosition.QuadPart = 0;

        hr = ((IStream*)stream)->Seek (Move, STREAM_SEEK_CUR, &NewPosition);
        if (hr == S_OK){
            return (long) NewPosition.QuadPart;
        }
        else {
            return -1;
        }
    }

    int ZCALLBACK cb_sclose (voidpf /*opaque*/, voidpf /*stream*/) {
        return 0;
    }

    int ZCALLBACK cb_serror (voidpf /*opaque*/, voidpf /*stream*/) {
        return 0;  //RJK - for now
    }

    uLong ZCALLBACK cb_swrite (voidpf /*opaque*/, voidpf stream, const void* buf, uLong size) {
        HRESULT hr;
        unsigned long writecount;
        hr = ((IStream*)stream)->Write (buf, size, &writecount);
        if (hr == S_OK)
            return (unsigned int)writecount;
        else
            return (uLong)0;
    }

    void fill_stream_filefunc (zlib_filefunc_def* pzlib_filefunc_def) {
        pzlib_filefunc_def->zopen_file = cb_sopen;
        pzlib_filefunc_def->zread_file = cb_sread;
        pzlib_filefunc_def->zwrite_file = cb_swrite;
        pzlib_filefunc_def->ztell_file = cb_stell;
        pzlib_filefunc_def->zseek_file = cb_sseek;
        pzlib_filefunc_def->zclose_file = cb_sclose;
        pzlib_filefunc_def->zerror_file = cb_serror;
    }
}