CGPDFDocument.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // CGPDFDocument.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-07-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "CGPDFDocument.h"
  26. //
  27. // CGPDFDocumentRef CGPDFDocumentCreateUsingUrl(CFURLRef, NSString *) function
  28. //
  29. CGPDFDocumentRef CGPDFDocumentCreateUsingUrl(CFURLRef theURL, NSString *password)
  30. {
  31. CGPDFDocumentRef thePDFDocRef = NULL; // CGPDFDocument
  32. if (theURL != NULL) // Check for non-NULL CFURLRef
  33. {
  34. thePDFDocRef = CGPDFDocumentCreateWithURL(theURL);
  35. if (thePDFDocRef != NULL) // Check for non-NULL CGPDFDocumentRef
  36. {
  37. if (CGPDFDocumentIsEncrypted(thePDFDocRef) == TRUE) // Encrypted
  38. {
  39. // Try a blank password first, per Apple's Quartz PDF example
  40. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, "") == FALSE)
  41. {
  42. // Nope, now let's try the provided password to unlock the PDF
  43. if ((password != nil) && (password.length > 0)) // Not blank?
  44. {
  45. char text[128]; // char array buffer for the string conversion
  46. [password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];
  47. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, text) == FALSE) // Log failure
  48. {
  49. #ifdef DEBUG
  50. NSLog(@"CGPDFDocumentCreateUsingUrl: Unable to unlock [%@] with [%@]", theURL, password);
  51. #endif
  52. }
  53. }
  54. }
  55. if (CGPDFDocumentIsUnlocked(thePDFDocRef) == FALSE) // Cleanup unlock failure
  56. {
  57. (void)(CGPDFDocumentRelease(thePDFDocRef)), thePDFDocRef = NULL;
  58. }
  59. }
  60. }
  61. }
  62. else // Log an error diagnostic
  63. {
  64. #ifdef DEBUG
  65. NSLog(@"CGPDFDocumentCreateUsingUrl: theURL == NULL");
  66. #endif
  67. }
  68. return thePDFDocRef;
  69. }
  70. //
  71. // CGPDFDocumentRef CGPDFDocumentCreateUsingData(CGDataProviderRef, NSString *) function
  72. //
  73. CGPDFDocumentRef CGPDFDocumentCreateUsingData(CGDataProviderRef dataProvider, NSString *password)
  74. {
  75. CGPDFDocumentRef thePDFDocRef = NULL; // CGPDFDocument
  76. if (dataProvider != NULL) // Check for non-NULL CGDataProviderRef
  77. {
  78. thePDFDocRef = CGPDFDocumentCreateWithProvider(dataProvider);
  79. if (thePDFDocRef != NULL) // Check for non-NULL CGPDFDocumentRef
  80. {
  81. if (CGPDFDocumentIsEncrypted(thePDFDocRef) == TRUE) // Encrypted
  82. {
  83. // Try a blank password first, per Apple's Quartz PDF example
  84. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, "") == FALSE)
  85. {
  86. // Nope, now let's try the provided password to unlock the PDF
  87. if ((password != nil) && (password.length > 0)) // Not blank?
  88. {
  89. char text[128]; // char array buffer for the string conversion
  90. [password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];
  91. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, text) == FALSE) // Log failure
  92. {
  93. #ifdef DEBUG
  94. NSLog(@"CGPDFDocumentCreateUsingData: Unable to unlock data with '%@'", password);
  95. #endif
  96. }
  97. }
  98. }
  99. if (CGPDFDocumentIsUnlocked(thePDFDocRef) == FALSE) // Cleanup unlock failure
  100. {
  101. (void)(CGPDFDocumentRelease(thePDFDocRef)), thePDFDocRef = NULL;
  102. }
  103. }
  104. }
  105. }
  106. else // Log an error diagnostic
  107. {
  108. #ifdef DEBUG
  109. NSLog(@"CGPDFDocumentCreateUsingData: theURL == NULL");
  110. #endif
  111. }
  112. return thePDFDocRef;
  113. }
  114. //
  115. // BOOL CGPDFDocumentUrlNeedsPassword(CFURLRef, NSString *) function
  116. //
  117. BOOL CGPDFDocumentUrlNeedsPassword(CFURLRef theURL, NSString *password)
  118. {
  119. BOOL needPassword = NO; // Default flag
  120. if (theURL != NULL) // Check for non-NULL CFURLRef
  121. {
  122. CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateWithURL(theURL);
  123. if (thePDFDocRef != NULL) // Check for non-NULL CGPDFDocumentRef
  124. {
  125. if (CGPDFDocumentIsEncrypted(thePDFDocRef) == TRUE) // Encrypted
  126. {
  127. // Try a blank password first, per Apple's Quartz PDF example
  128. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, "") == FALSE)
  129. {
  130. // Nope, now let's try the provided password to unlock the PDF
  131. if ((password != nil) && (password.length > 0)) // Not blank?
  132. {
  133. char text[128]; // char array buffer for the string conversion
  134. [password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];
  135. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, text) == FALSE)
  136. {
  137. needPassword = YES;
  138. }
  139. }
  140. else
  141. {
  142. needPassword = YES;
  143. }
  144. }
  145. }
  146. CGPDFDocumentRelease(thePDFDocRef); // Cleanup CGPDFDocumentRef
  147. }
  148. }
  149. else // Log an error diagnostic
  150. {
  151. #ifdef DEBUG
  152. NSLog(@"CGPDFDocumentUrlNeedsPassword: theURL == NULL");
  153. #endif
  154. }
  155. return needPassword;
  156. }
  157. //
  158. // BOOL CGPDFDocumentUrlNeedsPassword(CGDataProviderRef, NSString *) function
  159. //
  160. BOOL CGPDFDocumentDataNeedsPassword(CGDataProviderRef dataProvider, NSString *password)
  161. {
  162. BOOL needPassword = NO; // Default flag
  163. if (dataProvider != NULL) // Check for non-NULL CGDataProviderRef
  164. {
  165. CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateWithProvider(dataProvider);
  166. if (thePDFDocRef != NULL) // Check for non-NULL CGPDFDocumentRef
  167. {
  168. if (CGPDFDocumentIsEncrypted(thePDFDocRef) == TRUE) // Encrypted
  169. {
  170. // Try a blank password first, per Apple's Quartz PDF example
  171. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, "") == FALSE)
  172. {
  173. // Nope, now let's try the provided password to unlock the PDF
  174. if ((password != nil) && (password.length > 0)) // Not blank?
  175. {
  176. char text[128]; // char array buffer for the string conversion
  177. [password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];
  178. if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, text) == FALSE)
  179. {
  180. needPassword = YES;
  181. }
  182. }
  183. else
  184. {
  185. needPassword = YES;
  186. }
  187. }
  188. }
  189. CGPDFDocumentRelease(thePDFDocRef); // Cleanup CGPDFDocumentRef
  190. }
  191. }
  192. else // Log an error diagnostic
  193. {
  194. #ifdef DEBUG
  195. NSLog(@"CGPDFDocumentUrlNeedsPassword: theURL == NULL");
  196. #endif
  197. }
  198. return needPassword;
  199. }
  200. // EOF