summaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_encryption.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2009-09-27 22:05:38 +0200
committerJay Berkenbilt <ejb@ql.org>2009-09-27 22:05:38 +0200
commit8d7bb3ff50943fa51ac1d968930bd23071376904 (patch)
tree8667d6782295c92b960d1c2a613640daa463eac8 /libqpdf/QPDF_encryption.cc
parent40f4b1ef5237a51b38b74b04d53d6aa20819b5d7 (diff)
downloadqpdf-8d7bb3ff50943fa51ac1d968930bd23071376904.tar.zst
add methods for getting encryption data
git-svn-id: svn+q:///qpdf/trunk@733 71b93d88-0707-0410-a8cf-f5a4172ac649
Diffstat (limited to 'libqpdf/QPDF_encryption.cc')
-rw-r--r--libqpdf/QPDF_encryption.cc182
1 files changed, 182 insertions, 0 deletions
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;
+}