dslinux/toolchain/ndstool/include banner.h bigint.h crc.h header.h hook.h little.h logo.h ndscreate.h ndsextract.h ndstool.h ndstool_version.h ndstree.h overlay.h passme.h raster.h sha1.h

stsp stsp at user.in-berlin.de
Sun Aug 13 22:25:32 CEST 2006


Update of /cvsroot/dslinux/dslinux/toolchain/ndstool/include
In directory antilope:/tmp/cvs-serv5897/include

Added Files:
	banner.h bigint.h crc.h header.h hook.h little.h logo.h 
	ndscreate.h ndsextract.h ndstool.h ndstool_version.h ndstree.h 
	overlay.h passme.h raster.h sha1.h 
Log Message:
Adding pristine copy of ndstool so I can branch from it.


--- NEW FILE: banner.h ---
#pragma pack(1)

struct Banner
{
	unsigned_short version;
	unsigned_short crc;
	unsigned char reserved[28];
	unsigned char tile_data[4][4][8][4];
	unsigned_short palette[16];
	unsigned_short title[6][128];		// max. 3 lines. seperated by linefeed character
};

#pragma pack()

extern char *bannerLanguages[];

int InsertTitleString(char *String, FILE *file);
unsigned short CalcBannerCRC(Banner &banner);
void IconFromBMP();

--- NEW FILE: header.h ---
#pragma pack(1)

struct Header
{
	char title[0xC];
	char gamecode[0x4];
	char makercode[2];
	unsigned char unitcode;
	unsigned char devicetype;						// type of device in the game card
	unsigned char devicecap;						// device capacity (1<<n Mbit)
	unsigned char reserved1[0x9];
	unsigned char romversion;
	unsigned char reserved2;
	unsigned_int arm9_rom_offset;
	unsigned_int arm9_entry_address;
	unsigned_int arm9_ram_address;
	unsigned_int arm9_size;
	unsigned_int arm7_rom_offset;
	unsigned_int arm7_entry_address;
	unsigned_int arm7_ram_address;
	unsigned_int arm7_size;
	unsigned_int fnt_offset;
	unsigned_int fnt_size;
	unsigned_int fat_offset;
	unsigned_int fat_size;
	unsigned_int arm9_overlay_offset;
	unsigned_int arm9_overlay_size;
	unsigned_int arm7_overlay_offset;
	unsigned_int arm7_overlay_size;
	unsigned_int rom_control_info1;
	unsigned_int rom_control_info2;
	unsigned_int banner_offset;
	unsigned_short secure_area_crc;
	unsigned_short rom_control_info3;
	unsigned_int offset_0x70;						// ?
	unsigned_int offset_0x74;						// ?
	unsigned_int offset_0x78;						// unique ID for homebrew
	unsigned_int offset_0x7C;						// unique ID for homebrew
	unsigned_int application_end_offset;			// rom size
	unsigned_int rom_header_size;
	unsigned_int offset_0x88;						// reserved... ?
	unsigned_int offset_0x8C;
	unsigned_int offset_0x90;
	unsigned_int offset_0x94;
	unsigned_int offset_0x98;
	unsigned_int offset_0x9C;
	unsigned_int offset_0xA0;
	unsigned_int offset_0xA4;
	unsigned_int offset_0xA8;
	unsigned_int offset_0xAC;
	unsigned_int offset_0xB0;
	unsigned_int offset_0xB4;
	unsigned_int offset_0xB8;
	unsigned_int offset_0xBC;
	unsigned char logo[156];
	unsigned_short logo_crc;
	unsigned_short header_crc;
	unsigned_int offset_0x160;
	unsigned_int offset_0x164;
	unsigned_int offset_0x168;
	unsigned_int offset_0x16C;
	unsigned char zero[0x90];
};

#pragma pack()


struct Country
{
	char countrycode;
	char *name;
};

struct Maker
{
	char *makercode;
	char *name;
};

extern Country countries[];
extern int NumCountries;

extern Maker makers[];
extern int NumMakers;

unsigned short CalcHeaderCRC(Header &header);
unsigned short CalcLogoCRC(Header &header);
unsigned short CalcSecureAreaCRC();
void FixHeaderCRC(char *ndsfilename);
void ShowInfo(char *ndsfilename);
int HashAndCompareWithList(char *filename, unsigned char sha1[]);

