bool CryptFileDevice::seek(qint64 pos)
{
    bool result = QIODevice::seek(pos);
    if (m_encrypted)
    {
        m_device->seek(kHeaderLength + pos);
        initCtr(&m_ctrState, m_ctrState.ivec);
    }
    else
    {
        m_device->seek(pos);
    }

    return result;
}
bool CryptFileDevice::initCipher()
{
    const EVP_CIPHER *cipher = EVP_enc_null();
    if (m_aesKeyLength == kAesKeyLength128)
        cipher = EVP_aes_128_ctr();
    else if (m_aesKeyLength == kAesKeyLength192)
        cipher = EVP_aes_192_ctr();
    else if (m_aesKeyLength == kAesKeyLength256)
        cipher = EVP_aes_256_ctr();
    else
        Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown value of AesKeyLength");

    EVP_CIPHER_CTX ctx;

    EVP_CIPHER_CTX_init(&ctx);
    EVP_EncryptInit_ex(&ctx, cipher, NULL, NULL, NULL);
    int keyLength = EVP_CIPHER_CTX_key_length(&ctx);
    int ivLength = EVP_CIPHER_CTX_iv_length(&ctx);

    unsigned char key[keyLength];
    unsigned char iv[ivLength];

    int ok = EVP_BytesToKey(cipher,
                            EVP_sha256(),
                            m_salt.isEmpty() ? NULL : (unsigned char *)m_salt.data(),
                            (unsigned char *)m_password.data(),
                            m_password.length(),
                            m_numRounds,
                            key,
                            iv);

    EVP_CIPHER_CTX_cleanup(&ctx);

    if (ok == 0)
        return false;

    int res = AES_set_encrypt_key(key, keyLength * 8, &m_aesKey);
    if (res != 0)
        return false;

    initCtr(&m_ctrState, iv);

    return true;
}
PerfMeasurement::PerfMeasurement(PerfMeasurement::EventMask toMeasure)
  : impl(OffTheBooks::new_<Impl>()),
    eventsMeasured(impl ? static_cast<Impl*>(impl)->init(toMeasure)
                   : EventMask(0)),
    cpu_cycles(initCtr(CPU_CYCLES)),
    instructions(initCtr(INSTRUCTIONS)),
    cache_references(initCtr(CACHE_REFERENCES)),
    cache_misses(initCtr(CACHE_MISSES)),
    branch_instructions(initCtr(BRANCH_INSTRUCTIONS)),
    branch_misses(initCtr(BRANCH_MISSES)),
    bus_cycles(initCtr(BUS_CYCLES)),
    page_faults(initCtr(PAGE_FAULTS)),
    major_page_faults(initCtr(MAJOR_PAGE_FAULTS)),
    context_switches(initCtr(CONTEXT_SWITCHES)),
    cpu_migrations(initCtr(CPU_MIGRATIONS))
{
}