Read directory and files from CDROM

This commit is contained in:
Laurent Vivier 2005-11-12 19:12:13 +00:00
parent 0cad8c46bb
commit 7d9746cf92
11 changed files with 729 additions and 0 deletions

62
libiso9660/Makefile Normal file
View File

@ -0,0 +1,62 @@
#
# (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
#
TOP = $(shell pwd)
CFLAGS = -nostdlib -nodefaultlibs -Wall -Werror -Wno-multichar -fpic
CPPFLAGS = -I$(TOP) -I$(TOP)/../libmacos -DARCH_M68K
LIBRARY = libiso9660.a
SOURCES = iso9660_mount.c iso9660_opendir.c \
iso9660_closedir.c iso9660_readdir.c \
iso9660_is_directory.c iso9660_open.c \
iso9660_read.c iso9660_close.c
HEADERS = libiso9660.a
DISTFILES = $(SOURCES) $(HEADERS)
OBJS = $(patsubst %.S,%.o,$(SOURCES:.c=.o))
all: $(LIBRARY)
$(LIBRARY): $(OBJS)
$(AR) rc $@ $^
install: install-headers install-lib
install-lib:
install -d $(DESTDIR)/$(PREFIX)/usr/lib/
install $(LIBRARY) $(DESTDIR)/$(PREFIX)/usr/lib/$(LIBRARY)
install-headers:
for file in $(HEADERS); do \
dir=$$(dirname $$file); \
if [ "$$dir" != "" ] ; then \
mkdir -p $(DESTDIR)/$(PREFIX)/usr/include/$$dir; \
fi; \
cp -p $$file $(DESTDIR)/$(PREFIX)/usr/include/$$file; \
done
uninstall: uninstall-lib uninstall-headers
uninstall-lib:
rm -f $(DESTDIR)/$(PREFIX)/usr/lib/$(LIBRARY)
uninstall-headers:
for file in $(HEADERS); do \
rm -f $(DESTDIR)/$(PREFIX)/usr/include/$$file; \
done
dist:
for file in $(DISTFILES); do \
dir=$$(dirname $$file); \
if [ "$$dir" != "" ] ; then \
mkdir -p $(DISTDIR)/libunix/$$dir; \
fi; \
cp -p $$file $(DISTDIR)/libunix/$$file; \
done
clean:
rm -f $(OBJS) $(LIBRARY)

View File

@ -0,0 +1,14 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include "libiso9660.h"
void iso9660_close(iso9660_FILE *file)
{
free(file);
}

View File

@ -0,0 +1,19 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include "libiso9660.h"
int iso9660_closedir(iso9660_DIR *dir)
{
if (dir == NULL)
return -1;
free(dir);
return 0;
}

View File

@ -0,0 +1,14 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include "libiso9660.h"
void iso9660_dir_close(iso9660_DIR *dir)
{
free(dir);
}

View File

@ -0,0 +1,14 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include "libiso9660.h"
int iso9660_is_directory(struct iso_directory_record * idr)
{
return ((idr->flags[0] & 2) != 0 &&
(idr->name_len[0] != 1 ||
(idr->name[0] != 0 && idr->name[0] != 1)));
}

313
libiso9660/iso9660_mount.c Normal file
View File

