Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #144 from readium/develop
Browse files Browse the repository at this point in the history
1.2.5
  • Loading branch information
aferditamuriqi authored Jan 13, 2020
2 parents 12cba2f + 630d895 commit 081f1fe
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 64 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @aferditamuriqi
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github "readium/r2-shared-swift" == 1.4.2
github "readium/r2-shared-swift" == 1.4.3
github "dexman/Minizip" == 1.4.0
github "cezheng/Fuzi" == 3.1.1
github "krzyzanowskim/CryptoSwift" == 1.2.0
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ github "cezheng/Fuzi" "3.1.1"
github "dexman/Minizip" "1.4.0"
github "edrlab/GCDWebServer" "3.6.2"
github "krzyzanowskim/CryptoSwift" "1.2.0"
github "readium/r2-shared-swift" "1.4.2"
github "readium/r2-shared-swift" "1.4.3"
4 changes: 2 additions & 2 deletions R2Streamer.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|

s.name = "R2Streamer"
s.version = "1.2.4"
s.version = "1.2.5"
s.license = "BSD 3-Clause License"
s.summary = "R2 Streamer"
s.homepage = "http://readium.github.io"
s.author = { "Aferdita Muriqi" => "[email protected]" }
s.source = { :git => "https://github.com/readium/r2-streamer-swift.git", :tag => "1.2.4" }
s.source = { :git => "https://github.com/readium/r2-streamer-swift.git", :tag => "1.2.5" }
s.exclude_files = ["**/Info*.plist"]
s.requires_arc = true
s.resources = ['r2-streamer-swift/Resources/**']
Expand Down
15 changes: 6 additions & 9 deletions r2-streamer-swift/Fetcher/ContentFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ internal extension ContentFilters {
func apply(to input: Data, of publication: Publication, with container: Container, at path: String) throws -> Data {
let inputStream = DataInputStream(data: input)
let decodedInputStream = try apply(to: inputStream, of: publication, with: container, at: path)
guard let decodedDataStream = decodedInputStream as? DataInputStream else {
return Data()
}
return decodedDataStream.data
return try Data.reading(decodedInputStream)
}

}
Expand Down Expand Up @@ -126,13 +123,13 @@ final internal class ContentFiltersEpub: ContentFilters {

// Inserting at the start of <HEAD>.
guard let headStart = resourceHtml.endIndex(of: "<head>") else {
log(.error, "Invalid resource")
abort()
log(.error, "Invalid HTML resource: missing <head>")
return stream
}

guard let baseUrl = publication.baseURL?.deletingLastPathComponent() else {
log(.error, "Invalid host")
abort()
return stream
}


Expand All @@ -146,8 +143,8 @@ final internal class ContentFiltersEpub: ContentFilters {

// Inserting at the end of <HEAD>.
guard let headEnd = resourceHtml.startIndex(of: "</head>") else {
log(.error, "Invalid resource")
abort()
log(.error, "Invalid HTML resource: missing </head>")
return stream
}
let fontStyle = getHtmlFontStyle(forResource: "\(baseUrl)fonts/OpenDyslexic-Regular.otf", fontFamily: "OpenDyslexic")
resourceHtml = resourceHtml.insert(string: fontStyle, at: headEnd)
Expand Down
20 changes: 4 additions & 16 deletions r2-streamer-swift/Fetcher/DRM/CBCDRMInputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ final class CBCDRMInputStream: DRMInputStream {
enum Error: Swift.Error {
case invalidStream
case emptyDecryptedData
case readOutOfRange
case readEncryptedOutOfRange
case readFailed
case decryptionFailed
}
Expand Down Expand Up @@ -63,17 +61,12 @@ final class CBCDRMInputStream: DRMInputStream {
}

override func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
guard hasBytesAvailable else {
return 0
}

let len = Int64(len)
let offset = Int64(self.offset)
guard offset + len <= length else {
fail(with: Error.readOutOfRange)
return -1
let len = min(Int64(len), Int64(length) - offset)
guard hasBytesAvailable, len > 0 else {
return 0 // EOF
}

// Get offset result offset in the block.
let blockOffset = offset % AESBlockSize
// For beginning of the cipher text, IV used for XOR.
Expand Down Expand Up @@ -101,11 +94,6 @@ final class CBCDRMInputStream: DRMInputStream {
do {
let bufferSize = blocksCount * AESBlockSize
var buffer = Array<UInt8>(repeating: 0, count: Int(bufferSize))
guard readPosition + bufferSize <= stream.length else {
fail(with: Error.readEncryptedOutOfRange)
return -1
}

stream.open()
try stream.seek(offset: Int64(readPosition), whence: .startOfFile)
let numberOfBytesRead = stream.read(&buffer, maxLength: Int(bufferSize))
Expand Down
10 changes: 3 additions & 7 deletions r2-streamer-swift/Fetcher/DRM/DRMInputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ class DRMInputStream: SeekableInputStream, Loggable {
let length = Int64(self.length)
switch whence {
case .startOfFile:
assert(0...length ~= offset)
_offset = UInt64(offset)
_offset = UInt64(min(offset, length))
case .endOfFile:
assert(-length...0 ~= offset)
_offset = UInt64(length + offset)
_offset = UInt64(min(length + offset, length))
case .currentPosition:
let newOffset = Int64(_offset) + offset
assert(0...length ~= newOffset)
_offset = UInt64(offset)
_offset = min(_offset + UInt64(offset), UInt64(length))
}
}

Expand Down
18 changes: 6 additions & 12 deletions r2-streamer-swift/Fetcher/DRM/FullDRMInputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ final class FullDRMInputStream: DRMInputStream {

enum Error: Swift.Error {
case emptyDecryptedData
case readOutOfRange
case readFailed
case decryptionFailed
case inflateFailed
Expand Down Expand Up @@ -82,29 +81,24 @@ final class FullDRMInputStream: DRMInputStream {
}

override func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
guard hasBytesAvailable else {
return 0
}
guard offset + UInt64(len) <= length else {
fail(with: Error.readOutOfRange)
log(.error, "\(link.href): Decryption read out of range")
return -1
let len = min(len, Int(length - offset))
guard hasBytesAvailable, len > 0 else {
return 0 // EOF
}
guard let data = data else {
return -1
}

let readSize = (len > Int(length - offset) ? Int(length - offset) : len)
let start = data.index(0, offsetBy: Int(offset))
let end = data.index(start, offsetBy: readSize)
let end = data.index(start, offsetBy: len)
let range = Range(uncheckedBounds: (start, end))

data.copyBytes(to: buffer, from: range)
_offset += UInt64(readSize)
_offset += UInt64(len)
if _offset >= length {
_streamStatus = .atEnd
}
return readSize
return len
}

}
7 changes: 3 additions & 4 deletions r2-streamer-swift/Parser/EPUB/EPUBParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ final public class EpubParser: PublicationParser {
for mediaOverlayLink in mediaOverlays {
let node = MediaOverlayNode()

guard let smilDataOptional = try? fetcher.data(forLink: mediaOverlayLink),
// let smilData = smilDataOptional,
let smilXml = try? XMLDocument(data: smilDataOptional) else
guard let smilData = try? fetcher.data(forLink: mediaOverlayLink),
let smilXml = try? XMLDocument(data: smilData) else
{
throw OPFParserError.invalidSmilResource
}
Expand Down Expand Up @@ -326,7 +325,7 @@ final public class EpubParser: PublicationParser {
?? Int((try? container.dataLength(relativePath: link.href)) ?? 0)

// Arbitrary byte length of a single page in a resource.
let pageLength = 3500
let pageLength = 1024
let pageCount = max(1, Int(ceil((Double(length) / Double(pageLength)))))

let positionList = (1...pageCount).map { position in
Expand Down
2 changes: 1 addition & 1 deletion r2-streamer-swift/Parser/PDF/PDFFileCGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ final class PDFFileCGParser: PDFFileParser, Loggable {
}

let current = stream.offset
// SeekWhence.currentPosition is not supported at this time
do {
// SeekWhence.currentPosition is not supported at this time
try stream.seek(offset: Int64(current) + count, whence: .startOfFile)
} catch {
PDFParser.log(.error, error)
Expand Down
7 changes: 4 additions & 3 deletions r2-streamer-swift/Parser/PDF/PDFParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ public final class PDFParser: PublicationParser, Loggable {

// Calculates the page count of each resource from the reading order.
let resources = publication.readingOrder.map { link -> (Int, Link) in
guard let optionalData = try? fetcher.data(forLink: link),
// let data = optionalData,
let parser = try? parserType.init(stream: DataInputStream(data: optionalData)),
guard let stream = try? fetcher.dataStream(forLink: link),
// FIXME: We should be able to use the stream directly here instead of reading it fully into a Data object, but somehow it fails with random access in CBCDRMInputStream.
let data = try? Data.reading(stream),
let parser = try? parserType.init(stream: DataInputStream(data: data)),
let pageCount = try? parser.parseNumberOfPages() else
{
log(.warning, "Can't get the number of pages from PDF document at \(link)")
Expand Down
32 changes: 24 additions & 8 deletions r2-streamer-swift/Toolkit/DataExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,34 @@
import Foundation

extension Data {
init(reading input: InputStream) {
self.init()
input.open()

static func reading(_ stream: InputStream) throws -> Data {
if let dataStream = stream as? DataInputStream {
return dataStream.data
}

var data = Data()
stream.open()
defer {
stream.close()
}

let bufferSize = 1024
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
while input.hasBytesAvailable {
let read = input.read(buffer, maxLength: bufferSize)
self.append(buffer, count: read)
defer {
buffer.deallocate()
}
while stream.hasBytesAvailable {
let read = stream.read(buffer, maxLength: bufferSize)
if read < 0 {
throw stream.streamError ?? NSError()
} else if read == 0 {
break // EOF
}
data.append(buffer, count: read)
}
buffer.deallocate()

input.close()
return data
}

}

0 comments on commit 081f1fe

Please sign in to comment.