summaryrefslogtreecommitdiffstats
path: root/adbbu/twrpback.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'adbbu/twrpback.cpp')
-rw-r--r--adbbu/twrpback.cpp67
1 files changed, 33 insertions, 34 deletions
diff --git a/adbbu/twrpback.cpp b/adbbu/twrpback.cpp
index c59fd1cda..8652de2ac 100644
--- a/adbbu/twrpback.cpp
+++ b/adbbu/twrpback.cpp
@@ -34,9 +34,8 @@
#include "twadbstream.h"
#include "twrpback.hpp"
-#include "../variables.h"
-#include "../twcommon.h"
-#include "../twrpDigest.hpp"
+#include "../twrpDigest/twrpDigest.hpp"
+#include "../twrpDigest/twrpMD5.hpp"
twrpback::twrpback(void) {
read_fd = 0;
@@ -94,7 +93,7 @@ void twrpback::close_restore_fds() {
}
int twrpback::backup(std::string command) {
- twrpDigest adb_md5;
+ twrpMD5 digest;
bool breakloop = false;
int bytes = 0, errctr = 0;
char result[MAX_ADB_READ];
@@ -133,7 +132,12 @@ int twrpback::backup(std::string command) {
}
}
- sprintf(operation, "adbbackup %s", command.c_str());
+ memset(operation, 0, sizeof(operation));
+ if (snprintf(operation, sizeof(operation), "adbbackup %s", command.c_str()) >= sizeof(operation)) {
+ adblogwrite("Operation too big to write to ORS_INPUT_FILE\n");
+ close_backup_fds();
+ return -1;
+ }
if (write(write_fd, operation, sizeof(operation)) != sizeof(operation)) {
adblogwrite("Unable to write to ORS_INPUT_FILE\n");
close_backup_fds();
@@ -173,8 +177,7 @@ int twrpback::backup(std::string command) {
struct AdbBackupControlType structcmd;
memcpy(&structcmd, cmd, sizeof(cmd));
- std::string cmdstr(structcmd.type);
- std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1);
+ std::string cmdtype = structcmd.get_type();
//we received an error, exit and unlink
if (cmdtype == TWERROR) {
@@ -211,8 +214,7 @@ int twrpback::backup(std::string command) {
struct twfilehdr twimghdr;
adblogwrite("Writing TWIMG\n");
- adb_md5.initMD5();
-
+ digest.init();
memset(&twimghdr, 0, sizeof(twimghdr));
memcpy(&twimghdr, cmd, sizeof(cmd));
md5fnsize = twimghdr.size;
@@ -230,7 +232,7 @@ int twrpback::backup(std::string command) {
struct twfilehdr twfilehdr;
adblogwrite("Writing TWFN\n");
- adb_md5.initMD5();
+ digest.init();
ADBSTRUCT_STATIC_ASSERT(sizeof(twfilehdr) == MAX_ADB_READ);
@@ -265,8 +267,7 @@ int twrpback::backup(std::string command) {
totalbytes += bytes;
char *writeresult = new char [bytes];
memcpy(writeresult, result, bytes);
- if (adb_md5.updateMD5stream((unsigned char *) writeresult, bytes) == -1)
- adblogwrite("failed to update md5 stream\n");
+ digest.update((unsigned char *) writeresult, bytes);
if (fwrite(writeresult, 1, bytes, adbd_fp) != bytes) {
adblogwrite("Error writing backup data to adbd\n");
close_backup_fds();
@@ -286,8 +287,7 @@ int twrpback::backup(std::string command) {
close_backup_fds();
return -1;
}
- if (adb_md5.updateMD5stream((unsigned char *) padding, sizeof(padding)) == -1)
- adblogwrite("failed to update md5 stream\n");
+ digest.update((unsigned char *) padding, sizeof(padding));
fflush(adbd_fp);
totalbytes = 0;
}
@@ -295,9 +295,8 @@ int twrpback::backup(std::string command) {
AdbBackupFileTrailer md5trailer;
memset(&md5trailer, 0, sizeof(md5trailer));
- adb_md5.finalizeMD5stream();
- std::string md5string = adb_md5.createMD5string();
+ std::string md5string = digest.return_digest_string();
strncpy(md5trailer.start_of_trailer, TWRP, sizeof(md5trailer.start_of_trailer));
strncpy(md5trailer.type, MD5TRAILER, sizeof(md5trailer.type));
@@ -326,7 +325,7 @@ int twrpback::backup(std::string command) {
//to the adb stream.
//If the stream is compressed, we need to always write the data.
if (writedata || compressed) {
- while ((bytes = read(adb_read_fd, &result, sizeof(result))) == MAX_ADB_READ) {
+ while ((bytes = read(adb_read_fd, &result, sizeof(result))) > 0) {
if (firstDataPacket) {
struct AdbBackupControlType data_block;
@@ -346,8 +345,7 @@ int twrpback::backup(std::string command) {
char *writeresult = new char [bytes];
memcpy(writeresult, result, bytes);
- if (adb_md5.updateMD5stream((unsigned char *) writeresult, bytes) == -1)
- adblogwrite("failed to update md5 stream\n");
+ digest.update((unsigned char *) writeresult, bytes);
totalbytes += bytes;
dataChunkBytes += bytes;
@@ -395,7 +393,7 @@ int twrpback::backup(std::string command) {
}
int twrpback::restore(void) {
- twrpDigest adb_md5;
+ twrpMD5 digest;
char cmd[MAX_ADB_READ];
char result[MAX_ADB_READ];
struct AdbBackupControlType structcmd;
@@ -436,6 +434,7 @@ int twrpback::restore(void) {
}
}
+ memset(operation, 0, sizeof(operation));
sprintf(operation, "adbrestore");
if (write(write_fd, operation, sizeof(operation)) != sizeof(operation)) {
adblogwrite("Unable to write to ORS_INPUT_FILE\n");
@@ -489,8 +488,7 @@ int twrpback::restore(void) {
if (read(adb_control_bu_fd, &cmd, sizeof(cmd)) > 0) {
struct AdbBackupControlType structcmd;
memcpy(&structcmd, cmd, sizeof(cmd));
- std::string cmdstr(structcmd.type);
- std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1);
+ std::string cmdtype = structcmd.get_type();
//If we receive TWEOF from TWRP close adb data fifo
if (cmdtype == TWEOF) {
@@ -516,13 +514,11 @@ int twrpback::restore(void) {
}
//If we should read from the adb stream, write commands and data to TWRP
if (read_from_adb) {
- std::string cmdstr;
int readbytes;
if ((readbytes = fread(result, 1, sizeof(result), adbd_fp)) == sizeof(result)) {
totalbytes += readbytes;
memcpy(&structcmd, result, sizeof(result));
- cmdstr = structcmd.type;
- std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1);
+ std::string cmdtype = structcmd.get_type();
//Tell TWRP we have read the entire adb stream
if (cmdtype == TWENDADB) {
@@ -590,8 +586,8 @@ int twrpback::restore(void) {
struct twfilehdr twimghdr;
uint32_t crc, twimghdrcrc;
+ digest.init();
totalbytes -= sizeof(result);
- adb_md5.initMD5();
adblogwrite("Restoring TWIMG\n");
memset(&twimghdr, 0, sizeof(twimghdr));
memcpy(&twimghdr, result, sizeof(result));
@@ -622,9 +618,9 @@ int twrpback::restore(void) {
else if (cmdtype == TWFN) {
struct twfilehdr twfilehdr;
uint32_t crc, twfilehdrcrc;
+ digest.init();
totalbytes -= sizeof(result);
- adb_md5.initMD5();
adblogwrite("Restoring TWFN\n");
memset(&twfilehdr, 0, sizeof(twfilehdr));
memcpy(&twfilehdr, result, sizeof(result));
@@ -663,9 +659,9 @@ int twrpback::restore(void) {
}
totalbytes += readbytes;
memcpy(&structcmd, result, sizeof(result));
- cmdstr = structcmd.type;
+ std::string cmdtype = structcmd.get_type();
- if (cmdstr.substr(0, sizeof(MD5TRAILER) - 1) == MD5TRAILER) {
+ if (cmdtype.substr(0, sizeof(MD5TRAILER) - 1) == MD5TRAILER) {
struct AdbBackupFileTrailer md5tr;
uint32_t crc, md5trcrc, md5ident, md5identmatch;
@@ -702,15 +698,13 @@ int twrpback::restore(void) {
close_restore_fds();
return -1;
}
- adblogwrite("md5 finalize stream\n");
- adb_md5.finalizeMD5stream();
AdbBackupFileTrailer md5;
memset(&md5, 0, sizeof(md5));
strncpy(md5.start_of_trailer, TWRP, sizeof(md5.start_of_trailer));
strncpy(md5.type, TWMD5, sizeof(md5.type));
- std::string md5string = adb_md5.createMD5string();
+ std::string md5string = digest.return_digest_string();
strncpy(md5.md5, md5string.c_str(), sizeof(md5.md5));
adblogwrite("Sending MD5Check\n");
@@ -725,8 +719,7 @@ int twrpback::restore(void) {
break;
}
}
- if (adb_md5.updateMD5stream((unsigned char*)result, sizeof(result)) == -1)
- adblogwrite("failed to update md5 stream\n");
+ digest.update((unsigned char*)result, sizeof(result));
dataChunkBytes += readbytes;
if (write(adb_write_fd, result, sizeof(result)) < 0) {
@@ -778,6 +771,12 @@ int main(int argc, char **argv) {
return -1;
}
+ if (argc <= 1) {
+ tw.adblogwrite("No parameters given, exiting...\n");
+ tw.close_restore_fds();
+ return -1;
+ }
+
command = argv[1];
for (index = 2; index < argc; index++) {
command = command + " " + argv[index];