@ -0,0 +1,313 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
* some parts from mkisofs (c) J. Schilling
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include "libiso9660.h"
static int iso9660_ucs_level = 0;
static struct iso_primary_descriptor *iso9660_volume;
#ifdef DEBUG
void
printchars(s, n)
char *s;
int n;
{
int i;
char *p;
for (; n > 0 && *s; n--) {
if (*s == ' ') {
p = s;
i = n;
while (--i >= 0 && *p++ == ' ')
;
if (i <= 0)
break;
}
putchar(*s++);
}
}
void print_info(struct iso_primary_descriptor *ipd)
{
/* filesystem information */
printf("System id: ");
printchars(ipd->system_id, 32);
putchar('\n');
printf("Volume id: ");
printchars(ipd->volume_id, 32);
putchar('\n');
printf("Volume set id: ");
printchars(ipd->volume_set_id, 128);
putchar('\n');
printf("Publisher id: ");
printchars(ipd->publisher_id, 128);
putchar('\n');
printf("Data preparer id: ");
printchars(ipd->preparer_id, 128);
putchar('\n');
printf("Application id: ");
printchars(ipd->application_id, 128);
putchar('\n');
printf("Copyright File id: ");
printchars(ipd->copyright_file_id, 37);
putchar('\n');
printf("Abstract File id: ");
printchars(ipd->abstract_file_id, 37);
putchar('\n');
printf("Bibliographic File id: ");
printchars(ipd->bibliographic_file_id, 37);
putchar('\n');
printf("Volume set size is: %d\n", isonum_723(ipd->volume_set_size));
printf("Volume set sequence number is: %d\n", isonum_723(ipd->volume_sequence_number));
printf("Logical block size is: %d\n", isonum_723(ipd->logical_block_size));
printf("Volume size is: %d\n", isonum_733((unsigned char *)ipd->volume_space_size));
}
#endif
void iso9660_name(char *buffer, struct iso_directory_record * idr)
{
int j;
unsigned char uh, ul, uc;
buffer[0] = 0;
if (idr->name_len[0] == 1 && idr->name[0] == 0)
strcpy(buffer, ".");
else if (idr->name_len[0] == 1 && idr->name[0] == 1)
strcpy(buffer, "..");
else {
switch (iso9660_ucs_level) {
case 3:
case 2:
case 1:
/*
* Unicode name.
*/
for (j = 0; j < (int)idr->name_len[0] / 2; j++) {
uh = idr->name[j*2];
ul = idr->name[j*2+1];
/*
* unicode convertion
* up = unls->unls_uni2cs[uh];
*
* if (up == NULL)
* uc = '\0';
* else
* uc = up[ul];
*
* we use only low byte
*/
uc = ul;
buffer[j] = uc ? uc : '_';
}
buffer[idr->name_len[0]/2] = '\0';
break;
case 0:
/*
* Normal non-Unicode name.
*/
strncpy(buffer, idr->name, idr->name_len[0]);
buffer[idr->name_len[0]] = 0;
break;
default:
/*
* Don't know how to do these yet. Maybe they are the same
* as one of the above.
*/
break;
}
}
}
int iso9660_mount(char* name)
{
struct iso_primary_descriptor *jpd;
struct iso_primary_descriptor ipd;
struct iso_directory_record * idr;
int block;
if (iso9660_device_open() == -1)
return -1;
/* read filesystem descriptor */
iso9660_device_read(16, &ipd, sizeof (ipd));
idr = (struct iso_directory_record *)ipd.root_directory_record;
/*
* High sierra:
*
* DESC TYPE == 1 (VD_SFS) offset 8 len 1
* STR ID == "CDROM" offset 9 len 5
* STD_VER == 1 offset 14 len 1
*/
/* High Sierra format ? */
if ((((char *)&ipd)[8] == 1) &&
(strncmp(&((char *)&ipd)[9], "CDROM", 5) == 0) &&
(((char *)&ipd)[14] == 1)) {
printf("Incompatible format: High Sierra format\n");
return -1;
}
/*
* ISO 9660:
*
* DESC TYPE == 1 (VD_PVD) offset 0 len 1
* STR ID == "CD001" offset 1 len 5
* STD_VER == 1 offset 6 len 1
*/
/* NOT ISO 9660 format ? */
if ((ipd.type[0] != ISO_VD_PRIMARY) ||
(strncmp(ipd.id, ISO_STANDARD_ID, sizeof (ipd.id)) != 0) ||
(ipd.version[0] != 1)) {
printf("Not ISO 9660 format\n");
return -1;
}
#ifdef DEBUG
print_info(&ipd);
#endif
/* UCS info */
block = 16;
jpd = (struct iso_primary_descriptor *)
malloc(sizeof(struct iso_primary_descriptor));
if (jpd == NULL)
return -1;
memcpy(jpd, &ipd, sizeof (ipd));
while ((u_int8_t)jpd->type[0] != ISO_VD_END) {
/*
* If Joliet UCS escape sequence found, we may be wrong
*/
if (jpd->unused3[0] == '%' &&
jpd->unused3[1] == '/' &&
(jpd->unused3[3] == '\0' ||
jpd->unused3[3] == ' ') &&
(jpd->unused3[2] == '@' ||
jpd->unused3[2] == 'C' ||
jpd->unused3[2] == 'E')) {
if (jpd->version[0] == 1)
goto nextblock;
}
#ifdef DEBUG
if (jpd->type[0] == 0) {
printf("EL TORITO\n");
}
if (jpd->version[0] == 2) {
printf("CD-ROM uses ISO 9660:1999 relaxed format\n");
break;
}
#endif
nextblock:
block++;
iso9660_device_read(block, jpd, sizeof (*jpd));
}
/*
* ISO 9660:
*
* DESC TYPE == 1 (VD_PVD) offset 0 len 1
* STR ID == "CD001" offset 1 len 5
* STD_VER == 1 offset 6 len 1
*/
/* NOT ISO 9660 format ? */
if ((ipd.type[0] != ISO_VD_PRIMARY) ||
(strncmp(ipd.id, ISO_STANDARD_ID, sizeof (ipd.id)) != 0) ||
(ipd.version[0] != 1))
return -1;
block = 16;
memcpy(jpd, &ipd, sizeof (ipd));
while ((unsigned char) jpd->type[0] != ISO_VD_END) {
/*
* Find the UCS escape sequence.
*/
if (jpd->unused3[0] == '%' &&
jpd->unused3[1] == '/' &&
(jpd->unused3[3] == '\0' ||
jpd->unused3[3] == ' ') &&
(jpd->unused3[2] == '@' ||
jpd->unused3[2] == 'C' ||
jpd->unused3[2] == 'E')) {
break;
}
block++;
iso9660_device_read(block, jpd, sizeof (*jpd));
}
/* Unable to find Joliet SVD */
if (((unsigned char) jpd->type[0] == ISO_VD_END)) {
free(jpd);
return -1;
}
switch (jpd->unused3[2]) {
case '@':
iso9660_ucs_level = 1;
break;
case 'C':
iso9660_ucs_level = 2;
break;
case 'E':
iso9660_ucs_level = 3;
break;
}
if (iso9660_ucs_level > 3) {
/* Don't know what iso9660_ucs_level */
free(jpd);
return -1;
}
if (jpd->unused3[3] == ' ')
printf("Warning: Joliet escape sequence uses illegal space at offset 3\n");
iso9660_volume = jpd;
return 0;
}
void iso9660_umount(void)
{
free(iso9660_volume);
iso9660_device_close();
}
struct iso_directory_record *iso9660_get_root_node()
{
return (struct iso_directory_record *)iso9660_volume->root_directory_record;
}

