解决了语音上传到问题,接下来要解决下载播放问题
This commit is contained in:
21
Pods/Qiniu/QiniuSDK/Utils/NSData+QNGZip.h
generated
Normal file
21
Pods/Qiniu/QiniuSDK/Utils/NSData+QNGZip.h
generated
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// NSData+QNGZip.h
|
||||
// GZipTest
|
||||
//
|
||||
// Created by yangsen on 2020/8/12.
|
||||
// Copyright © 2020 yangsen. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSData(QNGZip)
|
||||
|
||||
+ (NSData *)qn_gZip:(NSData *)data;
|
||||
|
||||
+ (NSData *)qn_gUnzip:(NSData *)data;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
95
Pods/Qiniu/QiniuSDK/Utils/NSData+QNGZip.m
generated
Normal file
95
Pods/Qiniu/QiniuSDK/Utils/NSData+QNGZip.m
generated
Normal file
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// NSData+QNGZip.m
|
||||
// GZipTest
|
||||
//
|
||||
// Created by yangsen on 2020/8/12.
|
||||
// Copyright © 2020 yangsen. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSData+QNGZip.h"
|
||||
#import <zlib.h>
|
||||
|
||||
#pragma clang diagnostic ignored "-Wcast-qual"
|
||||
|
||||
@implementation NSData(QNGZip)
|
||||
|
||||
+ (NSData *)qn_gZip:(NSData *)data{
|
||||
|
||||
if (data.length == 0 || [self qn_isGzippedData:data]){
|
||||
return data;
|
||||
}
|
||||
|
||||
z_stream stream;
|
||||
stream.opaque = Z_NULL;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.total_out = 0;
|
||||
stream.avail_out = 0;
|
||||
stream.avail_in = (uint)data.length;
|
||||
stream.next_in = (Bytef *)(void *)data.bytes;
|
||||
|
||||
static const NSUInteger chunkSize = 16384;
|
||||
|
||||
NSMutableData *gzippedData = nil;
|
||||
|
||||
if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK) {
|
||||
gzippedData = [NSMutableData dataWithLength:chunkSize];
|
||||
while (stream.avail_out == 0) {
|
||||
if (stream.total_out >= gzippedData.length) {
|
||||
gzippedData.length += chunkSize;
|
||||
}
|
||||
stream.next_out = (uint8_t *)gzippedData.mutableBytes + stream.total_out;
|
||||
stream.avail_out = (uInt)(gzippedData.length - stream.total_out);
|
||||
deflate(&stream, Z_FINISH);
|
||||
}
|
||||
deflateEnd(&stream);
|
||||
gzippedData.length = stream.total_out;
|
||||
}
|
||||
|
||||
return gzippedData;
|
||||
}
|
||||
|
||||
+ (NSData *)qn_gUnzip:(NSData *)data{
|
||||
if (data.length == 0 || ![self qn_isGzippedData:data]){
|
||||
return data;
|
||||
}
|
||||
|
||||
z_stream stream;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.total_out = 0;
|
||||
stream.avail_out = 0;
|
||||
stream.avail_in = (uint)data.length;
|
||||
stream.next_in = (Bytef *)data.bytes;
|
||||
|
||||
NSMutableData *gunzippedData = nil;
|
||||
if (inflateInit2(&stream, 47) == Z_OK) {
|
||||
int status = Z_OK;
|
||||
gunzippedData = [NSMutableData dataWithCapacity:data.length * 2];
|
||||
while (status == Z_OK) {
|
||||
if (stream.total_out >= gunzippedData.length) {
|
||||
gunzippedData.length += data.length / 2;
|
||||
}
|
||||
stream.next_out = (uint8_t *)gunzippedData.mutableBytes + stream.total_out;
|
||||
stream.avail_out = (uInt)(gunzippedData.length - stream.total_out);
|
||||
status = inflate (&stream, Z_SYNC_FLUSH);
|
||||
}
|
||||
if (inflateEnd(&stream) == Z_OK) {
|
||||
if (status == Z_STREAM_END) {
|
||||
gunzippedData.length = stream.total_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gunzippedData;
|
||||
}
|
||||
|
||||
+ (BOOL)qn_isGzippedData:(NSData *)data{
|
||||
if (!data || data.length == 0) {
|
||||
return false;
|
||||
}
|
||||
const UInt8 *bytes = (const UInt8 *)data.bytes;
|
||||
return (data.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
|
||||
}
|
||||
|
||||
@end
|
||||
19
Pods/Qiniu/QiniuSDK/Utils/NSData+QNMD5.h
generated
Normal file
19
Pods/Qiniu/QiniuSDK/Utils/NSData+QNMD5.h
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// NSData+MD5.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by 杨森 on 2020/7/28.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSData(QNMD5)
|
||||
|
||||
- (NSString *)qn_md5;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
28
Pods/Qiniu/QiniuSDK/Utils/NSData+QNMD5.m
generated
Normal file
28
Pods/Qiniu/QiniuSDK/Utils/NSData+QNMD5.m
generated
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// NSData+MD5.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by 杨森 on 2020/7/28.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSData+QNMD5.h"
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
|
||||
@implementation NSData(QNMD5)
|
||||
|
||||
- (NSString *)qn_md5{
|
||||
|
||||
CC_MD5_CTX md5;
|
||||
CC_MD5_Init(&md5);
|
||||
CC_MD5_Update(&md5, self.bytes, (CC_LONG)self.length);
|
||||
unsigned char digest[CC_MD5_DIGEST_LENGTH];
|
||||
CC_MD5_Final(digest, &md5);
|
||||
NSMutableString *result = [NSMutableString string];
|
||||
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
|
||||
[result appendFormat:@"%02X", digest[i]];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
||||
28
Pods/Qiniu/QiniuSDK/Utils/NSObject+QNSwizzle.h
generated
Normal file
28
Pods/Qiniu/QiniuSDK/Utils/NSObject+QNSwizzle.h
generated
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// NSObject+QNSwizzle.h
|
||||
// HappyDNS
|
||||
//
|
||||
// Created by yangsen on 2020/4/13.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSObject(QNSwizzle)
|
||||
|
||||
/// swizzle 两个对象方法
|
||||
/// @param selectorA 方法a的sel
|
||||
/// @param selectorB 方法b的sel
|
||||
+ (BOOL)qn_swizzleInstanceMethodsOfSelectorA:(SEL)selectorA
|
||||
selectorB:(SEL)selectorB;
|
||||
|
||||
/// swizzle 两个类方法
|
||||
/// @param selectorA 方法a的sel
|
||||
/// @param selectorB 方法b的sel
|
||||
+ (BOOL)qn_swizzleClassMethodsOfSelectorA:(SEL)selectorA
|
||||
selectorB:(SEL)selectorB;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
63
Pods/Qiniu/QiniuSDK/Utils/NSObject+QNSwizzle.m
generated
Normal file
63
Pods/Qiniu/QiniuSDK/Utils/NSObject+QNSwizzle.m
generated
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// NSObject+QNSwizzle.m
|
||||
// HappyDNS
|
||||
//
|
||||
// Created by yangsen on 2020/4/13.
|
||||
//
|
||||
|
||||
#import "NSObject+QNSwizzle.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@implementation NSObject(QNSwizzle)
|
||||
|
||||
+ (BOOL)qn_swizzleInstanceMethodsOfSelectorA:(SEL)selectorA
|
||||
selectorB:(SEL)selectorB{
|
||||
|
||||
Method methodA = class_getInstanceMethod(self, selectorA);
|
||||
Method methodB = class_getInstanceMethod(self, selectorB);
|
||||
if (!methodA || !methodB) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
class_addMethod(self,
|
||||
selectorA,
|
||||
class_getMethodImplementation(self, selectorA),
|
||||
method_getTypeEncoding(methodA));
|
||||
|
||||
class_addMethod(self,
|
||||
selectorB,
|
||||
class_getMethodImplementation(self, selectorB),
|
||||
method_getTypeEncoding(methodB));
|
||||
|
||||
method_exchangeImplementations(class_getInstanceMethod(self, selectorA),
|
||||
class_getInstanceMethod(self, selectorB));
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (BOOL)qn_swizzleClassMethodsOfSelectorA:(SEL)selectorA
|
||||
selectorB:(SEL)selectorB{
|
||||
|
||||
Method methodA = class_getInstanceMethod(object_getClass(self), selectorA);
|
||||
Method methodB = class_getInstanceMethod(object_getClass(self), selectorB);
|
||||
if (!methodA || !methodB) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
class_addMethod(self,
|
||||
selectorA,
|
||||
class_getMethodImplementation(self, selectorA),
|
||||
method_getTypeEncoding(methodA));
|
||||
|
||||
class_addMethod(self,
|
||||
selectorB,
|
||||
class_getMethodImplementation(self, selectorB),
|
||||
method_getTypeEncoding(methodB));
|
||||
|
||||
method_exchangeImplementations(class_getInstanceMethod(self, selectorA),
|
||||
class_getInstanceMethod(self, selectorB));
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
30
Pods/Qiniu/QiniuSDK/Utils/QNALAssetFile.h
generated
Executable file
30
Pods/Qiniu/QiniuSDK/Utils/QNALAssetFile.h
generated
Executable file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// QNALAssetFile.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/7/25.
|
||||
// Copyright (c) 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "QNFileDelegate.h"
|
||||
|
||||
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED)
|
||||
#if !TARGET_OS_MACCATALYST
|
||||
@class ALAsset;
|
||||
@interface QNALAssetFile : NSObject <QNFileDelegate>;
|
||||
|
||||
/**
|
||||
* 打开指定文件
|
||||
*
|
||||
* @param asset 资源文件
|
||||
* @param error 输出的错误信息
|
||||
*
|
||||
* @return 实例
|
||||
*/
|
||||
- (instancetype)init:(ALAsset *)asset
|
||||
error:(NSError *__autoreleasing *)error NS_DEPRECATED_IOS(4_0, 9_0);
|
||||
@end
|
||||
#endif
|
||||
#endif
|
||||
91
Pods/Qiniu/QiniuSDK/Utils/QNALAssetFile.m
generated
Executable file
91
Pods/Qiniu/QiniuSDK/Utils/QNALAssetFile.m
generated
Executable file
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// QNALAssetFile.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/7/25.
|
||||
// Copyright (c) 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNALAssetFile.h"
|
||||
|
||||
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
|
||||
#if !TARGET_OS_MACCATALYST
|
||||
#import <AssetsLibrary/AssetsLibrary.h>
|
||||
#import "QNResponseInfo.h"
|
||||
|
||||
@interface QNALAssetFile ()
|
||||
|
||||
@property (nonatomic) ALAsset *asset NS_DEPRECATED_IOS(4_0, 9_0);
|
||||
|
||||
@property (readonly) int64_t fileSize;
|
||||
|
||||
@property (readonly) int64_t fileModifyTime;
|
||||
|
||||
@property (nonatomic, strong) NSLock *lock;
|
||||
|
||||
@end
|
||||
|
||||
@implementation QNALAssetFile
|
||||
- (instancetype)init:(ALAsset *)asset
|
||||
error:(NSError *__autoreleasing *)error {
|
||||
if (self = [super init]) {
|
||||
NSDate *createTime = [asset valueForProperty:ALAssetPropertyDate];
|
||||
int64_t t = 0;
|
||||
if (createTime != nil) {
|
||||
t = [createTime timeIntervalSince1970];
|
||||
}
|
||||
_fileModifyTime = t;
|
||||
_fileSize = asset.defaultRepresentation.size;
|
||||
_asset = asset;
|
||||
_lock = [[NSLock alloc] init];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSData *)read:(long long)offset
|
||||
size:(long)size
|
||||
error:(NSError **)error NS_DEPRECATED_IOS(4_0, 9_0) {
|
||||
|
||||
NSData *data = nil;
|
||||
@try {
|
||||
[_lock lock];
|
||||
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
|
||||
Byte *buffer = (Byte *)malloc(size);
|
||||
NSUInteger buffered = [rep getBytes:buffer fromOffset:offset length:size error:error];
|
||||
data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
|
||||
} @catch (NSException *exception) {
|
||||
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
|
||||
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
|
||||
} @finally {
|
||||
[_lock unlock];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
- (NSData *)readAllWithError:(NSError **)error {
|
||||
return [self read:0 size:(long)_fileSize error:error];
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
}
|
||||
|
||||
- (NSString *)path NS_DEPRECATED_IOS(4_0, 9_0) {
|
||||
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
|
||||
return [rep url].path;
|
||||
}
|
||||
|
||||
- (int64_t)modifyTime {
|
||||
return _fileModifyTime;
|
||||
}
|
||||
|
||||
- (int64_t)size {
|
||||
return _fileSize;
|
||||
}
|
||||
|
||||
- (NSString *)fileType {
|
||||
return @"ALAsset";
|
||||
}
|
||||
@end
|
||||
#endif
|
||||
#endif
|
||||
20
Pods/Qiniu/QiniuSDK/Utils/QNAsyncRun.h
generated
Executable file
20
Pods/Qiniu/QiniuSDK/Utils/QNAsyncRun.h
generated
Executable file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// QNAsyncRun.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14/10/17.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#define kQNBackgroundQueue dispatch_get_global_queue(0, 0)
|
||||
#define kQNMainQueue dispatch_get_main_queue()
|
||||
|
||||
typedef void (^QNRun)(void);
|
||||
|
||||
void QNAsyncRun(QNRun run);
|
||||
|
||||
void QNAsyncRunInMain(QNRun run);
|
||||
|
||||
void QNAsyncRunAfter(NSTimeInterval time, dispatch_queue_t queue, QNRun run);
|
||||
28
Pods/Qiniu/QiniuSDK/Utils/QNAsyncRun.m
generated
Executable file
28
Pods/Qiniu/QiniuSDK/Utils/QNAsyncRun.m
generated
Executable file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// QNAsyncRun.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14/10/17.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNAsyncRun.h"
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void QNAsyncRun(QNRun run) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
||||
run();
|
||||
});
|
||||
}
|
||||
|
||||
void QNAsyncRunInMain(QNRun run) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
run();
|
||||
});
|
||||
}
|
||||
|
||||
void QNAsyncRunAfter(NSTimeInterval time, dispatch_queue_t queue, QNRun run) {
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), queue, ^{
|
||||
run();
|
||||
});
|
||||
}
|
||||
48
Pods/Qiniu/QiniuSDK/Utils/QNCache.h
generated
Normal file
48
Pods/Qiniu/QiniuSDK/Utils/QNCache.h
generated
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// QNCache.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2023/9/20.
|
||||
// Copyright © 2023 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol QNCacheObject <NSObject>
|
||||
|
||||
- (nullable NSDictionary *)toDictionary;
|
||||
|
||||
- (nonnull id <QNCacheObject>)initWithDictionary:(nullable NSDictionary *)dictionary;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@interface QNCacheOption : NSObject
|
||||
|
||||
// 当 cache 修改数量到达这个值时,就会 flush,默认是 1
|
||||
@property (nonatomic, assign) int flushCount;
|
||||
// 缓存被持久化为一个文件,此文件的文件名为 version,version 默认为:v1.0.0
|
||||
@property (nonatomic, copy) NSString *version;
|
||||
|
||||
@end
|
||||
|
||||
@interface QNCache : NSObject
|
||||
|
||||
+ (instancetype)cache:(Class)objectClass option:(QNCacheOption *)option;
|
||||
|
||||
- (id <QNCacheObject>)cacheForKey:(NSString *)cacheKey;
|
||||
- (void)cache:(id<QNCacheObject>)object forKey:(NSString *)cacheKey atomically:(BOOL)atomically;
|
||||
|
||||
- (NSDictionary <NSString *, id <QNCacheObject>> *)allMemoryCache;
|
||||
|
||||
- (void)flush:(BOOL)atomically;
|
||||
|
||||
- (void)clearMemoryCache;
|
||||
|
||||
- (void)clearDiskCache;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
193
Pods/Qiniu/QiniuSDK/Utils/QNCache.m
generated
Normal file
193
Pods/Qiniu/QiniuSDK/Utils/QNCache.m
generated
Normal file
@@ -0,0 +1,193 @@
|
||||
//
|
||||
// QNCache.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2023/9/20.
|
||||
// Copyright © 2023 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNCache.h"
|
||||
#import "QNAsyncRun.h"
|
||||
#import "QNUtils.h"
|
||||
#import "QNFileRecorder.h"
|
||||
|
||||
@implementation QNCacheOption
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
self.flushCount = 1;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)version {
|
||||
if (_version == nil || _version.length == 0) {
|
||||
_version = @"v1.0.0";
|
||||
}
|
||||
return _version;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface QNCache()
|
||||
|
||||
@property (nonatomic, strong) Class objectClass;
|
||||
@property (nonatomic, strong) QNCacheOption *option;
|
||||
@property (nonatomic, strong) QNFileRecorder *diskCache;
|
||||
@property (nonatomic, assign) BOOL isFlushing;
|
||||
@property (nonatomic, assign) int needFlushCount;
|
||||
@property (nonatomic, strong) NSMutableDictionary<NSString *, id <QNCacheObject>> *memCache;
|
||||
|
||||
@end
|
||||
@implementation QNCache
|
||||
|
||||
+ (instancetype)cache:(Class)objectClass option:(QNCacheOption *)option {
|
||||
QNCache *cache = [[QNCache alloc] init];
|
||||
cache.objectClass = objectClass;
|
||||
cache.option = option ? option : [[QNCacheOption alloc] init];
|
||||
cache.isFlushing = false;
|
||||
cache.needFlushCount = 0;
|
||||
cache.memCache = [[NSMutableDictionary alloc] init];
|
||||
NSString *path = [[QNUtils sdkCacheDirectory] stringByAppendingFormat:@"/%@", objectClass];
|
||||
cache.diskCache = [QNFileRecorder fileRecorderWithFolder:path error:nil];
|
||||
[cache load];
|
||||
return cache;
|
||||
}
|
||||
|
||||
- (void)load {
|
||||
if (![self.objectClass conformsToProtocol:@protocol(QNCacheObject)]) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSData *data = nil;
|
||||
@synchronized (self) {
|
||||
data = [self.diskCache get:self.option.version];
|
||||
}
|
||||
if (data == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSError *error = nil;
|
||||
NSDictionary *cacheDict = [NSJSONSerialization JSONObjectWithData:data
|
||||
options:NSJSONReadingMutableLeaves
|
||||
error:&error];
|
||||
if (error != nil || cacheDict == nil || cacheDict.count == 0) {
|
||||
[self.diskCache del:self.option.version];
|
||||
return;
|
||||
}
|
||||
|
||||
NSMutableDictionary<NSString *, id<QNCacheObject>> *cache = [NSMutableDictionary dictionary];
|
||||
for (NSString *key in cacheDict.allKeys) {
|
||||
NSDictionary *objectDict = cacheDict[key];
|
||||
if (!objectDict || objectDict.count == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
id<QNCacheObject> object = [[self.objectClass alloc] initWithDictionary:objectDict];
|
||||
if (object != nil) {
|
||||
cache[key] = object;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cache || cache.count == 0) {
|
||||
[self.diskCache del:self.option.version];
|
||||
return;
|
||||
}
|
||||
|
||||
@synchronized (self) {
|
||||
self.memCache = cache;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSDictionary<NSString *,id<QNCacheObject>> *)allMemoryCache {
|
||||
@synchronized (self) {
|
||||
return [self.memCache copy];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)cache:(id<QNCacheObject>)object forKey:(NSString *)cacheKey atomically:(BOOL)atomically {
|
||||
if (!cacheKey || [cacheKey isEqualToString:@""] || object == nil ||
|
||||
![object isKindOfClass:self.objectClass]) {
|
||||
return;
|
||||
}
|
||||
|
||||
@synchronized (self) {
|
||||
self.needFlushCount ++;
|
||||
self.memCache[cacheKey] = object;
|
||||
}
|
||||
|
||||
if (self.needFlushCount >= self.option.flushCount) {
|
||||
[self flush:atomically];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)flush:(BOOL)atomically {
|
||||
@synchronized (self) {
|
||||
if (self.isFlushing) {
|
||||
return;
|
||||
}
|
||||
self.needFlushCount = 0;
|
||||
self.isFlushing = true;
|
||||
}
|
||||
|
||||
NSDictionary<NSString *, id <QNCacheObject>> *flushCache = nil;
|
||||
@synchronized (self) {
|
||||
if (self.memCache == nil || self.memCache.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
flushCache = [self.memCache copy];
|
||||
}
|
||||
|
||||
if (atomically) {
|
||||
[self flushCache:flushCache];
|
||||
} else {
|
||||
QNAsyncRun(^{
|
||||
[self flushCache:flushCache];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
- (void)flushCache:(NSDictionary <NSString *, id <QNCacheObject>> *)flushCache {
|
||||
if (flushCache == nil || flushCache.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSMutableDictionary *flushDict = [NSMutableDictionary dictionary];
|
||||
for (NSString *key in flushCache.allKeys) {
|
||||
id <QNCacheObject> object = flushCache[key];
|
||||
if (![object respondsToSelector:@selector(toDictionary)]) {
|
||||
continue;
|
||||
}
|
||||
flushDict[key] = [object toDictionary];
|
||||
}
|
||||
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:flushDict options:NSJSONWritingPrettyPrinted error:nil];
|
||||
if (!data || data.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self.diskCache set:self.option.version data:data];
|
||||
|
||||
@synchronized (self) {
|
||||
self.isFlushing = false;
|
||||
}
|
||||
}
|
||||
|
||||
- (id <QNCacheObject>)cacheForKey:(NSString *)cacheKey {
|
||||
@synchronized (self) {
|
||||
return [self.memCache valueForKey:cacheKey];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)clearMemoryCache {
|
||||
@synchronized (self) {
|
||||
self.memCache = [NSMutableDictionary dictionary];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)clearDiskCache {
|
||||
@synchronized (self) {
|
||||
[self.diskCache deleteAll];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
36
Pods/Qiniu/QiniuSDK/Utils/QNCrc32.h
generated
Executable file
36
Pods/Qiniu/QiniuSDK/Utils/QNCrc32.h
generated
Executable file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// QNCrc.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14-9-29.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* 生成crc32 校验码
|
||||
*/
|
||||
@interface QNCrc32 : NSObject
|
||||
|
||||
/**
|
||||
* 文件校验
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param error 文件读取错误
|
||||
*
|
||||
* @return 校验码
|
||||
*/
|
||||
+ (UInt32)file:(NSString *)filePath
|
||||
error:(NSError **)error;
|
||||
|
||||
/**
|
||||
* 二进制字节校验
|
||||
*
|
||||
* @param data 二进制数据
|
||||
*
|
||||
* @return 校验码
|
||||
*/
|
||||
+ (UInt32)data:(NSData *)data;
|
||||
|
||||
@end
|
||||
45
Pods/Qiniu/QiniuSDK/Utils/QNCrc32.m
generated
Executable file
45
Pods/Qiniu/QiniuSDK/Utils/QNCrc32.m
generated
Executable file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// QNCrc.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14-9-29.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <zlib.h>
|
||||
|
||||
#import "QNConfiguration.h"
|
||||
#import "QNCrc32.h"
|
||||
|
||||
@implementation QNCrc32
|
||||
|
||||
+ (UInt32)data:(NSData *)data {
|
||||
uLong crc = crc32(0L, Z_NULL, 0);
|
||||
|
||||
crc = crc32(crc, [data bytes], (uInt)[data length]);
|
||||
return (UInt32)crc;
|
||||
}
|
||||
|
||||
+ (UInt32)file:(NSString *)filePath
|
||||
error:(NSError **)error {
|
||||
@autoreleasepool {
|
||||
NSData *data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:error];
|
||||
if (*error != nil) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int len = (int)[data length];
|
||||
int count = (len + kQNBlockSize - 1) / kQNBlockSize;
|
||||
|
||||
uLong crc = crc32(0L, Z_NULL, 0);
|
||||
for (int i = 0; i < count; i++) {
|
||||
int offset = i * kQNBlockSize;
|
||||
int size = (len - offset) > kQNBlockSize ? kQNBlockSize : (len - offset);
|
||||
NSData *d = [data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
|
||||
crc = crc32(crc, [d bytes], (uInt)[d length]);
|
||||
}
|
||||
return (UInt32)crc;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
18
Pods/Qiniu/QiniuSDK/Utils/QNDefine.h
generated
Normal file
18
Pods/Qiniu/QiniuSDK/Utils/QNDefine.h
generated
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// QNDefine.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2020/9/4.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#define kQNWeakSelf __weak typeof(self) weak_self = self
|
||||
#define kQNStrongSelf __strong typeof(self) self = weak_self
|
||||
|
||||
#define kQNWeakObj(object) __weak typeof(object) weak_##object = object
|
||||
#define kQNStrongObj(object) __strong typeof(object) object = weak_##object
|
||||
|
||||
// 过期
|
||||
#define kQNDeprecated(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)
|
||||
11
Pods/Qiniu/QiniuSDK/Utils/QNDefine.m
generated
Normal file
11
Pods/Qiniu/QiniuSDK/Utils/QNDefine.m
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// QNDefine.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2020/9/4.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNDefine.h"
|
||||
|
||||
|
||||
35
Pods/Qiniu/QiniuSDK/Utils/QNEtag.h
generated
Executable file
35
Pods/Qiniu/QiniuSDK/Utils/QNEtag.h
generated
Executable file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// QNEtag.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14/10/4.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* 服务器 hash etag 生成
|
||||
*/
|
||||
@interface QNEtag : NSObject
|
||||
|
||||
/**
|
||||
* 文件etag 【已废除】
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param error 输出文件读取错误
|
||||
*
|
||||
* @return etag
|
||||
*/
|
||||
+ (NSString *)file:(NSString *)filePath
|
||||
error:(NSError **)error;
|
||||
|
||||
/**
|
||||
* 二进制数据etag 【已废除】
|
||||
*
|
||||
* @param data 数据
|
||||
*
|
||||
* @return etag
|
||||
*/
|
||||
+ (NSString *)data:(NSData *)data;
|
||||
@end
|
||||
60
Pods/Qiniu/QiniuSDK/Utils/QNEtag.m
generated
Executable file
60
Pods/Qiniu/QiniuSDK/Utils/QNEtag.m
generated
Executable file
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// QNEtag.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14/10/4.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#include <CommonCrypto/CommonCrypto.h>
|
||||
|
||||
#import "QNConfiguration.h"
|
||||
#import "QNEtag.h"
|
||||
#import "QNUrlSafeBase64.h"
|
||||
|
||||
@implementation QNEtag
|
||||
+ (NSString *)file:(NSString *)filePath
|
||||
error:(NSError **)error {
|
||||
@autoreleasepool {
|
||||
NSData *data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:error];
|
||||
if (error && *error) {
|
||||
return 0;
|
||||
}
|
||||
return [QNEtag data:data];
|
||||
}
|
||||
}
|
||||
|
||||
+ (NSString *)data:(NSData *)data {
|
||||
if (data == nil || [data length] == 0) {
|
||||
return @"Fto5o-5ea0sNMlW_75VgGJCv2AcJ";
|
||||
}
|
||||
int len = (int)[data length];
|
||||
int count = (len + kQNBlockSize - 1) / kQNBlockSize;
|
||||
|
||||
NSMutableData *retData = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH + 1];
|
||||
UInt8 *ret = [retData mutableBytes];
|
||||
|
||||
NSMutableData *blocksSha1 = nil;
|
||||
UInt8 *pblocksSha1 = ret + 1;
|
||||
if (count > 1) {
|
||||
blocksSha1 = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH * count];
|
||||
pblocksSha1 = [blocksSha1 mutableBytes];
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
int offset = i * kQNBlockSize;
|
||||
int size = (len - offset) > kQNBlockSize ? kQNBlockSize : (len - offset);
|
||||
NSData *d = [data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
|
||||
CC_SHA1([d bytes], (CC_LONG)size, pblocksSha1 + i * CC_SHA1_DIGEST_LENGTH);
|
||||
}
|
||||
if (count == 1) {
|
||||
ret[0] = 0x16;
|
||||
} else {
|
||||
ret[0] = 0x96;
|
||||
CC_SHA1(pblocksSha1, (CC_LONG)CC_SHA1_DIGEST_LENGTH * count, ret + 1);
|
||||
}
|
||||
|
||||
return [QNUrlSafeBase64 encodeData:retData];
|
||||
}
|
||||
|
||||
@end
|
||||
24
Pods/Qiniu/QiniuSDK/Utils/QNFile.h
generated
Executable file
24
Pods/Qiniu/QiniuSDK/Utils/QNFile.h
generated
Executable file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// QNFile.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/7/25.
|
||||
// Copyright (c) 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNFileDelegate.h"
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface QNFile : NSObject <QNFileDelegate>
|
||||
/**
|
||||
* 打开指定文件
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param error 输出的错误信息
|
||||
*
|
||||
* @return 实例
|
||||
*/
|
||||
- (instancetype)init:(NSString *)path
|
||||
error:(NSError *__autoreleasing *)error;
|
||||
|
||||
@end
|
||||
128
Pods/Qiniu/QiniuSDK/Utils/QNFile.m
generated
Executable file
128
Pods/Qiniu/QiniuSDK/Utils/QNFile.m
generated
Executable file
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// QNFile.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/7/25.
|
||||
// Copyright (c) 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNFile.h"
|
||||
#import "QNResponseInfo.h"
|
||||
|
||||
@interface QNFile ()
|
||||
|
||||
@property (nonatomic, readonly) NSString *filepath;
|
||||
|
||||
@property (nonatomic) NSData *data;
|
||||
|
||||
@property (readonly) int64_t fileSize;
|
||||
|
||||
@property (readonly) int64_t fileModifyTime;
|
||||
|
||||
@property (nonatomic) NSFileHandle *file;
|
||||
|
||||
@property (nonatomic) NSLock *lock;
|
||||
|
||||
@end
|
||||
|
||||
@implementation QNFile
|
||||
|
||||
- (instancetype)init:(NSString *)path
|
||||
error:(NSError *__autoreleasing *)error {
|
||||
if (self = [super init]) {
|
||||
_filepath = path;
|
||||
NSError *error2 = nil;
|
||||
NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error2];
|
||||
if (error2 != nil) {
|
||||
if (error != nil) {
|
||||
*error = error2;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
_fileSize = [fileAttr fileSize];
|
||||
NSDate *modifyTime = fileAttr[NSFileModificationDate];
|
||||
int64_t t = 0;
|
||||
if (modifyTime != nil) {
|
||||
t = [modifyTime timeIntervalSince1970];
|
||||
}
|
||||
_fileModifyTime = t;
|
||||
NSFileHandle *f = nil;
|
||||
NSData *d = nil;
|
||||
//[NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error] 不能用在大于 200M的文件上,改用filehandle
|
||||
// 参见 https://issues.apache.org/jira/browse/CB-5790
|
||||
if (_fileSize > 16 * 1024 * 1024) {
|
||||
f = [NSFileHandle fileHandleForReadingAtPath:path];
|
||||
if (f == nil) {
|
||||
if (error != nil) {
|
||||
*error = [[NSError alloc] initWithDomain:path code:kQNFileError userInfo:nil];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
} else {
|
||||
d = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:&error2];
|
||||
if (error2 != nil) {
|
||||
if (error != nil) {
|
||||
*error = error2;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
}
|
||||
_file = f;
|
||||
_data = d;
|
||||
_lock = [[NSLock alloc] init];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSData *)read:(long long)offset
|
||||
size:(long)size
|
||||
error:(NSError **)error {
|
||||
|
||||
NSData *data = nil;
|
||||
@try {
|
||||
[_lock lock];
|
||||
if (_data != nil && offset < _data.length) {
|
||||
NSUInteger realSize = MIN((NSUInteger)size, _data.length - ((NSUInteger)offset));
|
||||
data = [_data subdataWithRange:NSMakeRange((NSUInteger)offset, realSize)];
|
||||
} else if (_file != nil && offset < _fileSize) {
|
||||
[_file seekToFileOffset:offset];
|
||||
data = [_file readDataOfLength:size];
|
||||
} else {
|
||||
data = [NSData data];
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
|
||||
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
|
||||
} @finally {
|
||||
[_lock unlock];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
- (NSData *)readAllWithError:(NSError **)error {
|
||||
return [self read:0 size:(long)_fileSize error:error];
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
if (_file != nil) {
|
||||
[_file closeFile];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)path {
|
||||
return _filepath;
|
||||
}
|
||||
|
||||
- (int64_t)modifyTime {
|
||||
return _fileModifyTime;
|
||||
}
|
||||
|
||||
- (int64_t)size {
|
||||
return _fileSize;
|
||||
}
|
||||
|
||||
- (NSString *)fileType {
|
||||
return @"File";
|
||||
}
|
||||
@end
|
||||
65
Pods/Qiniu/QiniuSDK/Utils/QNFileDelegate.h
generated
Executable file
65
Pods/Qiniu/QiniuSDK/Utils/QNFileDelegate.h
generated
Executable file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// QNFileDelegate.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/7/25.
|
||||
// Copyright (c) 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* 文件处理接口,支持ALAsset, NSFileHandle, NSData
|
||||
*/
|
||||
@protocol QNFileDelegate <NSObject>
|
||||
|
||||
/**
|
||||
* 从指定偏移读取数据
|
||||
*
|
||||
* @param offset 偏移地址
|
||||
* @param size 大小
|
||||
* @param error 错误信息
|
||||
*
|
||||
* @return 数据
|
||||
*/
|
||||
- (NSData *)read:(long long)offset
|
||||
size:(long)size
|
||||
error:(NSError **)error;
|
||||
|
||||
/**
|
||||
* 读取所有文件内容
|
||||
* @param error 错误信息
|
||||
* @return 数据
|
||||
*/
|
||||
- (NSData *)readAllWithError:(NSError **)error;
|
||||
|
||||
/**
|
||||
* 关闭文件
|
||||
*/
|
||||
- (void)close;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*
|
||||
* @return 文件路径
|
||||
*/
|
||||
- (NSString *)path;
|
||||
|
||||
/**
|
||||
* 文件修改时间
|
||||
*
|
||||
* @return 修改时间
|
||||
*/
|
||||
- (int64_t)modifyTime;
|
||||
|
||||
/**
|
||||
* 文件大小
|
||||
*
|
||||
* @return 文件大小
|
||||
*/
|
||||
- (int64_t)size;
|
||||
|
||||
@optional
|
||||
- (NSString *)fileType;
|
||||
|
||||
@end
|
||||
52
Pods/Qiniu/QiniuSDK/Utils/QNLogUtil.h
generated
Normal file
52
Pods/Qiniu/QiniuSDK/Utils/QNLogUtil.h
generated
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// QNLogUtil.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2020/12/25.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef NS_ENUM(NSInteger, QNLogLevel){
|
||||
QNLogLevelNone,
|
||||
QNLogLevelError,
|
||||
QNLogLevelWarn,
|
||||
QNLogLevelInfo,
|
||||
QNLogLevelDebug,
|
||||
QNLogLevelVerbose
|
||||
};
|
||||
|
||||
@interface QNLogUtil : NSObject
|
||||
|
||||
+ (void)setLogLevel:(QNLogLevel)level;
|
||||
|
||||
+ (void)enableLogDate:(BOOL)enable;
|
||||
+ (void)enableLogFile:(BOOL)enable;
|
||||
+ (void)enableLogFunction:(BOOL)enable;
|
||||
|
||||
+ (void)log:(QNLogLevel)level
|
||||
file:(const char *)file
|
||||
function:(const char *)function
|
||||
line:(NSUInteger)line
|
||||
format:(NSString * _Nullable)format, ...;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
#define QNLog(level, fmt, ...) \
|
||||
[QNLogUtil log:level \
|
||||
file:__FILE__ \
|
||||
function:__FUNCTION__ \
|
||||
line:__LINE__ \
|
||||
format:(fmt), ##__VA_ARGS__]
|
||||
|
||||
#define QNLogError(format, ...) QNLog(QNLogLevelError, format, ##__VA_ARGS__)
|
||||
#define QNLogWarn(format, ...) QNLog(QNLogLevelWarn, format, ##__VA_ARGS__)
|
||||
#define QNLogInfo(format, ...) QNLog(QNLogLevelInfo, format, ##__VA_ARGS__)
|
||||
#define QNLogDebug(format, ...) QNLog(QNLogLevelDebug, format, ##__VA_ARGS__)
|
||||
#define QNLogVerbose(format, ...) QNLog(QNLogLevelVerbose, format, ##__VA_ARGS__)
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
84
Pods/Qiniu/QiniuSDK/Utils/QNLogUtil.m
generated
Normal file
84
Pods/Qiniu/QiniuSDK/Utils/QNLogUtil.m
generated
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// QNLogUtil.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2020/12/25.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNLogUtil.h"
|
||||
|
||||
#if DEBUG
|
||||
static QNLogLevel _level = QNLogLevelError;
|
||||
#else
|
||||
static QNLogLevel _level = QNLogLevelNone;
|
||||
#endif
|
||||
|
||||
static BOOL _enableDate = false;
|
||||
static BOOL _enableFile = true;
|
||||
static BOOL _enableFunction = false;
|
||||
|
||||
@implementation QNLogUtil
|
||||
|
||||
+ (void)setLogLevel:(QNLogLevel)level {
|
||||
_level = level < 0 ? 0 : level;
|
||||
}
|
||||
|
||||
+ (void)enableLogDate:(BOOL)enable {
|
||||
_enableDate = enable;
|
||||
}
|
||||
+ (void)enableLogFile:(BOOL)enable {
|
||||
_enableFile = enable;
|
||||
}
|
||||
+ (void)enableLogFunction:(BOOL)enable {
|
||||
_enableFunction = enable;
|
||||
}
|
||||
|
||||
|
||||
+ (void)log:(QNLogLevel)level
|
||||
file:(const char *)file
|
||||
function:(const char *)function
|
||||
line:(NSUInteger)line
|
||||
format:(NSString *)format, ... {
|
||||
|
||||
if (!format || level > _level) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
|
||||
va_end(args);
|
||||
|
||||
NSString *fileName = @"";
|
||||
if (_enableFile) {
|
||||
fileName = [NSString stringWithFormat:@"%s", file];
|
||||
if ([fileName containsString:@"/"]) {
|
||||
fileName = [fileName componentsSeparatedByString:@"/"].lastObject;
|
||||
}
|
||||
}
|
||||
|
||||
NSString *functionName = @"";
|
||||
if (_enableFunction) {
|
||||
functionName = [NSString stringWithFormat:@"->%s", function];
|
||||
}
|
||||
|
||||
NSString *lineNumber = [NSString stringWithFormat:@"->%ld", line];
|
||||
|
||||
NSString *date = @"";
|
||||
if (_enableDate) {
|
||||
date = [NSString stringWithFormat:@"%@", [NSDate date]];
|
||||
if ([date length] > 20) {
|
||||
date = [date substringToIndex:19];
|
||||
}
|
||||
}
|
||||
|
||||
NSThread *thread = [NSThread currentThread];
|
||||
NSString *levelString = @[@"N", @"E", @"W", @"I", @"D", @"V"][level%6];
|
||||
message = [NSString stringWithFormat:@"%@[%@] %@ %@%@%@ %@", date, levelString, thread, fileName, functionName, lineNumber, message];
|
||||
|
||||
NSLog(@"%@", message);
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
17
Pods/Qiniu/QiniuSDK/Utils/QNMutableArray.h
generated
Normal file
17
Pods/Qiniu/QiniuSDK/Utils/QNMutableArray.h
generated
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// QNMutableArray.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2021/7/5.
|
||||
// Copyright © 2021 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface QNMutableArray : NSMutableArray
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
373
Pods/Qiniu/QiniuSDK/Utils/QNMutableArray.m
generated
Normal file
373
Pods/Qiniu/QiniuSDK/Utils/QNMutableArray.m
generated
Normal file
@@ -0,0 +1,373 @@
|
||||
//
|
||||
// QNMutableArray.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2021/7/5.
|
||||
// Copyright © 2021 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNMutableArray.h"
|
||||
|
||||
#define INIT(...) self = super.init; \
|
||||
if (!self) return nil; \
|
||||
__VA_ARGS__; \
|
||||
if (!_arr) return nil; \
|
||||
_lock = dispatch_semaphore_create(1); \
|
||||
return self;
|
||||
|
||||
|
||||
#define LOCK(...) dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER); \
|
||||
__VA_ARGS__; \
|
||||
dispatch_semaphore_signal(_lock);
|
||||
|
||||
|
||||
@implementation QNMutableArray {
|
||||
NSMutableArray *_arr; //Subclass a class cluster...
|
||||
dispatch_semaphore_t _lock;
|
||||
}
|
||||
|
||||
#pragma mark - init
|
||||
|
||||
- (instancetype)init {
|
||||
INIT(_arr = [[NSMutableArray alloc] init]);
|
||||
}
|
||||
|
||||
- (instancetype)initWithCapacity:(NSUInteger)numItems {
|
||||
INIT(_arr = [[NSMutableArray alloc] initWithCapacity:numItems]);
|
||||
}
|
||||
|
||||
- (instancetype)initWithArray:(NSArray *)array {
|
||||
INIT(_arr = [[NSMutableArray alloc] initWithArray:array]);
|
||||
}
|
||||
|
||||
- (instancetype)initWithObjects:(const id[])objects count:(NSUInteger)cnt {
|
||||
INIT(_arr = [[NSMutableArray alloc] initWithObjects:objects count:cnt]);
|
||||
}
|
||||
|
||||
- (instancetype)initWithContentsOfFile:(NSString *)path {
|
||||
INIT(_arr = [[NSMutableArray alloc] initWithContentsOfFile:path]);
|
||||
}
|
||||
|
||||
- (instancetype)initWithContentsOfURL:(NSURL *)url {
|
||||
INIT(_arr = [[NSMutableArray alloc] initWithContentsOfURL:url]);
|
||||
}
|
||||
|
||||
#pragma mark - method
|
||||
|
||||
- (NSUInteger)count {
|
||||
LOCK(NSUInteger count = _arr.count); return count;
|
||||
}
|
||||
|
||||
- (id)objectAtIndex:(NSUInteger)index {
|
||||
LOCK(id obj = [_arr objectAtIndex:index]); return obj;
|
||||
}
|
||||
|
||||
- (NSArray *)arrayByAddingObject:(id)anObject {
|
||||
LOCK(NSArray * arr = [_arr arrayByAddingObject:anObject]); return arr;
|
||||
}
|
||||
|
||||
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray {
|
||||
LOCK(NSArray * arr = [_arr arrayByAddingObjectsFromArray:otherArray]); return arr;
|
||||
}
|
||||
|
||||
- (NSString *)componentsJoinedByString:(NSString *)separator {
|
||||
LOCK(NSString * str = [_arr componentsJoinedByString:separator]); return str;
|
||||
}
|
||||
|
||||
- (BOOL)containsObject:(id)anObject {
|
||||
LOCK(BOOL c = [_arr containsObject:anObject]); return c;
|
||||
}
|
||||
|
||||
- (NSString *)description {
|
||||
LOCK(NSString * d = _arr.description); return d;
|
||||
}
|
||||
|
||||
- (NSString *)descriptionWithLocale:(id)locale {
|
||||
LOCK(NSString * d = [_arr descriptionWithLocale:locale]); return d;
|
||||
}
|
||||
|
||||
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
|
||||
LOCK(NSString * d = [_arr descriptionWithLocale:locale indent:level]); return d;
|
||||
}
|
||||
|
||||
- (id)firstObjectCommonWithArray:(NSArray *)otherArray {
|
||||
LOCK(id o = [_arr firstObjectCommonWithArray:otherArray]); return o;
|
||||
}
|
||||
|
||||
- (void)getObjects:(id __unsafe_unretained[])objects range:(NSRange)range {
|
||||
LOCK([_arr getObjects:objects range:range]);
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObject:(id)anObject {
|
||||
LOCK(NSUInteger i = [_arr indexOfObject:anObject]); return i;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range {
|
||||
LOCK(NSUInteger i = [_arr indexOfObject:anObject inRange:range]); return i;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject {
|
||||
LOCK(NSUInteger i = [_arr indexOfObjectIdenticalTo:anObject]); return i;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range {
|
||||
LOCK(NSUInteger i = [_arr indexOfObjectIdenticalTo:anObject inRange:range]); return i;
|
||||
}
|
||||
|
||||
- (id)firstObject {
|
||||
LOCK(id o = _arr.firstObject); return o;
|
||||
}
|
||||
|
||||
- (id)lastObject {
|
||||
LOCK(id o = _arr.lastObject); return o;
|
||||
}
|
||||
|
||||
- (NSEnumerator *)objectEnumerator {
|
||||
LOCK(NSEnumerator * e = [_arr objectEnumerator]); return e;
|
||||
}
|
||||
|
||||
- (NSEnumerator *)reverseObjectEnumerator {
|
||||
LOCK(NSEnumerator * e = [_arr reverseObjectEnumerator]); return e;
|
||||
}
|
||||
|
||||
- (NSData *)sortedArrayHint {
|
||||
LOCK(NSData * d = [_arr sortedArrayHint]); return d;
|
||||
}
|
||||
|
||||
- (NSArray *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(id, id, void *))comparator context:(void *)context {
|
||||
LOCK(NSArray * arr = [_arr sortedArrayUsingFunction:comparator context:context]) return arr;
|
||||
}
|
||||
|
||||
- (NSArray *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint {
|
||||
LOCK(NSArray * arr = [_arr sortedArrayUsingFunction:comparator context:context hint:hint]); return arr;
|
||||
}
|
||||
|
||||
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator {
|
||||
LOCK(NSArray * arr = [_arr sortedArrayUsingSelector:comparator]); return arr;
|
||||
}
|
||||
|
||||
- (NSArray *)subarrayWithRange:(NSRange)range {
|
||||
LOCK(NSArray * arr = [_arr subarrayWithRange:range]) return arr;
|
||||
}
|
||||
|
||||
- (void)makeObjectsPerformSelector:(SEL)aSelector {
|
||||
LOCK([_arr makeObjectsPerformSelector:aSelector]);
|
||||
}
|
||||
|
||||
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument {
|
||||
LOCK([_arr makeObjectsPerformSelector:aSelector withObject:argument]);
|
||||
}
|
||||
|
||||
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes {
|
||||
LOCK(NSArray * arr = [_arr objectsAtIndexes:indexes]); return arr;
|
||||
}
|
||||
|
||||
- (id)objectAtIndexedSubscript:(NSUInteger)idx {
|
||||
LOCK(id o = [_arr objectAtIndexedSubscript:idx]); return o;
|
||||
}
|
||||
|
||||
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))block {
|
||||
LOCK([_arr enumerateObjectsUsingBlock:block]);
|
||||
}
|
||||
|
||||
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))block {
|
||||
LOCK([_arr enumerateObjectsWithOptions:opts usingBlock:block]);
|
||||
}
|
||||
|
||||
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))block {
|
||||
LOCK([_arr enumerateObjectsAtIndexes:s options:opts usingBlock:block]);
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObjectPassingTest:(BOOL (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))predicate {
|
||||
LOCK(NSUInteger i = [_arr indexOfObjectPassingTest:predicate]); return i;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))predicate {
|
||||
LOCK(NSUInteger i = [_arr indexOfObjectWithOptions:opts passingTest:predicate]); return i;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))predicate {
|
||||
LOCK(NSUInteger i = [_arr indexOfObjectAtIndexes:s options:opts passingTest:predicate]); return i;
|
||||
}
|
||||
|
||||
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))predicate {
|
||||
LOCK(NSIndexSet * i = [_arr indexesOfObjectsPassingTest:predicate]); return i;
|
||||
}
|
||||
|
||||
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))predicate {
|
||||
LOCK(NSIndexSet * i = [_arr indexesOfObjectsWithOptions:opts passingTest:predicate]); return i;
|
||||
}
|
||||
|
||||
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))predicate {
|
||||
LOCK(NSIndexSet * i = [_arr indexesOfObjectsAtIndexes:s options:opts passingTest:predicate]); return i;
|
||||
}
|
||||
|
||||
- (NSArray *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr {
|
||||
LOCK(NSArray * a = [_arr sortedArrayUsingComparator:cmptr]); return a;
|
||||
}
|
||||
|
||||
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr {
|
||||
LOCK(NSArray * a = [_arr sortedArrayWithOptions:opts usingComparator:cmptr]); return a;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmp {
|
||||
LOCK(NSUInteger i = [_arr indexOfObject:obj inSortedRange:r options:opts usingComparator:cmp]); return i;
|
||||
}
|
||||
|
||||
#pragma mark - mutable
|
||||
|
||||
- (void)addObject:(id)anObject {
|
||||
LOCK([_arr addObject:anObject]);
|
||||
}
|
||||
|
||||
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index {
|
||||
LOCK([_arr insertObject:anObject atIndex:index]);
|
||||
}
|
||||
|
||||
- (void)removeLastObject {
|
||||
LOCK([_arr removeLastObject]);
|
||||
}
|
||||
|
||||
- (void)removeObjectAtIndex:(NSUInteger)index {
|
||||
LOCK([_arr removeObjectAtIndex:index]);
|
||||
}
|
||||
|
||||
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
|
||||
LOCK([_arr replaceObjectAtIndex:index withObject:anObject]);
|
||||
}
|
||||
|
||||
- (void)addObjectsFromArray:(NSArray *)otherArray {
|
||||
LOCK([_arr addObjectsFromArray:otherArray]);
|
||||
}
|
||||
|
||||
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2 {
|
||||
LOCK([_arr exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2]);
|
||||
}
|
||||
|
||||
- (void)removeAllObjects {
|
||||
LOCK([_arr removeAllObjects]);
|
||||
}
|
||||
|
||||
- (void)removeObject:(id)anObject inRange:(NSRange)range {
|
||||
LOCK([_arr removeObject:anObject inRange:range]);
|
||||
}
|
||||
|
||||
- (void)removeObject:(id)anObject {
|
||||
LOCK([_arr removeObject:anObject]);
|
||||
}
|
||||
|
||||
- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range {
|
||||
LOCK([_arr removeObjectIdenticalTo:anObject inRange:range]);
|
||||
}
|
||||
|
||||
- (void)removeObjectIdenticalTo:(id)anObject {
|
||||
LOCK([_arr removeObjectIdenticalTo:anObject]);
|
||||
}
|
||||
|
||||
- (void)removeObjectsInArray:(NSArray *)otherArray {
|
||||
LOCK([_arr removeObjectsInArray:otherArray]);
|
||||
}
|
||||
|
||||
- (void)removeObjectsInRange:(NSRange)range {
|
||||
LOCK([_arr removeObjectsInRange:range]);
|
||||
}
|
||||
|
||||
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange {
|
||||
LOCK([_arr replaceObjectsInRange:range withObjectsFromArray:otherArray range:otherRange]);
|
||||
}
|
||||
|
||||
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray {
|
||||
LOCK([_arr replaceObjectsInRange:range withObjectsFromArray:otherArray]);
|
||||
}
|
||||
|
||||
- (void)setArray:(NSArray *)otherArray {
|
||||
LOCK([_arr setArray:otherArray]);
|
||||
}
|
||||
|
||||
- (void)sortUsingFunction:(NSInteger (NS_NOESCAPE *)(id, id, void *))compare context:(void *)context {
|
||||
LOCK([_arr sortUsingFunction:compare context:context]);
|
||||
}
|
||||
|
||||
- (void)sortUsingSelector:(SEL)comparator {
|
||||
LOCK([_arr sortUsingSelector:comparator]);
|
||||
}
|
||||
|
||||
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes {
|
||||
LOCK([_arr insertObjects:objects atIndexes:indexes]);
|
||||
}
|
||||
|
||||
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes {
|
||||
LOCK([_arr removeObjectsAtIndexes:indexes]);
|
||||
}
|
||||
|
||||
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects {
|
||||
LOCK([_arr replaceObjectsAtIndexes:indexes withObjects:objects]);
|
||||
}
|
||||
|
||||
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
|
||||
LOCK([_arr setObject:obj atIndexedSubscript:idx]);
|
||||
}
|
||||
|
||||
- (void)sortUsingComparator:(NSComparator NS_NOESCAPE)cmptr {
|
||||
LOCK([_arr sortUsingComparator:cmptr]);
|
||||
}
|
||||
|
||||
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr {
|
||||
LOCK([_arr sortWithOptions:opts usingComparator:cmptr]);
|
||||
}
|
||||
|
||||
- (BOOL)isEqualToArray:(NSArray *)otherArray {
|
||||
if (otherArray == self) return YES;
|
||||
if ([otherArray isKindOfClass:QNMutableArray.class]) {
|
||||
QNMutableArray *other = (id)otherArray;
|
||||
BOOL isEqual;
|
||||
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
|
||||
dispatch_semaphore_wait(other->_lock, DISPATCH_TIME_FOREVER);
|
||||
isEqual = [_arr isEqualToArray:other->_arr];
|
||||
dispatch_semaphore_signal(other->_lock);
|
||||
dispatch_semaphore_signal(_lock);
|
||||
return isEqual;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark - protocol
|
||||
|
||||
- (id)copyWithZone:(NSZone *)zone {
|
||||
return [self mutableCopyWithZone:zone];
|
||||
}
|
||||
|
||||
- (id)mutableCopyWithZone:(NSZone *)zone {
|
||||
LOCK(id copiedDictionary = [[self.class allocWithZone:zone] initWithArray:_arr]);
|
||||
return copiedDictionary;
|
||||
}
|
||||
|
||||
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
|
||||
objects:(id __unsafe_unretained[])stackbuf
|
||||
count:(NSUInteger)len {
|
||||
LOCK(NSUInteger count = [_arr countByEnumeratingWithState:state objects:stackbuf count:len]);
|
||||
return count;
|
||||
}
|
||||
|
||||
- (BOOL)isEqual:(id)object {
|
||||
if (object == self) return YES;
|
||||
|
||||
if ([object isKindOfClass:QNMutableArray.class]) {
|
||||
QNMutableArray *other = object;
|
||||
BOOL isEqual;
|
||||
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
|
||||
dispatch_semaphore_wait(other->_lock, DISPATCH_TIME_FOREVER);
|
||||
isEqual = [_arr isEqual:other->_arr];
|
||||
dispatch_semaphore_signal(other->_lock);
|
||||
dispatch_semaphore_signal(_lock);
|
||||
return isEqual;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSUInteger)hash {
|
||||
LOCK(NSUInteger hash = [_arr hash]);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@end
|
||||
25
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetFile.h
generated
Executable file
25
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetFile.h
generated
Executable file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// QNPHAssetFile.h
|
||||
// Pods
|
||||
//
|
||||
// Created by 何舒 on 15/10/21.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "QNFileDelegate.h"
|
||||
|
||||
@class PHAsset;
|
||||
API_AVAILABLE(ios(9.1)) @interface QNPHAssetFile : NSObject <QNFileDelegate>
|
||||
/**
|
||||
* 打开指定文件
|
||||
*
|
||||
* @param phAsset 文件资源
|
||||
* @param error 输出的错误信息
|
||||
*
|
||||
* @return 实例
|
||||
*/
|
||||
- (instancetype)init:(PHAsset *)phAsset
|
||||
error:(NSError *__autoreleasing *)error;
|
||||
@end
|
||||
242
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetFile.m
generated
Normal file
242
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetFile.m
generated
Normal file
@@ -0,0 +1,242 @@
|
||||
//
|
||||
// QNPHAssetFile.m
|
||||
// Pods
|
||||
//
|
||||
// Created by 何舒 on 15/10/21.
|
||||
//
|
||||
//
|
||||
|
||||
#import "QNPHAssetFile.h"
|
||||
#import <Photos/Photos.h>
|
||||
#import "QNResponseInfo.h"
|
||||
|
||||
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 90100)
|
||||
|
||||
@interface QNPHAssetFile ()
|
||||
|
||||
@property (nonatomic) PHAsset *phAsset;
|
||||
|
||||
@property (nonatomic) int64_t fileSize;
|
||||
|
||||
@property (nonatomic) int64_t fileModifyTime;
|
||||
|
||||
@property (nonatomic, strong) NSData *assetData;
|
||||
|
||||
// file path 可能是导出的 file path,并不是真正的 filePath, 导出的文件在上传结束会被删掉,并不是真正有效的文件路径。
|
||||
@property(nonatomic, assign)BOOL hasRealFilePath;
|
||||
@property (nonatomic, copy) NSString *filePath;
|
||||
|
||||
@property (nonatomic) NSFileHandle *file;
|
||||
|
||||
@property (nonatomic, strong) NSLock *lock;
|
||||
|
||||
@end
|
||||
|
||||
@implementation QNPHAssetFile
|
||||
|
||||
- (instancetype)init:(PHAsset *)phAsset error:(NSError *__autoreleasing *)error {
|
||||
if (self = [super init]) {
|
||||
NSDate *createTime = phAsset.creationDate;
|
||||
int64_t t = 0;
|
||||
if (createTime != nil) {
|
||||
t = [createTime timeIntervalSince1970];
|
||||
}
|
||||
_fileModifyTime = t;
|
||||
_phAsset = phAsset;
|
||||
[self getInfo];
|
||||
|
||||
_lock = [[NSLock alloc] init];
|
||||
if (self.assetData == nil && self.filePath != nil) {
|
||||
NSError *error2 = nil;
|
||||
NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:self.filePath error:&error2];
|
||||
if (error2 != nil) {
|
||||
if (error != nil) {
|
||||
*error = error2;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
_fileSize = [fileAttr fileSize];
|
||||
NSFileHandle *f = nil;
|
||||
NSData *d = nil;
|
||||
if (_fileSize > 16 * 1024 * 1024) {
|
||||
f = [NSFileHandle fileHandleForReadingFromURL:[NSURL fileURLWithPath:self.filePath] error:error];
|
||||
if (f == nil) {
|
||||
if (error != nil) {
|
||||
*error = [[NSError alloc] initWithDomain:self.filePath code:kQNFileError userInfo:[*error userInfo]];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
} else {
|
||||
d = [NSData dataWithContentsOfFile:self.filePath options:NSDataReadingMappedIfSafe error:&error2];
|
||||
if (error2 != nil) {
|
||||
if (error != nil) {
|
||||
*error = error2;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
}
|
||||
_file = f;
|
||||
_assetData = d;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSData *)read:(long long)offset
|
||||
size:(long)size
|
||||
error:(NSError **)error {
|
||||
|
||||
NSData *data = nil;
|
||||
@try {
|
||||
[_lock lock];
|
||||
if (_assetData != nil && offset < _assetData.length) {
|
||||
NSUInteger realSize = MIN((NSUInteger)size, _assetData.length - (NSUInteger)offset);
|
||||
data = [_assetData subdataWithRange:NSMakeRange((NSUInteger)offset, realSize)];
|
||||
} else if (_file != nil && offset < _fileSize) {
|
||||
[_file seekToFileOffset:offset];
|
||||
data = [_file readDataOfLength:size];
|
||||
} else {
|
||||
data = [NSData data];
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
|
||||
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
|
||||
} @finally {
|
||||
[_lock unlock];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
- (NSData *)readAllWithError:(NSError **)error {
|
||||
return [self read:0 size:(long)_fileSize error:error];
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
if (PHAssetMediaTypeVideo == self.phAsset.mediaType) {
|
||||
if (_file != nil) {
|
||||
[_file closeFile];
|
||||
}
|
||||
// 如果是导出的 file 删除
|
||||
if (!self.hasRealFilePath && self.filePath) {
|
||||
[[NSFileManager defaultManager] removeItemAtPath:self.filePath error:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)path {
|
||||
return self.hasRealFilePath ? self.filePath : nil;
|
||||
}
|
||||
|
||||
- (int64_t)modifyTime {
|
||||
return _fileModifyTime;
|
||||
}
|
||||
|
||||
- (int64_t)size {
|
||||
return _fileSize;
|
||||
}
|
||||
|
||||
- (NSString *)fileType {
|
||||
return @"PHAsset";
|
||||
}
|
||||
|
||||
- (void)getInfo {
|
||||
if (PHAssetMediaTypeImage == self.phAsset.mediaType) {
|
||||
[self getImageInfo];
|
||||
} else if (PHAssetMediaTypeVideo == self.phAsset.mediaType) {
|
||||
// 1. 获取 video url 在此处打断点 debug 时 file path 有效,去除断点不进行 debug file path 无效,所以取消这种方式。
|
||||
// [self getVideoInfo];
|
||||
|
||||
// 2. video url 获取失败则导出文件
|
||||
if (self.filePath == nil) {
|
||||
[self exportAssert];
|
||||
}
|
||||
} else {
|
||||
[self exportAssert];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)getImageInfo {
|
||||
PHImageRequestOptions *options = [PHImageRequestOptions new];
|
||||
options.version = PHImageRequestOptionsVersionCurrent;
|
||||
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
|
||||
options.resizeMode = PHImageRequestOptionsResizeModeNone;
|
||||
//不支持icloud上传
|
||||
options.networkAccessAllowed = NO;
|
||||
options.synchronous = YES;
|
||||
|
||||
#if TARGET_OS_MACCATALYST
|
||||
if (@available(macOS 10.15, *)) {
|
||||
[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, CGImagePropertyOrientation orientation, NSDictionary *info) {
|
||||
self.assetData = imageData;
|
||||
self.fileSize = imageData.length;
|
||||
self.hasRealFilePath = NO;
|
||||
}];
|
||||
}
|
||||
#else
|
||||
if (@available(iOS 13, *)) {
|
||||
[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, CGImagePropertyOrientation orientation, NSDictionary *info) {
|
||||
self.assetData = imageData;
|
||||
self.fileSize = imageData.length;
|
||||
self.hasRealFilePath = NO;
|
||||
}];
|
||||
} else {
|
||||
[[PHImageManager defaultManager] requestImageDataForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
|
||||
self.assetData = imageData;
|
||||
self.fileSize = imageData.length;
|
||||
self.hasRealFilePath = NO;
|
||||
}];
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
- (void)getVideoInfo {
|
||||
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
|
||||
options.version = PHVideoRequestOptionsVersionCurrent;
|
||||
options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
|
||||
//不支持icloud上传
|
||||
options.networkAccessAllowed = NO;
|
||||
|
||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||
[[PHImageManager defaultManager] requestAVAssetForVideo:self.phAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
|
||||
if ([asset isKindOfClass:[AVURLAsset class]]) {
|
||||
self.filePath = [[(AVURLAsset *)asset URL] path];
|
||||
self.hasRealFilePath = YES;
|
||||
}
|
||||
dispatch_semaphore_signal(semaphore);
|
||||
}];
|
||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
|
||||
- (void)exportAssert {
|
||||
NSArray *assetResources = [PHAssetResource assetResourcesForAsset:self.phAsset];
|
||||
PHAssetResource *resource;
|
||||
for (PHAssetResource *assetRes in assetResources) {
|
||||
if (assetRes.type == PHAssetResourceTypePairedVideo || assetRes.type == PHAssetResourceTypeVideo) {
|
||||
resource = assetRes;
|
||||
}
|
||||
}
|
||||
NSString *fileName = [NSString stringWithFormat:@"tempAsset-%f-%d.mov", [[NSDate date] timeIntervalSince1970], arc4random()%100000];
|
||||
PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
|
||||
//不支持icloud上传
|
||||
options.networkAccessAllowed = NO;
|
||||
|
||||
NSString *PATH_VIDEO_FILE = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
|
||||
[[NSFileManager defaultManager] removeItemAtPath:PATH_VIDEO_FILE error:nil];
|
||||
|
||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource toFile:[NSURL fileURLWithPath:PATH_VIDEO_FILE] options:options completionHandler:^(NSError *_Nullable error) {
|
||||
if (error) {
|
||||
self.filePath = nil;
|
||||
} else {
|
||||
self.filePath = PATH_VIDEO_FILE;
|
||||
}
|
||||
self.hasRealFilePath = NO;
|
||||
dispatch_semaphore_signal(semaphore);
|
||||
}];
|
||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
27
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetResource.h
generated
Executable file
27
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetResource.h
generated
Executable file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// QNPHAssetResource.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by 何舒 on 16/2/14.
|
||||
// Copyright © 2016年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "QNFileDelegate.h"
|
||||
|
||||
@class PHAssetResource;
|
||||
API_AVAILABLE(ios(9.0)) @interface QNPHAssetResource : NSObject <QNFileDelegate>
|
||||
|
||||
/**
|
||||
* 打开指定文件
|
||||
*
|
||||
* @param phAssetResource PHLivePhoto的PHAssetResource文件
|
||||
* @param error 输出的错误信息
|
||||
*
|
||||
* @return 实例
|
||||
*/
|
||||
- (instancetype)init:(PHAssetResource *)phAssetResource
|
||||
error:(NSError *__autoreleasing *)error;
|
||||
|
||||
@end
|
||||
177
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetResource.m
generated
Executable file
177
Pods/Qiniu/QiniuSDK/Utils/QNPHAssetResource.m
generated
Executable file
@@ -0,0 +1,177 @@
|
||||
//
|
||||
// QNPHAssetResource.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by 何舒 on 16/2/14.
|
||||
// Copyright © 2016年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNPHAssetResource.h"
|
||||
#import <Photos/Photos.h>
|
||||
#import "QNResponseInfo.h"
|
||||
|
||||
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000)
|
||||
|
||||
enum {
|
||||
kAMASSETMETADATA_PENDINGREADS = 1,
|
||||
kAMASSETMETADATA_ALLFINISHED = 0
|
||||
};
|
||||
|
||||
@interface QNPHAssetResource ()
|
||||
|
||||
@property (nonatomic) PHAssetResource *phAssetResource;
|
||||
|
||||
@property (nonatomic) int64_t fileSize;
|
||||
|
||||
@property (nonatomic) int64_t fileModifyTime;
|
||||
|
||||
@property (nonatomic, strong) NSData *assetData;
|
||||
|
||||
@property (nonatomic, assign)BOOL hasRealFilePath;
|
||||
@property (nonatomic, copy) NSString *filePath;
|
||||
@property (nonatomic, strong) NSFileHandle *file;
|
||||
|
||||
@property (nonatomic, strong) NSLock *lock;
|
||||
|
||||
@end
|
||||
|
||||
@implementation QNPHAssetResource
|
||||
- (instancetype)init:(PHAssetResource *)phAssetResource
|
||||
error:(NSError *__autoreleasing *)error {
|
||||
if (self = [super init]) {
|
||||
PHFetchResult<PHAsset *> *results = [PHAsset fetchAssetsWithBurstIdentifier:phAssetResource.assetLocalIdentifier options:nil];
|
||||
if (results.firstObject != nil) {
|
||||
PHAsset *phasset = results.firstObject;
|
||||
NSDate *createTime = phasset.creationDate;
|
||||
int64_t t = 0;
|
||||
if (createTime != nil) {
|
||||
t = [createTime timeIntervalSince1970];
|
||||
}
|
||||
_fileModifyTime = t;
|
||||
}
|
||||
|
||||
_phAssetResource = phAssetResource;
|
||||
_lock = [[NSLock alloc] init];
|
||||
[self getInfo:error];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSData *)read:(long long)offset
|
||||
size:(long)size
|
||||
error:(NSError **)error {
|
||||
|
||||
NSData *data = nil;
|
||||
@try {
|
||||
[_lock lock];
|
||||
if (_assetData != nil && offset < _assetData.length) {
|
||||
NSUInteger realSize = MIN((NSUInteger)size, _assetData.length - (NSUInteger)offset);
|
||||
data = [_assetData subdataWithRange:NSMakeRange((NSUInteger)offset, realSize)];
|
||||
} else if (_file != nil && offset < _fileSize) {
|
||||
[_file seekToFileOffset:offset];
|
||||
data = [_file readDataOfLength:size];
|
||||
} else {
|
||||
data = [NSData data];
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
|
||||
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
|
||||
} @finally {
|
||||
[_lock unlock];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
- (NSData *)readAllWithError:(NSError **)error {
|
||||
return [self read:0 size:(long)_fileSize error:error];
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
if (self.file) {
|
||||
[self.file closeFile];
|
||||
}
|
||||
|
||||
// 如果是导出的 file 删除
|
||||
if (self.filePath) {
|
||||
[[NSFileManager defaultManager] removeItemAtPath:self.filePath error:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)path {
|
||||
return self.filePath ? self.filePath : nil;
|
||||
}
|
||||
|
||||
- (int64_t)modifyTime {
|
||||
return _fileModifyTime;
|
||||
}
|
||||
|
||||
- (int64_t)size {
|
||||
return _fileSize;
|
||||
}
|
||||
|
||||
- (NSString *)fileType {
|
||||
return @"PHAssetResource";
|
||||
}
|
||||
|
||||
- (void)getInfo:(NSError **)error {
|
||||
[self exportAssert];
|
||||
|
||||
NSError *error2 = nil;
|
||||
NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:self.filePath error:&error2];
|
||||
if (error2 != nil) {
|
||||
if (error != nil) {
|
||||
*error = error2;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_fileSize = [fileAttr fileSize];
|
||||
NSFileHandle *file = nil;
|
||||
NSData *data = nil;
|
||||
if (_fileSize > 16 * 1024 * 1024) {
|
||||
file = [NSFileHandle fileHandleForReadingFromURL:[NSURL fileURLWithPath:self.filePath] error:error];
|
||||
if (file == nil) {
|
||||
if (error != nil) {
|
||||
*error = [[NSError alloc] initWithDomain:self.filePath code:kQNFileError userInfo:[*error userInfo]];
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
data = [NSData dataWithContentsOfFile:self.filePath options:NSDataReadingMappedIfSafe error:&error2];
|
||||
if (error2 != nil) {
|
||||
if (error != nil) {
|
||||
*error = error2;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self.file = file;
|
||||
self.assetData = data;
|
||||
}
|
||||
|
||||
- (void)exportAssert {
|
||||
PHAssetResource *resource = self.phAssetResource;
|
||||
NSString *fileName = [NSString stringWithFormat:@"tempAsset-%f-%d.mov", [[NSDate date] timeIntervalSince1970], arc4random()%100000];
|
||||
PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
|
||||
//不支持icloud上传
|
||||
options.networkAccessAllowed = NO;
|
||||
|
||||
NSString *PATH_VIDEO_FILE = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
|
||||
[[NSFileManager defaultManager] removeItemAtPath:PATH_VIDEO_FILE error:nil];
|
||||
|
||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource toFile:[NSURL fileURLWithPath:PATH_VIDEO_FILE] options:options completionHandler:^(NSError *_Nullable error) {
|
||||
if (error) {
|
||||
self.filePath = nil;
|
||||
} else {
|
||||
self.filePath = PATH_VIDEO_FILE;
|
||||
}
|
||||
dispatch_semaphore_signal(semaphore);
|
||||
}];
|
||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
30
Pods/Qiniu/QiniuSDK/Utils/QNSingleFlight.h
generated
Normal file
30
Pods/Qiniu/QiniuSDK/Utils/QNSingleFlight.h
generated
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// QNSingleFlight.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2021/1/4.
|
||||
// Copyright © 2021 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef void(^QNSingleFlightComplete)(id _Nullable value, NSError * _Nullable error);
|
||||
typedef void(^QNSingleFlightAction)(QNSingleFlightComplete _Nonnull complete);
|
||||
|
||||
@interface QNSingleFlight : NSObject
|
||||
|
||||
/**
|
||||
* 异步 SingleFlight 执行函数
|
||||
* @param key actionHandler 对应的 key,同一时刻同一个 key 最多只有一个对应的 actionHandler 在执行
|
||||
* @param action 执行函数,注意:action 有且只能回调一次
|
||||
* @param complete single flight 执行 complete 后的完成回调
|
||||
*/
|
||||
- (void)perform:(NSString * _Nullable)key
|
||||
action:(QNSingleFlightAction _Nonnull)action
|
||||
complete:(QNSingleFlightComplete _Nullable)complete;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
112
Pods/Qiniu/QiniuSDK/Utils/QNSingleFlight.m
generated
Normal file
112
Pods/Qiniu/QiniuSDK/Utils/QNSingleFlight.m
generated
Normal file
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// QNSingleFlight.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2021/1/4.
|
||||
// Copyright © 2021 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNDefine.h"
|
||||
#import "QNSingleFlight.h"
|
||||
|
||||
@interface QNSingleFlightTask : NSObject
|
||||
@property(nonatomic, copy)QNSingleFlightComplete complete;
|
||||
@end
|
||||
@implementation QNSingleFlightTask
|
||||
@end
|
||||
|
||||
@interface QNSingleFlightCall : NSObject
|
||||
@property(nonatomic, assign)BOOL isComplete;
|
||||
@property(nonatomic, strong)NSMutableArray <QNSingleFlightTask *> *tasks;
|
||||
@property(nonatomic, strong)id value;
|
||||
@property(nonatomic, strong)NSError *error;
|
||||
@end
|
||||
@implementation QNSingleFlightCall
|
||||
@end
|
||||
|
||||
@interface QNSingleFlight()
|
||||
@property(nonatomic, strong)NSMutableDictionary <NSString *, QNSingleFlightCall *> *callInfo;
|
||||
@end
|
||||
@implementation QNSingleFlight
|
||||
|
||||
- (void)perform:(NSString * _Nullable)key
|
||||
action:(QNSingleFlightAction _Nonnull)action
|
||||
complete:(QNSingleFlightComplete _Nullable)complete {
|
||||
if (!action) {
|
||||
return;
|
||||
}
|
||||
|
||||
BOOL isFirstTask = false;
|
||||
BOOL shouldComplete = false;
|
||||
QNSingleFlightCall *call = nil;
|
||||
@synchronized (self) {
|
||||
if (!self.callInfo) {
|
||||
self.callInfo = [NSMutableDictionary dictionary];
|
||||
}
|
||||
|
||||
if (key) {
|
||||
call = self.callInfo[key];
|
||||
}
|
||||
|
||||
if (!call) {
|
||||
call = [[QNSingleFlightCall alloc] init];
|
||||
call.isComplete = false;
|
||||
call.tasks = [NSMutableArray array];
|
||||
if (key) {
|
||||
self.callInfo[key] = call;
|
||||
}
|
||||
isFirstTask = true;
|
||||
}
|
||||
|
||||
@synchronized (call) {
|
||||
shouldComplete = call.isComplete;
|
||||
if (!shouldComplete) {
|
||||
QNSingleFlightTask *task = [[QNSingleFlightTask alloc] init];
|
||||
task.complete = complete;
|
||||
[call.tasks addObject:task];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldComplete) {
|
||||
if (complete) {
|
||||
complete(call.value, call.error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!isFirstTask) {
|
||||
return;
|
||||
}
|
||||
|
||||
kQNWeakSelf;
|
||||
kQNWeakObj(call);
|
||||
action(^(id value, NSError *error){
|
||||
kQNStrongSelf;
|
||||
kQNStrongObj(call);
|
||||
|
||||
NSArray *tasksP = nil;
|
||||
@synchronized (call) {
|
||||
if (call.isComplete) {
|
||||
return;
|
||||
}
|
||||
call.isComplete = true;
|
||||
call.value = value;
|
||||
call.error = error;
|
||||
tasksP = [call.tasks copy];
|
||||
}
|
||||
|
||||
if (key) {
|
||||
@synchronized (self) {
|
||||
[self.callInfo removeObjectForKey:key];
|
||||
}
|
||||
}
|
||||
|
||||
for (QNSingleFlightTask *task in tasksP) {
|
||||
if (task.complete) {
|
||||
task.complete(value, error);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
20
Pods/Qiniu/QiniuSDK/Utils/QNSystem.h
generated
Executable file
20
Pods/Qiniu/QiniuSDK/Utils/QNSystem.h
generated
Executable file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// QNSystem.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/10/13.
|
||||
// Copyright © 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef QNSystem_h
|
||||
#define QNSystem_h
|
||||
|
||||
BOOL hasNSURLSession(void);
|
||||
|
||||
BOOL hasAts(void);
|
||||
|
||||
BOOL allowsArbitraryLoads(void);
|
||||
|
||||
BOOL isIpV6FullySupported(void);
|
||||
|
||||
#endif /* QNSystem_h */
|
||||
33
Pods/Qiniu/QiniuSDK/Utils/QNSystem.m
generated
Executable file
33
Pods/Qiniu/QiniuSDK/Utils/QNSystem.m
generated
Executable file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// QNSystem.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 15/10/13.
|
||||
// Copyright © 2015年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||
#import <MobileCoreServices/MobileCoreServices.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#else
|
||||
#import <CoreServices/CoreServices.h>
|
||||
#endif
|
||||
|
||||
BOOL isIpV6FullySupported(void) {
|
||||
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED)
|
||||
float sysVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
|
||||
if (sysVersion < 9.0) {
|
||||
return NO;
|
||||
}
|
||||
#else
|
||||
NSOperatingSystemVersion sysVersion = [[NSProcessInfo processInfo] operatingSystemVersion];
|
||||
if (sysVersion.majorVersion < 10) {
|
||||
return NO;
|
||||
} else if (sysVersion.majorVersion == 10) {
|
||||
return sysVersion.minorVersion >= 11;
|
||||
}
|
||||
#endif
|
||||
return YES;
|
||||
}
|
||||
41
Pods/Qiniu/QiniuSDK/Utils/QNUrlSafeBase64.h
generated
Executable file
41
Pods/Qiniu/QiniuSDK/Utils/QNUrlSafeBase64.h
generated
Executable file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14-9-28.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* url safe base64 编码类, 对/ 做了处理
|
||||
*/
|
||||
@interface QNUrlSafeBase64 : NSObject
|
||||
|
||||
/**
|
||||
* 字符串编码
|
||||
*
|
||||
* @param source 字符串
|
||||
*
|
||||
* @return base64 字符串
|
||||
*/
|
||||
+ (NSString *)encodeString:(NSString *)source;
|
||||
|
||||
/**
|
||||
* 二进制数据编码
|
||||
*
|
||||
* @param source 二进制数据
|
||||
*
|
||||
* @return base64字符串
|
||||
*/
|
||||
+ (NSString *)encodeData:(NSData *)source;
|
||||
|
||||
/**
|
||||
* 字符串解码
|
||||
*
|
||||
* @param data 字符串
|
||||
*
|
||||
* @return 数据
|
||||
*/
|
||||
+ (NSData *)decodeString:(NSString *)data;
|
||||
@end
|
||||
29
Pods/Qiniu/QiniuSDK/Utils/QNUrlSafeBase64.m
generated
Executable file
29
Pods/Qiniu/QiniuSDK/Utils/QNUrlSafeBase64.m
generated
Executable file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14-9-28.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "QNUrlSafeBase64.h"
|
||||
|
||||
#import "QN_GTM_Base64.h"
|
||||
|
||||
@implementation QNUrlSafeBase64
|
||||
|
||||
+ (NSString *)encodeString:(NSString *)sourceString {
|
||||
NSData *data = [NSData dataWithBytes:[sourceString UTF8String] length:[sourceString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
|
||||
return [self encodeData:data];
|
||||
}
|
||||
|
||||
+ (NSString *)encodeData:(NSData *)data {
|
||||
return [QN_GTM_Base64 stringByWebSafeEncodingData:data padded:YES];
|
||||
}
|
||||
|
||||
+ (NSData *)decodeString:(NSString *)data {
|
||||
return [QN_GTM_Base64 webSafeDecodeString:data];
|
||||
}
|
||||
|
||||
@end
|
||||
19
Pods/Qiniu/QiniuSDK/Utils/QNUrlUtils.h
generated
Normal file
19
Pods/Qiniu/QiniuSDK/Utils/QNUrlUtils.h
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// QNUrlUtils.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2023/11/16.
|
||||
// Copyright © 2023 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface QNUrlUtils : NSObject
|
||||
|
||||
+ (NSString *)setHostScheme:(NSString *)host useHttps:(BOOL)useHttps;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
25
Pods/Qiniu/QiniuSDK/Utils/QNUrlUtils.m
generated
Normal file
25
Pods/Qiniu/QiniuSDK/Utils/QNUrlUtils.m
generated
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// QNUrlUtils.m
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by yangsen on 2023/11/16.
|
||||
// Copyright © 2023 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNUrlUtils.h"
|
||||
|
||||
@implementation QNUrlUtils
|
||||
|
||||
+ (NSString *)setHostScheme:(NSString *)host useHttps:(BOOL)useHttps {
|
||||
if (host == nil || host.length == 0) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if ([host hasPrefix:@"http://"] || [host hasPrefix:@"https://"]) {
|
||||
return host;
|
||||
}
|
||||
|
||||
return [NSString stringWithFormat:@"%@%@", (useHttps ? @"https://" : @"http://"), host];
|
||||
}
|
||||
|
||||
@end
|
||||
68
Pods/Qiniu/QiniuSDK/Utils/QNUtils.h
generated
Normal file
68
Pods/Qiniu/QiniuSDK/Utils/QNUtils.h
generated
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// QNUtils.h
|
||||
// QiniuSDK_Mac
|
||||
//
|
||||
// Created by yangsen on 2020/3/27.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface QNUtils : NSObject
|
||||
|
||||
/// SDK 名称
|
||||
+ (NSString *)sdkVersion;
|
||||
|
||||
/// SDK 开发语言
|
||||
+ (NSString *)sdkLanguage;
|
||||
|
||||
/// 获取当前进程ID
|
||||
+ (int64_t)getCurrentProcessID;
|
||||
|
||||
/// 获取当前线程ID
|
||||
+ (int64_t)getCurrentThreadID;
|
||||
|
||||
/// 系统名称
|
||||
+ (NSString *)systemName;
|
||||
|
||||
/// 系统版本
|
||||
+ (NSString *)systemVersion;
|
||||
|
||||
/// 信号格数
|
||||
+ (NSNumber *)getCurrentSignalStrength;
|
||||
|
||||
/// 网络类型
|
||||
+ (NSString *)getCurrentNetworkType;
|
||||
|
||||
/// 获取当前时间戳 单位:ms
|
||||
+ (NSTimeInterval)currentTimestamp;
|
||||
|
||||
/// sdk document文件路径
|
||||
+ (NSString *)sdkDocumentDirectory;
|
||||
|
||||
/// sdk cache文件路径
|
||||
+ (NSString *)sdkCacheDirectory;
|
||||
|
||||
/// form escape
|
||||
/// @param string escape string
|
||||
+ (NSString *)formEscape:(NSString *)string;
|
||||
|
||||
/// 两个时间的时间段 单位:毫秒
|
||||
+ (NSNumber *)dateDuration:(NSDate *)startDate endDate:(NSDate *)endDate;
|
||||
|
||||
/// 计算 上传 或 下载 速度 单位:B/s
|
||||
/// @param bytes 单位: B
|
||||
/// @param totalTime 单位:ms
|
||||
/// @return 速度
|
||||
+ (NSNumber *)calculateSpeed:(long long)bytes totalTime:(long long)totalTime;
|
||||
|
||||
/// 根据ip和host来确定IP的类型,host可为空
|
||||
/// @param ip ip
|
||||
/// @param host host
|
||||
+ (NSString *)getIpType:(NSString * _Nullable)ip host:(NSString * _Nullable)host;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
169
Pods/Qiniu/QiniuSDK/Utils/QNUtils.m
generated
Normal file
169
Pods/Qiniu/QiniuSDK/Utils/QNUtils.m
generated
Normal file
@@ -0,0 +1,169 @@
|
||||
//
|
||||
// QNUtils.m
|
||||
// QiniuSDK_Mac
|
||||
//
|
||||
// Created by yangsen on 2020/3/27.
|
||||
// Copyright © 2020 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QNUtils.h"
|
||||
#include <pthread.h>
|
||||
#import "QNVersion.h"
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||
#import <UIKit/UIKit.h>
|
||||
#endif
|
||||
|
||||
@implementation QNUtils
|
||||
|
||||
+ (NSString *)sdkVersion{
|
||||
return kQiniuVersion;
|
||||
}
|
||||
|
||||
+ (NSString *)sdkLanguage{
|
||||
return @"Object-C";
|
||||
}
|
||||
|
||||
+ (int64_t)getCurrentProcessID {
|
||||
return [[NSProcessInfo processInfo] processIdentifier];
|
||||
}
|
||||
|
||||
+ (int64_t)getCurrentThreadID {
|
||||
__uint64_t threadId = 0;
|
||||
if (pthread_threadid_np(0, &threadId)) {
|
||||
threadId = pthread_mach_thread_np(pthread_self());
|
||||
}
|
||||
return threadId;
|
||||
}
|
||||
|
||||
+ (NSString *)systemName{
|
||||
NSString *name = nil;
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||
name = [[UIDevice currentDevice] model];
|
||||
#else
|
||||
name = @"Mac OS X";
|
||||
#endif
|
||||
return name;
|
||||
}
|
||||
|
||||
+ (NSString *)systemVersion{
|
||||
NSString *version = nil;
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||
version = [[UIDevice currentDevice] systemVersion];
|
||||
#else
|
||||
version = [[NSProcessInfo processInfo] operatingSystemVersionString];
|
||||
#endif
|
||||
return version;
|
||||
}
|
||||
|
||||
/// 信号格数
|
||||
+ (NSNumber *)getCurrentSignalStrength{
|
||||
NSNumber *strength = nil;
|
||||
return strength;
|
||||
}
|
||||
|
||||
/// 网络类型
|
||||
+ (NSString *)getCurrentNetworkType{
|
||||
NSString *type = nil;
|
||||
return type;
|
||||
}
|
||||
|
||||
+ (NSTimeInterval)currentTimestamp{
|
||||
return [[NSDate date] timeIntervalSince1970] * 1000;
|
||||
}
|
||||
|
||||
|
||||
+ (NSString *)sdkDocumentDirectory{
|
||||
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"/qiniu"];
|
||||
}
|
||||
|
||||
+ (NSString *)sdkCacheDirectory{
|
||||
return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"/qiniu"];
|
||||
}
|
||||
|
||||
+ (NSString *)formEscape:(NSString *)string{
|
||||
NSString *ret = string;
|
||||
ret = [ret stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
|
||||
ret = [ret stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
|
||||
return ret;
|
||||
}
|
||||
|
||||
+ (NSNumber *)dateDuration:(NSDate *)startDate endDate:(NSDate *)endDate {
|
||||
if (startDate && endDate) {
|
||||
double time = [endDate timeIntervalSinceDate:startDate] * 1000;
|
||||
return @(time);
|
||||
} else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
+ (NSNumber *)calculateSpeed:(long long)bytes totalTime:(long long)totalTime {
|
||||
if (bytes < 0 || totalTime == 0) {
|
||||
return nil;
|
||||
}
|
||||
long long speed = bytes * 1000 / totalTime;
|
||||
return @(speed);
|
||||
}
|
||||
|
||||
+ (NSString *)getIpType:(NSString *)ip host:(NSString *)host{
|
||||
|
||||
NSString *type = host;
|
||||
if (!ip || ip.length == 0) {
|
||||
return type;
|
||||
}
|
||||
if ([ip rangeOfString:@":"].location != NSNotFound) {
|
||||
type = [self getIPV6StringType:ip host:host];
|
||||
} else if ([ip rangeOfString:@"."].location != NSNotFound){
|
||||
type = [self getIPV4StringType:ip host:host];
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
+ (NSString *)getIPV4StringType:(NSString *)ipv4String host:(NSString *)host{
|
||||
NSString *type = nil;
|
||||
NSArray *ipNumberStrings = [ipv4String componentsSeparatedByString:@"."];
|
||||
if (ipNumberStrings.count == 4) {
|
||||
NSInteger firstNumber = [ipNumberStrings.firstObject integerValue];
|
||||
NSInteger secondNumber = [ipNumberStrings[1] integerValue];
|
||||
type = [NSString stringWithFormat:@"%ld-%ld",(long)firstNumber, (long)secondNumber];
|
||||
}
|
||||
type = [NSString stringWithFormat:@"%@-%@", host ?:@"", type];
|
||||
return type;
|
||||
}
|
||||
|
||||
+ (NSString *)getIPV6StringType:(NSString *)ipv6String host:(NSString *)host{
|
||||
NSArray *ipNumberStrings = [ipv6String componentsSeparatedByString:@":"];
|
||||
NSMutableArray *ipNumberStringsReal = [@[@"0000", @"0000", @"0000", @"0000",
|
||||
@"0000", @"0000", @"0000", @"0000"] mutableCopy];
|
||||
NSArray *suppleStrings = @[@"0000", @"000", @"00", @"0", @""];
|
||||
NSInteger i = 0;
|
||||
while (i < ipNumberStrings.count) {
|
||||
NSString *ipNumberString = ipNumberStrings[i];
|
||||
if (ipNumberString.length > 0) {
|
||||
ipNumberString = [NSString stringWithFormat:@"%@%@", suppleStrings[ipNumberString.length], ipNumberString];
|
||||
ipNumberStringsReal[i] = ipNumberString;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
NSInteger j = ipNumberStrings.count - 1;
|
||||
NSInteger indexReal = ipNumberStringsReal.count - 1;
|
||||
while (i < j) {
|
||||
NSString *ipNumberString = ipNumberStrings[j];
|
||||
if (ipNumberString.length > 0) {
|
||||
ipNumberString = [NSString stringWithFormat:@"%@%@", suppleStrings[ipNumberString.length], ipNumberString];
|
||||
ipNumberStringsReal[indexReal] = ipNumberString;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
j--;
|
||||
indexReal--;
|
||||
}
|
||||
NSString *numberInfo = [[ipNumberStringsReal subarrayWithRange:NSMakeRange(0, 4)] componentsJoinedByString:@"-"];
|
||||
return [NSString stringWithFormat:@"%@-%@-%@", host ?:@"", @"ipv6", numberInfo];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
14
Pods/Qiniu/QiniuSDK/Utils/QNVersion.h
generated
Executable file
14
Pods/Qiniu/QiniuSDK/Utils/QNVersion.h
generated
Executable file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// QNVersion.h
|
||||
// QiniuSDK
|
||||
//
|
||||
// Created by bailong on 14-9-29.
|
||||
// Copyright (c) 2014年 Qiniu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* sdk 版本
|
||||
*/
|
||||
static NSString *const kQiniuVersion = @"8.8.1";
|
||||
182
Pods/Qiniu/QiniuSDK/Utils/QN_GTM_Base64.h
generated
Executable file
182
Pods/Qiniu/QiniuSDK/Utils/QN_GTM_Base64.h
generated
Executable file
@@ -0,0 +1,182 @@
|
||||
//
|
||||
// GTMBase64.h
|
||||
//
|
||||
// Copyright 2006-2008 Google Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
// use this file except in compliance with the License. You may obtain a copy
|
||||
// of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations under
|
||||
// the License.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
// GTMBase64
|
||||
//
|
||||
/// Helper for handling Base64 and WebSafeBase64 encodings
|
||||
//
|
||||
/// The webSafe methods use different character set and also the results aren't
|
||||
/// always padded to a multiple of 4 characters. This is done so the resulting
|
||||
/// data can be used in urls and url query arguments without needing any
|
||||
/// encoding. You must use the webSafe* methods together, the data does not
|
||||
/// interop with the RFC methods.
|
||||
//
|
||||
@interface QN_GTM_Base64 : NSObject
|
||||
|
||||
//
|
||||
// Standard Base64 (RFC) handling
|
||||
//
|
||||
|
||||
// encodeData:
|
||||
//
|
||||
/// Base64 encodes contents of the NSData object.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)encodeData:(NSData *)data;
|
||||
|
||||
// decodeData:
|
||||
//
|
||||
/// Base64 decodes contents of the NSData object.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the decoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)decodeData:(NSData *)data;
|
||||
|
||||
// encodeBytes:length:
|
||||
//
|
||||
/// Base64 encodes the data pointed at by |bytes|.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)encodeBytes:(const void *)bytes length:(NSUInteger)length;
|
||||
|
||||
// decodeBytes:length:
|
||||
//
|
||||
/// Base64 decodes the data pointed at by |bytes|.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)decodeBytes:(const void *)bytes length:(NSUInteger)length;
|
||||
|
||||
// stringByEncodingData:
|
||||
//
|
||||
/// Base64 encodes contents of the NSData object.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSString with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSString *)stringByEncodingData:(NSData *)data;
|
||||
|
||||
// stringByEncodingBytes:length:
|
||||
//
|
||||
/// Base64 encodes the data pointed at by |bytes|.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSString with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSString *)stringByEncodingBytes:(const void *)bytes length:(NSUInteger)length;
|
||||
|
||||
// decodeString:
|
||||
//
|
||||
/// Base64 decodes contents of the NSString.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the decoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)decodeString:(NSString *)string;
|
||||
|
||||
//
|
||||
// Modified Base64 encoding so the results can go onto urls.
|
||||
//
|
||||
// The changes are in the characters generated and also allows the result to
|
||||
// not be padded to a multiple of 4.
|
||||
// Must use the matching call to encode/decode, won't interop with the
|
||||
// RFC versions.
|
||||
//
|
||||
|
||||
// webSafeEncodeData:padded:
|
||||
//
|
||||
/// WebSafe Base64 encodes contents of the NSData object. If |padded| is YES
|
||||
/// then padding characters are added so the result length is a multiple of 4.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)webSafeEncodeData:(NSData *)data
|
||||
padded:(BOOL)padded;
|
||||
|
||||
// webSafeDecodeData:
|
||||
//
|
||||
/// WebSafe Base64 decodes contents of the NSData object.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the decoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)webSafeDecodeData:(NSData *)data;
|
||||
|
||||
// webSafeEncodeBytes:length:padded:
|
||||
//
|
||||
/// WebSafe Base64 encodes the data pointed at by |bytes|. If |padded| is YES
|
||||
/// then padding characters are added so the result length is a multiple of 4.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)webSafeEncodeBytes:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
padded:(BOOL)padded;
|
||||
|
||||
// webSafeDecodeBytes:length:
|
||||
//
|
||||
/// WebSafe Base64 decodes the data pointed at by |bytes|.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)webSafeDecodeBytes:(const void *)bytes length:(NSUInteger)length;
|
||||
|
||||
// stringByWebSafeEncodingData:padded:
|
||||
//
|
||||
/// WebSafe Base64 encodes contents of the NSData object. If |padded| is YES
|
||||
/// then padding characters are added so the result length is a multiple of 4.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSString with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSString *)stringByWebSafeEncodingData:(NSData *)data
|
||||
padded:(BOOL)padded;
|
||||
|
||||
// stringByWebSafeEncodingBytes:length:padded:
|
||||
//
|
||||
/// WebSafe Base64 encodes the data pointed at by |bytes|. If |padded| is YES
|
||||
/// then padding characters are added so the result length is a multiple of 4.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSString with the encoded payload. nil for any error.
|
||||
//
|
||||
+ (NSString *)stringByWebSafeEncodingBytes:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
padded:(BOOL)padded;
|
||||
|
||||
// webSafeDecodeString:
|
||||
//
|
||||
/// WebSafe Base64 decodes contents of the NSString.
|
||||
//
|
||||
/// Returns:
|
||||
/// A new autoreleased NSData with the decoded payload. nil for any error.
|
||||
//
|
||||
+ (NSData *)webSafeDecodeString:(NSString *)string;
|
||||
|
||||
@end
|
||||
693
Pods/Qiniu/QiniuSDK/Utils/QN_GTM_Base64.m
generated
Executable file
693
Pods/Qiniu/QiniuSDK/Utils/QN_GTM_Base64.m
generated
Executable file
@@ -0,0 +1,693 @@
|
||||
//
|
||||
// GTMBase64.m
|
||||
//
|
||||
// Copyright 2006-2008 Google Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
// use this file except in compliance with the License. You may obtain a copy
|
||||
// of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations under
|
||||
// the License.
|
||||
//
|
||||
|
||||
#import "QN_GTM_Base64.h"
|
||||
|
||||
static const char *kBase64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static const char *kWebSafeBase64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
static const char kBase64PaddingChar = '=';
|
||||
static const char kBase64InvalidChar = 99;
|
||||
|
||||
static const char kBase64DecodeChars[] = {
|
||||
// This array was generated by the following code:
|
||||
// #include <sys/time.h>
|
||||
// #include <stdlib.h>
|
||||
// #include <string.h>
|
||||
// main()
|
||||
// {
|
||||
// static const char Base64[] =
|
||||
// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
// char *pos;
|
||||
// int idx, i, j;
|
||||
// printf(" ");
|
||||
// for (i = 0; i < 255; i += 8) {
|
||||
// for (j = i; j < i + 8; j++) {
|
||||
// pos = strchr(Base64, j);
|
||||
// if ((pos == NULL) || (j == 0))
|
||||
// idx = 99;
|
||||
// else
|
||||
// idx = pos - Base64;
|
||||
// if (idx == 99)
|
||||
// printf(" %2d, ", idx);
|
||||
// else
|
||||
// printf(" %2d/*%c*/,", idx, j);
|
||||
// }
|
||||
// printf("\n ");
|
||||
// }
|
||||
// }
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 62 /*+*/, 99, 99, 99, 63 /*/ */,
|
||||
52 /*0*/, 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
|
||||
60 /*8*/, 61 /*9*/, 99, 99, 99, 99, 99, 99,
|
||||
99, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/, 5 /*F*/, 6 /*G*/,
|
||||
7 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/, 12 /*M*/, 13 /*N*/, 14 /*O*/,
|
||||
15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/, 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/,
|
||||
23 /*X*/, 24 /*Y*/, 25 /*Z*/, 99, 99, 99, 99, 99,
|
||||
99, 26 /*a*/, 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/,
|
||||
33 /*h*/, 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
|
||||
41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/, 48 /*w*/,
|
||||
49 /*x*/, 50 /*y*/, 51 /*z*/, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99};
|
||||
|
||||
static const char kWebSafeBase64DecodeChars[] = {
|
||||
// This array was generated by the following code:
|
||||
// #include <sys/time.h>
|
||||
// #include <stdlib.h>
|
||||
// #include <string.h>
|
||||
// main()
|
||||
// {
|
||||
// static const char Base64[] =
|
||||
// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
// char *pos;
|
||||
// int idx, i, j;
|
||||
// printf(" ");
|
||||
// for (i = 0; i < 255; i += 8) {
|
||||
// for (j = i; j < i + 8; j++) {
|
||||
// pos = strchr(Base64, j);
|
||||
// if ((pos == NULL) || (j == 0))
|
||||
// idx = 99;
|
||||
// else
|
||||
// idx = pos - Base64;
|
||||
// if (idx == 99)
|
||||
// printf(" %2d, ", idx);
|
||||
// else
|
||||
// printf(" %2d/*%c*/,", idx, j);
|
||||
// }
|
||||
// printf("\n ");
|
||||
// }
|
||||
// }
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 62 /*-*/, 99, 99,
|
||||
52 /*0*/, 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
|
||||
60 /*8*/, 61 /*9*/, 99, 99, 99, 99, 99, 99,
|
||||
99, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/, 5 /*F*/, 6 /*G*/,
|
||||
7 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/, 12 /*M*/, 13 /*N*/, 14 /*O*/,
|
||||
15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/, 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/,
|
||||
23 /*X*/, 24 /*Y*/, 25 /*Z*/, 99, 99, 99, 99, 63 /*_*/,
|
||||
99, 26 /*a*/, 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/,
|
||||
33 /*h*/, 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
|
||||
41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/, 48 /*w*/,
|
||||
49 /*x*/, 50 /*y*/, 51 /*z*/, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99};
|
||||
|
||||
// Tests a character to see if it's a whitespace character.
|
||||
//
|
||||
// Returns:
|
||||
// YES if the character is a whitespace character.
|
||||
// NO if the character is not a whitespace character.
|
||||
//
|
||||
BOOL QN_IsSpace(unsigned char c) {
|
||||
// we use our own mapping here because we don't want anything w/ locale
|
||||
// support.
|
||||
static BOOL kSpaces[256] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // 0-9
|
||||
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 10-19
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-29
|
||||
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 30-39
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-49
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-59
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-69
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-79
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-89
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-99
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 100-109
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 110-119
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 120-129
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 130-139
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 140-149
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 150-159
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160-169
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 170-179
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 180-189
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 190-199
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 200-209
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 210-219
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 220-229
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 230-239
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 240-249
|
||||
0, 0, 0, 0, 0, 1, // 250-255
|
||||
};
|
||||
return kSpaces[c];
|
||||
}
|
||||
|
||||
// Calculate how long the data will be once it's base64 encoded.
|
||||
//
|
||||
// Returns:
|
||||
// The guessed encoded length for a source length
|
||||
//
|
||||
NSUInteger QN_CalcEncodedLength(NSUInteger srcLen, BOOL padded) {
|
||||
NSUInteger intermediate_result = 8 * srcLen + 5;
|
||||
NSUInteger len = intermediate_result / 6;
|
||||
if (padded) {
|
||||
len = ((len + 3) / 4) * 4;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// Tries to calculate how long the data will be once it's base64 decoded.
|
||||
// Unlike the above, this is always an upperbound, since the source data
|
||||
// could have spaces and might end with the padding characters on them.
|
||||
//
|
||||
// Returns:
|
||||
// The guessed decoded length for a source length
|
||||
//
|
||||
NSUInteger QN_GuessDecodedLength(NSUInteger srcLen) {
|
||||
return (srcLen + 3) / 4 * 3;
|
||||
}
|
||||
|
||||
@interface QN_GTM_Base64 (PrivateMethods)
|
||||
|
||||
+ (NSData *)baseEncode:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
charset:(const char *)charset
|
||||
padded:(BOOL)padded;
|
||||
|
||||
+ (NSData *)baseDecode:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
charset:(const char *)charset
|
||||
requirePadding:(BOOL)requirePadding;
|
||||
|
||||
+ (NSUInteger)baseEncode:(const char *)srcBytes
|
||||
srcLen:(NSUInteger)srcLen
|
||||
destBytes:(char *)destBytes
|
||||
destLen:(NSUInteger)destLen
|
||||
charset:(const char *)charset
|
||||
padded:(BOOL)padded;
|
||||
|
||||
+ (NSUInteger)baseDecode:(const char *)srcBytes
|
||||
srcLen:(NSUInteger)srcLen
|
||||
destBytes:(char *)destBytes
|
||||
destLen:(NSUInteger)destLen
|
||||
charset:(const char *)charset
|
||||
requirePadding:(BOOL)requirePadding;
|
||||
|
||||
@end
|
||||
|
||||
@implementation QN_GTM_Base64
|
||||
|
||||
//
|
||||
// Standard Base64 (RFC) handling
|
||||
//
|
||||
|
||||
+ (NSData *)encodeData:(NSData *)data {
|
||||
return [self baseEncode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kBase64EncodeChars
|
||||
padded:YES];
|
||||
}
|
||||
|
||||
+ (NSData *)decodeData:(NSData *)data {
|
||||
return [self baseDecode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kBase64DecodeChars
|
||||
requirePadding:YES];
|
||||
}
|
||||
|
||||
+ (NSData *)encodeBytes:(const void *)bytes length:(NSUInteger)length {
|
||||
return [self baseEncode:bytes
|
||||
length:length
|
||||
charset:kBase64EncodeChars
|
||||
padded:YES];
|
||||
}
|
||||
|
||||
+ (NSData *)decodeBytes:(const void *)bytes length:(NSUInteger)length {
|
||||
return [self baseDecode:bytes
|
||||
length:length
|
||||
charset:kBase64DecodeChars
|
||||
requirePadding:YES];
|
||||
}
|
||||
|
||||
+ (NSString *)stringByEncodingData:(NSData *)data {
|
||||
NSString *result = nil;
|
||||
NSData *converted = [self baseEncode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kBase64EncodeChars
|
||||
padded:YES];
|
||||
if (converted) {
|
||||
result = [[NSString alloc] initWithData:converted
|
||||
encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+ (NSString *)stringByEncodingBytes:(const void *)bytes length:(NSUInteger)length {
|
||||
NSString *result = nil;
|
||||
NSData *converted = [self baseEncode:bytes
|
||||
length:length
|
||||
charset:kBase64EncodeChars
|
||||
padded:YES];
|
||||
if (converted) {
|
||||
result = [[NSString alloc] initWithData:converted
|
||||
encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+ (NSData *)decodeString:(NSString *)string {
|
||||
NSData *result = nil;
|
||||
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];
|
||||
if (data) {
|
||||
result = [self baseDecode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kBase64DecodeChars
|
||||
requirePadding:YES];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Modified Base64 encoding so the results can go onto urls.
|
||||
//
|
||||
// The changes are in the characters generated and also the result isn't
|
||||
// padded to a multiple of 4.
|
||||
// Must use the matching call to encode/decode, won't interop with the
|
||||
// RFC versions.
|
||||
//
|
||||
|
||||
+ (NSData *)webSafeEncodeData:(NSData *)data
|
||||
padded:(BOOL)padded {
|
||||
return [self baseEncode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kWebSafeBase64EncodeChars
|
||||
padded:padded];
|
||||
}
|
||||
|
||||
+ (NSData *)webSafeDecodeData:(NSData *)data {
|
||||
return [self baseDecode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kWebSafeBase64DecodeChars
|
||||
requirePadding:NO];
|
||||
}
|
||||
|
||||
+ (NSData *)webSafeEncodeBytes:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
padded:(BOOL)padded {
|
||||
return [self baseEncode:bytes
|
||||
length:length
|
||||
charset:kWebSafeBase64EncodeChars
|
||||
padded:padded];
|
||||
}
|
||||
|
||||
+ (NSData *)webSafeDecodeBytes:(const void *)bytes length:(NSUInteger)length {
|
||||
return [self baseDecode:bytes
|
||||
length:length
|
||||
charset:kWebSafeBase64DecodeChars
|
||||
requirePadding:NO];
|
||||
}
|
||||
|
||||
+ (NSString *)stringByWebSafeEncodingData:(NSData *)data
|
||||
padded:(BOOL)padded {
|
||||
NSString *result = nil;
|
||||
NSData *converted = [self baseEncode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kWebSafeBase64EncodeChars
|
||||
padded:padded];
|
||||
if (converted) {
|
||||
result = [[NSString alloc] initWithData:converted
|
||||
encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+ (NSString *)stringByWebSafeEncodingBytes:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
padded:(BOOL)padded {
|
||||
NSString *result = nil;
|
||||
NSData *converted = [self baseEncode:bytes
|
||||
length:length
|
||||
charset:kWebSafeBase64EncodeChars
|
||||
padded:padded];
|
||||
if (converted) {
|
||||
result = [[NSString alloc] initWithData:converted
|
||||
encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+ (NSData *)webSafeDecodeString:(NSString *)string {
|
||||
NSData *result = nil;
|
||||
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];
|
||||
if (data) {
|
||||
result = [self baseDecode:[data bytes]
|
||||
length:[data length]
|
||||
charset:kWebSafeBase64DecodeChars
|
||||
requirePadding:NO];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation QN_GTM_Base64 (PrivateMethods)
|
||||
|
||||
//
|
||||
// baseEncode:length:charset:padded:
|
||||
//
|
||||
// Does the common lifting of creating the dest NSData. it creates & sizes the
|
||||
// data for the results. |charset| is the characters to use for the encoding
|
||||
// of the data. |padding| controls if the encoded data should be padded to a
|
||||
// multiple of 4.
|
||||
//
|
||||
// Returns:
|
||||
// an autorelease NSData with the encoded data, nil if any error.
|
||||
//
|
||||
+ (NSData *)baseEncode:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
charset:(const char *)charset
|
||||
padded:(BOOL)padded {
|
||||
// how big could it be?
|
||||
NSUInteger maxLength = QN_CalcEncodedLength(length, padded);
|
||||
// make space
|
||||
NSMutableData *result = [NSMutableData data];
|
||||
[result setLength:maxLength];
|
||||
// do it
|
||||
NSUInteger finalLength = [self baseEncode:bytes
|
||||
srcLen:length
|
||||
destBytes:[result mutableBytes]
|
||||
destLen:[result length]
|
||||
charset:charset
|
||||
padded:padded];
|
||||
if (finalLength) {
|
||||
// _GTMDevAssert(finalLength == maxLength, @"how did we calc the length wrong?");
|
||||
} else {
|
||||
// shouldn't happen, this means we ran out of space
|
||||
result = nil;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// baseDecode:length:charset:requirePadding:
|
||||
//
|
||||
// Does the common lifting of creating the dest NSData. it creates & sizes the
|
||||
// data for the results. |charset| is the characters to use for the decoding
|
||||
// of the data.
|
||||
//
|
||||
// Returns:
|
||||
// an autorelease NSData with the decoded data, nil if any error.
|
||||
//
|
||||
//
|
||||
+ (NSData *)baseDecode:(const void *)bytes
|
||||
length:(NSUInteger)length
|
||||
charset:(const char *)charset
|
||||
requirePadding:(BOOL)requirePadding {
|
||||
// could try to calculate what it will end up as
|
||||
NSUInteger maxLength = QN_GuessDecodedLength(length);
|
||||
// make space
|
||||
NSMutableData *result = [NSMutableData data];
|
||||
[result setLength:maxLength];
|
||||
// do it
|
||||
NSUInteger finalLength = [self baseDecode:bytes
|
||||
srcLen:length
|
||||
destBytes:[result mutableBytes]
|
||||
destLen:[result length]
|
||||
charset:charset
|
||||
requirePadding:requirePadding];
|
||||
if (finalLength) {
|
||||
if (finalLength != maxLength) {
|
||||
// resize down to how big it was
|
||||
[result setLength:finalLength];
|
||||
}
|
||||
} else {
|
||||
// either an error in the args, or we ran out of space
|
||||
result = nil;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// baseEncode:srcLen:destBytes:destLen:charset:padded:
|
||||
//
|
||||
// Encodes the buffer into the larger. returns the length of the encoded
|
||||
// data, or zero for an error.
|
||||
// |charset| is the characters to use for the encoding
|
||||
// |padded| tells if the result should be padded to a multiple of 4.
|
||||
//
|
||||
// Returns:
|
||||
// the length of the encoded data. zero if any error.
|
||||
//
|
||||
+ (NSUInteger)baseEncode:(const char *)srcBytes
|
||||
srcLen:(NSUInteger)srcLen
|
||||
destBytes:(char *)destBytes
|
||||
destLen:(NSUInteger)destLen
|
||||
charset:(const char *)charset
|
||||
padded:(BOOL)padded {
|
||||
if (!srcLen || !destLen || !srcBytes || !destBytes) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *curDest = destBytes;
|
||||
const unsigned char *curSrc = (const unsigned char *)(srcBytes);
|
||||
|
||||
// Three bytes of data encodes to four characters of cyphertext.
|
||||
// So we can pump through three-byte chunks atomically.
|
||||
while (srcLen > 2) {
|
||||
// space?
|
||||
// _GTMDevAssert(destLen >= 4, @"our calc for encoded length was wrong");
|
||||
curDest[0] = charset[curSrc[0] >> 2];
|
||||
curDest[1] = charset[((curSrc[0] & 0x03) << 4) + (curSrc[1] >> 4)];
|
||||
curDest[2] = charset[((curSrc[1] & 0x0f) << 2) + (curSrc[2] >> 6)];
|
||||
curDest[3] = charset[curSrc[2] & 0x3f];
|
||||
|
||||
curDest += 4;
|
||||
curSrc += 3;
|
||||
srcLen -= 3;
|
||||
destLen -= 4;
|
||||
}
|
||||
|
||||
// now deal with the tail (<=2 bytes)
|
||||
switch (srcLen) {
|
||||
case 0:
|
||||
// Nothing left; nothing more to do.
|
||||
break;
|
||||
|
||||
case 1:
|
||||
// One byte left: this encodes to two characters, and (optionally)
|
||||
// two pad characters to round out the four-character cypherblock.
|
||||
// _GTMDevAssert(destLen >= 2, @"our calc for encoded length was wrong");
|
||||
curDest[0] = charset[curSrc[0] >> 2];
|
||||
curDest[1] = charset[(curSrc[0] & 0x03) << 4];
|
||||
curDest += 2;
|
||||
destLen -= 2;
|
||||
if (padded) {
|
||||
// _GTMDevAssert(destLen >= 2, @"our calc for encoded length was wrong");
|
||||
curDest[0] = kBase64PaddingChar;
|
||||
curDest[1] = kBase64PaddingChar;
|
||||
curDest += 2;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// Two bytes left: this encodes to three characters, and (optionally)
|
||||
// one pad character to round out the four-character cypherblock.
|
||||
// _GTMDevAssert(destLen >= 3, @"our calc for encoded length was wrong");
|
||||
curDest[0] = charset[curSrc[0] >> 2];
|
||||
curDest[1] = charset[((curSrc[0] & 0x03) << 4) + (curSrc[1] >> 4)];
|
||||
curDest[2] = charset[(curSrc[1] & 0x0f) << 2];
|
||||
curDest += 3;
|
||||
destLen -= 3;
|
||||
if (padded) {
|
||||
// _GTMDevAssert(destLen >= 1, @"our calc for encoded length was wrong");
|
||||
curDest[0] = kBase64PaddingChar;
|
||||
curDest += 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// return the length
|
||||
return (curDest - destBytes);
|
||||
}
|
||||
|
||||
//
|
||||
// baseDecode:srcLen:destBytes:destLen:charset:requirePadding:
|
||||
//
|
||||
// Decodes the buffer into the larger. returns the length of the decoded
|
||||
// data, or zero for an error.
|
||||
// |charset| is the character decoding buffer to use
|
||||
//
|
||||
// Returns:
|
||||
// the length of the encoded data. zero if any error.
|
||||
//
|
||||
+ (NSUInteger)baseDecode:(const char *)srcBytes
|
||||
srcLen:(NSUInteger)srcLen
|
||||
destBytes:(char *)destBytes
|
||||
destLen:(NSUInteger)destLen
|
||||
charset:(const char *)charset
|
||||
requirePadding:(BOOL)requirePadding {
|
||||
if (!srcLen || !destLen || !srcBytes || !destBytes) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int decode;
|
||||
NSUInteger destIndex = 0;
|
||||
int state = 0;
|
||||
char ch = 0;
|
||||
while (srcLen-- && (ch = *srcBytes++) != 0) {
|
||||
if (QN_IsSpace(ch)) // Skip whitespace
|
||||
continue;
|
||||
|
||||
if (ch == kBase64PaddingChar)
|
||||
break;
|
||||
|
||||
decode = charset[(unsigned int)ch];
|
||||
if (decode == kBase64InvalidChar)
|
||||
return 0;
|
||||
|
||||
// Four cyphertext characters decode to three bytes.
|
||||
// Therefore we can be in one of four states.
|
||||
switch (state) {
|
||||
case 0:
|
||||
// We're at the beginning of a four-character cyphertext block.
|
||||
// This sets the high six bits of the first byte of the
|
||||
// plaintext block.
|
||||
// _GTMDevAssert(destIndex < destLen, @"our calc for decoded length was wrong");
|
||||
destBytes[destIndex] = decode << 2;
|
||||
state = 1;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
// We're one character into a four-character cyphertext block.
|
||||
// This sets the low two bits of the first plaintext byte,
|
||||
// and the high four bits of the second plaintext byte.
|
||||
// _GTMDevAssert((destIndex+1) < destLen, @"our calc for decoded length was wrong");
|
||||
destBytes[destIndex] |= decode >> 4;
|
||||
destBytes[destIndex + 1] = (decode & 0x0f) << 4;
|
||||
destIndex++;
|
||||
state = 2;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// We're two characters into a four-character cyphertext block.
|
||||
// This sets the low four bits of the second plaintext
|
||||
// byte, and the high two bits of the third plaintext byte.
|
||||
// However, if this is the end of data, and those two
|
||||
// bits are zero, it could be that those two bits are
|
||||
// leftovers from the encoding of data that had a length
|
||||
// of two mod three.
|
||||
// _GTMDevAssert((destIndex+1) < destLen, @"our calc for decoded length was wrong");
|
||||
destBytes[destIndex] |= decode >> 2;
|
||||
destBytes[destIndex + 1] = (decode & 0x03) << 6;
|
||||
destIndex++;
|
||||
state = 3;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// We're at the last character of a four-character cyphertext block.
|
||||
// This sets the low six bits of the third plaintext byte.
|
||||
// _GTMDevAssert(destIndex < destLen, @"our calc for decoded length was wrong");
|
||||
destBytes[destIndex] |= decode;
|
||||
destIndex++;
|
||||
state = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// We are done decoding Base-64 chars. Let's see if we ended
|
||||
// on a byte boundary, and/or with erroneous trailing characters.
|
||||
if (ch == kBase64PaddingChar) { // We got a pad char
|
||||
if ((state == 0) || (state == 1)) {
|
||||
return 0; // Invalid '=' in first or second position
|
||||
}
|
||||
if (srcLen == 0) {
|
||||
if (state == 2) { // We run out of input but we still need another '='
|
||||
return 0;
|
||||
}
|
||||
// Otherwise, we are in state 3 and only need this '='
|
||||
} else {
|
||||
if (state == 2) { // need another '='
|
||||
while ((ch = *srcBytes++) && (srcLen-- > 0)) {
|
||||
if (!QN_IsSpace(ch))
|
||||
break;
|
||||
}
|
||||
if (ch != kBase64PaddingChar) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// state = 1 or 2, check if all remain padding is space
|
||||
while ((ch = *srcBytes++) && (srcLen-- > 0)) {
|
||||
if (!QN_IsSpace(ch)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// We ended by seeing the end of the string.
|
||||
|
||||
if (requirePadding) {
|
||||
// If we require padding, then anything but state 0 is an error.
|
||||
if (state != 0) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
// Make sure we have no partial bytes lying around. Note that we do not
|
||||
// require trailing '=', so states 2 and 3 are okay too.
|
||||
if (state == 1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If then next piece of output was valid and got written to it means we got a
|
||||
// very carefully crafted input that appeared valid but contains some trailing
|
||||
// bits past the real length, so just toss the thing.
|
||||
if ((destIndex < destLen) &&
|
||||
(destBytes[destIndex] != 0)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return destIndex;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user