 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sat Jul 18, 2009 6:20 am Post subject: Problem with code |
|
|
Hey everyone
I have a problem with a code i made to pack different files into one. Well, its not about the packing, its about reading the data on the file. Awhile back, J.F. told me that the bytes might need to be swap in order for it to be read properly. Well I done all i could - Including remaking the reading part of the code, but no luck. I am hoping that someone here can help me with this code.
| Code: |
int pshnLoadSection(FILE *fd){
int count = 0, cmd, len, size = 0;
char *buf;
if(ftell(fd) != 0){
fseek(fd, 1, SEEK_CUR);
do{
//cmd = 0;
fread(&cmd, 1, 4, fd);printf("%d %d\n", cmd, ftell(fd));
if(cmd <= 5){
printf("Command 0x%02x found at offset %d\n", cmd, ftell(fd));
if(cmd == 0x01){
printf("Found section\n");
fread(&len, 1, 4, fd);
buf = malloc(len);
fread(buf, 1, len, fd);
section[count].section = triplecryptReverse(buf);
free(buf);
len = 0;
}
if(cmd == 0x02){
count += 1;
}
if(cmd == 0x03){
printf("Filename found\n");
fread(&len, 1, 4, fd);
section[count].filename = malloc(len);
fread(section[count].filename, 1, len, fd);
printf("Filename: %s\n", section[count].filename);
}
}
}while(ftell(fd) != 0 && cmd != 0xFFFFFFFF && cmd < 5);
}
return 0;
}
|
The variable "cmd" is what use to read the different commands. Idk if its the loop, or what. If you remove "cmd != 0xFFFFFFFF && cmd < 5" then it will go into a complete loop. It anyone need me to post my full source, then I will. It will be greatfully helpful if anyone can help me solve this problem.
Thanks _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sat Jul 18, 2009 8:53 am Post subject: |
|
|
| You STILL haven't mentioned what the source of the data is - what format it's supposed to be in, nor what machine it is produced on. Without the info, there's no way to know how the data should be parsed by the PSP. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sat Jul 18, 2009 8:59 am Post subject: |
|
|
What you've posted so far looks like it will probably work, given a correctly formatted input stream and allocation of 'section'.
So, possibilities are
1) a mismatch between loader and saver
2) problem with the 'section' object
3) JF is probably saying that where you read 4 bytes into the cmd or len variables that the byte order might not be right - depends what system you are using to do the packing.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sat Jul 18, 2009 11:31 am Post subject: |
|
|
Im using windows xp.
Jim, what do you think I should do? I dont know if my computer is a big endian PC, but could it be the problem? Should I post my whole code to see if there's any problem? Its made for both the PC and PSP. _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sat Jul 18, 2009 1:40 pm Post subject: |
|
|
| If it works on an x86 CPU based PC, then it won't be an endian problem on the PSP since they are both little endian. |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sat Jul 18, 2009 3:45 pm Post subject: |
|
|
Then why does it read large bytes then it should be reading small bytes on the PC?
I have a x86 PC. Like I told you before J.F., the packing works, its just the reading/loading part _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sat Jul 18, 2009 10:02 pm Post subject: |
|
|
How do you know that if you can't unpack it?
If you post more code it might be possible to see the problem. And/or pack a couple of 1 byte files and post the hex for the packed file?
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sat Jul 18, 2009 10:29 pm Post subject: |
|
|
here is my whole code
pshnimg.c
| Code: | #include "pshn-img.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
//#ifdef WORDS_BIGENDIAN
// Swap the bytes of an int for big-endian machines
static int swap_int(int n)
{
return ((n>>24)&0xff)|((n>>8)&0xff00)|((n<<8)&0xff0000)|((n<<24)&0xff000000);
}
//#endif
int fg;
/*
In future updates, the sections will have there own headers for the title, version, and data count.
and within the future in version 1.05, everything such as file sections, and plan sections will have there own header
*/
char *hexdec(char *str){
char *string = malloc(sizeof(str));
int i;
for(i=0;i<strlen(str);i++){
sprintf(string, "%02x", str[i]);
}
return string;
};
int verifyHeader(void){
char signature[]={0x50, 0x53, 0x48, 0x49, 0x4D, 0x47};
if(!memcmp(header.signature, signature, 6)){
return 1;
}
return 0;
}
void swapints(int *array, int ndx1, int ndx2)
{
int temp = array[ndx1];
array[ndx1] = array[ndx2];
array[ndx2] = temp;
}
char *crypt(const char *pszText, int iTextLen, const char *pszKey)
{
char *cipher; /* Output buffer */
int a, b, i=0, j=0, k; /* Ambiguously named counters */
int ilen; /* Length of a string */
int sbox[256]; /* Encryption array */
int key[256]; /* Numeric key values */
ilen = strlen(pszKey);
for (a=0; a < 256; a++)
{
key[a] = pszKey[a % ilen];
sbox[a] = a;
}
for (a=0, b=0; a < 256; a++)
{
b = (b + sbox[a] + key[a]) % 256;
swapints(sbox, a, b);
}
cipher = (char *)malloc(iTextLen);
for (a=0; a < iTextLen; a++)
{
i = (i + 1) % 256;
j = (j + sbox[i]) % 256;
swapints(sbox, i, j);
k = sbox[(sbox[i] + sbox[j]) % 256];
cipher[a] = pszText[a] ^ k;
}
return cipher;
}
char * triplecrypt(char *data){
char *string = malloc(sizeof(data));
string = crypt(data, strlen(data), header.keyset_1);
string = crypt(string, strlen(data), header.keyset_2);
string = crypt(string, strlen(data), header.keyset_3);
return string;
}
char * triplecryptReverse(char *data){
char *string = malloc(sizeof(data));
string = crypt(data, strlen(data), header.keyset_3);
string = crypt(string, strlen(data), header.keyset_2);
string = crypt(string, strlen(data), header.keyset_1);
return string;
}
//#ifdef PC_ENABLE
int pshnInit(FILE *fd, int flag){
switch(flag){
case 0://read
fread(&header, 1, sizeof(header), fd);
return verifyHeader();
case 1://write
fg = 1;
//memset(&header, 0, sizeof(header));
char signature[]={0x50, 0x53, 0x48, 0x49, 0x4D, 0x47};
int i;
for(i=0;i<6;i++){
header.signature[i] = signature[i];
}
header.version[0] = 0x01;
return 1;
break;
}
return 0;
}
int pshnAddOptional(FILE *fd, char firmware[4], char title[32], int mode,int key, int seed, int bytecode, int compress){
memcpy(header.firmware, firmware, 4);
memcpy(header.title, title, strlen(title));
header.mode = mode;
if(key == 1){
header.key = 1;
pshnGenerateKeyset(seed+0x4C, header.keyset_1);
pshnGenerateKeyset(seed+0x3C, header.keyset_2);
pshnGenerateKeyset(seed+0x2C, header.keyset_3);
}
header.bytecode = bytecode; //0 = nulll for now - bytecode use to execute commands built with your library
header.compress = compress; //0 = null for now - compress use to compress files to a smaller rate for easyier storage
fwrite(&header, 1, sizeof(header), fd);
return 0;
}
int pshnGenerateKeyset(int seed, char key[128]){
srand(seed+128);
int i;
for(i=0;i<128;i++){
key[i] = rand() % 255;
key[i] ^= seed & 128;
}
return 0;
}
int pshnAddSection(FILE *fd, char *section){
int secmd = 0x01;
fseek(fd, 1, SEEK_CUR);
fwrite(&secmd, 1, 4, fd);
char *sC = malloc(sizeof(section));
sC = crypt(section, strlen(section), header.keyset_1);
sC = crypt(sC, strlen(sC), header.keyset_2);
sC = crypt(sC, strlen(sC), header.keyset_3);
int len = strlen(sC);
fwrite(&len, 1, 4, fd);
fwrite(sC, 1, len, fd);
return len;
}
int pshnAddFile(FILE *fd, char *file){
int filecmd = 0x03;
fseek(fd, 1, SEEK_CUR);
fwrite(&filecmd, 1, 4, fd);
int len = strlen(file);
fwrite(&len, 1, 4, fd);
fwrite(file,1, len, fd);
char *buf;
FILE *in = fopen(file, "rb");
fseek(in, 0, SEEK_END);
int size = ftell(in);
fseek(in, 0, SEEK_SET);
buf = malloc(size);
fread(buf, 1, size, in);
fclose(in);
fwrite(&size, 1, 4, fd);
buf = crypt(buf, size, header.keyset_3);
fwrite(buf, 1, size, fd);
int endsec = 0x04;
fwrite(&endsec, 1, 4, fd);
return 0;
}
int pshnLoadSection(FILE *fd){
int count = 0, cmd, len, size = 0;
char *buf;
if(ftell(fd) != 0){
do{
fseek(fd, 1, SEEK_CUR);
//cmd = 0;
fread(&cmd, 1, 4, fd);
if(cmd <= 5){
printf("Command 0x%02x found at offset 0x%02x\n", cmd, ftell(fd));
if(cmd == 0x01){
printf("Found section\n");
fread(&len, 1, 4, fd);
buf = malloc(len);
fread(buf, 1, len, fd);
section[count].section = triplecryptReverse(buf);
free(buf);
len = 0;
printf("Section Name: %s\n", section[count].section);
count += 1;
cmd = 0;
}
if(cmd == 0x03){
printf("Filename found\n");
fread(&len, 1, 4, fd);
section[count].filename = malloc(len);
fread(section[count].filename, 1, len, fd);
printf("Filename: %s\n", section[count].filename);
cmd = 0;
}
}
}while(ftell(fd) != 0 && cmd != 0xFFFFFFFF && cmd < 5);
printf("PSHN Section Reader Exits\n");
}
return 0;
}
int pshnTerm(FILE *fd){
if(fg == 1){
int end = 0xFFFFFFFF;
fwrite(&end, 1, 4, fd);
}
return fclose(fd);
}
|
pshnimg.h
| Code: |
#include <stdio.h>
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
struct {
char signature[6];//PSHIMG
char version[4];//1.00
char firmware[4];//5.00 fw or use 0x00000000 for using on any firmware
char title[32];//128 byte Title of the image (for the game/app)
int mode; //user = 0, kernel = 0x1000 (use for future purposes only)
int key;//0 for no encryption key, 1 for usage of encryption key (XOR)
//keysets for encryption/decryption purpose only (xor encryption for now)
char keyset_1[128];//keyset 1 - 128 bytes
char keyset_2[128];//keyset 2 - 128 bytes
char keyset_3[128];//keyset 3 - 128 bytes
u8 hash[20];//hash of the data after the header
u8 enHash[20];//hash that was encrypted via xor 3 times using all 3 keys (will be NULL if header.key = 0)
int bytecode;//enable bytecode to use a bytecode like formate to parse and execute with your own code (only on 1.01)
int compress;//enable compression using zlib. 1 enable, else dont (only on 1.01)
int size;//size of the data after the header;
} header;
struct section_data {
char *section;//use to verify what files go where
char *filename;
char *buf;
int size;
};
struct var {
char *variable;
char *content;
};
struct section_data section[];
struct var storage[];
int pshnInit(FILE *fd, int flag);
int pshnAddOptional(FILE *fd, char firmware[4], char title[128], int mode,int key, int seed, int bytecode, int compress);
int pshnGenerateKeyset(int seed, char key[128]);
int pshnAddSection(FILE *fd, char *section);
int pshnEndSection(FILE *fd);
int pshnAddFile(FILE *fd, char *file);
int pshnAddByteCode(char bytecode[4], int prem, int (*handler)(int arg[4], void *data));
// int pshnWriteByteCode(FILE *fd, char bytecode[4], int prem, int arg[4], void *data);
int pshnAddVariable(char *var, void *content);
int pshnLoadSection(FILE *fd);
int pshnTerm(FILE *fd);
|
_________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ?
Last edited by Dariusc123456 on Sun Jul 19, 2009 2:06 am; edited 1 time in total |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sun Jul 19, 2009 2:00 am Post subject: |
|
|
Here is a testing file that have 1 section and 1 files (28 bytes) within it.
This is in hex
| Code: | | 50 53 48 49 4D 47 01 00 00 00 05 00 00 00 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 6F A1 68 1A 5C 6F DF 2E 9D 9D D4 A1 7F C8 5C 2E 4A BB 5B C4 FB 97 52 F7 25 36 9B 42 D6 CA 08 9A D2 4D AF 4C 24 CF A0 6A FE B3 E9 FE 0C 4A 75 68 FC 0F 72 93 90 8B 30 AD 95 1F 79 77 DA 0B A9 10 BF 88 5A 94 0A 65 27 19 D7 A4 03 46 5F 60 45 EA 3A CF 52 E2 DF 5F 72 9B 32 0D 54 40 F1 14 59 CA A8 81 BA 3F 12 69 CF BB DE 8E C4 E1 83 01 86 AC 6C 5B 25 95 EC 06 84 E2 F5 6A C8 9B 22 58 84 CC F1 AC A6 A5 0E 06 DE 59 1F 23 AA 05 43 00 9F 63 69 73 04 E5 E1 18 B4 88 FE D2 4D A2 3B F7 5F 8E EB E8 A5 81 4F F2 EC 33 A3 28 8E 4D D3 FE 3A D4 F4 2F 98 5B 85 E5 11 B4 E1 2C 55 1B 3B 29 B3 40 66 9C AC F0 A6 8E F1 BB 77 CA 33 B3 1B EC 12 4C F2 3A B7 F0 03 A3 F6 8A 23 8F 0C 8A 29 99 C5 D6 26 4F F7 BF 3A 01 30 FD 38 0A 94 42 59 74 DB 5D 7E 5A 63 81 E5 B9 26 83 A0 26 9A E0 95 BB F6 93 AC B7 15 F0 C7 9C 6D 84 A0 22 C0 25 47 7D A2 DB 48 2B 68 07 CB 98 D6 0D D8 6F FE CE 9F 25 29 1C 34 8F E7 B6 67 02 BD 77 30 0A 01 3A 4D 0E F1 CD 2C A8 AA E0 A5 B4 DB FB 54 3F 45 98 0B 2C 22 59 C3 EA 7F 91 C0 66 FA FA 75 DF 73 9B 0C 48 E0 7C 2D DD 1E F2 E5 F2 E5 6B FC 10 8F 5A FD C4 D4 14 23 06 52 A6 EF 35 82 B7 A7 33 C7 F2 0C C1 ED BA 0B 66 09 F1 5D B0 BC DC 6A AF 1B B8 A4 04 56 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 00 00 00 08 C2 68 38 00 03 00 00 00 08 00 00 00 74 65 73 74 2E 74 78 74 1C 00 00 00 EF 26 34 CB 9D 37 EB F5 38 2F A4 00 B2 E7 04 98 11 7C D4 84 1B B4 6A DF C3 C2 1B 1C FF FF FF FF |
_________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sun Jul 19, 2009 2:14 am Post subject: |
|
|
I think i got it working, but im not really sure. It reads the filename, but the size it read 4 bytes for each file in the package. _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sun Jul 19, 2009 10:16 am Post subject: |
|
|
| Code: |
char * triplecryptReverse(char *data){
char *string = malloc(sizeof(data));
string = crypt(data, strlen(data), header.keyset_3);
string = crypt(string, strlen(data), header.keyset_2);
string = crypt(string, strlen(data), header.keyset_1);
return string;
}
|
sizeof(data) will be 4 since data is a pointer. You need to pass in the length. strlen(data) will be the length of the data up to the first 0. I'm positive that's not what you want. These two functions are horridly broken and are tramping over memory.
Since this encryption offers no security at all, you should try removing it and seeing if that fixes some of your problems.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sun Jul 19, 2009 1:38 pm Post subject: |
|
|
When I started out ont hsi project, I didnt use RC4 encryption. I use XOR or use use no encryption. But i was thinking of compressing the data, but i dont know the affect it will cause on the data it self.
I got everything fix up now abit now. Is there away to avoid memory leakage? _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sun Jul 19, 2009 2:16 pm Post subject: |
|
|
| Dariusc123456 wrote: | | Is there away to avoid memory leakage? |
Keep better track of your memory. :) |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sun Jul 19, 2009 2:28 pm Post subject: |
|
|
| J.F. wrote: | | Dariusc123456 wrote: | | Is there away to avoid memory leakage? |
Keep better track of your memory. :) | I do, and will. _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sun Jul 19, 2009 9:10 pm Post subject: |
|
|
Glad it's working. The encryption you are using isn't that great. Amongst the problems are
1) the algorithm isn't that hot.
2) applying a bad algorithm 3 times doesn't make it any more secure. Applying a *good* algorithm 3 times doesn't necessarily make it any more secure.
3) even if your algorithm was good you are publishing all your keys in the header of your file! Anyone with the source you just posted can decrypt any of your files.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Dariusc123456
Joined: 12 Aug 2008 Posts: 394
|
Posted: Sun Jul 19, 2009 11:58 pm Post subject: |
|
|
Ha, who ever said i would keep the algorithm? Ill be using AES-128 & RSA very soon with this, but for now, its no problem.
Jim, all I have todo is change how things operate, but right now, nothing is not much of a threat because every file I make will be compress, encrypted, and signed. So even if someone was able to decrypt it, its no way of encrypting it without the private keys. _________________ PSHN - Playstation Hacking Network
PSX/PS1 - HACK - Game Shark
PS2 - HACK - Swap
PSP - HACK - Pandora
PS3 - ? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|