36
libiso9660/iso9660_open.c Normal file
View File

@ -0,0 +1,36 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include "libiso9660.h"
iso9660_FILE* iso9660_open(char* pathname)
{
struct iso_directory_record *root;
struct iso_directory_record *idr;
iso9660_FILE *file;
root = iso9660_get_root_node();
if (root == NULL)
return NULL;
idr = iso9660_get_node(root, pathname);
if (idr == NULL)
return NULL;
file = (iso9660_FILE*)malloc(sizeof(iso9660_FILE));
if (file == NULL)
return NULL;
file->extent = isonum_733((unsigned char *)idr->extent);
file->len = isonum_733((unsigned char *)idr->size);
file->index = sizeof (file->buffer);
free(idr);
return file;
}

View File

@ -0,0 +1,120 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include <string.h>
#include "libiso9660.h"
static iso9660_DIR* iso9660_opendir_node(struct iso_directory_record *node)
{
iso9660_DIR *dir;
dir = (iso9660_DIR*)malloc(sizeof(iso9660_DIR));
if (dir == NULL)
return NULL;
dir->extent = isonum_733((unsigned char *)node->extent);
dir->len = isonum_733((unsigned char *)node->size);
dir->index = sizeof (dir->buffer);
return dir;
}
static struct iso_directory_record* idr_new(struct iso_directory_record* idr)
{
struct iso_directory_record* result;
int size = sizeof(*idr) + (int)idr->name_len[0];
result = (struct iso_directory_record*)malloc(size);
memcpy(result, idr, size);
return result;
}
static struct iso_directory_record * seek_name(struct iso_directory_record *idr,
char *name)
{
struct iso_directory_record *result;
char name_buf[256];
iso9660_DIR *dir;
dir = iso9660_opendir_node(idr);
if (dir == NULL)
return NULL;
while ((idr = iso9660_readdir(dir)) != NULL)
{
iso9660_name(name_buf, idr);
if (strcmp(name, name_buf) == 0)
{
result = idr_new(idr);
iso9660_closedir(dir);
return result;
}
}
iso9660_closedir(dir);
return NULL;
}
struct iso_directory_record* iso9660_get_node(
struct iso_directory_record *dirnode,
char *path)
{
struct iso_directory_record* result;
struct iso_directory_record* current;
char name[256];
int i;
current = idr_new(dirnode);
while(1)
{
/* ignore head '/' */
while (*path && *path == '/')
path++;
if (*path == 0)
break;
/* extract first path component */
i = 0;
while (*path && *path != '/')
name[i++] = *path++;
name[i] = 0;
/* seek first component in current directory */
result = seek_name(current, name);
if (result == NULL)
return NULL;
free(current);
current = result;
}
return current;
}
iso9660_DIR* iso9660_opendir(char *name)
{
iso9660_DIR *dir;
struct iso_directory_record *node;
node = iso9660_get_root_node();
if (node == NULL)
return NULL;
node = iso9660_get_node(node, name);
if (node == NULL)
return NULL;
dir = iso9660_opendir_node(node);
free(node);
return dir;
}

