Skip to content

Commit

Permalink
Added IPS creation functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Jun 23, 2013
1 parent 557a730 commit 6dbd31b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 16 deletions.
91 changes: 91 additions & 0 deletions src/contrib/ips.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <string.h>
#include "ips.h"

/*
Expand Down Expand Up @@ -274,3 +275,93 @@ IPSResult IPSProcessRecord (struct IPSPatch *ips)

return IPS_OK;
}

/*
* Open the file and write IPS header in it.
*/
IPSResult IPSWriteBegin( FILE** out, char* filename )
{
const char buffer[5] = { 'P', 'A', 'T', 'C', 'H' };
size_t nWritten;

*out = fopen( filename, "wb" );
if( *out == NULL )
{
return IPS_ERROR_OPEN;
}

fseek( *out, 0, SEEK_SET );

nWritten = fwrite( buffer, 1, 5, *out );
if( nWritten != 5 )
{
return IPS_ERROR_WRITE;
}

return IPS_OK;
}

/*
* Append a new record to IPS file
*/
IPSResult IPSWriteRecord ( FILE* out, uint32_t offset, uint16_t size, uint8_t *data )
{
uint8_t buffer[5];
size_t nWritten;

if( out == NULL )
{
return IPS_ERROR;
}

/* serialize offset */
buffer[0] = (offset >> 16) & 0xff;
buffer[1] = (offset >> 8) & 0xff;
buffer[2] = (offset ) & 0xff;

/* ... and data size */
buffer[3] = (size >> 8);
buffer[4] = (size ) & 0xff;

nWritten = fwrite( buffer, 1, 5, out );
if( nWritten != 5 )
{
return IPS_ERROR_WRITE;
}

/* write data */
nWritten = fwrite( data, 1, size, out );
if( nWritten != size )
{
return IPS_ERROR_WRITE;
}

return IPS_OK;
}

/*
* Write IPS footer and close file.
*/
IPSResult IPSWriteEnd( FILE** out )
{
const char endStr[3] = { 'E', 'O', 'F' };
size_t nWritten;

if( *out == NULL )
{
fclose( *out );
*out = NULL;
return IPS_ERROR;
}

nWritten = fwrite( endStr, 1, 3, *out );
if( nWritten != 3 )
{
return IPS_ERROR_WRITE;
}

fclose( *out );
*out = NULL;

return IPS_OK;
}
53 changes: 37 additions & 16 deletions src/contrib/ips.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
#ifndef _IPS_H_
#define _IPS_H_

#include <stdio.h>
#include <stdint.h>

/**
* IPS result values.
*/
enum IPSResult
typedef enum
{
IPS_ERROR_OFFSET = -7,
IPS_ERROR_PROCESS = -6,
IPS_ERROR_READ = -5,
IPS_ERROR_OFFSET = -8,
IPS_ERROR_PROCESS = -7,
IPS_ERROR_READ = -6,
IPS_ERROR_WRITE = -5,
IPS_ERROR_SAVE = -4,
IPS_ERROR_OPEN = -3,
IPS_ERROR_FILE_TYPE = -2,
IPS_ERROR = -1,
IPS_PATCH_END = 0,
IPS_OK = 1
};
} IPSResult;

#define IPS_RECORD_OFFSET(record) ((record).offset & 0x00ffffff)
#define IPS_RECORD_INFO(record) (((record).offset >> 24) & 0xff)
#define IPS_RECORD_RLE 1

/*
* Structures
/**
* IPS record.
*/

struct IPSRecord
{
uint32_t offset;
uint16_t size;
uint8_t rleData;
uint32_t offset; /**< Destination offset */
uint16_t size; /**< Size of the data to be replaced */
uint8_t rleData; /**< RLE encoded byte */
};

/**
* IPS patch helper.
*/
struct IPSPatch
{
FILE *rom;
uint32_t romSize;
FILE *patch;
struct IPSRecord record;
FILE *rom; /**< File to be patched */
size_t romSize; /**< Input file size */
FILE *patch; /**< IPS file */
struct IPSRecord record; /**< Current IPS record */
};

/*
* Reset ips patch structure
*/
IPSResult IPSReset(struct IPSPatch *ips);
void IPSReset(struct IPSPatch *ips);

/*
* Open rom and patch
Expand All @@ -67,4 +73,19 @@ IPSResult IPSReadRecord(struct IPSPatch *ips);
*/
IPSResult IPSProcessRecord (struct IPSPatch *ips);

/*
* Open the file and write IPS header in i
*/
IPSResult IPSWriteBegin(FILE** out, char* filename);

/*
* Append a new record to IPS file
*/
IPSResult IPSWriteRecord (FILE* out, uint32_t offset, uint16_t size, uint8_t *data);

/*
* Write IPS footer and close file.
*/
IPSResult IPSWriteEnd(FILE** out);

#endif /* _IPS_H_ */

0 comments on commit 6dbd31b

Please sign in to comment.