forked from heardrwt/RevealLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RHDownloadReveal.m
108 lines (81 loc) · 3.45 KB
/
RHDownloadReveal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// RHDownloadReveal.m
// RHDownloadReveal
//
// Created by Richard Heard on 21/03/2014.
// Copyright (c) 2014 Richard Heard. All rights reserved.
//
#include <unistd.h>
#include <sys/stat.h>
#include "common.h"
#include <partial/partial.h>
char endianness = IS_LITTLE_ENDIAN;
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
//download libReveal using partialzip
NSString *downloadURL = @"http://download.revealapp.com/Reveal.app.zip";
NSString *zipPath = @"Reveal.app/Contents/SharedSupport/iOS-Libraries/libReveal.dylib";
NSString *folder = @"/Library/RHRevealLoader";
NSString *filename = @"libReveal.dylib";
struct partialFile {
unsigned char *pos;
size_t fileSize;
size_t downloadedBytes;
float lastPercentageLogged;
};
size_t data_callback(ZipInfo* info, CDFile* file, unsigned char *buffer, size_t size, void *userInfo) {
struct partialFile *pfile = (struct partialFile *)userInfo;
memcpy(pfile->pos, buffer, size);
pfile->pos += size;
pfile->downloadedBytes += size;
float newPercentage = (int)(((float)pfile->downloadedBytes/(float)pfile->fileSize) * 100.f);
if (newPercentage > pfile->lastPercentageLogged){
if ((int)newPercentage % 5 == 0 || pfile->lastPercentageLogged == 0.0f){
printf("Downloading.. %g%%\n", newPercentage);
pfile->lastPercentageLogged = newPercentage;
}
}
return size;
}
int main(int argc, const char *argv[], const char *envp[]){
NSString *libraryPath = [folder stringByAppendingPathComponent:filename];
if (argc > 1 && strcmp(argv[1], "upgrade") != 0) {
printf("CYDIA upgrade, nuking existing %s\n", [libraryPath UTF8String]);
[[NSFileManager defaultManager] removeItemAtPath:libraryPath error:nil];
}
if (![[NSFileManager defaultManager] fileExistsAtPath:libraryPath]) {
//download libReveal.dylib
printf("Downloading '%s /%s' to '%s'.\n", [downloadURL UTF8String], [zipPath UTF8String], [libraryPath UTF8String]);
ZipInfo* info = PartialZipInit([downloadURL UTF8String]);
if(!info) {
printf("Cannot find %s\n", [downloadURL UTF8String]);
return 0;
}
CDFile *file = PartialZipFindFile(info, [zipPath UTF8String]);
if(!file) {
printf("Cannot find %s in %s\n", [zipPath UTF8String], [downloadURL UTF8String]);
return 0;
}
int dataLen = file->size;
unsigned char *data = malloc(dataLen+1);
struct partialFile pfile = (struct partialFile){data, dataLen, 0};
PartialZipGetFile(info, file, data_callback, &pfile);
*(pfile.pos) = '\0';
PartialZipRelease(info);
NSData *dylibData = [NSData dataWithBytes:data length:dataLen];
if (![[NSFileManager defaultManager] createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:nil]){
printf("Failed to create folder %s\n", [folder UTF8String]);
return 0;
}
if (![dylibData writeToFile:libraryPath atomically:YES]){
printf("Failed to write file to path %s\n", [libraryPath UTF8String]);
return 0;
}
free(data);
printf("Successfully downloaded %s to path %s\n", [downloadURL UTF8String], [libraryPath UTF8String]);
} else {
printf("libReveal.dylib already exists at path %s\n", [libraryPath UTF8String]);
}
return 0;
}