Skip to content

Commit

Permalink
0.8.2 (#103)
Browse files Browse the repository at this point in the history
* Fixed crash when detecting mime type of a very small file

* version bump
  • Loading branch information
makoni authored Oct 3, 2022
1 parent e633281 commit 68abc7e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Sources/Uploadcare/Uploadcare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Uploadcare: NSObject {
/// Library name
private var libraryName = "UploadcareSwift"
/// Library version
private var libraryVersion = "0.8.1"
private var libraryVersion = "0.8.2"

/// Performs network requests
private let requestManager: RequestManager
Expand Down
66 changes: 33 additions & 33 deletions Sources/Uploadcare/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,71 +41,71 @@ func detectMimeType(for data: Data) -> String {
var bytes = [UInt8].init(repeating: 0, count: data.count)
data.copyBytes(to: &bytes, count: data.count)

if bytes[0...2] == [0xFF, 0xD8, 0xFF] {
if data.count > 2, bytes[0...2] == [0xFF, 0xD8, 0xFF] {
return "image/jpeg"
}

if bytes[0...3] == [0x89, 0x50, 0x4E, 0x47] {
if data.count > 3, bytes[0...3] == [0x89, 0x50, 0x4E, 0x47] {
return "image/png"
}

if bytes[0...2] == [0x47, 0x49, 0x46] {
if data.count > 2, bytes[0...2] == [0x47, 0x49, 0x46] {
return "image/gif"
}

if bytes[8...11] == [0x57, 0x45, 0x42, 0x50] {
if data.count > 11, bytes[8...11] == [0x57, 0x45, 0x42, 0x50] {
return "image/webp"
}

if (bytes[0...3] == [0x49, 0x49, 0x2A, 0x00] || bytes[0...3] == [0x4D, 0x4D, 0x00, 0x2A]) && bytes[8...9] == [0x43, 0x52] {
if data.count > 9, (bytes[0...3] == [0x49, 0x49, 0x2A, 0x00] || bytes[0...3] == [0x4D, 0x4D, 0x00, 0x2A]) && bytes[8...9] == [0x43, 0x52] {
return "image/x-canon-cr2"
}

if bytes[0...3] == [0x49, 0x49, 0x2A, 0x00] || bytes[0...3] == [0x4D, 0x4D, 0x00, 0x2A] {
if data.count > 3, bytes[0...3] == [0x49, 0x49, 0x2A, 0x00] || bytes[0...3] == [0x4D, 0x4D, 0x00, 0x2A] {
return "image/tiff"
}

if bytes[0...1] == [0x42, 0x4D] {
if data.count > 1, bytes[0...1] == [0x42, 0x4D] {
return "image/bmp"
}

if bytes[0...3] == [0x50, 0x4B, 0x03, 0x04] && bytes[30...57] == [
if data.count > 57, bytes[0...3] == [0x50, 0x4B, 0x03, 0x04] && bytes[30...57] == [
0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C,
0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, 0x70, 0x75, 0x62,
0x2B, 0x7A, 0x69, 0x70
] {
return "application/epub+zip"
}

if bytes[0...1] == [0x50, 0x4B] && (bytes[2] == 0x3 || bytes[2] == 0x5 || bytes[2] == 0x7) && (bytes[3] == 0x4 || bytes[3] == 0x6 || bytes[3] == 0x8) {
if data.count > 3, bytes[0...1] == [0x50, 0x4B] && (bytes[2] == 0x3 || bytes[2] == 0x5 || bytes[2] == 0x7) && (bytes[3] == 0x4 || bytes[3] == 0x6 || bytes[3] == 0x8) {
return "application/zip"
}

if bytes[257...261] == [0x75, 0x73, 0x74, 0x61, 0x72] {
if data.count > 261, bytes[257...261] == [0x75, 0x73, 0x74, 0x61, 0x72] {
return "application/x-tar"
}

if bytes[0...5] == [0x52, 0x61, 0x72, 0x21, 0x1A, 0x07] && (bytes[6] == 0x0 || bytes[6] == 0x1) {
if data.count > 6, bytes[0...5] == [0x52, 0x61, 0x72, 0x21, 0x1A, 0x07] && (bytes[6] == 0x0 || bytes[6] == 0x1) {
return "application/x-rar-compressed"
}

if bytes[0...2] == [0x1F, 0x8B, 0x08] {
if data.count > 2, bytes[0...2] == [0x1F, 0x8B, 0x08] {
return "application/gzip"
}

if bytes[0...2] == [0x42, 0x5A, 0x68] {
if data.count > 2, bytes[0...2] == [0x42, 0x5A, 0x68] {
return "application/x-bzip2"
}

if bytes[0...5] == [0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C] {
if data.count > 5, bytes[0...5] == [0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C] {
return "application/x-7z-compressed"
}

if bytes[0...1] == [0x78, 0x01] {
if data.count > 1, bytes[0...1] == [0x78, 0x01] {
return "application/x-apple-diskimage"
}

if (bytes[0...2] == [0x00, 0x00, 0x00] && (bytes[3] == 0x18 || bytes[3] == 0x20) && bytes[4...7] == [0x66, 0x74, 0x79, 0x70]) ||
if data.count > 27, (bytes[0...2] == [0x00, 0x00, 0x00] && (bytes[3] == 0x18 || bytes[3] == 0x20) && bytes[4...7] == [0x66, 0x74, 0x79, 0x70]) ||
(bytes[0...3] == [0x33, 0x67, 0x70, 0x35]) ||
(bytes[0...11] == [0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34, 0x32] &&
bytes[16...27] == [0x6D, 0x70, 0x34, 0x31, 0x6D, 0x70, 0x34, 0x32, 0x69, 0x73, 0x6F, 0x6D]) ||
Expand All @@ -114,71 +114,71 @@ func detectMimeType(for data: Data) -> String {
return "video/mp4"
}

if bytes[0...10] == [0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x56] {
if data.count > 10, bytes[0...10] == [0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x56] {
return "video/x-m4v"
}

if bytes[0...7] == [0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70] {
if data.count > 7, bytes[0...7] == [0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70] {
return "video/quicktime"
}

if bytes[0...3] == [0x52, 0x49, 0x46, 0x46] && bytes[8...10] == [0x41, 0x56, 0x49] {
if data.count > 3, bytes[0...3] == [0x52, 0x49, 0x46, 0x46] && bytes[8...10] == [0x41, 0x56, 0x49] {
return "video/x-msvideo"
}

if bytes[0...9] == [0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9] {
if data.count > 9, bytes[0...9] == [0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9] {
return "video/x-ms-wmv"
}

if bytes[0...2] == [0x49, 0x44, 0x33] || bytes[0...1] == [0xFF, 0xFB] {
if data.count > 2, bytes[0...2] == [0x49, 0x44, 0x33] || bytes[0...1] == [0xFF, 0xFB] {
return "audio/mpeg"
}

if bytes[0...3] == [0x4D, 0x34, 0x41, 0x20] || bytes[4...10] == [0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41] {
if data.count > 10, bytes[0...3] == [0x4D, 0x34, 0x41, 0x20] || bytes[4...10] == [0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41] {
return "audio/m4a"
}

if bytes[28...35] == [0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64] {
if data.count > 35, bytes[28...35] == [0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64] {
return "audio/opus"
}

if bytes[0...3] == [0x4F, 0x67, 0x67, 0x53] {
if data.count > 3, bytes[0...3] == [0x4F, 0x67, 0x67, 0x53] {
return "audio/ogg"
}

if bytes[0...3] == [0x66, 0x4C, 0x61, 0x43] {
if data.count > 3, bytes[0...3] == [0x66, 0x4C, 0x61, 0x43] {
return "audio/x-flac"
}

if bytes[0...3] == [0x52, 0x49, 0x46, 0x46] && bytes[8...11] == [0x57, 0x41, 0x56, 0x45] {
if data.count > 3, bytes[0...3] == [0x52, 0x49, 0x46, 0x46] && bytes[8...11] == [0x57, 0x41, 0x56, 0x45] {
return "audio/x-wav"
}

if bytes[0...3] == [0x25, 0x50, 0x44, 0x46] {
if data.count > 3, bytes[0...3] == [0x25, 0x50, 0x44, 0x46] {
return "application/pdf"
}

if bytes[0...1] == [0x4D, 0x5A] {
if data.count > 1, bytes[0...1] == [0x4D, 0x5A] {
return "application/x-msdownload"
}

if bytes[0...4] == [0x7B, 0x5C, 0x72, 0x74, 0x66] {
if data.count > 4, bytes[0...4] == [0x7B, 0x5C, 0x72, 0x74, 0x66] {
return "application/rtf"
}

if bytes[0...4] == [0x00, 0x01, 0x00, 0x00, 0x00] {
if data.count > 4, bytes[0...4] == [0x00, 0x01, 0x00, 0x00, 0x00] {
return "application/font-sfnt"
}

if bytes[0...4] == [0x4F, 0x54, 0x54, 0x4F, 0x00] {
if data.count > 4, bytes[0...4] == [0x4F, 0x54, 0x54, 0x4F, 0x00] {
return "application/font-sfnt"
}

if bytes[0...7] == [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1] {
if data.count > 7, bytes[0...7] == [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1] {
return "application/x-msi"
}

if bytes[0] == 0x46 {
if data.count > 0, bytes[0] == 0x46 {
return "text/plain"
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Uploadcare/requestManager/RequestManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class RequestManager {
/// Library name
private var libraryName = "UploadcareSwift"
/// Library version
private var libraryVersion = "0.8.1"
private var libraryVersion = "0.8.2"
/// API public key
private var publicKey: String
/// Secret Key. Optional. Is used for authorization
Expand Down
2 changes: 1 addition & 1 deletion Uploadcare.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Uploadcare'
s.version = '0.8.1'
s.version = '0.8.2'
s.summary = 'Swift integration for Uploadcare'

# This description is used to generate tags and improve search results.
Expand Down

0 comments on commit 68abc7e

Please sign in to comment.