From 2652b284c7cdb50249591f540651943ec0330e98 Mon Sep 17 00:00:00 2001 From: wetor <2929339419@qq.com> Date: Mon, 11 Mar 2019 14:58:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=B0=94=E6=B3=A1=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=83=A8=E5=88=86=E6=BA=90=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 源码来自 https://github.com/devnoname120/vhbb --- vpkinstall/filesystem.cpp | 308 +++++++++++++++++++++++++++++++++++++ vpkinstall/filesystem.h | 14 ++ vpkinstall/license.txt | 104 +++++++++++++ vpkinstall/sha1.cpp | 149 ++++++++++++++++++ vpkinstall/sha1.h | 44 ++++++ vpkinstall/vitaPackage.cpp | 291 +++++++++++++++++++++++++++++++++++ vpkinstall/vitaPackage.h | 21 +++ 7 files changed, 931 insertions(+) create mode 100644 vpkinstall/filesystem.cpp create mode 100644 vpkinstall/filesystem.h create mode 100644 vpkinstall/license.txt create mode 100644 vpkinstall/sha1.cpp create mode 100644 vpkinstall/sha1.h create mode 100644 vpkinstall/vitaPackage.cpp create mode 100644 vpkinstall/vitaPackage.h diff --git a/vpkinstall/filesystem.cpp b/vpkinstall/filesystem.cpp new file mode 100644 index 0000000..6afd364 --- /dev/null +++ b/vpkinstall/filesystem.cpp @@ -0,0 +1,308 @@ +#include "filesystem.h" + + +#include +#include +#include +#include +#include +#include + +#define MAX_PATH_LENGTH 1024 +#define MAX_NAME_LENGTH 256 +#define MAX_SHORT_NAME_LENGTH 64 + +#define TRANSFER_SIZE (128 * 1024) +#define SCE_ERROR_ERRNO_EEXIST 0x80010011 +#define SCE_ERROR_ERRNO_ENODEV 0x80010013 + +std::string getFileName(std::string path) { + + int pos = path.find_last_of('/'); + std::string s(path.substr(pos + 1)); + return s; + +} + +int hasEndSlash(const char *path) { + return path[strlen(path) - 1] == '/'; +} + +int checkFileExist(const char *file) { + SceIoStat stat; + memset(&stat, 0, sizeof(SceIoStat)); + + return sceIoGetstat(file, &stat) >= 0; +} +int checkFolderExist(const char *folder) { + SceUID dfd = sceIoDopen(folder); + if (dfd < 0) + return 0; + + sceIoDclose(dfd); + return 1; +} +int copyFile(const char *src_path, const char *dst_path) { + // The source and destination paths are identical + if (strcasecmp(src_path, dst_path) == 0) { + return -1; + } + + // The destination is a subfolder of the source folder + int len = strlen(src_path); + if (strncasecmp(src_path, dst_path, len) == 0 && (dst_path[len] == '/' || dst_path[len - 1] == '/')) { + return -2; + } + + SceUID fdsrc = sceIoOpen(src_path, SCE_O_RDONLY, 0); + if (fdsrc < 0) { + + return fdsrc; + } + SceUID fddst = sceIoOpen(dst_path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777); + if (fddst < 0) { + sceIoClose(fdsrc); + return fddst; + } + + void *buf = malloc( TRANSFER_SIZE); + + while (1) { + int read = sceIoRead(fdsrc, buf, TRANSFER_SIZE); + + if (read < 0) { + free(buf); + + sceIoClose(fddst); + sceIoClose(fdsrc); + + sceIoRemove(dst_path); + + return read; + } + + if (read == 0) + break; + + int written = sceIoWrite(fddst, buf, read); + + if (written < 0) { + free(buf); + + sceIoClose(fddst); + sceIoClose(fdsrc); + + sceIoRemove(dst_path); + + return written; + } + + + } + + free(buf); + + // Inherit file stat + SceIoStat stat; + memset(&stat, 0, sizeof(SceIoStat)); + sceIoGetstatByFd(fdsrc, &stat); + sceIoChstatByFd(fddst, &stat, 0x3B); + + sceIoClose(fddst); + sceIoClose(fdsrc); + + return 1; +} + +int copyPath(const char *src_path, const char *dst_path) { + // The source and destination paths are identical + if (strcasecmp(src_path, dst_path) == 0) { + return -1; + } + + // The destination is a subfolder of the source folder + int len = strlen(src_path); + if (strncasecmp(src_path, dst_path, len) == 0 && (dst_path[len] == '/' || dst_path[len - 1] == '/')) { + return -2; + } + + SceUID dfd = sceIoDopen(src_path); + if (dfd >= 0) { + SceIoStat stat; + memset(&stat, 0, sizeof(SceIoStat)); + sceIoGetstatByFd(dfd, &stat); + + stat.st_mode |= SCE_S_IWUSR; + + int ret = sceIoMkdir(dst_path, 0777); + if (ret < 0 && ret != SCE_ERROR_ERRNO_EEXIST) { + sceIoDclose(dfd); + return ret; + } + + if (ret == SCE_ERROR_ERRNO_EEXIST) { + sceIoChstat(dst_path, &stat, 0x3B); + } + + int res = 0; + + do { + SceIoDirent dir; + memset(&dir, 0, sizeof(SceIoDirent)); + + res = sceIoDread(dfd, &dir); + if (res > 0) { + char *new_src_path = (char*)malloc(strlen(src_path) + strlen(dir.d_name) + 2); + snprintf(new_src_path, MAX_PATH_LENGTH - 1, "%s%s%s", src_path, hasEndSlash(src_path) ? "" : "/", dir.d_name); + + char *new_dst_path = (char*)malloc(strlen(dst_path) + strlen(dir.d_name) + 2); + snprintf(new_dst_path, MAX_PATH_LENGTH - 1, "%s%s%s", dst_path, hasEndSlash(dst_path) ? "" : "/", dir.d_name); + + int ret = 0; + + if (SCE_S_ISDIR(dir.d_stat.st_mode)) { + ret = copyPath(new_src_path, new_dst_path); + } + else { + ret = copyFile(new_src_path, new_dst_path); + } + + free(new_dst_path); + free(new_src_path); + + if (ret <= 0) { + sceIoDclose(dfd); + return ret; + } + } + } while (res > 0); + + sceIoDclose(dfd); + } + else { + return copyFile(src_path, dst_path); + } + + return 1; +} +// Path must end with '/' +int removePath(std::string path) { + // sceIoDopen doesn't work if there is a '/' at the end + if (path.back() == '/') + path.pop_back(); + + SceUID dfd = sceIoDopen(path.c_str()); + if (dfd >= 0) { + path += "/"; + int res = 0; + + do { + SceIoDirent dir; + memset(&dir, 0, sizeof(SceIoDirent)); + + res = sceIoDread(dfd, &dir); + if (res > 0) { + auto new_path = path + dir.d_name; + + if (SCE_S_ISDIR(dir.d_stat.st_mode)) { + int ret = removePath(new_path); + if (ret <= 0) { + sceIoDclose(dfd); + return ret; + } + } else { + int ret = sceIoRemove(new_path.c_str()); + if (ret < 0) { + sceIoDclose(dfd); + return ret; + } + } + } + } while (res > 0); + + sceIoDclose(dfd); + + int ret = sceIoRmdir(path.c_str()); + if (ret < 0) + return ret; + } else { + int ret = sceIoRemove(path.c_str()); + if (ret < 0) + return ret; + } + + return 1; +} +void getSizeString(char string[16], uint64_t size) { + double double_size = (double)size; + + int i = 0; + static const char *units[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; + while (double_size >= 1024.0) { + double_size /= 1024.0; + i++; + } + + snprintf(string, 16, "%.*f %s", (i == 0) ? 0 : 2, double_size, units[i]); +} + +int getPathInfo(char *path, uint64_t *size, uint32_t *folders, uint32_t *files) { + SceUID dfd = sceIoDopen(path); + if (dfd >= 0) { + int res = 0; + + do { + SceIoDirent dir; + memset(&dir, 0, sizeof(SceIoDirent)); + + res = sceIoDread(dfd, &dir); + if (res > 0) { + if (strcmp(dir.d_name, ".") == 0 || strcmp(dir.d_name, "..") == 0) + continue; + + char *new_path = (char *)malloc(strlen(path) + strlen(dir.d_name) + 2); + snprintf(new_path, MAX_PATH_LENGTH, "%s%s%s", path, hasEndSlash(path) ? "" : "/", dir.d_name); + + if (SCE_S_ISDIR(dir.d_stat.st_mode)) { + int ret = getPathInfo(new_path, size, folders, files); + if (ret <= 0) { + free(new_path); + sceIoDclose(dfd); + return ret; + } + } + else { + if (size) + (*size) += dir.d_stat.st_size; + + if (files) + (*files)++; + } + + free(new_path); + } + } while (res > 0); + + sceIoDclose(dfd); + + if (folders) + (*folders)++; + } + else { + if (size) { + SceIoStat stat; + memset(&stat, 0, sizeof(SceIoStat)); + + int res = sceIoGetstat(path, &stat); + if (res < 0) + return res; + + (*size) += stat.st_size; + } + + if (files) + (*files)++; + } + + return 1; +} \ No newline at end of file diff --git a/vpkinstall/filesystem.h b/vpkinstall/filesystem.h new file mode 100644 index 0000000..ba4a4f2 --- /dev/null +++ b/vpkinstall/filesystem.h @@ -0,0 +1,14 @@ +#pragma once +#include +#include +#include +int removePath(std::string path); +int hasEndSlash(const char *path); +int copyFile(const char *src_path, const char *dst_path); +int copyPath(const char *src_path, const char *dst_path); +int checkFolderExist(const char *folder); +int checkFileExist(const char *file); +std::string getFileName(std::string path); +void getSizeString(char string[16], uint64_t size); +//int getPathInfo(const char *path, uint64_t *size, uint32_t *folders,uint32_t *files, int(*handler)(const char *path)); +int getPathInfo(char *path, uint64_t *size, uint32_t *folders, uint32_t *files); diff --git a/vpkinstall/license.txt b/vpkinstall/license.txt new file mode 100644 index 0000000..029e286 --- /dev/null +++ b/vpkinstall/license.txt @@ -0,0 +1,104 @@ +Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. + +Section 1 Definitions. + + Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. + Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. + BY-NC-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License. + Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. + Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. + Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. + License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike. + Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License. + Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. + Licensor means the individual(s) or entity(ies) granting rights under this Public License. + NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange. + Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. + Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. + You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. + +Section 2 Scope. + + License grant. + Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: + reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and + produce, reproduce, and Share Adapted Material for NonCommercial purposes only. + Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. + Term. The term of this Public License is specified in Section 6(a). + Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. + Downstream recipients. + Offer from the Licensor Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. + Additional offer from the Licensor Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapters License You apply. + No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. + No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). + + Other rights. + Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. + Patent and trademark rights are not licensed under this Public License. + To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes. + +Section 3 License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the following conditions. + + Attribution. + + If You Share the Licensed Material (including in modified form), You must: + retain the following if it is supplied by the Licensor with the Licensed Material: + identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); + a copyright notice; + a notice that refers to this Public License; + a notice that refers to the disclaimer of warranties; + a URI or hyperlink to the Licensed Material to the extent reasonably practicable; + indicate if You modified the Licensed Material and retain an indication of any previous modifications; and + indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. + You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. + If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. + ShareAlike. + + In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. + The Adapters License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License. + You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. + You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. + +Section 4 Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: + + for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only; + if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and + You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. + +Section 5 Disclaimer of Warranties and Limitation of Liability. + + Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You. + To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You. + + The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. + +Section 6 Term and Termination. + + This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. + + Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: + automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or + upon express reinstatement by the Licensor. + For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. + For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. + Sections 1, 5, 6, 7, and 8 survive termination of this Public License. + +Section 7 Other Terms and Conditions. + + The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. + Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. + +Section 8 Interpretation. + + For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. + To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. + No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. + Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. diff --git a/vpkinstall/sha1.cpp b/vpkinstall/sha1.cpp new file mode 100644 index 0000000..eb0bda0 --- /dev/null +++ b/vpkinstall/sha1.cpp @@ -0,0 +1,149 @@ +/********************************************************************* +* Filename: sha1.c +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Implementation of the SHA1 hashing algorithm. + Algorithm specification can be found here: + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf + This implementation uses little endian byte order. +*********************************************************************/ + +/*************************** HEADER FILES ***************************/ +#include +#include +#include "sha1.h" + +/****************************** MACROS ******************************/ +#define ROTLEFT(a, b) ((a << b) | (a >> (32 - b))) + +/*********************** FUNCTION DEFINITIONS ***********************/ +void sha1_transform(SHA1_CTX *ctx, const BYTE data[]) +{ + WORD a, b, c, d, e, i, j, t, m[80]; + + for (i = 0, j = 0; i < 16; ++i, j += 4) + m[i] = (data[j] << 24) + (data[j + 1] << 16) + (data[j + 2] << 8) + (data[j + 3]); + for ( ; i < 80; ++i) { + m[i] = (m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16]); + m[i] = (m[i] << 1) | (m[i] >> 31); + } + + a = ctx->state[0]; + b = ctx->state[1]; + c = ctx->state[2]; + d = ctx->state[3]; + e = ctx->state[4]; + + for (i = 0; i < 20; ++i) { + t = ROTLEFT(a, 5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i]; + e = d; + d = c; + c = ROTLEFT(b, 30); + b = a; + a = t; + } + for ( ; i < 40; ++i) { + t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[1] + m[i]; + e = d; + d = c; + c = ROTLEFT(b, 30); + b = a; + a = t; + } + for ( ; i < 60; ++i) { + t = ROTLEFT(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2] + m[i]; + e = d; + d = c; + c = ROTLEFT(b, 30); + b = a; + a = t; + } + for ( ; i < 80; ++i) { + t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[3] + m[i]; + e = d; + d = c; + c = ROTLEFT(b, 30); + b = a; + a = t; + } + + ctx->state[0] += a; + ctx->state[1] += b; + ctx->state[2] += c; + ctx->state[3] += d; + ctx->state[4] += e; +} + +void sha1_init(SHA1_CTX *ctx) +{ + ctx->datalen = 0; + ctx->bitlen = 0; + ctx->state[0] = 0x67452301; + ctx->state[1] = 0xEFCDAB89; + ctx->state[2] = 0x98BADCFE; + ctx->state[3] = 0x10325476; + ctx->state[4] = 0xc3d2e1f0; + ctx->k[0] = 0x5a827999; + ctx->k[1] = 0x6ed9eba1; + ctx->k[2] = 0x8f1bbcdc; + ctx->k[3] = 0xca62c1d6; +} + +void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len) +{ + size_t i; + + for (i = 0; i < len; ++i) { + ctx->data[ctx->datalen] = data[i]; + ctx->datalen++; + if (ctx->datalen == 64) { + sha1_transform(ctx, ctx->data); + ctx->bitlen += 512; + ctx->datalen = 0; + } + } +} + +void sha1_final(SHA1_CTX *ctx, BYTE hash[]) +{ + WORD i; + + i = ctx->datalen; + + // Pad whatever data is left in the buffer. + if (ctx->datalen < 56) { + ctx->data[i++] = 0x80; + while (i < 56) + ctx->data[i++] = 0x00; + } + else { + ctx->data[i++] = 0x80; + while (i < 64) + ctx->data[i++] = 0x00; + sha1_transform(ctx, ctx->data); + memset(ctx->data, 0, 56); + } + + // Append to the padding the total message's length in bits and transform. + ctx->bitlen += ctx->datalen * 8; + ctx->data[63] = ctx->bitlen; + ctx->data[62] = ctx->bitlen >> 8; + ctx->data[61] = ctx->bitlen >> 16; + ctx->data[60] = ctx->bitlen >> 24; + ctx->data[59] = ctx->bitlen >> 32; + ctx->data[58] = ctx->bitlen >> 40; + ctx->data[57] = ctx->bitlen >> 48; + ctx->data[56] = ctx->bitlen >> 56; + sha1_transform(ctx, ctx->data); + + // Since this implementation uses little endian byte ordering and MD uses big endian, + // reverse all the bytes when copying the final state to the output hash. + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; + } +} diff --git a/vpkinstall/sha1.h b/vpkinstall/sha1.h new file mode 100644 index 0000000..8169363 --- /dev/null +++ b/vpkinstall/sha1.h @@ -0,0 +1,44 @@ +/********************************************************************* +* Filename: sha1.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding SHA1 implementation. +*********************************************************************/ + +#ifndef SHA1_H +#define SHA1_H + +/*************************** HEADER FILES ***************************/ +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/****************************** MACROS ******************************/ +#define SHA1_BLOCK_SIZE 20 // SHA1 outputs a 20 byte digest + +/**************************** DATA TYPES ****************************/ +typedef uint8_t BYTE; // 8-bit byte +typedef uint32_t WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[5]; + WORD k[4]; +} SHA1_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void sha1_init(SHA1_CTX *ctx); +void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len); +void sha1_final(SHA1_CTX *ctx, BYTE hash[]); + +#ifdef __cplusplus +} +#endif + +#endif // SHA1_H \ No newline at end of file diff --git a/vpkinstall/vitaPackage.cpp b/vpkinstall/vitaPackage.cpp new file mode 100644 index 0000000..62ac0b3 --- /dev/null +++ b/vpkinstall/vitaPackage.cpp @@ -0,0 +1,291 @@ +#include "vitaPackage.h" +#include "sha1.h" +#include "filesystem.h" +#include "../Utils.h" +#include + +#define ntohl __builtin_bswap32 + +extern unsigned char _binary_res_head_bin_start; +extern unsigned char _binary_res_head_bin_size; + +#define SFO_MAGIC 0x46535000 + +#define PSF_TYPE_BIN 0 +#define PSF_TYPE_STR 2 +#define PSF_TYPE_VAL 4 + +std::string VitaPackage::Package_path = PACKAGE_TEMP_FOLDER; + +static void fpkg_hmac(const uint8_t *data, unsigned int len, uint8_t hmac[16]) { + SHA1_CTX ctx; + char sha1[20]; + char buf[64]; + + sha1_init(&ctx); + sha1_update(&ctx, (BYTE*)data, len); + sha1_final(&ctx, (BYTE*)sha1); + + memset(buf, 0, 64); + memcpy(&buf[0], &sha1[4], 8); + memcpy(&buf[8], &sha1[4], 8); + memcpy(&buf[16], &sha1[12], 4); + buf[20] = sha1[16]; + buf[21] = sha1[1]; + buf[22] = sha1[2]; + buf[23] = sha1[3]; + memcpy(&buf[24], &buf[16], 8); + + sha1_init(&ctx); + sha1_update(&ctx, (BYTE*)buf, 64); + sha1_final(&ctx, (BYTE*)sha1); + memcpy(hmac, sha1, 16); +} + +typedef struct SfoHeader { + uint32_t magic; + uint32_t version; + uint32_t keyofs; + uint32_t valofs; + uint32_t count; +} __attribute__((packed)) SfoHeader; + +typedef struct SfoEntry { + uint16_t nameofs; + uint8_t alignment; + uint8_t type; + uint32_t valsize; + uint32_t totalsize; + uint32_t dataofs; +} __attribute__((packed)) SfoEntry; + +int getSfoString(char *buffer, const char *name, char *string, int length) { + SfoHeader *header = (SfoHeader *)buffer; + SfoEntry *entries = (SfoEntry *)((uint32_t)buffer + sizeof(SfoHeader)); + + if (header->magic != SFO_MAGIC) + return -1; + + int i; + for (i = 0; i < header->count; i++) { + if (strcmp(buffer + header->keyofs + entries[i].nameofs, name) == 0) { + memset(string, 0, length); + strncpy(string, buffer + header->valofs + entries[i].dataofs, length); + string[length-1] = '\0'; + return 0; + } + } + + return -2; +} +int setSfoString(char *buffer, const char *name, const char *string) { + SfoHeader *header = (SfoHeader *)buffer; + SfoEntry *entries = (SfoEntry *)((unsigned int)buffer + sizeof(SfoHeader)); + + if (header->magic != SFO_MAGIC) + return -1; + + int i; + for (i = 0; i < header->count; i++) { + if (strcmp(buffer + header->keyofs + entries[i].nameofs, name) == 0) { + strcpy(buffer + header->valofs + entries[i].dataofs, string); + return 0; + } + } + + return -2; +} +int WriteFile(const char *file, const void *buf, int size) { + SceUID fd = sceIoOpen(file, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777); + if (fd < 0) + return fd; + + int written = sceIoWrite(fd, buf, size); + + sceIoClose(fd); + return written; +} + + + +int allocateReadFile(const char *file, char **buffer) { + SceUID fd = sceIoOpen(file, SCE_O_RDONLY, 0); + if (fd < 0) + return fd; + + int size = sceIoLseek32(fd, 0, SCE_SEEK_END); + sceIoLseek32(fd, 0, SCE_SEEK_SET); + + *buffer = (char *)malloc(size); + if (!*buffer) { + sceIoClose(fd); + return -1; + } + + int read = sceIoRead(fd, *buffer, size); + sceIoClose(fd); + + return read; +} + +int makeHeadBin() +{ + uint8_t hmac[16]; + uint32_t off; + uint32_t len; + uint32_t out; + + if (checkFileExist((VitaPackage::Package_path + "sce_sys/package/head.bin").c_str())) + return 0; + + // Read param.sfo + char *sfo_buffer = nullptr; + int res = allocateReadFile((VitaPackage::Package_path + "sce_sys/param.sfo").c_str(), &sfo_buffer); + if (res < 0) + return res; + + // Get title id + char titleid[12]; + memset(titleid, 0, sizeof(titleid)); + getSfoString(sfo_buffer, "TITLE_ID", titleid, sizeof(titleid)); + + // Enforce TITLE_ID format + if (strlen(titleid) != 9) + return -1; + + // Get content id + char contentid[48]; + memset(contentid, 0, sizeof(contentid)); + getSfoString(sfo_buffer, "CONTENT_ID", contentid, sizeof(contentid)); + + // Free sfo buffer + free(sfo_buffer); + + // Allocate head.bin buffer + uint8_t *head_bin = (uint8_t *)malloc((int)&_binary_res_head_bin_size); + memcpy(head_bin, (void *)&_binary_res_head_bin_start, (int)&_binary_res_head_bin_size); + + // Write full title id + char full_title_id[48]; + snprintf(full_title_id, sizeof(full_title_id), "EP9000-%s_00-0000000000000000", titleid); + strncpy((char *)&head_bin[0x30], strlen(contentid) > 0 ? contentid : full_title_id, 48); + + // hmac of pkg header + len = ntohl(*(uint32_t *)&head_bin[0xD0]); + fpkg_hmac(&head_bin[0], len, hmac); + memcpy(&head_bin[len], hmac, 16); + + // hmac of pkg info + off = ntohl(*(uint32_t *)&head_bin[0x8]); + len = ntohl(*(uint32_t *)&head_bin[0x10]); + out = ntohl(*(uint32_t *)&head_bin[0xD4]); + fpkg_hmac(&head_bin[off], len-64, hmac); + memcpy(&head_bin[out], hmac, 16); + + // hmac of everything + len = ntohl(*(uint32_t *)&head_bin[0xE8]); + fpkg_hmac(&head_bin[0], len, hmac); + memcpy(&head_bin[len], hmac, 16); + + // Make dir + sceIoMkdir((VitaPackage::Package_path + "sce_sys/package").c_str(), 0777); + + // Write head.bin + WriteFile((VitaPackage::Package_path + "sce_sys/package/head.bin").c_str(), head_bin, (int)&_binary_res_head_bin_size); + + free(head_bin); + + return 0; +} + + + +//TITLE = STITLE TITLE_ID +int VitaPackage::SetSFOString(std::string title, std::string title_id) { + // Read param.sfo + if (title_id.length() != 9) { + return -1; + } + char *sfo_buffer = nullptr; + int res = allocateReadFile((VitaPackage::Package_path + "sce_sys/param.sfo").c_str(), &sfo_buffer); + if (res < 0) + return res; + sceIoRemove((VitaPackage::Package_path + "sce_sys/param.sfo").c_str()); + setSfoString(sfo_buffer, "TITLE", title.c_str()); + setSfoString(sfo_buffer, "STITLE", title.c_str()); + setSfoString(sfo_buffer, "TITLE_ID", title_id.c_str()); + WriteFile((VitaPackage::Package_path + "sce_sys/param.sfo").c_str(), sfo_buffer, SFO_SIZE); + +} +#define ntohl __builtin_bswap32 +VitaPackage::VitaPackage(const std::string path) : + path_(path) +{ + VitaPackage::Package_path = path; + + + // ScePaf is required for PromoterUtil + uint32_t ptr[0x100] = {0}; + ptr[0] = 0; + ptr[1] = (uint32_t)&ptr[0]; + uint32_t scepaf_argp[] = {0x400000, 0xEA60, 0x40000, 0, 0}; + sceSysmoduleLoadModuleInternalWithArg(SCE_SYSMODULE_INTERNAL_PAF, sizeof(scepaf_argp), scepaf_argp, ptr); + + sceSysmoduleLoadModuleInternal(SCE_SYSMODULE_INTERNAL_PROMOTER_UTIL); + scePromoterUtilityInit(); +} + +VitaPackage::~VitaPackage() +{ + scePromoterUtilityExit(); + sceSysmoduleUnloadModuleInternal(SCE_SYSMODULE_INTERNAL_PROMOTER_UTIL); +} +/* +int VitaPackage::Install(InfoProgress progress) +{ + return Install(&progress); +}*/ + +int VitaPackage::Install() +{ + + int ret = makeHeadBin(); + if (ret < 0) { + utils::printInfo("Can't make head.bin for : 0x%08X\n", path_.c_str(), ret); + return 0; + } + + //InfoProgress progress3; + //if(progress) progress3 = progress->Range(60, 100); + ret = scePromoterUtilityPromotePkg(path_.c_str(), 0); + if (ret < 0) { + utils::printInfo("Can't Promote %s: scePromoterUtilityPromotePkgWithRif() = 0x%08X\n", path_.c_str(), ret); + return 0; + } + + int state = 0; + unsigned int i = 0; + do { + ret = scePromoterUtilityGetState(&state); + if (ret < 0) { + utils::printInfo("Can't Promote %s: scePromoterUtilityGetState() = 0x%08X\n", path_.c_str(), ret); + return 0; + } + + i+= 1; + //if (i<50 && progress) progress3.percent(i*2); + sceKernelDelayThread(150 * 1000); + } while (state); + + int result = 0; + ret = scePromoterUtilityGetResult(&result); + if (ret < 0) { + utils::printInfo("Can't Promote %s: scePromoterUtilityGetResult() = 0x%08X\n", path_.c_str(), ret); + return 0; + } + + removePath(path_); + + //if(progress) progress->percent(100); + return 1; +} diff --git a/vpkinstall/vitaPackage.h b/vpkinstall/vitaPackage.h new file mode 100644 index 0000000..572acce --- /dev/null +++ b/vpkinstall/vitaPackage.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#define PACKAGE_TEMP_FOLDER std::string("ux0:/temp/pkg/") +#define SFO_SIZE 912 + +class VitaPackage{ +public: + VitaPackage(const std::string path); + + ~VitaPackage(); + //TITLE=STITLE TITLE_ID + int SetSFOString(std::string title, std::string title_id); + int Install(); + //int Install(); + static std::string Package_path; +private: + std::string path_; +}; +