C language to extract Huawei firmware instance code _c language

Source: Internet
Author: User
Tags fread

Copy Code code as follows:

/*#####################################################################
#
# 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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.