/*#####################################################################
#
# File: unpack_update.c
# Author: michellgaby (vincent.18023962826@gmail.com)
# Description: Decompress Huawei's ' Updata. APP ' official system pack.
#
# Date Created: January 22, 2013-michellgaby
#
#####################################################################*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxLen 10240
void usage ();
int main (int argc,char *argv[])
{
int count,packetlength,datalength,olddatalength,datasum,line,remainder,*length;
Char *filename,*outname;
Char int2char[10];
unsigned char Buffer[maxlen];
FILE *infile,*outfile;
Gets the input parameters
if (argc = 1)
Filename= "UPDATE. APP ";
Else
FILENAME=ARGV[1];
Open input file in binary
if ((INFILE = fopen (FILENAME, "RB")) = = NULL) usage ();
Create a folder and enter the directory
mkdir ("Output");
ChDir ("Output");
Skip 92 Empty bytes
Fseek (INFILE, 92, 0);
For (Count=1;infile!= null;count++)
{
Determine if Huawei Firmware index header
Fread (buffer, 4, 1, INFILE);
if (Buffer[0]!= 0x55) break;
if (buffer[1]!= 0XAA) break;
if (buffer[2]!= 0x5a) break;
if (Buffer[3]!= 0xa5) break;
Get header file length
Fread (buffer, 4, 1, INFILE);
Length = (int *) buffer;
Packetlength=*length;
Skip 16 bytes
Fseek (INFILE, 16, 1);
Get content Length
Fread (buffer, 4, 1, INFILE);
Length = (int *) buffer;
Datalength=*length;
concatenate integers and strings and copy them to file names
Itoa (count, Int2char, 10);
Outname=strcat (Int2char, ". img");
Skip end of header file
Fseek (INFILE, packetLength-28, 1);
Create a file
if ((outfile = fopen (Outname, "wb") = = NULL)
{
printf ("Unrecognised file format. Wrong identifier.\n ");
return-1;
else printf ("Extracted output/%s\n", outname);
Dividing the content data into multiple parts
Datasum=datalength/maxlen;
For (Line=0;line <= datasum;line++)
{
Get Content Data
if (datasum = = line)
Fread (buffer, datalength% maxlen, 1, INFILE);
Else
Fread (buffer, MaxLen, 1, INFILE);
Output file
if (datasum = = line)
fwrite (buffer, datalength% maxlen, 1, outfile);
Else
fwrite (buffer, MaxLen, 1, outfile);
}
Close Output File
Fclose (outfile);
Pointer rounding, multiples of 4
remainder = 4-(ftell (INFILE)% 4);
if (Remainder < 4)
{
To populate the remaining bytes
Fseek (INFILE, remainder, 1);
}
}
Close input File
Fclose (INFILE);
return 0;
}
void usage ()
{
Help functions
printf ("Uasge:unpack_update.exe [UPDATE]. app| Updata. App]\n ");
Exit (0);
}