Ejemplo n.º 1
0
void ExternalExtractor::extract(ExtractionResult* result)
{
    Q_D(ExternalExtractor);

    QJsonDocument writeData;
    QJsonObject writeRootObject;
    QByteArray output;
    QByteArray errorOutput;

    writeRootObject[QStringLiteral("path")] = QJsonValue(result->inputUrl());
    writeRootObject[QStringLiteral("mimetype")] = result->inputMimetype();
    writeData.setObject(writeRootObject);

    QProcess extractorProcess;
    extractorProcess.start(d->mainPath, QIODevice::ReadWrite);
    extractorProcess.write(writeData.toJson());
    extractorProcess.closeWriteChannel();
    extractorProcess.waitForFinished(EXTRACTOR_TIMEOUT_MS);

    output = extractorProcess.readAll();
    errorOutput = extractorProcess.readAllStandardError();

    if (extractorProcess.exitStatus()) {
        qDebug() << errorOutput;
        return;
    }

    // now we read in the output (which is a standard json format) into the
    // ExtractionResult

    QJsonDocument extractorData = QJsonDocument::fromJson(output);
    if (!extractorData.isObject()) {
        return;
    }
    QJsonObject rootObject = extractorData.object();
    QJsonObject propertiesObject = rootObject[QStringLiteral("properties")].toObject();

    Q_FOREACH(auto key, propertiesObject.keys()) {
        if (key == QStringLiteral("typeInfo")) {
            TypeInfo info = TypeInfo::fromName(propertiesObject.value(key).toString());
            result->addType(info.type());
            continue;
        }

        // for plaintext extraction
        if (key == QStringLiteral("text")) {
            result->append(propertiesObject.value(key).toString(QStringLiteral("")));
            continue;
        }

        PropertyInfo info = PropertyInfo::fromName(key);
        if (info.name() != key) {
            continue;
        }
        result->add(info.property(), propertiesObject.value(key).toVariant());
    }

    if (rootObject[QStringLiteral("status")].toString() != QStringLiteral("OK")) {
        qDebug() << rootObject[QStringLiteral("error")].toString();
    }
}
Ejemplo n.º 2
0
void Decoder::DecoderState::addType(TypeInfo& tinfo,
                                    const StructField& field,
                                    int16_t tag, uint32_t count) {
  const bool isStruct = (state == IN_STRUCT);

  auto type = tinfo.type();
  switch (type) {
  case reflection::TYPE_BOOL:
    bools.count += count;
    if (isStruct) {
      str.boolTags.emplace_back(tag, type);
    }
    break;
  case reflection::TYPE_BYTE:
    bytes.count += count;
    if (isStruct) {
      str.byteTags.emplace_back(tag, type);
    }
    break;
  case reflection::TYPE_I64:     // fallthrough
  case reflection::TYPE_DOUBLE:
    int64s.count += count;
    if (isStruct) {
      if (field.isFixed) {
        str.fixedInt64Tags.emplace_back(tag, type);
      } else {
        str.int64Tags.emplace_back(tag, type);
      }
    }
    break;
  case reflection::TYPE_I16:
    ints.count += count;
    if (isStruct) {
      if (field.isFixed) {
        str.fixedInt16Tags.emplace_back(tag, type);
      } else {
        str.intTags.emplace_back(tag, type);
      }
    }
    break;
  case reflection::TYPE_ENUM:
    if (field.isStrictEnum) {
      strictEnums.count += count;
      totalStrictEnumBits += count * tinfo.dataType->enumBits();
      if (isStruct) {
        str.strictEnumTags.emplace_back(tag, tinfo);
      }
      break;
    }
    type = reflection::TYPE_I32;  // handle non-strict enums as i32
    // fallthrough (to TYPE_I32)
  case reflection::TYPE_I32:
    ints.count += count;
    if (isStruct) {
      if (field.isFixed) {
        str.fixedInt32Tags.emplace_back(tag, type);
      } else {
        str.intTags.emplace_back(tag, type);
      }
    }
    break;
  case reflection::TYPE_STRING:
    if (field.isInterned) {
      internedStrings.count += count;
      if (isStruct) {
        str.internedStringTags.emplace_back(tag, type);
      }
    } else {
      if (field.isTerminated) {
        tinfo.length = kTerminated;
        tinfo.terminator = field.terminator;
      } else if (field.isFixed) {
        tinfo.length = field.fixedStringSize;
      } else {
        vars.count += count;
      }
      strings.count += count;
      if (isStruct) {
        str.stringTags.emplace_back(tag, tinfo);
      }
    }
    break;

  default:  // user-defined type
    strings.count += count;
    if (isStruct) {
      str.stringTags.emplace_back(tag, tinfo);
    }
    break;
  }
}