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
|
//========================================================================
//
// GnomeVFSStream.cc
//
// Copyright 1996-2002 Glyph & Cog, LLC
// Copyright 2003 Martin Kretzschmar
//
//========================================================================
#ifdef __GNUC__
#pragma implementation
#endif
#include <aconf.h>
#include "config.h"
#include "GnomeVFSStream.h"
#include "gpdf-g-switch.h"
# include <libgnomevfs/gnome-vfs.h>
#include "gpdf-g-switch.h"
#ifndef NO_DECRYPTION
#include "Decrypt.h"
#endif
GnomeVFSStream::GnomeVFSStream(GnomeVFSHandle *handleA, Guint startA,
GBool limitedA, Guint lengthA, Object *dictA):
BaseStream(dictA) {
handle = handleA;
start = startA;
limited = limitedA;
length = lengthA;
bufPtr = bufEnd = buf;
bufPos = start;
savePos = 0;
saved = gFalse;
}
GnomeVFSStream::~GnomeVFSStream() {
close();
}
Stream *GnomeVFSStream::makeSubStream(Guint startA, GBool limitedA,
Guint lengthA, Object *dictA) {
return new GnomeVFSStream(handle, startA, limitedA, lengthA, dictA);
}
void GnomeVFSStream::reset() {
GnomeVFSFileSize offsetReturn;
if (gnome_vfs_tell(handle, &offsetReturn) == GNOME_VFS_OK) {
savePos = (Guint)offsetReturn;
saved = gTrue;
}
gnome_vfs_seek(handle, GNOME_VFS_SEEK_START, start);
bufPtr = bufEnd = buf;
bufPos = start;
#ifndef NO_DECRYPTION
if (decrypt)
decrypt->reset();
#endif
}
void GnomeVFSStream::close() {
if (saved) {
gnome_vfs_seek(handle, GNOME_VFS_SEEK_START, savePos);
saved = gFalse;
}
}
GBool GnomeVFSStream::fillBuf() {
int n;
GnomeVFSFileSize bytesRead;
#ifndef NO_DECRYPTION
char *p;
#endif
bufPos += bufEnd - buf;
bufPtr = bufEnd = buf;
if (limited && bufPos >= start + length) {
return gFalse;
}
if (limited && bufPos + gnomeVFSStreamBufSize > start + length) {
n = start + length - bufPos;
} else {
n = gnomeVFSStreamBufSize;
}
if (gnome_vfs_read(handle, buf, n, &bytesRead) != GNOME_VFS_OK) {
return gFalse;
}
bufEnd = buf + bytesRead;
if (bufPtr >= bufEnd) {
return gFalse;
}
#ifndef NO_DECRYPTION
if (decrypt) {
for (p = buf; p < bufEnd; ++p) {
*p = (char)decrypt->decryptByte((Guchar)*p);
}
}
#endif
return gTrue;
}
void GnomeVFSStream::setPos(Guint pos, int dir) {
if (dir >= 0) {
if (gnome_vfs_seek(handle, GNOME_VFS_SEEK_START, pos) == GNOME_VFS_OK) {
bufPos = pos;
}
} else {
GnomeVFSFileSize offsetReturn;
if (gnome_vfs_seek(handle, GNOME_VFS_SEEK_END, 0) == GNOME_VFS_OK &&
gnome_vfs_tell(handle, &offsetReturn) == GNOME_VFS_OK) {
bufPos = (Guint)offsetReturn;
if (pos > bufPos)
pos = (Guint)bufPos;
if (gnome_vfs_seek(handle, GNOME_VFS_SEEK_END, -(int)pos) == GNOME_VFS_OK &&
gnome_vfs_tell(handle, &offsetReturn) == GNOME_VFS_OK) {
bufPos = (Guint)offsetReturn;
}
}
}
bufPtr = bufEnd = buf;
}
void GnomeVFSStream::moveStart(int delta) {
start += delta;
bufPtr = bufEnd = buf;
bufPos = start;
}
|