From 8d7bb3ff50943fa51ac1d968930bd23071376904 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 27 Sep 2009 20:05:38 +0000 Subject: add methods for getting encryption data git-svn-id: svn+q:///qpdf/trunk@733 71b93d88-0707-0410-a8cf-f5a4172ac649 --- libqpdf/QPDF_encryption.cc | 182 +++++++++++++++++++++++++++++++++++++++++++++ libqpdf/qpdf-c.cc | 63 ++++++++++++++++ 2 files changed, 245 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index 2e0e59e7..5d061dfd 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -463,3 +463,185 @@ QPDF::isEncrypted() const { return this->encrypted; } + +DLL_EXPORT +bool +QPDF::isEncrypted(int& R, int& P) +{ + if (this->encrypted) + { + QPDFObjectHandle trailer = getTrailer(); + QPDFObjectHandle encrypt = trailer.getKey("/Encrypt"); + QPDFObjectHandle Pkey = encrypt.getKey("/P"); + QPDFObjectHandle Rkey = encrypt.getKey("/R"); + P = Pkey.getIntValue(); + R = Rkey.getIntValue(); + return true; + } + else + { + return false; + } +} + +static bool +is_bit_set(int P, int bit) +{ + // Bits in P are numbered from 1 in the spec + return (P & (1 << (bit - 1))); +} + +DLL_EXPORT +bool +QPDF::allowAccessibility() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + if (R < 3) + { + status = is_bit_set(P, 5); + } + else + { + status = is_bit_set(P, 10); + } + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowExtractAll() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + status = is_bit_set(P, 5); + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowPrintLowRes() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + status = is_bit_set(P, 3); + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowPrintHighRes() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + status = is_bit_set(P, 3); + if ((R >= 3) && (! is_bit_set(P, 12))) + { + status = false; + } + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowModifyAssembly() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + if (R < 3) + { + status = is_bit_set(P, 4); + } + else + { + status = is_bit_set(P, 11); + } + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowModifyForm() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + if (R < 3) + { + status = is_bit_set(P, 6); + } + else + { + status = is_bit_set(P, 9); + } + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowModifyAnnotation() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + status = is_bit_set(P, 6); + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowModifyOther() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + status = is_bit_set(P, 4); + } + return status; +} + +DLL_EXPORT +bool +QPDF::allowModifyAll() +{ + int R = 0; + int P = 0; + bool status = true; + if (isEncrypted(R, P)) + { + status = (is_bit_set(P, 4) && is_bit_set(P, 6)); + if (R >= 3) + { + status = status && (is_bit_set(P, 9) && is_bit_set(P, 11)); + } + } + return status; +} diff --git a/libqpdf/qpdf-c.cc b/libqpdf/qpdf-c.cc index d7f8d40f..5f894895 100644 --- a/libqpdf/qpdf-c.cc +++ b/libqpdf/qpdf-c.cc @@ -185,6 +185,69 @@ QPDF_BOOL qpdf_is_encrypted(qpdf_data qpdf) return (qpdf->qpdf->isEncrypted() ? QPDF_TRUE : QPDF_FALSE); } +DLL_EXPORT +QPDF_BOOL qpdf_allow_accessibility(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_accessibility"); + return qpdf->qpdf->allowAccessibility(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_extract_all(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_extract_all"); + return qpdf->qpdf->allowExtractAll(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_print_low_res(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_print_low_res"); + return qpdf->qpdf->allowPrintLowRes(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_print_high_res(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_print_high_res"); + return qpdf->qpdf->allowPrintHighRes(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_modify_assembly(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_modify_assembly"); + return qpdf->qpdf->allowModifyAssembly(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_modify_form(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_modify_form"); + return qpdf->qpdf->allowModifyForm(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_modify_annotation(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_modify_annotation"); + return qpdf->qpdf->allowModifyAnnotation(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_modify_other(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_modify_other"); + return qpdf->qpdf->allowModifyOther(); +} + +DLL_EXPORT +QPDF_BOOL qpdf_allow_modify_all(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_allow_modify_all"); + return qpdf->qpdf->allowModifyAll(); +} + DLL_EXPORT QPDF_ERROR_CODE qpdf_init_write(qpdf_data qpdf, char const* filename) { -- cgit v1.2.3-54-g00ecf