extern void EnDecryptSecureArea(char *ndsfilename);
extern void (*EncryptSecureArea)(unsigned char *data);
extern void (*DecryptSecureArea)(unsigned char *data);

--- NEW FILE: logo.h ---
int LogoConvert(unsigned char *srcp, unsigned char *dstp, unsigned char white);

--- NEW FILE: little.h ---
#pragma pack(1)

#ifndef BYTE_ORDER
	#error "BYTE_ORDER not defined"
#endif

#if BYTE_ORDER != BIG_ENDIAN
	#define NOSWAP
#endif

//template <typename T> struct Blah32
//{
//	unsigned T i;
//	#ifdef NOSWAP
//		operator T () { return i; }
//		T & operator = (T i) { return this->i = i; }
//	#else
//		operator T () { return i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
//		T & operator = (T i) { return this->i = i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
//	#endif
//	Blah() {}
//	Blah(T i) { *this = i; }
//};

struct unsigned_int
{
	unsigned int i;
	#ifdef NOSWAP
		operator unsigned int () { return i; }
		unsigned int & operator = (unsigned int i) { return this->i = i; }
	#else
		operator unsigned int () { return i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
		unsigned int & operator = (unsigned int i) { return this->i = i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
	#endif
	unsigned_int() {}
	unsigned_int(unsigned int i) { *this = i; }
};

struct unsigned_long
{
	unsigned long i;
	#ifdef NOSWAP
		operator unsigned long () { return i; }
		unsigned long & operator = (unsigned long i) { return this->i = i; }
	#else
		operator unsigned long () { return i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
		unsigned long & operator = (unsigned long i) { return this->i = i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
	#endif
	unsigned_long() {}
	unsigned_long(unsigned long i) { *this = i; }
};

struct unsigned_short
{
	unsigned short i;
	#ifdef NOSWAP
		operator unsigned short () { return i; }
		unsigned short & operator = (unsigned short i) { return this->i = i; }
	#else
		operator unsigned short () { return i>>8 | i<<8; }
		unsigned short & operator = (unsigned short i) { return this->i = i>>8 | i<<8; }
	#endif
	unsigned_short() {}
	unsigned_short(unsigned short i) { *this = i; }
};

struct signed_int
{
	unsigned int i;
	#ifdef NOSWAP
		operator signed int () { return i; }
		signed int & operator = (signed int i) { return (signed int &)this->i = i; }
	#else
		operator signed int () { return i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
		signed int & operator = (signed int i) { return (signed int &)this->i = i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
	#endif
	signed_int() {}
	signed_int(signed int i) { *this = i; }
};

struct signed_long
{
	unsigned long i;
	#ifdef NOSWAP
		operator signed long () { return i; }
		signed long & operator = (signed long i) { return (signed long &)this->i = i; }
	#else
		operator signed long () { return i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
		signed long & operator = (signed long i) { return (signed long &)this->i = i<<24 | i<<8&0xFF0000 | i>>8&0xFF00 | i>>24; }
	#endif
	signed_long() {}
	signed_long(signed long i) { *this = i; }
};

struct signed_short
{
	unsigned short i;
	#ifdef NOSWAP
		operator signed short () { return i; }
		signed short & operator = (unsigned short i) { return (signed short &)this->i = i; }
	#else
		operator signed short () { return i>>8 | i<<8; }
		signed short & operator = (unsigned short i) { return (signed short &)this->i = i>>8 | i<<8; }
	#endif
	signed_short() {}
	signed_short(signed short i) { *this = i; }
};

#pragma pack()

--- NEW FILE: ndstree.h ---
struct Tree
{
	unsigned int dir_id;	// directory ID in case of directory entry
	char *name;				// file or directory name
	Tree *directory;		// nonzero indicates directory. first tree node is a dummy
	Tree *next;				// linked list

	Tree()
	{
		dir_id = 0;
		name = 0;
		directory = 0;
		next = 0;
	}
};

Tree *ReadDirectory(Tree *tree, char *path);

--- NEW FILE: overlay.h ---
#pragma pack(1)

struct OverlayEntry
{
	unsigned int id;
	unsigned int ram_address;
	unsigned int ram_size;
	unsigned int bss_size;
	unsigned int sinit_init;
	unsigned int sinit_init_end;
	unsigned int file_id;
	unsigned int reserved;
};

#pragma pack()

#define OVERLAY_FMT		"overlay_%04u.bin"

--- NEW FILE: ndstool_version.h ---
#ifndef _ndstool_version_h_
#define _ndstool_version_h_

#define VER	"1.27"
extern const char CompileDate[];
extern const char CompileTime[];

#endif

--- NEW FILE: passme.h ---
int PassMe(char *ndsfilename, char *vhdfilename, char *sramfilename);

--- NEW FILE: ndstool.h ---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <libelf.h>
#include "little.h"
#include "header.h"

#define ROMTYPE_HOMEBREW	0
#define ROMTYPE_MULTIBOOT	1
#define ROMTYPE_NDSDUMPED	2	// decrypted secure area
#define ROMTYPE_ENCRSECURE	3
#define ROMTYPE_MASKROM		4	// unknown layout

#define MAX_FILEMASKS		16

enum { BANNER_BINARY, BANNER_IMAGE };

extern unsigned int free_file_id;
extern unsigned int file_end;

extern unsigned int free_dir_id;
extern unsigned int directory_count;
extern unsigned int file_count;
extern unsigned int total_name_size;

extern unsigned int free_file_id;
extern unsigned int _entry_start;
extern unsigned int file_top;

extern int verbose;
extern Header header;
extern FILE *fNDS;
extern char *romlistfilename;
extern char *filemasks[MAX_FILEMASKS];
extern int filemask_num;
extern char *ndsfilename;
extern char *arm7filename;
extern char *arm9filename;
extern char *filerootdir;
extern char *overlaydir;
extern char *arm7ovltablefilename;
extern char *arm9ovltablefilename;
extern char *bannerfilename;
extern char *bannertext;
extern int bannertype;
extern char *headerfilename;
extern char *uniquefilename;
extern char *logofilename;
extern unsigned int arm9RamAddress;
extern unsigned int arm7RamAddress;
extern unsigned int arm9Entry;
extern unsigned int arm7Entry;
extern char *makercode;
extern char *gamecode;

--- NEW FILE: raster.h ---
#pragma pack(2)

typedef struct tagRGBQUAD
{
	unsigned char rgbBlue;
	unsigned char rgbGreen;
	unsigned char rgbRed;
	unsigned char rgbReserved;
} RGBQUAD;

typedef struct tagBITMAPINFOHEADER
{
	unsigned_int biSize;
	signed_int biWidth;
	signed_int biHeight;
	unsigned_short biPlanes;
	unsigned_short biBitCount;
	unsigned_int biCompression;
	unsigned_int biSizeImage;
	signed_int biXPelsPerMeter;
	signed_int biYPelsPerMeter;
	unsigned_int biClrUsed;
	unsigned_int biClrImportant;
} BITMAPINFOHEADER;

typedef struct tagBITMAPINFO
{
	BITMAPINFOHEADER bmiHeader;
	RGBQUAD bmiColors[256];
} BITMAPINFO, *PBITMAPINFO;

typedef struct tagBITMAPFILEHEADER
{
	unsigned char bfType[2];
	unsigned_int bfSize;
	unsigned_short bfReserved1;
	unsigned_short bfReserved2;
	unsigned_int bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;

#pragma pack()


struct CRaster
{
	unsigned int width, height;
	unsigned char *raster;
	RGBQUAD palette[256];

	int LoadBMP(char *filename);

	unsigned char * operator [] (unsigned int y)
	{
		return raster + y*width;
	}
};

--- NEW FILE: bigint.h ---
struct BigInt
{
	unsigned char data[129*2];	// 128*2 doesn't seem to be enough for 1024 bit keys

	int Sub(BigInt &a, BigInt &b, int b_shift);
	void MulMod(BigInt &a, BigInt &b, BigInt &m);
	void PowMod(BigInt &n, BigInt &m);
	void print();
	void Set(unsigned char *data, unsigned int length);
	void Get(unsigned char *data, unsigned int length);
};

--- NEW FILE: ndsextract.h ---
void ExtractFiles(char *ndsfilename);
void ExtractOverlayFiles();
void Extract(char *outfilename, bool indirect_offset, unsigned int offset, bool indirect_size, unsigned size, bool with_footer = false);

--- NEW FILE: crc.h ---
/*
	Cyclic Redundancy Code (CRC) functions
	by Rafael Vuijk (aka DarkFader)
*/

#ifndef __CRC_H
#define __CRC_H

//#include "little.h"		// FixCrc is not yet big endian compatible

/*
 * Data
 */
extern unsigned short crc16tab[];
extern unsigned long crc32tab[];

/*
 * Defines
 */
#define CRC_TEMPLATE	template <typename CrcType, CrcType *crcTable>

/*
 * CalcCrc
 * Does not perform final inversion.
 */
#define CalcCrc_	CalcCrc<CrcType, crcTable>
#define CalcCrc16	CalcCrc<typeof(*crc16tab), crc16tab>
#define CalcCrc32	CalcCrc<typeof(*crc32tab), crc32tab>
CRC_TEMPLATE inline CrcType CalcCrc(unsigned char *data, unsigned int length, CrcType crc = (CrcType)~0)
{
	for (unsigned int i=0; i<length; i++)
	{
		crc = (crc >> 8) ^ crcTable[(crc ^ data[i]) & 0xFF];
	}
	return crc;
}

/*
 * FCalcCrc
 * Does not perform final inversion.
 */
#define FCalcCrc_	FCalcCrc<CrcType, crcTable>
#define FCalcCrc16	FCalcCrc<typeof(*crc16tab), crc16tab>
#define FCalcCrc32	FCalcCrc<typeof(*crc32tab), crc32tab>
CRC_TEMPLATE inline CrcType FCalcCrc(FILE *f, unsigned int offset, unsigned int length, CrcType crc = (CrcType)~0)
{
	fseek(f, offset, SEEK_SET);
	for (unsigned int i=0; i<length; i++)
	{
		crc = (crc >> 8) ^ crcTable[(crc ^ fgetc(f)) & 0xFF];
	}
	return crc;
}

/*
 * RevCrc
 * Reverse table lookup.
 */
#define RevCrc_		RevCrc<CrcType, crcTable>
CRC_TEMPLATE inline unsigned char RevCrc(unsigned char x, CrcType *value = 0)
{
	for (int y=0; y<256; y++)
	{
		if ((crcTable[y] >> (8*sizeof(CrcType)-8)) == x)
		{
			if (value) *value = crcTable[y];
			return y;
		}
	}
	return 0;
}

/*
 * FixCrc
 */
#define FixCrc_		FixCrc<CrcType, crcTable>
#define FixCrc16	FixCrc<typeof(*crc16tab), crc16tab>
#define FixCrc32	FixCrc<typeof(*crc32tab), crc32tab>
CRC_TEMPLATE void FixCrc
(
	unsigned char *data,				// data to be patched
	unsigned int patch_offset, unsigned char *patch_data, unsigned int patch_length,	// patch data
	unsigned int fix_offset = 0,		// position to write the fix. by default, it is immediately after the patched data
	CrcType initial_crc = (CrcType)~0	// useful when manually calculating leading data
)
{
	if (!fix_offset) fix_offset = patch_offset + patch_length;

	// calculate CRC after leading data
	initial_crc = CalcCrc_(data, patch_offset, initial_crc);

	// calculate CRC before fix
	unsigned char buf[2*sizeof(CrcType)];
	CrcType crc_before_fix = CalcCrc_(data + patch_offset, fix_offset - patch_offset);
	*(CrcType *)(buf + 0) = crc_before_fix;

	// patch
	memcpy(data + patch_offset, patch_data, patch_length);

	// calculate CRC after unfixed
	CrcType crc_after_unfix = CalcCrc_(data + patch_offset, fix_offset - patch_offset + sizeof(CrcType));
	*(CrcType *)(buf + sizeof(CrcType)) = crc_after_unfix;

	// fix it
	for (int i=sizeof(CrcType); i>=1; i--)
	{
		CrcType value;
		unsigned char index = RevCrc_(buf[i + sizeof(CrcType) - 1], &value);
		*(CrcType *)(buf + i) ^= value;
		buf[i - 1] ^= index;
	}
	memcpy(data + fix_offset, buf, sizeof(CrcType));
}

/*
 * FFixCrc
 */
#define FFixCrc_	FFixCrc<CrcType, crcTable>
#define FFixCrc16	FFixCrc<typeof(*crc16tab), crc16tab>
#define FFixCrc32	FFixCrc<typeof(*crc32tab), crc32tab>
CRC_TEMPLATE void FFixCrc
(
	FILE *f,							// file to be patched
	unsigned int patch_offset, unsigned char *patch_data, unsigned int patch_length,	// patch data
	unsigned int fix_offset = 0,		// position to write the fix. by default, it is immediately after the patched data
	CrcType initial_crc = (CrcType)~0	// useful when manually calculating leading data
)
{
	if (!fix_offset) fix_offset = patch_offset + patch_length;

	// calculate CRC after leading data
	initial_crc = FCalcCrc_(f, 0, patch_offset, initial_crc);

	// calculate CRC before fix
	unsigned char buf[2*sizeof(CrcType)];
	CrcType crc_before_fix = FCalcCrc_(f, patch_offset, fix_offset - patch_offset);
	*(CrcType *)(buf + 0) = crc_before_fix;

	// patch
	fseek(f, patch_offset, SEEK_SET);
	fwrite(patch_data, 1, patch_length, f);

	// calculate CRC after unfixed
	CrcType crc_after_unfix = FCalcCrc_(f, patch_offset, fix_offset - patch_offset + sizeof(CrcType));
	*(CrcType *)(buf + sizeof(CrcType)) = crc_after_unfix;

	// fix it
	for (int i=sizeof(CrcType); i>=1; i--)
	{
		CrcType value;
		unsigned char index = RevCrc_(buf[i + sizeof(CrcType) - 1], &value);
		*(CrcType *)(buf + i) ^= value;
		buf[i - 1] ^= index;
	}
	fseek(f, fix_offset, SEEK_SET);
	fwrite(buf, sizeof(CrcType), 1, f);
}

#endif	// __CRC_H

--- NEW FILE: ndscreate.h ---
#include "ndstree.h"

void AddFile(char *rootdir, char *prefix, char *entry_name, unsigned int file_id, unsigned int alignmask);
void AddDirectory(Tree *tree, char *prefix, unsigned int this_dir_id, unsigned int _parent_id, unsigned int alignmask);
void Create();

--- NEW FILE: hook.h ---
void Hook(char *ndsfilename, char *arm7filename);

--- NEW FILE: sha1.h ---
/*
 ---------------------------------------------------------------------------
 Copyright (c) 2002, Dr Brian Gladman <brg at gladman.me.uk>, Worcester, UK.
 All rights reserved.

 LICENSE TERMS

 The free distribution and use of this software in both source and binary 
 form is allowed (with or without changes) provided that:

   1. distributions of this source code include the above copyright 
      notice, this list of conditions and the following disclaimer;

   2. distributions in binary form include the above copyright
      notice, this list of conditions and the following disclaimer
      in the documentation and/or other associated materials;

   3. the copyright holder's name is not used to endorse products 
      built using this software without specific written permission. 

 ALTERNATIVELY, provided that this notice is retained in full, this product
 may be distributed under the terms of the GNU General Public License (GPL),
 in which case the provisions of the GPL apply INSTEAD OF those given above.
 
 DISCLAIMER

 This software is provided 'as is' with no explicit or implied warranties
 in respect of its properties, including, but not limited to, correctness 
 and/or fitness for purpose.
 ---------------------------------------------------------------------------
Issue Date: 30/11/2002
*/

#ifndef _SHA1_H
#define _SHA1_H


typedef unsigned int sha1_32t;


#if defined(__cplusplus)
extern "C"
{
#endif

#define SHA1_BLOCK_SIZE  64
#define SHA1_DIGEST_SIZE 20
#define SHA2_GOOD         0
#define SHA2_BAD          1

/* type to hold the SHA256 context  */

typedef struct
{   sha1_32t count[2];
    sha1_32t hash[5];
    sha1_32t wbuf[16];
} sha1_ctx;

void sha1_compile(sha1_ctx ctx[1]);

void sha1_begin(sha1_ctx ctx[1]);
void sha1_hash(const unsigned char data[], unsigned int len, sha1_ctx ctx[1]);
void sha1_end(unsigned char hval[], sha1_ctx ctx[1]);
void sha1(unsigned char hval[], const unsigned char data[], unsigned int len);

#if defined(__cplusplus)
}
#endif

#endif	// _SHA1_H




More information about the dslinux-commit mailing list