freemyipod r447 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r446‎ | r447 | r448 >
Date:23:59, 18 January 2011
Author:theseven
Status:new
Tags:
Comment:
elf2flt: Ignore R_ARM_V4BX relocations (we don't care about pre-ARMv4t) and switch compression from gzip to UCL
Modified paths:
  • /tools/elf2flt/compress.c (modified) (history)
  • /tools/elf2flt/compress.h (modified) (history)
  • /tools/elf2flt/elf2flt.c (modified) (history)

Diff [purge]

Index: tools/elf2flt/elf2flt.c
@@ -645,6 +645,7 @@
646646 case R_ARM_PLT32:
647647 case R_ARM_GOTPC:
648648 case R_ARM_GOT32:
 649+ case R_ARM_V4BX:
649650 relocation_needed = 0;
650651 break;
651652 default:
Index: tools/elf2flt/compress.h
@@ -10,7 +10,7 @@
1111 #ifndef __ELF2FLT_COMPRESS_H__
1212 #define __ELF2FLT_COMPRESS_H__
1313
14 -#include <zlib.h>
 14+//#include <zlib.h>
1515
1616 typedef enum
1717 {
@@ -25,11 +25,15 @@
2626 {
2727 stream_type type;
2828 const char *mode;
29 - union
30 - {
31 - FILE *filep;
32 - gzFile gzfilep;
33 - } u;
 29+ FILE *filep;
 30+ char* buffer;
 31+ int bufsize;
 32+ int bufused;
 33+// union
 34+// {
 35+// FILE *filep;
 36+// gzFile gzfilep;
 37+// } u;
3438 } stream;
3539
3640 int fopen_stream_u(stream *fp, const char *path, const char *mode);
Index: tools/elf2flt/compress.c
@@ -10,8 +10,10 @@
1111 #include <assert.h>
1212 #include <stdio.h>
1313 #include <stdlib.h>
 14+#include <string.h>
1415
15 -#include <zlib.h>
 16+//#include <zlib.h>
 17+#include "ucl/ucl.h"
1618 #include "compress.h"
1719 #include "stubs.h"
1820
@@ -24,10 +26,13 @@
2527 int
2628 fopen_stream_u(stream *fp, const char *path, const char *mode)
2729 {
28 - fp->u.filep = fopen(path, mode);
29 - fp->type = (fp->u.filep) ? UNCOMPRESSED : INVALID;
 30+ fp->filep = fopen(path, mode);
 31+ fp->type = (fp->filep) ? UNCOMPRESSED : INVALID;
3032 fp->mode = mode;
31 - return (fp->u.filep) ? 0 : 1;
 33+ fp->buffer = NULL;
 34+ fp->bufsize = 0;
 35+ fp->bufused = 0;
 36+ return (fp->filep) ? 0 : 1;
3237 }
3338
3439 /* Read from stream. Return number of elements read. */
@@ -39,12 +44,12 @@
4045
4146 switch (str->type) {
4247 case UNCOMPRESSED:
43 - read = fread(ptr, size, nmemb, str->u.filep);
 48+ read = fread(ptr, size, nmemb, str->filep);
4449 break;
4550
46 - case COMPRESSED:
47 - read = gzread(str->u.gzfilep, ptr, size * nmemb) / size;
48 - break;
 51+// case COMPRESSED:
 52+// read = gzread(str->u.gzfilep, ptr, size * nmemb) / size;
 53+// break;
4954
5055 default:
5156 abort();
@@ -62,11 +67,19 @@
6368
6469 switch (str->type) {
6570 case UNCOMPRESSED:
66 - written = fwrite(ptr, size, nmemb, str->u.filep);
 71+ written = fwrite(ptr, size, nmemb, str->filep);
6772 break;
6873
6974 case COMPRESSED:
70 - written = gzwrite(str->u.gzfilep, ptr, size * nmemb) / size;
 75+ if (str->bufused + size * nmemb > str->bufsize)
 76+ {
 77+ str->bufsize = (str->bufused + size * nmemb + 0xfff) & ~0xfff;
 78+ str->buffer = (char*)realloc(str->buffer, str->bufsize);
 79+ }
 80+ memcpy(&str->buffer[str->bufused], ptr, size * nmemb);
 81+ str->bufused += size * nmemb;
 82+ written = nmemb;
 83+ //written = gzwrite(str->u.gzfilep, ptr, size * nmemb) / size;
7184 break;
7285
7386 default:
@@ -83,10 +96,34 @@
8497 {
8598 switch (str->type) {
8699 case UNCOMPRESSED:
87 - return fclose(str->u.filep);
 100+ return fclose(str->filep);
88101
89102 case COMPRESSED:
90 - return gzclose(str->u.gzfilep);
 103+ {
 104+ int outsize = str->bufused + str->bufused / 8 + 256;
 105+ str->buffer = (char*)realloc(str->buffer, str->bufused);
 106+ char* buffer = (char*)malloc(outsize);
 107+ int r = ucl_nrv2e_99_compress(str->buffer, str->bufused, buffer, &outsize, 0, 10, NULL, NULL);
 108+ free(str->buffer);
 109+ if (r != UCL_E_OK)
 110+ {
 111+ free(buffer);
 112+ /* this should NEVER happen */
 113+ printf("internal error - compression failed: %d\n", r);
 114+ return 2;
 115+ }
 116+ buffer = (char*)realloc(buffer, outsize);
 117+ int done = 0;
 118+ while (done < outsize)
 119+ {
 120+ int bytes = fwrite(&buffer[done], 1, outsize - done, str->filep);
 121+ if (bytes <= 0) return 3;
 122+ done += bytes;
 123+ }
 124+ free(buffer);
 125+ return fclose(str->filep);
 126+ }
 127+// return gzclose(str->u.gzfilep);
91128
92129 default:
93130 abort();
@@ -100,25 +137,26 @@
101138 {
102139 switch (str->type) {
103140 case UNCOMPRESSED:
104 - return ferror(str->u.filep);
 141+ return ferror(str->filep);
105142
106143 case COMPRESSED:
107 - {
108 - const char *err;
109 - int errno;
 144+ return ferror(str->filep);
 145+// {
 146+// const char *err;
 147+// int errno;
 148+//
 149+// err = gzerror(str->u.gzfilep, &errno);
 150+// if (errno == Z_OK || errno == Z_STREAM_END)
 151+// return 0;
 152+// else if (errno == Z_ERRNO)
 153+// return 1;
 154+// else {
 155+// fprintf(stderr, "%s\n", err);
 156+// return 1;
 157+// }
 158+// }
 159+// break;
110160
111 - err = gzerror(str->u.gzfilep, &errno);
112 - if (errno == Z_OK || errno == Z_STREAM_END)
113 - return 0;
114 - else if (errno == Z_ERRNO)
115 - return 1;
116 - else {
117 - fprintf(stderr, "%s\n", err);
118 - return 1;
119 - }
120 - }
121 - break;
122 -
123161 default:
124162 abort();
125163 }
@@ -131,10 +169,10 @@
132170 {
133171 switch (str->type) {
134172 case UNCOMPRESSED:
135 - return fseek(str->u.filep, offset, whence);
 173+ return fseek(str->filep, offset, whence);
136174
137 - case COMPRESSED:
138 - return gzseek(str->u.gzfilep, offset, whence);
 175+// case COMPRESSED:
 176+// return gzseek(str->u.gzfilep, offset, whence);
139177
140178 default:
141179 abort();
@@ -156,12 +194,12 @@
157195 if (str->type == INVALID)
158196 abort();
159197
160 - fd = fileno(str->u.filep);
 198+ fd = fileno(str->filep);
161199 /* Get current (buffered) file position. */
162 - offset = ftell(str->u.filep);
 200+ offset = ftell(str->filep);
163201
164202 /* Make sure there's nothing left in buffers. */
165 - fflush(str->u.filep);
 203+ fflush(str->filep);
166204
167205 /* Reposition underlying FD. (Might be unnecessary?) */
168206 roffset = lseek(fd, offset, SEEK_SET);
@@ -169,8 +207,8 @@
170208 assert(roffset == offset);
171209
172210 /* Reopen as compressed stream. */
173 - str->u.gzfilep = gzdopen(fd, str->mode);
174 - gzsetparams(str->u.gzfilep, 9, Z_DEFAULT_STRATEGY);
 211+ //str->u.gzfilep = gzdopen(fd, str->mode);
 212+ //gzsetparams(str->u.gzfilep, 9, Z_DEFAULT_STRATEGY);
175213 str->type = COMPRESSED;
176214 }
177215