45
libiso9660/iso9660_read.c Normal file
View File

@ -0,0 +1,45 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <string.h>
#include <stdio.h>
#include "libiso9660.h"
ssize_t iso9660_read(iso9660_FILE *file, void *buf, size_t count)
{
size_t read = 0;
while (count > 0)
{
size_t part;
if (file->index == 2048)
{
if (file->len <= 0)
return read;
iso9660_device_read(file->extent,
file->buffer,
sizeof (file->buffer));
file->len -= sizeof (file->buffer);
file->extent++;
file->index = 0;
}
if (count > sizeof(file->buffer) - file->index)
part = sizeof(file->buffer) - file->index;
else
part = count;
memcpy(buf + read, file->buffer + file->index, part);
count -= part;
file->index += part;
read += part;
}
return read;
}

View File

@ -0,0 +1,30 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include "libiso9660.h"
struct iso_directory_record *iso9660_readdir(iso9660_DIR *dir)
{
struct iso_directory_record *idr;
if (dir->index > 2048 - offsetof(struct iso_directory_record, name[0]))
{
if (dir->len <= 0)
return NULL;
iso9660_device_read(dir->extent, dir->buffer, sizeof (dir->buffer));
dir->len -= sizeof (dir->buffer);
dir->extent++;
dir->index = 0;
}
idr = (struct iso_directory_record *) &dir->buffer[dir->index];
if (idr->length[0] == 0)
return NULL;
dir->index += dir->buffer[dir->index];
return idr;
}

62
libiso9660/libiso9660.h Normal file
View File

@ -0,0 +1,62 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#ifndef __LIBISO9660_H__
#define __LIBISO9660_H__
#include <unistd.h>
#include <linux/iso_fs.h>
typedef struct iso9660_DIR {
int extent;
int len;
int index;
unsigned char buffer[2048];
} iso9660_DIR;
typedef struct iso9660_FILE {
int extent;
int len;
int index;
unsigned char buffer[2048];
} iso9660_FILE;
static inline int isonum_721(char *p)
{
return ((p[0] & 0xff)
| ((p[1] & 0xff) << 8));
}
static inline int isonum_723(char *p)
{
return (isonum_721(p));
}
static inline int isonum_733(char *p)
{
return ((p[0] & 0xff) | ((p[1] & 0xff) << 8) |
((p[2] & 0xff) << 16) | ((p[3] & 0xff) << 24));
}
extern int iso9660_mount(char* name);
extern void iso9660_name(char *buffer, struct iso_directory_record * idr);
extern void iso9660_umount(void);
extern struct iso_directory_record *iso9660_get_root_node(void);
extern iso9660_DIR* iso9660_opendir(char *name);
extern int iso9660_closedir(iso9660_DIR *dir);
extern struct iso_directory_record *iso9660_readdir(iso9660_DIR *dir);
extern int iso9660_is_directory(struct iso_directory_record * idr);
extern struct iso_directory_record* iso9660_get_node(struct iso_directory_record *dirnode, char *path);
extern iso9660_FILE* iso9660_open(char* pathname);
extern ssize_t iso9660_read(iso9660_FILE *file, void *buf, size_t count);
extern void iso9660_close(iso9660_FILE *file);
/* imported function */
extern int iso9660_device_open(void);
extern void iso9660_device_close(void);
extern void iso9660_device_read(off_t offset, void* buffer, size_t size);
#endif /* __LIBISO9660_H__ */