File: BonoboStream.cc

package info (click to toggle)
gpdf 2.8.2-1.2sarge6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,220 kB
  • ctags: 6,624
  • sloc: cpp: 61,071; ansic: 12,100; sh: 6,017; xml: 2,299; makefile: 589
file content (205 lines) | stat: -rw-r--r-- 4,221 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//========================================================================
//
// BonoboFile.cc
//
// Copyright 1999 Derek B. Noonburg assigned by Michael Meeks.
//
//========================================================================

#ifdef __GNUC__
#pragma implementation
#endif

#include <aconf.h>
#include "config.h"

#include "BonoboStream.h"
#include "gpdf-g-switch.h"
#  include <bonobo.h>
#include "gpdf-g-switch.h"

#ifndef NO_DECRYPTION
#include "Decrypt.h"
#endif

typedef Bonobo_Stream BaseFile;

#if 0
#  define DBG_MORE(x) x
#else
#  define DBG_MORE(x)
#endif

static inline size_t
bfread (void *ptr, size_t size, size_t nmemb, BaseFile file)
{
  CORBA_long len;
  CORBA_Environment ev;
  Bonobo_Stream_iobuf *buffer = NULL;

  g_return_val_if_fail (ptr != NULL, 0);

  DBG_MORE (g_message ("read %p %d %d to %p\n", file, size, nmemb, ptr));

  CORBA_exception_init (&ev);
  Bonobo_Stream_read (file, size*nmemb, &buffer, &ev);
  if (ev._major != CORBA_NO_EXCEPTION) {
    g_warning ("Failed bfread");
    CORBA_exception_free (&ev);
    return 0;
  }
  CORBA_exception_free (&ev);
  memcpy (ptr, buffer->_buffer, buffer->_length);
  len = buffer->_length;

  DBG_MORE (g_message ("Read %d bytes %p %d\n",
                       len, buffer->_buffer, buffer->_length));

  CORBA_free (buffer);

  return len;
}

static inline int
bfseek (BaseFile file, long offset, int whence)
{
  CORBA_Environment ev;
  Bonobo_Stream_SeekType t;
  CORBA_long ans;

  DBG_MORE (g_message ("Seek %p %ld %d\n", file, offset, whence));

  if (whence == SEEK_SET)
    t = Bonobo_Stream_SeekSet;
  else if (whence == SEEK_CUR)
    t = Bonobo_Stream_SeekCur;
  else if (whence == SEEK_END)
    t = Bonobo_Stream_SeekEnd;
  else {
    g_warning ("Serious error in seek type");
    t = Bonobo_Stream_SeekSet;
  }
  
  CORBA_exception_init (&ev);
  ans = Bonobo_Stream_seek (file, offset, t, &ev);
  CORBA_exception_free (&ev);
  return ans;
}

static inline void
brewind (BaseFile file)
{
  bfseek (file, 0, SEEK_SET);
}

static inline long
bftell (BaseFile file)
{
  CORBA_Environment ev;
  CORBA_long pos;

  DBG_MORE (g_message ("tell %p\n", file));

  CORBA_exception_init (&ev);
  pos = Bonobo_Stream_seek (file, 0, Bonobo_Stream_SeekCur, &ev);
  CORBA_exception_free (&ev);

  DBG_MORE (g_message ("tell returns %d\n", pos));

  return pos;
}

bonoboStream::bonoboStream(Bonobo_Stream fA, Guint startA, GBool limitedA,
			   Guint lengthA, Object *dictA):
  BaseStream(dictA) {
  f = fA;
  start = startA;
  limited = limitedA;
  length = lengthA;
  bufPtr = bufEnd = buf;
  bufPos = start;
  savePos = 0;
  saved = gFalse;
}

bonoboStream::~bonoboStream() {
  close();
}

Stream *bonoboStream::makeSubStream(Guint startA, GBool limitedA,
				    Guint lengthA, Object *dictA) {
  return new bonoboStream(f, startA, limitedA, lengthA, dictA);
}

void bonoboStream::reset() {
  savePos = (Guint)bftell(f);
  saved = gTrue;
  bfseek(f, start, SEEK_SET);
  bufPtr = bufEnd = buf;
  bufPos = start;
#ifndef NO_DECRYPTION
  if (decrypt)
    decrypt->reset();
#endif
}

void bonoboStream::close() {
  if (saved) {
    bfseek(f, savePos, SEEK_SET);
    saved = gFalse;
  }
}

GBool bonoboStream::fillBuf() {
  int n;
#ifndef NO_DECRYPTION
  char *p;
#endif

  bufPos += bufEnd - buf;
  bufPtr = bufEnd = buf;
  if (limited && bufPos >= start + length) {
    return gFalse;
  }
  if (limited && bufPos + bonoboStreamBufSize > start + length) {
    n = start + length - bufPos;
  } else {
    n = bonoboStreamBufSize;
  }
  n = bfread(buf, 1, n, f);
  bufEnd = buf + n;
  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 bonoboStream::setPos(Guint pos, int dir) {
  Guint size;

  if (dir >= 0) {
    bfseek(f, pos, SEEK_SET);
    bufPos = pos;
  } else {
    bfseek(f, 0, SEEK_END);
    size = (Guint)bftell(f);
    if (pos > size)
      pos = (Guint)size;
    bfseek(f, -(int)pos, SEEK_END);
    bufPos = (Guint)bftell(f);
  }
  bufPtr = bufEnd = buf;
}

void bonoboStream::moveStart(int delta) {
  start += delta;
  bufPtr = bufEnd = buf;
  bufPos = start;
}