summaryrefslogtreecommitdiffstats
path: root/akregator/src/mk4storage/metakit/src/viewx.cpp
blob: db30d14e54420a48585eb83e330decf816406b3a (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// viewx.cpp --
// $Id$
// This is part of Metakit, see http://www.equi4.com/metakit/

/** @file
 * Implements c4_Sequence, c4_Reference, and c4_...Ref
 */

#include "header.h"
#include "handler.h"
#include "store.h"
#include "column.h"

/////////////////////////////////////////////////////////////////////////////

c4_Sequence::c4_Sequence ()
  : _refCount (0), _dependencies (0), _propertyLimit (0), _tempBuf (0)
{            
}

c4_Sequence::~c4_Sequence ()
{
  d4_assert(_refCount == 0);

  d4_assert(!_dependencies); // there can be no dependencies left

  ClearCache();

  delete _tempBuf;
}

c4_Persist* c4_Sequence::Persist() const
{
  return 0;
}

  /// Increment the reference count of this sequence
void c4_Sequence::IncRef()
{
  ++_refCount;
  
  d4_assert(_refCount != 0);
}

  /// Decrement the reference count, delete objects when last
void c4_Sequence::DecRef()
{
  d4_assert(_refCount != 0);

  if (--_refCount == 0)
    delete this;
}

  /// Return the current reference count
int c4_Sequence::NumRefs() const
{
  return _refCount;
}

  /// Compare the specified row with another one
int c4_Sequence::Compare(int index_, c4_Cursor cursor_) const
{
  d4_assert(cursor_._seq != 0);
  
  c4_Bytes data;

  for (int colNum = 0; colNum < NumHandlers(); ++colNum)
  {
    c4_Handler& h = NthHandler(colNum);

    const c4_Sequence* hc = HandlerContext(colNum);
    int i = RemapIndex(index_, hc);

    if (!cursor_._seq->Get(cursor_._index, h.PropId(), data))
      h.ClearBytes(data);
        
    int f = h.Compare(i, data);
    if (f != 0)
      return f;
  }        
  
  return 0;
}

  /// Restrict the search range for rows
bool c4_Sequence::RestrictSearch(c4_Cursor, int&, int&)
{
  return true;
}

  /// Replace the contents of a specified row
void c4_Sequence::SetAt(int index_, c4_Cursor newElem_)
{
  d4_assert(newElem_._seq != 0);

  c4_Bytes data;

  c4_Notifier change (this);
  if (GetDependencies())
    change.StartSetAt(index_, newElem_);

  for (int i = 0; i < newElem_._seq->NumHandlers(); ++i)
  {
    c4_Handler& h = newElem_._seq->NthHandler(i);

      // added 06-12-1999 to do index remapping for derived seq's
    const c4_Sequence* hc = newElem_._seq->HandlerContext(i);
    int ri = newElem_._seq->RemapIndex(newElem_._index, hc);

    h.GetBytes(ri, data);

//    Set(index_, cursor._seq->NthProperty(i), data);
    int colNum = PropIndex(h.Property());
    d4_assert(colNum >= 0);

    NthHandler(colNum).Set(index_, data);
  }

    // if number of props in dest is larger after adding, clear the rest
    // this way, new props get copied and undefined props get cleared
  if (newElem_._seq->NumHandlers() < NumHandlers())
  {
    for (int j = 0; j < NumHandlers(); ++j)
    {
      c4_Handler& h = NthHandler(j);

        // if the property does not appear in the source
      if (newElem_._seq->PropIndex(h.PropId()) < 0)
      {
        h.ClearBytes(data);
        h.Set(index_, data);
      }
    }
  }
}

  /// Remap the index to an underlying view
int c4_Sequence::RemapIndex(int index_, const c4_Sequence* seq_) const
{
  return seq_ == this ? index_ : -1;
}

  /// Gives access to a general purpose temporary buffer
c4_Bytes& c4_Sequence::Buffer()
{
  if (_tempBuf == 0)
    _tempBuf = d4_new c4_Bytes;
  return *_tempBuf;
}

  // 1.8.5: extra buffer to hold returned description strings
const char* c4_Sequence::UseTempBuffer(const char* str_)
{
  return strcpy((char*) Buffer().SetBuffer(strlen(str_) + 1), str_);
}

  /// Change number of rows, either by inserting or removing them
void c4_Sequence::Resize(int newSize_, int)
{
  if (NumHandlers() > 0)
  {
    int diff = newSize_ - NumRows();

    if (diff > 0)
    {
      c4_Row empty; // make sure this doesn't recurse, see below
      InsertAt(NumRows(), &empty, diff);
    }
    else if (diff < 0)
      RemoveAt(newSize_, - diff);
  }
  else  // need special case to avoid recursion for c4_Row allocations
    SetNumRows(newSize_);
}
  
  /// Insert one or more rows into this sequence
void c4_Sequence::InsertAt(int index_, c4_Cursor newElem_, int count_)
{
  d4_assert(newElem_._seq != 0);

  c4_Notifier change (this);
  if (GetDependencies())
    change.StartInsertAt(index_, newElem_, count_);

  SetNumRows(NumRows() + count_);

  c4_Bytes data;

  for (int i = 0; i < newElem_._seq->NumHandlers(); ++i)
  {
    c4_Handler& h = newElem_._seq->NthHandler(i);

      // added 06-12-1999 to do index remapping for derived seq's
    const c4_Sequence* hc = newElem_._seq->HandlerContext(i);
    int ri = newElem_._seq->RemapIndex(newElem_._index, hc);

    h.GetBytes(ri, data);

    int colNum = PropIndex(h.Property());
    d4_assert(colNum >= 0);

    if (h.Property().Type() == 'V')
    {
        // special treatment for subviews, insert empty, then overwrite
        // changed 19990904 - probably fixes a long-standing limitation
      c4_Bytes temp;
      h.ClearBytes(temp);

      c4_Handler& h2 = NthHandler(colNum);
      h2.Insert(index_, temp, count_);
      for (int j = 0; j < count_; ++j)
        h2.Set(index_ + j, data);
    }
    else
      NthHandler(colNum).Insert(index_, data, count_);
  }

    // if number of props in dest is larger after adding, clear the rest
    // this way, new props get copied and undefined props get cleared
  if (newElem_._seq->NumHandlers() < NumHandlers())
  {
    for (int j = 0; j < NumHandlers(); ++j)
    {
      c4_Handler& h = NthHandler(j);

        // if the property does not appear in the source
      if (newElem_._seq->PropIndex(h.PropId()) < 0)
      {
        h.ClearBytes(data);
        h.Insert(index_, data, count_);
      }
    }
  }
}

  /// Remove one or more rows from this sequence
void c4_Sequence::RemoveAt(int index_, int count_)
{
  c4_Notifier change (this);
  if (GetDependencies())
    change.StartRemoveAt(index_, count_);

  SetNumRows(NumRows() - count_);

    //! careful, this does no index remapping, wrong for derived seq's
  for (int i = 0; i < NumHandlers(); ++i)
    NthHandler(i).Remove(index_, count_);
}

  /// Move a row to another position
void c4_Sequence::Move(int from_, int to_)
{
  c4_Notifier change (this);
  if (GetDependencies())
    change.StartMove(from_, to_);

    //! careful, this does no index remapping, wrong for derived seq's
  for (int i = 0; i < NumHandlers(); ++i)
    NthHandler(i).Move(from_, to_);
}

  /// Return the id of the N-th property
int c4_Sequence::NthPropId(int index_) const
{
  return NthHandler(index_).PropId();
}

void c4_Sequence::ClearCache()
{
  if (_propertyLimit > 0)
  {
    delete [] _propertyMap;     // property indexes may change
    _propertyLimit = 0;
  }
}

  /// Find the index of a property by its id
int c4_Sequence::PropIndex(int propId_)
{
//! CACHING NOTE: derived views will fail if underlying view is restructured
//          still, this cache is kept, since sort will fail anyway...
//  The only safe change in these cases is adding new properties at the end.

    // use the map for the fastest result once known
  if (propId_ < _propertyLimit && _propertyMap[propId_] >= 0)
    return _propertyMap[propId_];

    // locate the property using a linear search, return if not present
  int n = NumHandlers();
  do
    if (--n < 0)
      return -1;
  while (NthPropId(n) != propId_);

    // if the map is too small, resize it (with a little slack)
  if (propId_ >= _propertyLimit)
  {
    int round = (propId_ + 8) & ~0x07;
    short* vec = d4_new short [round];

    for (int i = 0; i < round; ++i)
      vec[i] = i < _propertyLimit ? _propertyMap[i] : -1;
    
    if (_propertyLimit > 0)
      delete [] _propertyMap;

    _propertyMap = vec;
    _propertyLimit = round;
  }

    // we have a map, adjust the entry and return
  return _propertyMap[propId_] = n;
}

  /// Find the index of a property, or create a new entry
int c4_Sequence::PropIndex(const c4_Property& prop_)
{
  int pos = PropIndex(prop_.GetId());
  if (pos >= 0)
  {
    d4_assert(NthHandler(pos).Property().Type() == prop_.Type());
    return pos;
  }

  c4_Handler* h = CreateHandler(prop_);
  d4_assert(h != 0);

  int i = AddHandler(h);
  if (i >= 0 && NumRows() > 0)
  {
    c4_Bytes data;
    h->ClearBytes(data);
    h->Insert(0, data, NumRows());
  }

  return i;
}

const char* c4_Sequence::Description()
{
  return 0;
}

int c4_Sequence::ItemSize(int index_, int propId_)
{
  int colNum = PropIndex(propId_);
  return colNum >= 0 ? NthHandler(colNum).ItemSize(index_) : -1;
}

bool c4_Sequence::Get(int index_, int propId_, c4_Bytes& buf_)
{
  int colNum = PropIndex(propId_);
  if (colNum < 0)
    return false;

  NthHandler(colNum).GetBytes(index_, buf_);
  return true;
}

void c4_Sequence::Set(int index_, const c4_Property& prop_, const c4_Bytes& buf_)
{
  int colNum = PropIndex(prop_);
  d4_assert(colNum >= 0);

  c4_Handler& h = NthHandler(colNum);

  c4_Notifier change (this);
  if (GetDependencies())
    change.StartSet(index_, prop_.GetId(), buf_);

  if (buf_.Size())
    h.Set(index_, buf_);
  else
  {
    c4_Bytes empty;
    h.ClearBytes(empty);
    h.Set(index_, empty);
  }
}

  /// Register a sequence to receive change notifications
void c4_Sequence::Attach(c4_Sequence* child_)
{
  IncRef();

  if (!_dependencies)
    _dependencies = d4_new c4_Dependencies;

  _dependencies->Add(child_);
}

  /// Unregister a sequence which received change notifications
void c4_Sequence::Detach(c4_Sequence* child_)
{
  d4_assert(_dependencies != 0);
  
  if (!_dependencies->Remove(child_))
  {
    delete _dependencies;
    _dependencies = 0;
  }

  DecRef();
}

  /// Called just before a change is made to the sequence
c4_Notifier* c4_Sequence::PreChange(c4_Notifier&)
{
  d4_assert(0); // should not be called, because it should not attach
  return 0;
}

  /// Called after changes have been made to the sequence
void c4_Sequence::PostChange(c4_Notifier&)
{
}

/////////////////////////////////////////////////////////////////////////////

c4_Reference& c4_Reference::operator= (const c4_Reference& value_)
{      
  c4_Bytes result;
  value_.GetData(result);
  SetData(result);

  return *this;
}

bool operator== (const c4_Reference& a_, const c4_Reference& b_)
{
  c4_Bytes buf1;
  bool f1 = a_.GetData(buf1);

  c4_Bytes buf2;
  bool f2 = b_.GetData(buf2);

    // if absent, fill either with zero bytes to match length
  if (!f1)
    buf1.SetBufferClear(buf2.Size());
  if (!f2)
    buf2.SetBufferClear(buf1.Size());

  return buf1 == buf2;
}

/////////////////////////////////////////////////////////////////////////////

c4_IntRef::operator t4_i32 () const
{
  c4_Bytes result;
  if (!GetData(result))
    return 0;

  d4_assert(result.Size() == sizeof (t4_i32));
  return *(const t4_i32*) result.Contents();
}
  
c4_IntRef& c4_IntRef::operator= (t4_i32 value_)
{
  SetData(c4_Bytes (&value_, sizeof value_));
  return *this;
}

/////////////////////////////////////////////////////////////////////////////
#if !q4_TINY
/////////////////////////////////////////////////////////////////////////////

c4_LongRef::operator t4_i64 () const
{
  c4_Bytes result;
  if (!GetData(result))
  {
    static t4_i64 zero;
    return zero;
  }
  
  d4_assert(result.Size() == sizeof (t4_i64));
  return *(const t4_i64*) result.Contents();
}
  
c4_LongRef& c4_LongRef::operator= (t4_i64 value_)
{
  SetData(c4_Bytes (&value_, sizeof value_));
  return *this;
}

/////////////////////////////////////////////////////////////////////////////

c4_FloatRef::operator double () const
{
  c4_Bytes result;
  if (!GetData(result))
    return 0;

  d4_assert(result.Size() == sizeof (float));
  return *(const float*) result.Contents();
}
  
c4_FloatRef& c4_FloatRef::operator= (double value_)
{
  float v = (float) value_; // loses precision
  SetData(c4_Bytes (&v, sizeof v));
  return *this;
}

/////////////////////////////////////////////////////////////////////////////

c4_DoubleRef::operator double () const
{
  c4_Bytes result;
  if (!GetData(result))
    return 0;

  d4_assert(result.Size() == sizeof (double));
  return *(const double*) result.Contents();
}
  
c4_DoubleRef& c4_DoubleRef::operator= (double value_)
{
  SetData(c4_Bytes (&value_, sizeof value_));
  return *this;
}

/////////////////////////////////////////////////////////////////////////////
#endif // !q4_TINY
/////////////////////////////////////////////////////////////////////////////

c4_BytesRef::operator c4_Bytes () const
{
  c4_Bytes result;
  GetData(result);

    // the result must immediately be used, its lifetime may be limited
  return result;
}

c4_BytesRef& c4_BytesRef::operator= (const c4_Bytes& value_)
{
  SetData(value_);
  return *this;
}

c4_Bytes c4_BytesRef::Access(t4_i32 off_, int len_) const
{
  c4_Bytes& buffer = _cursor._seq->Buffer();
  
  int colNum = _cursor._seq->PropIndex(_property.GetId());
  if (colNum >= 0)
  {
    c4_Handler& h = _cursor._seq->NthHandler(colNum);
    int sz = h.ItemSize(_cursor._index);
    if (len_ == 0 || off_ + len_ > sz)
      len_ = sz - off_;

    c4_Column* col = h.GetNthMemoCol(_cursor._index, true);
    if (col != 0)
    {

      if (len_ > 0) {
        col->FetchBytes(off_, len_, buffer, true);
        return buffer;
      }
    }
    else // do it the hard way for custom/mapped views (2002-03-13)
    {
      c4_Bytes result;
      GetData(result);
      d4_assert(off_ + len_ <= result.Size());
      return c4_Bytes (result.Contents() + off_, len_, true);
    }
  }

  return c4_Bytes ();
}

bool c4_BytesRef::Modify(const c4_Bytes& buf_, t4_i32 off_, int diff_) const
{
  int colNum = _cursor._seq->PropIndex(_property.GetId());
  if (colNum >= 0)
  {
    c4_Handler& h = _cursor._seq->NthHandler(colNum);
    const int n = buf_.Size();
    const t4_i32 limit = off_ + n; // past changed bytes
    const t4_i32 overshoot = limit - h.ItemSize(_cursor._index);

    if (diff_ < overshoot)
      diff_ = overshoot;

    c4_Column* col = h.GetNthMemoCol(_cursor._index, true);
    if (col != 0)
    {
      if (diff_ < 0)
        col->Shrink(limit, - diff_);
      else if (diff_ > 0)
          // insert bytes in the highest possible spot
          // if a gap is created, it will contain garbage
        col->Grow(overshoot > 0 ? col->ColSize() :
               diff_ > n ? off_ : limit - diff_, diff_);

      col->StoreBytes(off_, buf_);
    }
    else // do it the hard way for custom/mapped views (2002-03-13)
    {
      c4_Bytes orig;
      GetData(orig);

      c4_Bytes result;
      t4_byte* ptr = result.SetBuffer(orig.Size() + diff_);

      memcpy(ptr, orig.Contents(), off_);
      memcpy(ptr + off_, buf_.Contents(), n);
      memcpy(ptr + off_ + n, orig.Contents() + off_, orig.Size() - off_);

      SetData(result);
    }
    return true;
  }

  return false;
}

/////////////////////////////////////////////////////////////////////////////

c4_StringRef::operator const char* () const
{
  c4_Bytes result;
  GetData(result);

  return result.Size() > 0 ? (const char*) result.Contents() : "";
}

c4_StringRef& c4_StringRef::operator= (const char* value_)
{
  SetData(c4_Bytes (value_, strlen(value_) + 1));
  return *this;
}

/////////////////////////////////////////////////////////////////////////////

c4_ViewRef::operator c4_View () const
{
  c4_Bytes result;
  if (!GetData(result))
    return (c4_Sequence*) 0; // resolve ambiguity

  d4_assert(result.Size() == sizeof (c4_Sequence*));
  return *(c4_Sequence* const*) result.Contents();
}

c4_ViewRef& c4_ViewRef::operator= (const c4_View& value_)
{
  SetData(c4_Bytes (&value_._seq, sizeof value_._seq));
  return *this;
}

/////////////////////////////////////////////////////////////////////////////

c4_Stream::~c4_Stream ()
{
}

/////////////////////////////////////////////////////////////////////////////

c4_Strategy::c4_Strategy ()
  : _bytesFlipped (false), _failure (0),
    _mapStart (0), _dataSize (0), _baseOffset (0), _rootPos (-1), _rootLen (-1)
{
}

c4_Strategy::~c4_Strategy ()
{
  d4_assert(_mapStart == 0);
}

  /// Read a number of bytes
int c4_Strategy::DataRead(t4_i32, void*, int)
{
/*
  if (_mapStart != 0 && pos_ + length_ <= _dataSize)
  {
  memcpy(buffer_, _mapStart + pos_, length_);
  return length_;
  }
*/
  ++_failure;
  return -1;
}

  /// Write a number of bytes, return true if successful
void c4_Strategy::DataWrite(t4_i32, const void*, int)
{
  ++_failure;
}

  /// Flush and truncate file
void c4_Strategy::DataCommit(t4_i32)
{
}

  /// Override to support memory-mapped files
void c4_Strategy::ResetFileMapping()
{
}

  /// Report total size of the datafile
t4_i32 c4_Strategy::FileSize()
{
  return _dataSize;
}

  /// Return a value to use as fresh generation counter
t4_i32 c4_Strategy::FreshGeneration()
{
  return 1;
}

  /// Define the base offset where data is stored
void c4_Strategy::SetBase(t4_i32 base_)
{
  t4_i32 off = base_ - _baseOffset;
  _baseOffset = base_;
  _dataSize -= off;
  if (_mapStart != 0)
  _mapStart += off;
}

/*
  end_ is file position to start from (0 defaults to FileSize())

  result is the logical end of the datafile (or -1 if no data)

  This code uses a tiny state machine so all the code to read and decode
  file marks is in one place within the loop.
*/

  /// Scan datafile head/tail markers, return logical end of data
t4_i32 c4_Strategy::EndOfData(t4_i32 end_)
{
  enum { kStateAtEnd, kStateCommit, kStateHead, kStateOld, kStateDone };

  t4_i32 pos = (end_ >= 0 ? end_ : FileSize()) - _baseOffset;
  t4_i32 last = pos;
  t4_i32 rootPos = 0;
  t4_i32 rootLen = -1; // impossible value, flags old-style header
  t4_byte mark [8];
  
  for (int state = kStateAtEnd; state != kStateDone; )
  {
    pos -= 8;
    if (pos + _baseOffset < 0 && state != kStateOld)
  {
    // bad offset, try old-style header from start of file
    pos = - _baseOffset;
    state = kStateOld;
    }

    if (DataRead(pos, &mark, sizeof mark) != sizeof mark)
      return -1;

  t4_i32 count = 0;
  for (int i = 1; i < 4; ++i)
    count = (count << 8) + mark[i];

  t4_i32 offset = 0;
  for (int j = 4; j < 8; ++j)
    offset = (offset << 8) + mark[j];

  const bool isSkipTail = mark[0] == 0x80 && count == 0 && offset > 0;
  const bool isCommitTail = mark[0] == 0x80 && count > 0 && offset > 0;
  const bool isHeader = (mark[0] == 'J' || mark[0] == 'L') &&
        (mark[0] ^ mark[1]) == ('J' ^ 'L') && 
           mark[2] == 0x1A;
    switch (state)
    {
  case kStateAtEnd: // no commit tail found yet

    if (isSkipTail)
    {
    pos -= offset;
    last = pos;
    }
    else if (isCommitTail)
    {
    rootPos = offset;
    rootLen = count;
    state = kStateCommit;
    }
    else
    {
    pos = 8;
    state = kStateOld;
    }
    break;

  case kStateCommit: // commit tail must be preceded by skip tail

    if (!isSkipTail)
    return -1;
    pos -= offset - 8;
    state = kStateHead;
    break;

  case kStateHead: // fetch the header

    if (!isHeader)
    {
    pos = 8;
    state = kStateOld;
    }
    else
    {
    state = kStateDone;
    }
    break;

  case kStateOld: // old format, look for header in first 4 Kb

    if (isHeader && mark[3] == 0x80)
    {
    d4_assert(rootPos == 0);
    for (int k = 8; --k >= 4; ) // old header is little-endian
      rootPos = (rootPos << 8) + mark[k];
    state = kStateDone;
    } 
    else
    {
    pos += 16;
    if (pos > 4096)
      return -1;
    }
    break;
  }
  }

  last += _baseOffset; // all seeks were relative to current offset

  if (end_ >= 0) // if end was specified, then adjust this strategy object
  {
  _baseOffset += pos;
  d4_assert(_baseOffset >= 0);
  if (_mapStart != 0)
  {
    _mapStart += pos;
    _dataSize -= pos;
  }

  _rootPos = rootPos;
  _rootLen = rootLen;
  }

  d4_assert(mark[0] == 'J' || mark[1] == 'J');
  _bytesFlipped = (char) *(const short*) mark != 'J';

  return last;
}

/////////////////////////////////////////////////////////////////////////////