../org/cramfs-1.1/だけに発見: cramfsck
../org/cramfs-1.1/だけに発見: mkcramfs
diff -ur ../org/cramfs-1.1/mkcramfs.c cramfs-1.1-nomem/mkcramfs.c
--- ../org/cramfs-1.1/mkcramfs.c	2002-02-20 17:03:32.000000000 +0900
+++ cramfs-1.1-nomem/mkcramfs.c	2010-04-20 12:43:46.392456000 +0900
@@ -18,10 +18,13 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#define NOMEM
 /*
  * If you change the disk format of cramfs, please update fs/cramfs/README.
  */
 
+#define NOMEM
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <sys/stat.h>
@@ -217,7 +220,11 @@
  * We define our own sorting function instead of using alphasort which
  * uses strcoll and changes ordering based on locale information.
  */
+#if __GLIBC_MINOR__ < 10
 static int cramsort (const void *a, const void *b)
+#else
+static int cramsort (const struct dirent **a, const struct dirent **b)
+#endif
 {
 	return strcmp ((*(const struct dirent **) a)->d_name,
 		       (*(const struct dirent **) b)->d_name);
@@ -284,7 +291,7 @@
 		if (!entry) {
 			die(MKFS_ERROR, 1, "calloc failed");
 		}
-		entry->name = strdup(dirent->d_name);
+		entry->name = (unsigned char *)strdup(dirent->d_name);
 		if (!entry->name) {
 			die(MKFS_ERROR, 1, "strdup failed");
 		}
@@ -358,8 +365,10 @@
 			int blocks = ((entry->size - 1) / blksize + 1);
 
 			/* block pointers & data expansion allowance + data */
+#ifndef NOMEM
 			if (entry->size)
 				*fslen_ub += (4+26)*blocks + entry->size + 3;
+#endif
 		}
 
 		/* Link it into the list */
@@ -396,9 +405,9 @@
 
 	memset(super->name, 0x00, sizeof(super->name));
 	if (opt_name)
-		strncpy(super->name, opt_name, sizeof(super->name));
+		strncpy((char *)super->name, opt_name, sizeof(super->name));
 	else
-		strncpy(super->name, "Compressed", sizeof(super->name));
+		strncpy((char *)super->name, "Compressed", sizeof(super->name));
 
 	super->root.mode = root->mode;
 	super->root.uid = root->uid;
@@ -477,7 +486,7 @@
 		int dir_start = stack_entries;
 		while (entry) {
 			struct cramfs_inode *inode = (struct cramfs_inode *) (base + offset);
-			size_t len = strlen(entry->name);
+			size_t len = strlen((char *)entry->name);
 
 			entry->dir_offset = offset;
 
@@ -575,7 +584,11 @@
  * Note that size > 0, as a zero-sized file wouldn't ever
  * have gotten here in the first place.
  */
+#ifdef NOMEM
+static unsigned int do_compress(char *base, unsigned int offset, char const *name, char *uncompressed, unsigned int size, FILE *fp)
+#else
 static unsigned int do_compress(char *base, unsigned int offset, char const *name, char *uncompressed, unsigned int size)
+#endif
 {
 	unsigned long original_size = size;
 	unsigned long original_offset = offset;
@@ -583,6 +596,15 @@
 	unsigned long blocks = (size - 1) / blksize + 1;
 	unsigned long curr = offset + 4 * blocks;
 	int change;
+#ifdef NOMEM
+	unsigned long bak_offset = offset;
+
+	base = malloc(size + curr);
+	if (!base)
+		die(MKFS_ERROR, 0, "Not enough memory.");
+	offset = original_offset = 0;
+	curr = 4 * blocks;
+#endif
 
 	total_blocks += blocks;
 
@@ -595,7 +617,7 @@
 			input = blksize;
 		size -= input;
 		if (!(opt_holes && is_zero (uncompressed, input))) {
-			err = compress2(base + curr, &len, uncompressed, input, Z_BEST_COMPRESSION);
+			err = compress2((unsigned char *)base + curr, &len, (unsigned char *)uncompressed, input, Z_BEST_COMPRESSION);
 			if (err != Z_OK) {
 				die(MKFS_ERROR, 0, "compression error: %s", zError(err));
 			}
@@ -608,7 +630,11 @@
 			die(MKFS_ERROR, 0, "AIEEE: block \"compressed\" to > 2*blocklength (%ld)", len);
 		}
 
+#ifdef NOMEM
+		*(u32 *) (base + offset) = curr + bak_offset;
+#else
 		*(u32 *) (base + offset) = curr;
+#endif
 		offset += 4;
 	} while (size);
 
@@ -622,6 +648,12 @@
 		printf("%6.2f%% (%+d bytes)\t%s\n",
 		       (change * 100) / (double) original_size, change, name);
 	}
+#ifdef NOMEM
+	if (fwrite(base, curr, 1, fp) != 1)
+			die(MKFS_ERROR, 0, "write error!");
+	free(base);
+	curr += bak_offset;
+#endif
 
 	return curr;
 }
@@ -632,7 +664,12 @@
  * non-null entry->path (i.e. every non-empty regfile) and non-null
  * entry->uncompressed (i.e. every symlink).
  */
+#ifdef NOMEM
+static unsigned int write_data(struct entry *entry, char *base, unsigned int offset,
+		FILE *fp)
+#else
 static unsigned int write_data(struct entry *entry, char *base, unsigned int offset)
