parseIndexEntry static method

void parseIndexEntry(
  1. MobiIndx indx,
  2. MobiIdxt idxt,
  3. MobiTagx tagx,
  4. MobiOrdt ordt,
  5. MobiBuffer buf,
  6. int currNumber,
)

Implementation

static void parseIndexEntry(MobiIndx indx, MobiIdxt idxt, MobiTagx tagx,
    MobiOrdt ordt, MobiBuffer buf, int currNumber) {
  final entryOffset = indx.entriesCount;
  final entryLength = idxt.offsets[currNumber + 1] - idxt.offsets[currNumber];
  buf.setPos(idxt.offsets[currNumber]);
  int entryNumber = currNumber + entryOffset;
  if (entryNumber >= indx.totalEntriesCount) {
    throw MobiInvalidDataException("Too Many Entries in Indx Record");
  }
  final maxlen = buf.maxlen;
  if (buf.offset + entryLength > maxlen) {
    throw MobiInvalidDataException("Index Entry Too Long");
  }
  buf.maxlen = buf.offset + entryLength;
  int labelLength = buf.getInt8();
  if (labelLength > entryLength) {
    throw MobiInvalidDataException("Label Length Too Long");
  }
  var label = "";
  if (ordt.ordt2.isNotEmpty) {
    label = getStringOrdt(ordt, buf, labelLength);
    labelLength = label.length;
  } else {
    label = indxGetLabel(buf, labelLength, indx.ligtEntriesCount != 0);
  }
  indx.entries[entryNumber].label = label;
  int controlBytesOffset = buf.offset;
  buf.seek(tagx.controlByteCount);
  indx.entries[entryNumber].tagsCount = 0;
  indx.entries[entryNumber].tags = [];
  if (tagx.tagsCount > 0) {
    List<MobiPtagx> ptagx = List.generate(tagx.tagsCount, (i) => MobiPtagx());
    int ptagxCount = 0;
    int len = 0;
    int i = 0;
    while (i < tagx.tagsCount) {
      if (tagx.tags[i].controlByte == 1) {
        controlBytesOffset++;
        i++;
        continue;
      }
      int value = buf.data[controlBytesOffset] & tagx.tags[i].bitMask;
      if (value != 0) {
        int valueCount = mobiNotSet;
        int valueBytes = mobiNotSet;
        if (value == tagx.tags[i].bitMask) {
          if (mobiBigCount(tagx.tags[i].bitMask) > 1) {
            len = 0;
            (len, valueBytes) = buf.getVarLen(len);
          } else {
            valueCount = 1;
          }
        } else {
          int mask = tagx.tags[i].bitMask;
          while ((mask & 1) == 0) {
            mask >>= 1;
            value >>= 1;
          }
          valueCount = value;
        }
        ptagx[ptagxCount].tag = tagx.tags[i].tag;
        ptagx[ptagxCount].tagValueCount = tagx.tags[i].valuesCount;
        ptagx[ptagxCount].valueCount = valueCount;
        ptagx[ptagxCount].valueBytes = valueBytes;
        ptagxCount++;
      }
      i++;
    }
    indx.entries[entryNumber].tags =
        List.generate(tagx.tagsCount, (i) => MobiIndexTag());
    i = 0;
    int valueBytes = 0;
    while (i < ptagxCount) {
      int tagValuesCount = 0;
      List<int> tagValues = [];
      if (ptagx[i].valueCount != mobiNotSet) {
        int count = ptagx[i].valueCount * ptagx[i].tagValueCount;
        while (count-- != 0 && tagValuesCount < indxTagValuesMax) {
          len = 0;
          (len, valueBytes) = buf.getVarLen(len);
          tagValues.add(valueBytes);
          tagValuesCount++;
        }
      } else {
        len = 0;
        while (
            len < ptagx[i].valueBytes && tagValuesCount < indxTagValuesMax) {
          (len, valueBytes) = buf.getVarLen(len);
          tagValues[tagValuesCount++] = valueBytes;
        }
      }
      if (tagValuesCount != 0) {
        indx.entries[entryNumber].tags[i].tagValues = tagValues;
      } else {
        indx.entries[entryNumber].tags[i].tagValues = [];
      }
      indx.entries[entryNumber].tags[i].tagId = ptagx[i].tag;
      indx.entries[entryNumber].tags[i].tagValuesCount = tagValuesCount;
      indx.entries[entryNumber].tagsCount++;
      i++;
    }
  }
  buf.maxlen = maxlen;
}