source: trunk/libdjvu/IFFByteStream.cpp @ 101

Last change on this file since 101 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

File size: 14.4 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: IFFByteStream.cpp,v 1.10 2004/08/06 15:11:29 leonb Exp $
55// $Name:  $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64// -- Implementation of IFFByteStream
65// - Author: Leon Bottou, 06/1998
66
67// From: Leon Bottou, 1/31/2002
68// This has been changed by Lizardtech to fit better
69// with their re-implementation of ByteStreams.
70
71#include <assert.h>
72#include "IFFByteStream.h"
73
74
75#ifdef HAVE_NAMESPACES
76namespace DJVU {
77# ifdef NOT_DEFINED // Just to fool emacs c++ mode
78}
79#endif
80#endif
81
82
83// Constructor
84IFFByteStream::IFFByteStream(const GP<ByteStream> &xbs,const int xpos)
85: ByteStream::Wrapper(xbs), has_magic(false), ctx(0), dir(0)
86{
87  offset = seekto = xpos;
88}
89
90// Destructor
91IFFByteStream::~IFFByteStream()
92{
93  while (ctx)
94    close_chunk();
95}
96
97GP<IFFByteStream>
98IFFByteStream::create(const GP<ByteStream> &bs)
99{
100  const int pos=bs->tell();
101  return new IFFByteStream(bs,pos);
102}
103
104
105// IFFByteStream::ready
106// -- indicates if bytestream is ready for reading
107//    returns number of bytes available
108
109int 
110IFFByteStream::ready()
111{
112  if (ctx && dir < 0)
113    return ctx->offEnd - offset;
114  else if (ctx)
115    return 1;
116  else
117    return 0;
118}
119
120
121// IFFByteStream::composite
122// -- indicates if bytestream is ready for putting or getting chunks
123
124int 
125IFFByteStream::composite()
126{
127  if (ctx && !ctx->bComposite)
128    return 0;
129  else
130    return 1;
131}
132
133
134
135
136// IFFByteStream::check_id
137// -- checks if an id is legal
138
139int 
140IFFByteStream::check_id(const char *id)
141{
142  int i;
143  // check absence of null bytes
144  for (i=0; i<4; i++)
145    if (id[i]<0x20 || id[i]>0x7e)
146      return -1;
147  // check composite chunks
148  static char *szComposite[] = { "FORM", "LIST", "PROP", "CAT ", 0 };
149  for (i=0; szComposite[i]; i++) 
150    if (!memcmp(id, szComposite[i], 4))
151      return 1;
152  // check reserved chunks
153  static char *szReserved[] = { "FOR", "LIS", "CAT", 0 };
154  for (i=0; szReserved[i]; i++) 
155    if (!memcmp(id, szReserved[i], 3) && id[3]>='1' && id[3]<='9')
156      return -1;
157  // regular chunk
158  return 0;
159}
160
161
162
163// IFFByteStream::get_chunk
164// -- get next chunk header
165
166int 
167IFFByteStream::get_chunk(GUTF8String &chkid, int *rawoffsetptr, int *rawsizeptr)
168{
169  int bytes;
170  char buffer[8];
171 
172  // Check that we are allowed to read a chunk
173  if (dir > 0)
174    G_THROW( ERR_MSG("IFFByteStream.read_write") );
175  if (ctx && !ctx->bComposite)
176    G_THROW( ERR_MSG("IFFByteStream.not_ready") );
177  dir = -1;
178
179  // Seek to end of previous chunk if necessary
180  if (seekto > offset)
181    {
182      bs->seek(seekto);
183      offset = seekto;
184    }
185
186  // Skip padding byte
187  if (ctx && offset == ctx->offEnd)
188    return 0;
189  if (offset & 1)
190    {
191      bytes = bs->read( (void*)buffer, 1);
192      if (bytes==0 && !ctx)
193        return 0;
194      offset += bytes;
195    }
196 
197  // Record raw offset
198  int rawoffset = offset;
199 
200  // Read chunk id (skipping magic sequences inserted here to make
201  // DjVu files recognizable.)
202  for(;;)
203  {
204    if (ctx && offset == ctx->offEnd)
205      return 0;
206    if (ctx && offset+4 > ctx->offEnd)
207      G_THROW( ERR_MSG("IFFByteStream.corrupt_end") );
208    bytes = bs->readall( (void*)&buffer[0], 4);
209    offset = seekto = offset + bytes;
210    if (bytes==0 && !ctx)
211      return 0;
212    if (bytes != 4)
213      G_THROW( ByteStream::EndOfFile );
214    if(buffer[0] != 0x41 || buffer[1] != 0x54 ||
215       buffer[2] != 0x26 || buffer[3] != 0x54 )
216      break;
217    has_magic=true;
218  }
219 
220  // Read chunk size
221  if (ctx && offset+4 > ctx->offEnd)
222    G_THROW( ERR_MSG("IFFByteStream.corrupt_end2") );
223  bytes = bs->readall( (void*)&buffer[4], 4);
224  offset = seekto = offset + bytes;
225  if (bytes != 4)
226    G_THROW( ByteStream::EndOfFile );
227  long size = ((unsigned char)buffer[4]<<24) |
228              ((unsigned char)buffer[5]<<16) |
229              ((unsigned char)buffer[6]<<8)  |
230              ((unsigned char)buffer[7]);
231  if (ctx && offset+size > ctx->offEnd)
232    G_THROW( ERR_MSG("IFFByteStream.corrupt_mangled") );
233 
234  // Check if composite
235  int composite = check_id(buffer);
236  if (composite < 0)
237    G_THROW( ERR_MSG("IFFByteStream.corrupt_id") );
238 
239  // Read secondary id of composite chunk
240  if (composite)
241  {
242    if (ctx && ctx->offEnd<offset+4)
243      G_THROW( ERR_MSG("IFFByteStream.corrupt_header") );
244    bytes = bs->readall( (void*)&buffer[4], 4);
245    offset += bytes;
246    if (bytes != 4)
247      G_THROW( ByteStream::EndOfFile );
248    if (check_id(&buffer[4]))
249      G_THROW( ERR_MSG("IFFByteStream.corrupt_2nd_id") );
250  }
251
252  // Create context record
253  IFFContext *nctx = new IFFContext;
254  G_TRY
255  {
256    nctx->next = ctx;
257    nctx->offStart = seekto;
258    nctx->offEnd = seekto + size;
259    if (composite)
260    {
261      memcpy( (void*)(nctx->idOne), (void*)&buffer[0], 4);
262      memcpy( (void*)(nctx->idTwo), (void*)&buffer[4], 4);
263      nctx->bComposite = 1;
264    }
265    else
266    {
267      memcpy( (void*)(nctx->idOne), (void*)&buffer[0], 4);
268      memset( (void*)(nctx->idTwo), 0, 4);
269      nctx->bComposite = 0;
270    }
271  }
272  G_CATCH_ALL
273  {
274    delete nctx;
275    G_RETHROW;
276  }
277  G_ENDCATCH;
278 
279  // Install context record
280  ctx = nctx;
281  chkid = GUTF8String(ctx->idOne, 4);
282  if (composite)
283    chkid = chkid + ":" + GUTF8String(ctx->idTwo, 4);
284
285  // Return
286  if (rawoffsetptr)
287    *rawoffsetptr = rawoffset;
288  if (rawsizeptr)
289    *rawsizeptr = ( ctx->offEnd - rawoffset + 1) & ~0x1;
290  return size;
291}
292
293
294
295// IFFByteStream::put_chunk
296// -- write new chunk header
297
298void 
299IFFByteStream::put_chunk(const char *chkid, int insert_magic)
300{
301  int bytes;
302  char buffer[8];
303
304  // Check that we are allowed to write a chunk
305  if (dir < 0)
306    G_THROW( ERR_MSG("IFFByteStream.read_write") );
307  if (ctx && !ctx->bComposite)
308    G_THROW( ERR_MSG("IFFByteStream.not_ready2") );
309  dir = +1;
310
311  // Check primary id
312  int composite = check_id(chkid);
313  if ((composite<0) || (composite==0 && chkid[4])
314      || (composite && (chkid[4]!=':' || check_id(&chkid[5]) || chkid[9])) )
315    G_THROW( ERR_MSG("IFFByteStream.bad_chunk") );
316
317  // Write padding byte
318  assert(seekto <= offset);
319  memset((void*)buffer, 0, 8);
320  if (offset & 1)
321    offset += bs->write((void*)&buffer[4], 1);
322
323  // Insert magic to make this file recognizable as DjVu
324  if (insert_magic)
325  {
326    // Don't change the way you do the file magic!
327    // I rely on these bytes letters in some places
328    // (like DjVmFile.cpp and djvm.cpp) -- eaf
329    buffer[0]=0x41;
330    buffer[1]=0x54;
331    buffer[2]=0x26;
332    buffer[3]=0x54;
333    offset += bs->writall((void*)&buffer[0], 4);
334  }
335
336  // Write chunk header
337  memcpy((void*)&buffer[0], (void*)&chkid[0], 4);
338  bytes = bs->writall((void*)&buffer[0], 8);
339  offset = seekto = offset + bytes;
340  if (composite)
341  {
342    memcpy((void*)&buffer[4], (void*)&chkid[5], 4);
343    bytes = bs->writall((void*)&buffer[4], 4);
344    offset = offset + bytes;   
345  }
346
347  // Create new context record
348  IFFContext *nctx = new IFFContext;
349  G_TRY
350  {
351    nctx->next = ctx;
352    nctx->offStart = seekto;
353    nctx->offEnd = 0;
354    if (composite)
355    {
356      memcpy( (void*)(nctx->idOne), (void*)&buffer[0], 4);
357      memcpy( (void*)(nctx->idTwo), (void*)&buffer[4], 4);
358      nctx->bComposite = 1;
359    }
360    else
361    {
362      memcpy( (void*)(nctx->idOne), (void*)&buffer[0], 4);
363      memset( (void*)(nctx->idTwo), 0, 4);
364      nctx->bComposite = 0;
365    }
366  }
367  G_CATCH_ALL
368  {
369    delete nctx;
370    G_RETHROW;
371  }
372  G_ENDCATCH; 
373  // Install context record and leave
374  ctx = nctx;
375}
376
377
378
379void 
380IFFByteStream::close_chunk()
381{
382  // Check that this is ok
383  if (!ctx)
384    G_THROW( ERR_MSG("IFFByteStream.cant_close") );
385  // Patch size field in new chunk
386  if (dir > 0)
387    {
388      ctx->offEnd = offset;
389      long size = ctx->offEnd - ctx->offStart;
390      char buffer[4];
391      buffer[0] = (unsigned char)(size>>24);
392      buffer[1] = (unsigned char)(size>>16);
393      buffer[2] = (unsigned char)(size>>8);
394      buffer[3] = (unsigned char)(size);
395      bs->seek(ctx->offStart - 4);
396      bs->writall((void*)buffer, 4);
397      bs->seek(offset);
398    }
399  // Arrange for reader to seek at next chunk
400  seekto = ctx->offEnd;
401  // Remove ctx record
402  IFFContext *octx = ctx;
403  ctx = octx->next;
404  assert(ctx==0 || ctx->bComposite);
405  delete octx;
406}
407
408// This is the same as above, but adds a seek to the close
409// Otherwise an EOF in this chunk won't be reported until we
410// try to open the next chunk, which makes error recovery
411// very difficult.
412void 
413IFFByteStream::seek_close_chunk(void)
414{
415  close_chunk();
416  if ((dir <= 0)&&((!ctx)||(ctx->bComposite))&&(seekto > offset))
417  {
418    bs->seek(seekto);
419    offset = seekto;
420  }
421}
422
423// IFFByteStream::short_id
424// Returns the id of the current chunk
425
426void 
427IFFByteStream::short_id(GUTF8String &chkid)
428{
429  if (!ctx)
430    G_THROW( ERR_MSG("IFFByteStream.no_chunk_id") );
431  if (ctx->bComposite)
432    chkid = GUTF8String(ctx->idOne, 4) + ":" + GUTF8String(ctx->idTwo, 4);
433  else
434    chkid = GUTF8String(ctx->idOne, 4);
435}
436
437
438// IFFByteStream::full_id
439// Returns the full chunk id of the current chunk
440
441void 
442IFFByteStream::full_id(GUTF8String &chkid)
443{
444  short_id(chkid);
445  if (ctx->bComposite)
446    return;
447  // Search parent FORM or PROP chunk.
448  for (IFFContext *ct = ctx->next; ct; ct=ct->next)
449    if (memcmp(ct->idOne, "FOR", 3)==0 || 
450        memcmp(ct->idOne, "PRO", 3)==0  )
451      {
452        chkid = GUTF8String(ct->idTwo, 4) + "." + chkid;
453        break;
454      }
455}
456
457
458
459// IFFByteStream::read
460// -- read bytes from IFF file chunk
461
462size_t 
463IFFByteStream::read(void *buffer, size_t size)
464{
465  if (! (ctx && dir < 0))
466    G_THROW( ERR_MSG("IFFByteStream.not_ready3") );
467  // Seek if necessary
468  if (seekto > offset) {
469    bs->seek(seekto);
470    offset = seekto;
471  }
472  // Ensure that read does not extend beyond chunk
473  if (offset > ctx->offEnd)
474    G_THROW( ERR_MSG("IFFByteStream.bad_offset") );
475  if (offset + (long)size >  ctx->offEnd)
476    size = (size_t) (ctx->offEnd - offset);
477  // Read bytes
478  size_t bytes = bs->read(buffer, size);
479  offset += bytes;
480  return bytes;
481}
482
483
484// IFFByteStream::write
485// -- write bytes to IFF file chunk
486
487size_t 
488IFFByteStream::write(const void *buffer, size_t size)
489{
490  if (! (ctx && dir > 0))
491    G_THROW( ERR_MSG("IFFByteStream.not_ready4") );
492  if (seekto > offset)
493    G_THROW( ERR_MSG("IFFByteStream.cant_write") );
494  size_t bytes = bs->write(buffer, size);
495  offset += bytes;
496  return bytes;
497}
498
499// IFFByteStream::tell
500// -- tell position
501
502long 
503IFFByteStream::tell() const
504{
505  return (seekto>offset)?seekto:offset;
506}
507
508bool
509IFFByteStream::compare(IFFByteStream &iff)
510{
511  bool retval=(&iff == this);
512  if(!retval)
513  {
514    GUTF8String chkid1, chkid2;
515    int size;
516    while((size=get_chunk(chkid1)) == iff.get_chunk(chkid2))
517    {
518      if(chkid1 != chkid2)
519      {
520        break;
521      }
522      if(!size)
523      {
524        retval=true;
525        break;
526      }
527      char buf[4096];
528      int len;
529      while((len=read(buf,sizeof(buf))))
530      {
531        int s=0;
532        char buf2[sizeof(buf)];
533        while(s<len)
534        {
535          const int i=iff.read(buf2+s,len-s);
536          if(!i)
537            break;
538          s+=i;
539        }
540        if((s != len)||memcmp(buf,buf2,len))
541          break;
542      }
543      if(len)
544        break;
545      iff.close_chunk();
546      close_chunk();
547    }
548  }
549  return retval;
550}
551
552
553#ifdef HAVE_NAMESPACES
554}
555# ifndef NOT_USING_DJVU_NAMESPACE
556using namespace DJVU;
557# endif
558#endif
Note: See TracBrowser for help on using the repository browser.