Skip to content

Commit

Permalink
Change PDFPassword' s constractor and add function verify
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Jul 10, 2016
1 parent 0aa514a commit 42465bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
2 changes: 2 additions & 0 deletions PDFGenerator/PDFGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,14 @@ private extension PDFGenerator {
}

class func outputToFile(outputPath: String, password: PDFPassword, process: Process) rethrows {
try { try password.verify() }()
UIGraphicsBeginPDFContextToFile(outputPath, .zero, password.toDocumentInfo())
defer { UIGraphicsEndPDFContext() }
try process()
}

class func outputToData(password password: PDFPassword, process: Process) rethrows -> NSData {
try { try password.verify() }()
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, .zero, password.toDocumentInfo())
defer { UIGraphicsEndPDFContext() }
Expand Down
58 changes: 24 additions & 34 deletions PDFGenerator/PDFPassword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,53 @@ public struct PDFPassword {
let userPassword: String
let ownerPassword: String

public init(user userPassword: String, owner ownerPassword: String) throws {
guard userPassword.canBeConvertedToEncoding(NSASCIIStringEncoding) else {
throw PDFGenerateError.InvalidPassword(userPassword)
}
guard userPassword.characters.count <= self.dynamicType.PasswordLengthMax else {
throw PDFGenerateError.TooLongPassword(userPassword.characters.count)
}

guard ownerPassword.canBeConvertedToEncoding(NSASCIIStringEncoding) else {
throw PDFGenerateError.InvalidPassword(ownerPassword)
}
guard ownerPassword.characters.count <= self.dynamicType.PasswordLengthMax else {
throw PDFGenerateError.TooLongPassword(ownerPassword.characters.count)
}

public init(user userPassword: String, owner ownerPassword: String) {
self.userPassword = userPassword
self.ownerPassword = ownerPassword
}

public init(_ password: String) throws {
try self.init(user: password, owner: password)
public init(_ password: String) {
self.init(user: password, owner: password)
}

func toDocumentInfo() -> [String: AnyObject] {
var info: [String: AnyObject] = [:]
if userPassword.characters.count > 0 {
if userPassword != self.dynamicType.NoPassword {
info[String(kCGPDFContextUserPassword)] = userPassword
}
if ownerPassword.characters.count > 0 {
if ownerPassword != self.dynamicType.NoPassword {
info[String(kCGPDFContextOwnerPassword)] = ownerPassword
}
return info
}

func verify() throws {
guard userPassword.canBeConvertedToEncoding(NSASCIIStringEncoding) else {
throw PDFGenerateError.InvalidPassword(userPassword)
}
guard userPassword.characters.count <= self.dynamicType.PasswordLengthMax else {
throw PDFGenerateError.TooLongPassword(userPassword.characters.count)
}

guard ownerPassword.canBeConvertedToEncoding(NSASCIIStringEncoding) else {
throw PDFGenerateError.InvalidPassword(ownerPassword)
}
guard ownerPassword.characters.count <= self.dynamicType.PasswordLengthMax else {
throw PDFGenerateError.TooLongPassword(ownerPassword.characters.count)
}
}
}

extension PDFPassword: StringLiteralConvertible {
public init(unicodeScalarLiteral value: String) {
do {
try self.init(value)
} catch {
try! self.init(self.dynamicType.NoPassword)
}
self.init(value)
}

public init(extendedGraphemeClusterLiteral value: String) {
do {
try self.init(value)
} catch {
try! self.init(self.dynamicType.NoPassword)
}
self.init(value)
}

public init(stringLiteral value: String) {
do {
try self.init(value)
} catch {
try! self.init(self.dynamicType.NoPassword)
}
self.init(value)
}
}

0 comments on commit 42465bb

Please sign in to comment.