+#endif
 {
 	do {
 		if (entry->path || entry->uncompressed) {
@@ -644,12 +681,21 @@
 				set_data_offset(entry, base, offset);
 				entry->offset = offset;
 				map_entry(entry);
-				offset = do_compress(base, offset, entry->name, entry->uncompressed, entry->size);
+#ifdef NOMEM
+				printf("%s\n", entry->name);
+				offset = do_compress(base, offset, (char *)entry->name, entry->uncompressed, entry->size, fp);
+#else
+				offset = do_compress(base, offset, (char *)entry->name, entry->uncompressed, entry->size);
+#endif
 				unmap_entry(entry);
 			}
 		}
 		else if (entry->child)
+#ifdef NOMEM
+			offset = write_data(entry->child, base, offset, fp);
+#else
 			offset = write_data(entry->child, base, offset);
+#endif
 		entry=entry->next;
 	} while (entry);
 	return offset;
@@ -679,6 +725,28 @@
 	return (offset + image_length);
 }
 
+#ifdef NOMEM
+static u32 crc32_file(u32 crc, const char *opt_image, int *size)
+{
+	FILE *fp;
+	unsigned char buf[256];
+	int s;
+
+	fp = fopen(opt_image, "r");
+	if (!fp)
+		return crc;
+
+	*size = 0;
+	while (!feof(fp)) {
+		s = fread(buf, 1, sizeof(buf), fp);
+		crc = crc32(crc, buf, s);
+		*size += s;
+	}
+	fclose(fp);
+	return crc;
+}
+#endif
+
 int main(int argc, char **argv)
 {
 	struct stat st;		/* used twice... */
@@ -692,6 +760,11 @@
 	u32 crc;
 	int c;			/* for getopt */
 	char *ep;		/* for strtoul */
+#ifdef NOMEM
+	int data_offset = 0;
+	int data_size = 0;
+	FILE *tmp_fp;
+#endif
 
 	total_blocks = 0;
 
@@ -748,6 +821,12 @@
 		die(MKFS_USAGE, 1, "stat failed: %s", dirname);
 	}
 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+#ifdef NOMEM
+	tmp_fp = fopen("tmp.img", "w");
+	if (!tmp_fp) {
+		die(MKFS_USAGE, 1, "open failed: tmp.img\n");
+	}
+#endif
 	if (fd < 0) {
 		die(MKFS_USAGE, 1, "open failed: %s", outfile);
 	}
@@ -809,7 +888,12 @@
 	offset = write_directory_structure(root_entry->child, rom_image, offset);
 	printf("Directory data: %d bytes\n", offset);
 
+#ifdef NOMEM
+	data_offset = offset;
+	offset = write_data(root_entry, rom_image, offset, tmp_fp);
+#else
 	offset = write_data(root_entry, rom_image, offset);
+#endif
 
 	/* We always write a multiple of blksize bytes, so that
 	   losetup works. */
@@ -822,15 +906,74 @@
 
 	/* Put the checksum in. */
 	crc = crc32(0L, Z_NULL, 0);
-	crc = crc32(crc, (rom_image+opt_pad), (offset-opt_pad));
+#ifdef NOMEM
+	crc = crc32(crc, (unsigned char *)(rom_image+opt_pad), (data_offset-opt_pad));
+	fclose(tmp_fp);
+
+	crc = crc32_file(crc, "tmp.img", &data_size);
+	data_size += data_offset;
+	while (data_size < offset) {
+			int tmp = 0;
+			crc = crc32(crc, (unsigned char *)&tmp, 4);
+			data_size += 4;
+	}
+#else
+	crc = crc32(crc, (unsigned char *)(rom_image+opt_pad), (offset-opt_pad));
+#endif
 	((struct cramfs_super *) (rom_image+opt_pad))->fsid.crc = crc;
 	printf("CRC: %x\n", crc);
 
 	/* Check to make sure we allocated enough space. */
+#ifdef NOMEM
+	if (fslen_ub < data_offset) {
+		die(MKFS_ERROR, 0, "not enough space allocated for ROM image (%Ld allocated, %d used)", fslen_ub, offset);
+	}
+#else
 	if (fslen_ub < offset) {
 		die(MKFS_ERROR, 0, "not enough space allocated for ROM image (%Ld allocated, %d used)", fslen_ub, offset);
 	}
+#endif
+
+#ifdef NOMEM
+	written = write(fd, rom_image, data_offset);
+	if (written < 0) {
+		die(MKFS_ERROR, 1, "write failed");
+	}
+	if (data_offset != written) {
+		die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
+	}
+
+	tmp_fp = fopen("tmp.img", "r");
+
+	while (!feof(tmp_fp)) {
+			char buf[1024];
+			int size;
+
+			size = fread(buf, 1, 1024, tmp_fp);
+			if (size <= 0)
+					die(MKFS_ERROR, 0, "read error");
 
+			written = write(fd, buf, size);
+			if (written < 0) {
+					die(MKFS_ERROR, 1, "write failed");
+			}
+			if (size != written) {
+					die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
+			}
+			data_offset += size;
+	}
+	while (data_offset < offset) {
+			int tmp = 0;
+			written = write(fd, (unsigned char *)&tmp, 4);
+			if (written < 0) {
+					die(MKFS_ERROR, 1, "write failed");
+			}
+			if (4 != written) {
+					die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
+			}
+			data_offset += 4;
+	}
+#else
 	written = write(fd, rom_image, offset);
 	if (written < 0) {
 		die(MKFS_ERROR, 1, "write failed");
@@ -838,6 +981,7 @@
 	if (offset != written) {
 		die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
 	}
+#endif
 
 	/* (These warnings used to come at the start, but they scroll off the
 	   screen too quickly.) */
../org/cramfs-1.1/だけに発見: test.bin
