wok diff syslinux/stuff/iso2exe/iso9660.c @ rev 16022

syslinux/iso2exe: add zimage support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Mar 06 19:57:41 2014 +0000 (2014-03-06)
parents
children 6aed6fc5819d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/syslinux/stuff/iso2exe/iso9660.c	Thu Mar 06 19:57:41 2014 +0000
     1.3 @@ -0,0 +1,155 @@
     1.4 +#include <sys/types.h>
     1.5 +#include <fcntl.h>
     1.6 +#include <stdio.h>
     1.7 +#include "iso9660.h"
     1.8 +#define __ROCKRIDGE
     1.9 +
    1.10 +char *isofilename;
    1.11 +unsigned long isofileofs, isofilesize;
    1.12 +unsigned short isofilemod;
    1.13 +int isofd;
    1.14 +
    1.15 +#define SECTORSZ 2048
    1.16 +#define SECTORBITS 11
    1.17 +static char buffer[SECTORSZ];
    1.18 +
    1.19 +static int readsector(unsigned long offset)
    1.20 +{
    1.21 +	return (lseek(isofd, offset, SEEK_SET) != -1
    1.22 +		    && read(isofd, buffer, SECTORSZ) == SECTORSZ);
    1.23 +}
    1.24 +
    1.25 +int isoread(char *data, unsigned size)
    1.26 +{
    1.27 +	int get, n;
    1.28 +	
    1.29 +	if (size > isofilesize)
    1.30 +		size = isofilesize;
    1.31 +	if (lseek(isofd, isofileofs, SEEK_SET) == -1)
    1.32 +		return -1;
    1.33 +	for (get = size; get; get -= n, data += n) {
    1.34 +		n = read(isofd,data,get);
    1.35 +		if (n < 0)
    1.36 +			return n;
    1.37 +		if (n == 0)
    1.38 +			break;
    1.39 +		isofileofs += n;
    1.40 +		isofilesize -= n;
    1.41 +	}
    1.42 +	return size - get;
    1.43 +}
    1.44 +
    1.45 +static unsigned long isodirofs, isodirsize;
    1.46 +int isoreset(char *name)
    1.47 +{
    1.48 +	if (name)
    1.49 +		isofd = open(name, O_RDONLY);
    1.50 +	if (!readsector(16UL * 2048) || strncmp(buffer+1,"CD001",5))
    1.51 +		return -1;
    1.52 +	isodirofs = * (unsigned long *) (buffer + 0x9E);
    1.53 +	isodirofs <<= SECTORBITS;
    1.54 +	isodirsize = * (unsigned long *) (buffer + 0xA6);
    1.55 +	return 0;
    1.56 +}
    1.57 +
    1.58 +int isoreaddir(int restart)
    1.59 +{
    1.60 +	static unsigned long pos, dirofs, dirsize;
    1.61 +	static char dots[] = "..";
    1.62 +	int size, n;
    1.63 +#ifdef __ROCKRIDGE
    1.64 +	char *endname;
    1.65 +#endif
    1.66 +
    1.67 +	if (restart) {
    1.68 +		dirofs = isodirofs;
    1.69 +		dirsize = isodirsize;
    1.70 +		pos = SECTORSZ;
    1.71 +	}
    1.72 +	if (pos >= SECTORSZ) {
    1.73 +		if (dirsize < SECTORSZ) return -1;
    1.74 +		readsector(dirofs);
    1.75 +		dirofs += SECTORSZ;
    1.76 +		dirsize -= SECTORSZ;
    1.77 +		pos = 0;
    1.78 +	}
    1.79 +	size = * (short *) (buffer + pos);
    1.80 +	if (size == 0)
    1.81 +		return -1;
    1.82 +	isofileofs = (* (unsigned long *) (buffer + pos + 2)) << SECTORBITS;
    1.83 +	isofilesize = * (unsigned long *) (buffer + pos + 10);
    1.84 +	isofilemod = (buffer[pos + 25] & 2) ? 0040755 : 0100755;
    1.85 +#ifdef __ROCKRIDGE
    1.86 +	endname = NULL;
    1.87 +	n = (buffer[pos + 32] + pos + 34) & -2;
    1.88 +	do {
    1.89 +		int len = buffer[n + 2];
    1.90 +		switch (* (short *) (buffer + n)) {
    1.91 +		case 0x4D4E: // NM
    1.92 +			isofilename = buffer + n + 5;
    1.93 +			endname = buffer + n + len;
    1.94 +			break;
    1.95 +		case 0x5850: // PX
    1.96 +			isofilemod = * (short *) (buffer + n + 4);
    1.97 +			break;
    1.98 +		}
    1.99 +		n += len;
   1.100 +	}
   1.101 +	while (n + 2 < pos + size);
   1.102 +	if (endname)
   1.103 +		*endname = 0;
   1.104 +	else
   1.105 +#endif
   1.106 +	{
   1.107 +		isofilename = buffer + pos + 33;
   1.108 +		switch (* (short *) (isofilename - 1)) {
   1.109 +		case 0x0101:
   1.110 +			isofilename = dots;
   1.111 +			break;
   1.112 +		case 0x0001:
   1.113 +			isofilename = dots + 1;
   1.114 +			break;
   1.115 +		default:
   1.116 +			n = isofilename[-1];
   1.117 +			if (* (short *) (isofilename + n - 2) == 0x313B)
   1.118 +				n -= 2; // remove ;1
   1.119 +			if (isofilename[n - 1] == '.') n--;
   1.120 +			isofilename[n] = 0;
   1.121 +		}
   1.122 +	}
   1.123 +	pos += size;
   1.124 +	return 0;
   1.125 +}
   1.126 +
   1.127 +#define IS_DIR(x)( ((x) & ~0777) == 040000)
   1.128 +int isoopen(char *name)
   1.129 +{
   1.130 +	int restart;
   1.131 +	char *s, c;
   1.132 +
   1.133 +	while (*name == '/') {
   1.134 +		name++;
   1.135 +		isoreset(NULL);
   1.136 +	}
   1.137 +	s = name;
   1.138 +	while (1) {
   1.139 +		while (*s && *s != '/') s++;
   1.140 +		c = *s;
   1.141 +		*s = 0;
   1.142 +		for (restart = 1; isoreaddir(restart) == 0; restart = 0) {
   1.143 +			if (strcmp(name, isofilename)) continue;
   1.144 +			if (IS_DIR(isofilemod)) {
   1.145 +				isodirofs = isofileofs;
   1.146 +				isodirsize = isofilesize;
   1.147 +				if (c) {
   1.148 +					*s++ = c;
   1.149 +					name = s;
   1.150 +					goto next;
   1.151 +				}
   1.152 +			}
   1.153 +			return 0;
   1.154 +		}
   1.155 +		return -1;
   1.156 +	  next: ;
   1.157 +	}
   1.158 +}