summaryrefslogtreecommitdiff
path: root/cdutils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cdutils.cpp')
-rw-r--r--cdutils.cpp493
1 files changed, 491 insertions, 2 deletions
diff --git a/cdutils.cpp b/cdutils.cpp
index bba473a..230099c 100644
--- a/cdutils.cpp
+++ b/cdutils.cpp
@@ -20,6 +20,8 @@
#include <stdio.h>
#include <string.h>
#include <malloc.h>
+#include <stdlib.h>
+#include <unistd.h>
#include "cdutils.h"
#include "fileutils.h"
#include "generic.h"
@@ -86,6 +88,34 @@ FILE * open_ppf(char * ppf, FILE * iso, char * comment) {
return ppf_file;
}
+FILE * open_ppf(char * ppf, int iso, char * comment) {
+ int i, l;
+ char buffer[1024];
+
+ if (!(ppf_file = fopen(ppf, "w")))
+ return ppf_file;
+ fwrite("PPF20\001", 1, 6, ppf_file);
+
+ l = strlen(comment);
+ if (l >= 50) {
+ fwrite(comment, 1, 50, ppf_file);
+ } else {
+ char * t = " ";
+ fwrite(comment, 1, l, ppf_file);
+ for (i = l; i < 50; i++) {
+ fwrite(t, 1, 1, ppf_file);
+ }
+ }
+
+ l = filesize(iso);
+ fwrite(&l, 1, 4, ppf_file);
+
+ lseek(iso, 0x9320, SEEK_SET);
+ read(iso, buffer, 1024);
+ fwrite(buffer, 1, 1024, ppf_file);
+ return ppf_file;
+}
+
void write_ppf(unsigned char * old_sec, unsigned char * new_sec, int sec_num) {
int i, l = 0, o;
@@ -144,7 +174,23 @@ void sector_seek(FILE * f_iso, long sector) {
long curpos;
curpos = (2352 * (long) sector);
- fseek(f_iso, curpos, SEEK_SET);
+ if (fseek(f_iso, curpos, SEEK_SET)) {
+ printm(M_ERROR, "Unable to seek at %i\n", curpos);
+ exit(-1);
+ } else if (ftell(f_iso) != curpos) {
+ printm(M_ERROR, "Seek slipped when seeking at %i\n", curpos);
+ exit(-1);
+ }
+}
+
+void sector_seek(int f_iso, long sector) {
+ long curpos;
+
+ curpos = (2352 * (long) sector);
+ if (lseek(f_iso, curpos, SEEK_SET) != curpos) {
+ printm(M_ERROR, "Unable to seek at %i\n", curpos);
+ exit(-1);
+ }
}
int guess_type(FILE * f_iso, int number) {
@@ -174,6 +220,33 @@ int guess_type(FILE * f_iso, int number) {
return MODE_0;
}
+int guess_type(int f_iso, int number) {
+ unsigned char header[24];
+
+ if (number >= 0) {
+ sector_seek(f_iso, number);
+ }
+
+ read(f_iso, header, 24);
+ lseek(f_iso, -24, SEEK_CUR);
+ if (header[15] == 1) {
+ return MODE_1;
+ } else if (header[15] == 2) {
+ if (*((unsigned long *) &(header[16])) == *((unsigned long *) &(header[20]))) {
+ if ((header[16] == 0) && (header[17] == 0) && (header[19] == 0)) {
+ if (header[18] & 0x20) {
+ return MODE_2_FORM_2;
+ } else {
+ return MODE_2_FORM_1;
+ }
+ }
+ }
+ return MODE_2;
+ }
+
+ return MODE_0;
+}
+
long read_sector(FILE * f_iso, unsigned char * buffer, char type, int number) {
if (number >= 0) {
sector_seek(f_iso, number);
@@ -189,6 +262,21 @@ long read_sector(FILE * f_iso, unsigned char * buffer, char type, int number) {
return sec_sizes[type];
}
+long read_sector(int f_iso, unsigned char * buffer, char type, int number) {
+ if (number >= 0) {
+ sector_seek(f_iso, number);
+ }
+
+ if (type == GUESS) {
+ type = guess_type(f_iso, number);
+ }
+
+ lseek(f_iso, sec_offsts[type], SEEK_CUR);
+ read(f_iso, buffer, sec_sizes[type]);
+ lseek(f_iso, 2352 - sec_offsts[type] - sec_sizes[type], SEEK_CUR);
+ return sec_sizes[type];
+}
+
void read_datas(FILE * f_iso, unsigned char * buffer, int type, int number, long size) {
unsigned char sector[2352];
int i, n, reste;
@@ -211,6 +299,28 @@ void read_datas(FILE * f_iso, unsigned char * buffer, int type, int number, long
}
}
+void read_datas(int f_iso, unsigned char * buffer, int type, int number, long size) {
+ unsigned char sector[2352];
+ int i, n, reste;
+
+ if (type == GUESS) {
+ type = guess_type(f_iso, number);
+ }
+
+ n = size / sec_sizes[type];
+ reste = size - n * sec_sizes[type];
+
+ sector_seek(f_iso, number);
+ for (i = 0; i < n; i++) {
+ read_sector(f_iso, buffer + i * sec_sizes[type], type);
+ }
+
+ if (reste) {
+ read_sector(f_iso, sector, type);
+ bcopy((char *) sector, (char *) (buffer + n * sec_sizes[type]), reste);
+ }
+}
+
void read_file(FILE * f_iso, FILE * file, char type, int number, long size) {
unsigned char sector[2352];
int i, n, reste;
@@ -234,6 +344,29 @@ void read_file(FILE * f_iso, FILE * file, char type, int number, long size) {
}
}
+void read_file(int f_iso, int file, char type, int number, long size) {
+ unsigned char sector[2352];
+ int i, n, reste;
+
+ if (type == GUESS) {
+ type = guess_type(f_iso, number);
+ }
+
+ n = size / sec_sizes[type];
+ reste = size - n * sec_sizes[type];
+
+ sector_seek(f_iso, number);
+ for (i = 0; i < n; i++) {
+ read_sector(f_iso, sector, type);
+ write(file, sector, sec_sizes[type]);
+ }
+
+ if (reste) {
+ read_sector(f_iso, sector, type);
+ write(file, sector, reste);
+ }
+}
+
void write_sector(FILE * f_iso_r, FILE * f_iso_w, unsigned char * buffer, char type, int number) {
unsigned char old_sector[2352], new_sector[2352];
@@ -256,13 +389,42 @@ void write_sector(FILE * f_iso_r, FILE * f_iso_w, unsigned char * buffer, char t
bcopy((char *) buffer, (char *) new_sector + sec_offsts[type], sec_sizes[type]);
do_encode_L2(new_sector, type, 0);
- if (ppf_file < 0) {
+ if (!ppf_file) {
fwrite(new_sector, 1, 2352, f_iso_w);
} else {
write_ppf(old_sector, new_sector, number);
}
}
+void write_sector(int f_iso_r, int f_iso_w, unsigned char * buffer, char type, int number) {
+ unsigned char old_sector[2352], new_sector[2352];
+
+ if (type == GUESS) {
+ type = guess_type(f_iso_r, number);
+ }
+
+ if (number >= 0) {
+ sector_seek(f_iso_r, number);
+ sector_seek(f_iso_w, number);
+ }
+
+ read(f_iso_r, old_sector, 2352);
+
+ minute = old_sector[12];
+ second = old_sector[13];
+ frame = old_sector[14];
+
+ bcopy((char *) old_sector, (char *) new_sector, 2532);
+ bcopy((char *) buffer, (char *) new_sector + sec_offsts[type], sec_sizes[type]);
+
+ do_encode_L2(new_sector, type, 0);
+ if (!ppf_file) {
+ write(f_iso_w, new_sector, 2352);
+ } else {
+ write_ppf(old_sector, new_sector, number);
+ }
+}
+
void write_datas(FILE * f_iso_r, FILE * f_iso_w, unsigned char * buffer, char type, int number, long size) {
long nbsectors, i;
unsigned char sector[2352];
@@ -289,6 +451,32 @@ void write_datas(FILE * f_iso_r, FILE * f_iso_w, unsigned char * buffer, char ty
}
}
+void write_datas(int f_iso_r, int f_iso_w, unsigned char * buffer, char type, int number, long size) {
+ long nbsectors, i;
+ unsigned char sector[2352];
+
+ if (type == GUESS) {
+ type = guess_type(f_iso_r, number);
+ }
+
+ if (number >= 0) {
+ sector_seek(f_iso_r, number);
+ sector_seek(f_iso_w, number);
+ }
+
+ nbsectors = size / sec_sizes[type];
+
+ for (i = 0; i < nbsectors; i++) {
+ write_sector(f_iso_r, f_iso_w, buffer + i * sec_sizes[type], type, number + i);
+ }
+
+ if (size % sec_sizes[type]) {
+ memset(sector, 0, 2352);
+ bcopy((char *) (buffer + i * sec_sizes[type]), (char *) sector, size % sec_sizes[type]);
+ write_sector(f_iso_r, f_iso_w, sector, type, number + i);
+ }
+}
+
void write_file(FILE * f_iso_r, FILE * f_iso_w, FILE * file, char type, int number) {
long size, nbsectors, i;
unsigned char buffer[2352];
@@ -316,6 +504,33 @@ void write_file(FILE * f_iso_r, FILE * f_iso_w, FILE * file, char type, int numb
}
}
+void write_file(int f_iso_r, int f_iso_w, int file, char type, int number) {
+ long size, nbsectors, i;
+ unsigned char buffer[2352];
+
+ if (type == GUESS) {
+ type = guess_type(f_iso_r, number);
+ }
+
+ if (number >= 0) {
+ sector_seek(f_iso_r, number);
+ sector_seek(f_iso_w, number);
+ }
+
+ size = filesize(file);
+ nbsectors = size / sec_sizes[type];
+
+ if (size % sec_sizes[type]) {
+ nbsectors++;
+ }
+
+ for (i = 0; i < nbsectors; i++) {
+ memset(buffer, 0, 2352);
+ read(file, buffer, sec_sizes[type]);
+ write_sector(f_iso_r, f_iso_w, buffer, type);
+ }
+}
+
void show_head_entry(void) {
printm(M_BARE, "Sector - Size - Date - Time - Flags - Name\n");
}
@@ -356,6 +571,26 @@ int show_dir(FILE * f_iso, struct DirEntry * dir) {
return 1;
}
+int show_dir(int f_iso, struct DirEntry * dir) {
+ unsigned int ptr;
+ unsigned char * buffer;
+
+ if (!(dir->Flags & 2)) {
+ return 0;
+ }
+
+ buffer = (unsigned char *) malloc(dir->Size);
+ read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size);
+
+ ptr = 0;
+ while(ptr < dir->Size) {
+ ptr += show_entry((struct DirEntry *) &(buffer[ptr]));
+ }
+
+ free(buffer);
+ return 1;
+}
+
struct DirEntry find_dir_entry(FILE * f_iso, struct DirEntry * dir, char * name) {
unsigned int ptr, size;
unsigned char * buffer;
@@ -385,6 +620,35 @@ struct DirEntry find_dir_entry(FILE * f_iso, struct DirEntry * dir, char * name)
return r;
}
+struct DirEntry find_dir_entry(int f_iso, struct DirEntry * dir, char * name) {
+ unsigned int ptr, size;
+ unsigned char * buffer;
+ struct DirEntry r = {0, 0, 0, 0, 0};
+
+ if (!(dir->Flags & 2)) {
+ return r;
+ }
+
+ buffer = (unsigned char *) malloc(size = dir->Size);
+ read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size);
+
+ ptr = 0;
+ while(ptr < size) {
+ dir = (struct DirEntry *) &(buffer[ptr]);
+ if (!dir->R) {
+ ptr++;
+ } else {
+ if (!strncmp(name, (char *) &(dir->id), dir->N)) {
+ r = *dir;
+ }
+ ptr += dir->R;
+ }
+ }
+
+ free(buffer);
+ return r;
+}
+
struct DirEntry * find_dir_entry(FILE * f_iso, unsigned char ** bufout, struct DirEntry * dir, char * name) {
unsigned int ptr, size;
unsigned char * buffer;
@@ -419,6 +683,40 @@ struct DirEntry * find_dir_entry(FILE * f_iso, unsigned char ** bufout, struct D
return rdir;
}
+struct DirEntry * find_dir_entry(int f_iso, unsigned char ** bufout, struct DirEntry * dir, char * name) {
+ unsigned int ptr, size;
+ unsigned char * buffer;
+ struct DirEntry * rdir = 0;
+ *bufout = 0;
+
+ if (!(dir->Flags & 2)) {
+ return 0;
+ }
+
+ buffer = (unsigned char *) malloc(size = dir->Size);
+ read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size);
+
+ ptr = 0;
+ while(ptr < size) {
+ dir = (struct DirEntry *) &(buffer[ptr]);
+ if (!dir->R) {
+ ptr++;
+ } else {
+ if (!strncmp(name, (char *) &(dir->id), dir->N)) {
+ rdir = dir;
+ }
+ ptr += dir->R;
+ }
+ }
+
+ if (rdir->R) {
+ *bufout = buffer;
+ } else {
+ free(buffer);
+ }
+ return rdir;
+}
+
int show_iso_infos(FILE * f_iso) {
unsigned char buffer[2048];
char pbuff[130];
@@ -491,6 +789,78 @@ int show_iso_infos(FILE * f_iso) {
return 1;
}
+int show_iso_infos(int f_iso) {
+ unsigned char buffer[2048];
+ char pbuff[130];
+ short int s, nogood = 0;
+
+ read_sector(f_iso, buffer, GUESS, 16);
+
+ printm(M_BARE, "Sector guessed mode : %s\n", sec_modes[guess_type(f_iso, 16)]);
+ printm(M_BARE, "offset-size-info : contents\n");
+ printm(M_BARE, " 0 - 1- 1 : %i\n", buffer[0]);
+ memcpy(pbuff, buffer + 1, 5);
+ pbuff[5] = 0;
+ printm(M_BARE, " 1 - 5-`CD001' : %s\n", pbuff);
+ printm(M_BARE, " 6 - 2- 1 : %i\n", s = *((short int *) &(buffer[6])));
+ printm(M_BARE, "(*this was the signature*)\n");
+ if (buffer[0] != 1) nogood = 1;
+ if (strcmp(pbuff, "CD001")) nogood = 1;
+ if (s != 1) nogood = 1;
+
+ if (nogood) {
+ printm(M_BARE, "Not a valid iso9660 file.\n");
+ return 0;
+ }
+
+ memcpy(pbuff, buffer + 8, 32);
+ pbuff[32] = 0;
+ printm(M_BARE, " 8 - 32- SYSID : %s\n", pbuff);
+ memcpy(pbuff, buffer + 40, 32);
+ pbuff[32] = 0;
+ printm(M_BARE, " 40 - 32- VOLID : %s\n", pbuff);
+ printm(M_BARE, " 80 - 8- SNum : %li\n", *((long int *) &(buffer[80])));
+ printm(M_BARE, " 120 - 4- VOLSiz : %i\n", *((short int *) &(buffer[120])));
+ printm(M_BARE, " 124 - 4- VOLNum : %i\n", *((short int *) &(buffer[124])));
+ printm(M_BARE, " 128 - 4- SSize : %i\n", *((short int *) &(buffer[128])));
+ printm(M_BARE, " 132 - 8- PSize : %li\n", *((long int *) &(buffer[132])));
+ printm(M_BARE, " 140 - 4- 1SLPath: %i\n", *((short int *) &(buffer[140])));
+ printm(M_BARE, " 144 - 4- 2SLPath: %i\n", *((short int *) &(buffer[144])));
+ printm(M_BARE, " 148 - 4- 1SBPath: %i\n", swap_dword(*((short int *) &(buffer[148]))));
+ printm(M_BARE, " 152 - 4- 2SBPath: %i\n", swap_dword(*((short int *) &(buffer[152]))));
+ memcpy(pbuff, buffer + 190, 128);
+ pbuff[128] = 0;
+ printm(M_BARE, " 190 - 128- VStId : %s\n", pbuff);
+ memcpy(pbuff, buffer + 318, 128);
+ pbuff[128] = 0;
+ printm(M_BARE, " 318 - 128- PubId : %s\n", pbuff);
+ memcpy(pbuff, buffer + 446, 128);
+ pbuff[128] = 0;
+ printm(M_BARE, " 446 - 128- DPrId : %s\n", pbuff);
+ memcpy(pbuff, buffer + 574, 128);
+ pbuff[128] = 0;
+ printm(M_BARE, " 574 - 128- AppId : %s\n", pbuff);
+ memcpy(pbuff, buffer + 702, 37);
+ pbuff[37] = 0;
+ printm(M_BARE, " 702 - 37- CpyFile: %s\n", pbuff);
+ memcpy(pbuff, buffer + 739, 37);
+ pbuff[37] = 0;
+ printm(M_BARE, " 739 - 37- AbsFile: %s\n", pbuff);
+ memcpy(pbuff, buffer + 776, 37);
+ pbuff[37] = 0;
+ printm(M_BARE, " 776 - 37- BibFile: %s\n", pbuff);
+ printm(M_BARE, " 813 - 17- DTCreat: %s\n", format_date(&buffer[813]));
+ printm(M_BARE, " 830 - 17- DTModif: %s\n", format_date(&buffer[830]));
+ printm(M_BARE, " 847 - 17- DTExpir: %s\n", format_date(&buffer[847]));
+ printm(M_BARE, " 864 - 17- DTEffec: %s\n", format_date(&buffer[864]));
+
+ printm(M_BARE, "Root record:\n");
+ show_head_entry();
+ show_entry((DirEntry *) &(buffer[156]));
+
+ return 1;
+}
+
int get_iso_infos(FILE * f_iso) {
unsigned char buffer[2048];
char pbuff[130];
@@ -519,6 +889,34 @@ int get_iso_infos(FILE * f_iso) {
return 1;
}
+int get_iso_infos(int f_iso) {
+ unsigned char buffer[2048];
+ char pbuff[130];
+ short int s, nogood = 0;
+
+ read_sector(f_iso, buffer, GUESS, 16);
+
+ memcpy(pbuff, buffer + 1, 5);
+ pbuff[5] = 0;
+
+ s = *((short int *) &(buffer[6]));
+ if (buffer[0] != 1) nogood = 1;
+ if (strcmp(pbuff, "CD001")) nogood = 1;
+ if (s != 1) nogood = 1;
+
+ if (nogood) {
+ printm(M_ERROR, "Not a valid iso9660 file.\n");
+ return 0;
+ }
+
+ pt1 = *((short int *) &(buffer[140]));
+ pt2 = *((short int *) &(buffer[144]));
+ snum = *((int *) &(buffer[80]));
+ ptl = *((long int *) &(buffer[132]));
+ rootDir = *((struct DirEntry *) &(buffer[156]));
+ return 1;
+}
+
int get_pt_infos(FILE * f_iso) {
unsigned char * buffer;
@@ -575,6 +973,35 @@ int show_pt_infos(FILE * f_iso) {
return 1;
}
+int show_pt_infos(int f_iso) {
+ unsigned char * buffer;
+ char pbuf[100];
+ int i, ptr;
+
+ if ((pt1 <= 0) && (pt2 <= 0))
+ if (!get_iso_infos(f_iso))
+ return 0;
+
+ if ((!pt1) & (!pt2)) {
+ printm(M_ERROR, "No path table defined.\n");
+ return 0;
+ }
+
+ buffer = (unsigned char *) malloc(ptl);
+ read_datas(f_iso, buffer, GUESS, !pt1 ? pt2 : pt1, ptl);
+
+ printm(M_BARE, "node^paren@sector : name\n");
+ for (ptr = 0, i = 1; buffer[ptr]; ptr += ptr & 1, i++) {
+ strncpy(pbuf, (char *) &(buffer[8 + ptr]), buffer[ptr]);
+ pbuf[buffer[ptr]] = 0;
+ printm(M_BARE, "%3i ^ %3i @ %6i: %s\n", i, *((unsigned short *) &(buffer[6 + ptr])), *((unsigned long *) &(buffer[2 + ptr])), pbuf);
+ ptr += 8 + buffer[ptr];
+ }
+
+ free(buffer);
+ return 1;
+}
+
struct DirEntry find_path(FILE * f_iso, char * path) {
char ** pts = split(path, '/');
struct DirEntry dir = {0, 0, 0, 0, 0};
@@ -602,6 +1029,33 @@ struct DirEntry find_path(FILE * f_iso, char * path) {
return dir;
}
+struct DirEntry find_path(int f_iso, char * path) {
+ char ** pts = split(path, '/');
+ struct DirEntry dir = {0, 0, 0, 0, 0};
+
+ if ((pt1 <= 0) && (pt2 <= 0))
+ if (!get_iso_infos(f_iso))
+ return dir;
+
+ if ((!pt1) & (!pt2)) {
+ printm(M_ERROR, "No path table defined.\n");
+ return dir;
+ }
+
+ if (!**pts)
+ pts++;
+
+ for (dir = rootDir; *pts; pts++) {
+ if (!strlen(*pts))
+ continue;
+ dir = find_dir_entry(f_iso, &dir, *pts);
+ if (!dir.R)
+ return dir;
+ }
+
+ return dir;
+}
+
struct DirEntry find_parent(FILE * f_iso, char * path) {
char ** pts, * p;
struct DirEntry dir = {0, 0, 0, 0, 0};
@@ -636,3 +1090,38 @@ struct DirEntry find_parent(FILE * f_iso, char * path) {
return dir;
}
+
+struct DirEntry find_parent(int f_iso, char * path) {
+ char ** pts, * p;
+ struct DirEntry dir = {0, 0, 0, 0, 0};
+
+ if ((p = strchr(path, '/'))) {
+ *p = 0;
+ }
+ if (!*path) {
+ return rootDir;
+ }
+ pts = split(path, '/');
+
+ if ((pt1 <= 0) && (pt2 <= 0))
+ if (!get_iso_infos(f_iso))
+ return dir;
+
+ if ((!pt1) & (!pt2)) {
+ printm(M_ERROR, "No path table defined.\n");
+ return dir;
+ }
+
+ if (!**pts)
+ pts++;
+
+ for (dir = rootDir; *pts; pts++) {
+ if (!strlen(*pts))
+ continue;
+ dir = find_dir_entry(f_iso, &dir, *pts);
+ if (!dir.R)
+ return dir;
+ }
+
+ return dir;
+}