diff src/common/Util.cpp @ 496:a6d060a64246

pixel introspection. but entire image is upside down.
author Robert McIntyre <rlm@mit.edu>
date Mon, 11 Jun 2012 06:04:25 -0500
parents f94fa48624d0
children
line wrap: on
line diff
     1.1 --- a/src/common/Util.cpp	Mon Jun 11 00:55:51 2012 -0500
     1.2 +++ b/src/common/Util.cpp	Mon Jun 11 06:04:25 2012 -0500
     1.3 @@ -48,238 +48,247 @@
     1.4  //due to void* and const void* differences
     1.5  //--Felipe 
     1.6  int gzWrite(gzFile file, void* buf, unsigned len){
     1.7 -	return gzwrite(file,buf,len);
     1.8 +  return gzwrite(file,buf,len);
     1.9  }
    1.10  
    1.11  void utilPutDword(u8 *p, u32 value)
    1.12  {
    1.13 -	*p++ = value & 255;
    1.14 -	*p++ = (value >> 8) & 255;
    1.15 -	*p++ = (value >> 16) & 255;
    1.16 -	*p	 = (value >> 24) & 255;
    1.17 +  *p++ = value & 255;
    1.18 +  *p++ = (value >> 8) & 255;
    1.19 +  *p++ = (value >> 16) & 255;
    1.20 +  *p	 = (value >> 24) & 255;
    1.21  }
    1.22  
    1.23  void utilPutWord(u8 *p, u16 value)
    1.24  {
    1.25 -	*p++ = value & 255;
    1.26 -	*p	 = (value >> 8) & 255;
    1.27 +  *p++ = value & 255;
    1.28 +  *p	 = (value >> 8) & 255;
    1.29  }
    1.30  
    1.31  void utilWriteBMP(u8 *b, int w, int h, int dstDepth, u8 *pix)
    1.32  {
    1.33 -	int sizeX = w;
    1.34 -	int sizeY = h;
    1.35 +  int sizeX = w;
    1.36 +  int sizeY = h;
    1.37  
    1.38 -	switch (dstDepth > 0 ? dstDepth : systemColorDepth)
    1.39 -	{
    1.40 -	case 16:
    1.41 -	{
    1.42 -		u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line
    1.43 -		for (int y = 0; y < sizeY; y++)
    1.44 -		{
    1.45 -			for (int x = 0; x < sizeX; x++)
    1.46 -			{
    1.47 -				u16 v = *p++;
    1.48 +  switch (dstDepth > 0 ? dstDepth : systemColorDepth)
    1.49 +    {
    1.50 +    case 16:
    1.51 +      {
    1.52 +	u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line
    1.53 +	for (int y = 0; y < sizeY; y++)
    1.54 +	  {
    1.55 +	    for (int x = 0; x < sizeX; x++)
    1.56 +	      {
    1.57 +		u16 v = *p++;
    1.58  
    1.59 -				*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
    1.60 -				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
    1.61 -				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
    1.62 -			}
    1.63 -			p++; // skip black pixel for filters
    1.64 -			p++; // skip black pixel for filters
    1.65 -			p -= 2 * (w + 2);
    1.66 -		}
    1.67 -		break;
    1.68 -	}
    1.69 -	case 24:
    1.70 -	{
    1.71 -		u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1);
    1.72 -		for (int y = 0; y < sizeY; y++)
    1.73 -		{
    1.74 -			for (int x = 0; x < sizeX; x++)
    1.75 -			{
    1.76 -				if (systemRedShift > systemBlueShift)
    1.77 -				{
    1.78 -					*b++ = *pixU8++; // B
    1.79 -					*b++ = *pixU8++; // G
    1.80 -					*b++ = *pixU8++; // R
    1.81 -				}
    1.82 -				else
    1.83 -				{
    1.84 -					int red	  = *pixU8++;
    1.85 -					int green = *pixU8++;
    1.86 -					int blue  = *pixU8++;
    1.87 +		*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
    1.88 +		*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
    1.89 +		*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
    1.90 +	      }
    1.91 +	    p++; // skip black pixel for filters
    1.92 +	    p++; // skip black pixel for filters
    1.93 +	    p -= 2 * (w + 2);
    1.94 +	  }
    1.95 +	break;
    1.96 +      }
    1.97 +    case 24:
    1.98 +      {
    1.99 +	u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1);
   1.100 +	for (int y = 0; y < sizeY; y++)
   1.101 +	  {
   1.102 +	    for (int x = 0; x < sizeX; x++)
   1.103 +	      {
   1.104 +		if (systemRedShift > systemBlueShift)
   1.105 +		  {
   1.106 +		    *b++ = *pixU8++; // B
   1.107 +		    *b++ = *pixU8++; // G
   1.108 +		    *b++ = *pixU8++; // R
   1.109 +		  }
   1.110 +		else
   1.111 +		  {
   1.112 +		    int red	  = *pixU8++;
   1.113 +		    int green = *pixU8++;
   1.114 +		    int blue  = *pixU8++;
   1.115  
   1.116 -					*b++ = blue;
   1.117 -					*b++ = green;
   1.118 -					*b++ = red;
   1.119 -				}
   1.120 -			}
   1.121 -			pixU8 -= 2 * 3 * w;
   1.122 -		}
   1.123 -		break;
   1.124 -	}
   1.125 -	case 32:
   1.126 -	{
   1.127 -		u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h));
   1.128 -		for (int y = 0; y < sizeY; y++)
   1.129 -		{
   1.130 -			for (int x = 0; x < sizeX; x++)
   1.131 -			{
   1.132 -				u32 v = *pixU32++;
   1.133 +		    *b++ = blue;
   1.134 +		    *b++ = green;
   1.135 +		    *b++ = red;
   1.136 +		  }
   1.137 +	      }
   1.138 +	    pixU8 -= 2 * 3 * w;
   1.139 +	  }
   1.140 +	break;
   1.141 +      }
   1.142 +    case 32:
   1.143 +      {
   1.144 +	u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h));
   1.145 +	for (int y = 0; y < sizeY; y++)
   1.146 +	  {
   1.147 +	    for (int x = 0; x < sizeX; x++)
   1.148 +	      {
   1.149 +		u32 v = *pixU32++;
   1.150 +		//RLM pack bits for java ARGB
   1.151  
   1.152 -				*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.153 -				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.154 -				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.155 -			}
   1.156 -			pixU32++;
   1.157 -			pixU32 -= 2 * (w + 1);
   1.158 -		}
   1.159 -		break;
   1.160 -	}
   1.161 -	}
   1.162 +
   1.163 +		*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R	
   1.164 +		*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.165 +		*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.166 +		*b++ = 0; // Alpha
   1.167 +
   1.168 +		
   1.169 +
   1.170 +		// end RLM
   1.171 +		
   1.172 +		
   1.173 +	      }
   1.174 +	    pixU32++;
   1.175 +	    pixU32 -= 2 * (w + 1);
   1.176 +	  }
   1.177 +	break;
   1.178 +      }
   1.179 +    }
   1.180  }
   1.181  
   1.182  bool utilWriteBMPFile(const char *fileName, int w, int h, u8 *pix)
   1.183  {
   1.184 -	u8 writeBuffer[256 * 3];
   1.185 +  u8 writeBuffer[256 * 3];
   1.186  
   1.187 -	FILE *fp = fopen(fileName, "wb");
   1.188 +  FILE *fp = fopen(fileName, "wb");
   1.189  
   1.190 -	if (!fp)
   1.191 -	{
   1.192 -		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
   1.193 -		return false;
   1.194 -	}
   1.195 +  if (!fp)
   1.196 +    {
   1.197 +      systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
   1.198 +      return false;
   1.199 +    }
   1.200  
   1.201 -	struct
   1.202 -	{
   1.203 -		u8 ident[2];
   1.204 -		u8 filesize[4];
   1.205 -		u8 reserved[4];
   1.206 -		u8 dataoffset[4];
   1.207 -		u8 headersize[4];
   1.208 -		u8 width[4];
   1.209 -		u8 height[4];
   1.210 -		u8 planes[2];
   1.211 -		u8 bitsperpixel[2];
   1.212 -		u8 compression[4];
   1.213 -		u8 datasize[4];
   1.214 -		u8 hres[4];
   1.215 -		u8 vres[4];
   1.216 -		u8 colors[4];
   1.217 -		u8 importantcolors[4];
   1.218 -		//    u8 pad[2];
   1.219 -	} bmpheader;
   1.220 -	memset(&bmpheader, 0, sizeof(bmpheader));
   1.221 +  struct
   1.222 +  {
   1.223 +    u8 ident[2];
   1.224 +    u8 filesize[4];
   1.225 +    u8 reserved[4];
   1.226 +    u8 dataoffset[4];
   1.227 +    u8 headersize[4];
   1.228 +    u8 width[4];
   1.229 +    u8 height[4];
   1.230 +    u8 planes[2];
   1.231 +    u8 bitsperpixel[2];
   1.232 +    u8 compression[4];
   1.233 +    u8 datasize[4];
   1.234 +    u8 hres[4];
   1.235 +    u8 vres[4];
   1.236 +    u8 colors[4];
   1.237 +    u8 importantcolors[4];
   1.238 +    //    u8 pad[2];
   1.239 +  } bmpheader;
   1.240 +  memset(&bmpheader, 0, sizeof(bmpheader));
   1.241  
   1.242 -	bmpheader.ident[0] = 'B';
   1.243 -	bmpheader.ident[1] = 'M';
   1.244 +  bmpheader.ident[0] = 'B';
   1.245 +  bmpheader.ident[1] = 'M';
   1.246  
   1.247 -	u32 fsz = sizeof(bmpheader) + w * h * 3;
   1.248 -	utilPutDword(bmpheader.filesize, fsz);
   1.249 -	utilPutDword(bmpheader.dataoffset, 0x36);
   1.250 -	utilPutDword(bmpheader.headersize, 0x28);
   1.251 -	utilPutDword(bmpheader.width, w);
   1.252 -	utilPutDword(bmpheader.height, h);
   1.253 -	utilPutDword(bmpheader.planes, 1);
   1.254 -	utilPutDword(bmpheader.bitsperpixel, 24);
   1.255 -	utilPutDword(bmpheader.datasize, 3 * w * h);
   1.256 +  u32 fsz = sizeof(bmpheader) + w * h * 3;
   1.257 +  utilPutDword(bmpheader.filesize, fsz);
   1.258 +  utilPutDword(bmpheader.dataoffset, 0x36);
   1.259 +  utilPutDword(bmpheader.headersize, 0x28);
   1.260 +  utilPutDword(bmpheader.width, w);
   1.261 +  utilPutDword(bmpheader.height, h);
   1.262 +  utilPutDword(bmpheader.planes, 1);
   1.263 +  utilPutDword(bmpheader.bitsperpixel, 24);
   1.264 +  utilPutDword(bmpheader.datasize, 3 * w * h);
   1.265  
   1.266 -	fwrite(&bmpheader, 1, sizeof(bmpheader), fp);
   1.267 +  fwrite(&bmpheader, 1, sizeof(bmpheader), fp);
   1.268  
   1.269  #if 0
   1.270 -	// FIXME: need sufficient buffer
   1.271 -	utilWriteBMP(writeBuffer, w, h, systemColorDepth, pix);
   1.272 +  // FIXME: need sufficient buffer
   1.273 +  utilWriteBMP(writeBuffer, w, h, systemColorDepth, pix);
   1.274  #else
   1.275 -	u8 *b = writeBuffer;
   1.276 +  u8 *b = writeBuffer;
   1.277  
   1.278 -	int sizeX = w;
   1.279 -	int sizeY = h;
   1.280 +  int sizeX = w;
   1.281 +  int sizeY = h;
   1.282  
   1.283 -	switch (systemColorDepth)
   1.284 -	{
   1.285 -	case 16:
   1.286 -	{
   1.287 -		u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line
   1.288 -		for (int y = 0; y < sizeY; y++)
   1.289 -		{
   1.290 -			for (int x = 0; x < sizeX; x++)
   1.291 -			{
   1.292 -				u16 v = *p++;
   1.293 +  switch (systemColorDepth)
   1.294 +    {
   1.295 +    case 16:
   1.296 +      {
   1.297 +	u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line
   1.298 +	for (int y = 0; y < sizeY; y++)
   1.299 +	  {
   1.300 +	    for (int x = 0; x < sizeX; x++)
   1.301 +	      {
   1.302 +		u16 v = *p++;
   1.303  
   1.304 -				*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
   1.305 -				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.306 -				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.307 -			}
   1.308 -			p++; // skip black pixel for filters
   1.309 -			p++; // skip black pixel for filters
   1.310 -			p -= 2 * (w + 2);
   1.311 -			fwrite(writeBuffer, 1, 3 * w, fp);
   1.312 +		*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
   1.313 +		*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.314 +		*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.315 +	      }
   1.316 +	    p++; // skip black pixel for filters
   1.317 +	    p++; // skip black pixel for filters
   1.318 +	    p -= 2 * (w + 2);
   1.319 +	    fwrite(writeBuffer, 1, 3 * w, fp);
   1.320  
   1.321 -			b = writeBuffer;
   1.322 -		}
   1.323 -		break;
   1.324 -	}
   1.325 -	case 24:
   1.326 -	{
   1.327 -		u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1);
   1.328 -		for (int y = 0; y < sizeY; y++)
   1.329 -		{
   1.330 -			for (int x = 0; x < sizeX; x++)
   1.331 -			{
   1.332 -				if (systemRedShift > systemBlueShift)
   1.333 -				{
   1.334 -					*b++ = *pixU8++; // B
   1.335 -					*b++ = *pixU8++; // G
   1.336 -					*b++ = *pixU8++; // R
   1.337 -				}
   1.338 -				else
   1.339 -				{
   1.340 -					int red	  = *pixU8++;
   1.341 -					int green = *pixU8++;
   1.342 -					int blue  = *pixU8++;
   1.343 +	    b = writeBuffer;
   1.344 +	  }
   1.345 +	break;
   1.346 +      }
   1.347 +    case 24:
   1.348 +      {
   1.349 +	u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1);
   1.350 +	for (int y = 0; y < sizeY; y++)
   1.351 +	  {
   1.352 +	    for (int x = 0; x < sizeX; x++)
   1.353 +	      {
   1.354 +		if (systemRedShift > systemBlueShift)
   1.355 +		  {
   1.356 +		    *b++ = *pixU8++; // B
   1.357 +		    *b++ = *pixU8++; // G
   1.358 +		    *b++ = *pixU8++; // R
   1.359 +		  }
   1.360 +		else
   1.361 +		  {
   1.362 +		    int red	  = *pixU8++;
   1.363 +		    int green = *pixU8++;
   1.364 +		    int blue  = *pixU8++;
   1.365  
   1.366 -					*b++ = blue;
   1.367 -					*b++ = green;
   1.368 -					*b++ = red;
   1.369 -				}
   1.370 -			}
   1.371 -			pixU8 -= 2 * 3 * w;
   1.372 -			fwrite(writeBuffer, 1, 3 * w, fp);
   1.373 +		    *b++ = blue;
   1.374 +		    *b++ = green;
   1.375 +		    *b++ = red;
   1.376 +		  }
   1.377 +	      }
   1.378 +	    pixU8 -= 2 * 3 * w;
   1.379 +	    fwrite(writeBuffer, 1, 3 * w, fp);
   1.380  
   1.381 -			b = writeBuffer;
   1.382 -		}
   1.383 -		break;
   1.384 -	}
   1.385 -	case 32:
   1.386 -	{
   1.387 -		u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h));
   1.388 -		for (int y = 0; y < sizeY; y++)
   1.389 -		{
   1.390 -			for (int x = 0; x < sizeX; x++)
   1.391 -			{
   1.392 -				u32 v = *pixU32++;
   1.393 +	    b = writeBuffer;
   1.394 +	  }
   1.395 +	break;
   1.396 +      }
   1.397 +    case 32:
   1.398 +      {
   1.399 +	u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h));
   1.400 +	for (int y = 0; y < sizeY; y++)
   1.401 +	  {
   1.402 +	    for (int x = 0; x < sizeX; x++)
   1.403 +	      {
   1.404 +		u32 v = *pixU32++;
   1.405  
   1.406 -				*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.407 -				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.408 -				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.409 -			}
   1.410 -			pixU32++;
   1.411 -			pixU32 -= 2 * (w + 1);
   1.412 +		*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.413 +		*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.414 +		*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.415 +	      }
   1.416 +	    pixU32++;
   1.417 +	    pixU32 -= 2 * (w + 1);
   1.418  
   1.419 -			fwrite(writeBuffer, 1, 3 * w, fp);
   1.420 +	    fwrite(writeBuffer, 1, 3 * w, fp);
   1.421  
   1.422 -			b = writeBuffer;
   1.423 -		}
   1.424 -		break;
   1.425 -	}
   1.426 -	}
   1.427 +	    b = writeBuffer;
   1.428 +	  }
   1.429 +	break;
   1.430 +      }
   1.431 +    }
   1.432  #endif
   1.433  
   1.434 -	fclose(fp);
   1.435 +  fclose(fp);
   1.436  
   1.437 -	return true;
   1.438 +  return true;
   1.439  }
   1.440  
   1.441  bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix)
   1.442 @@ -426,403 +435,403 @@
   1.443  
   1.444  static int utilReadInt2(FILE *f)
   1.445  {
   1.446 -	int res = 0;
   1.447 -	int c	= fgetc(f);
   1.448 -	if (c == EOF)
   1.449 -		return -1;
   1.450 -	res = c;
   1.451 -	c	= fgetc(f);
   1.452 -	if (c == EOF)
   1.453 -		return -1;
   1.454 -	return c + (res << 8);
   1.455 +  int res = 0;
   1.456 +  int c	= fgetc(f);
   1.457 +  if (c == EOF)
   1.458 +    return -1;
   1.459 +  res = c;
   1.460 +  c	= fgetc(f);
   1.461 +  if (c == EOF)
   1.462 +    return -1;
   1.463 +  return c + (res << 8);
   1.464  }
   1.465  
   1.466  static int utilReadInt3(FILE *f)
   1.467  {
   1.468 -	int res = 0;
   1.469 -	int c	= fgetc(f);
   1.470 -	if (c == EOF)
   1.471 -		return -1;
   1.472 -	res = c;
   1.473 -	c	= fgetc(f);
   1.474 -	if (c == EOF)
   1.475 -		return -1;
   1.476 -	res = c + (res << 8);
   1.477 -	c	= fgetc(f);
   1.478 -	if (c == EOF)
   1.479 -		return -1;
   1.480 -	return c + (res << 8);
   1.481 +  int res = 0;
   1.482 +  int c	= fgetc(f);
   1.483 +  if (c == EOF)
   1.484 +    return -1;
   1.485 +  res = c;
   1.486 +  c	= fgetc(f);
   1.487 +  if (c == EOF)
   1.488 +    return -1;
   1.489 +  res = c + (res << 8);
   1.490 +  c	= fgetc(f);
   1.491 +  if (c == EOF)
   1.492 +    return -1;
   1.493 +  return c + (res << 8);
   1.494  }
   1.495  
   1.496  void utilApplyIPS(const char *ips, u8 * *r, int *s)
   1.497  {
   1.498 -	// from the IPS spec at http://zerosoft.zophar.net/ips.htm
   1.499 -	FILE *f = fopen(ips, "rb");
   1.500 -	if (!f)
   1.501 -		return;
   1.502 -	u8 *rom	 = *r;
   1.503 -	int size = *s;
   1.504 -	if (fgetc(f) == 'P' &&
   1.505 -	    fgetc(f) == 'A' &&
   1.506 -	    fgetc(f) == 'T' &&
   1.507 -	    fgetc(f) == 'C' &&
   1.508 -	    fgetc(f) == 'H')
   1.509 +  // from the IPS spec at http://zerosoft.zophar.net/ips.htm
   1.510 +  FILE *f = fopen(ips, "rb");
   1.511 +  if (!f)
   1.512 +    return;
   1.513 +  u8 *rom	 = *r;
   1.514 +  int size = *s;
   1.515 +  if (fgetc(f) == 'P' &&
   1.516 +      fgetc(f) == 'A' &&
   1.517 +      fgetc(f) == 'T' &&
   1.518 +      fgetc(f) == 'C' &&
   1.519 +      fgetc(f) == 'H')
   1.520 +    {
   1.521 +      int b;
   1.522 +      int offset;
   1.523 +      int len;
   1.524 +      for (;; )
   1.525  	{
   1.526 -		int b;
   1.527 -		int offset;
   1.528 -		int len;
   1.529 -		for (;; )
   1.530 +	  // read offset
   1.531 +	  offset = utilReadInt3(f);
   1.532 +	  // if offset == EOF, end of patch
   1.533 +	  if (offset == 0x454f46)
   1.534 +	    break;
   1.535 +	  // read length
   1.536 +	  len = utilReadInt2(f);
   1.537 +	  if (!len)
   1.538 +	    {
   1.539 +	      // len == 0, RLE block
   1.540 +	      len = utilReadInt2(f);
   1.541 +	      // byte to fill
   1.542 +	      int c = fgetc(f);
   1.543 +	      if (c == -1)
   1.544 +		break;
   1.545 +	      b = (u8)c;
   1.546 +	    }
   1.547 +	  else
   1.548 +	    b = -1;
   1.549 +	  // check if we need to reallocate our ROM
   1.550 +	  if ((offset + len) >= size)
   1.551 +	    {
   1.552 +	      size *= 2;
   1.553 +	      rom	  = (u8 *)realloc(rom, size);
   1.554 +	      *r	  = rom;
   1.555 +	      *s	  = size;
   1.556 +	    }
   1.557 +	  if (b == -1)
   1.558 +	    {
   1.559 +	      // normal block, just read the data
   1.560 +	      if (fread(&rom[offset], 1, len, f) != (size_t)len)
   1.561 +		break;
   1.562 +	    }
   1.563 +	  else
   1.564 +	    {
   1.565 +	      // fill the region with the given byte
   1.566 +	      while (len--)
   1.567  		{
   1.568 -			// read offset
   1.569 -			offset = utilReadInt3(f);
   1.570 -			// if offset == EOF, end of patch
   1.571 -			if (offset == 0x454f46)
   1.572 -				break;
   1.573 -			// read length
   1.574 -			len = utilReadInt2(f);
   1.575 -			if (!len)
   1.576 -			{
   1.577 -				// len == 0, RLE block
   1.578 -				len = utilReadInt2(f);
   1.579 -				// byte to fill
   1.580 -				int c = fgetc(f);
   1.581 -				if (c == -1)
   1.582 -					break;
   1.583 -				b = (u8)c;
   1.584 -			}
   1.585 -			else
   1.586 -				b = -1;
   1.587 -			// check if we need to reallocate our ROM
   1.588 -			if ((offset + len) >= size)
   1.589 -			{
   1.590 -				size *= 2;
   1.591 -				rom	  = (u8 *)realloc(rom, size);
   1.592 -				*r	  = rom;
   1.593 -				*s	  = size;
   1.594 -			}
   1.595 -			if (b == -1)
   1.596 -			{
   1.597 -				// normal block, just read the data
   1.598 -				if (fread(&rom[offset], 1, len, f) != (size_t)len)
   1.599 -					break;
   1.600 -			}
   1.601 -			else
   1.602 -			{
   1.603 -				// fill the region with the given byte
   1.604 -				while (len--)
   1.605 -				{
   1.606 -					rom[offset++] = b;
   1.607 -				}
   1.608 -			}
   1.609 +		  rom[offset++] = b;
   1.610  		}
   1.611 +	    }
   1.612  	}
   1.613 -	// close the file
   1.614 -	fclose(f);
   1.615 +    }
   1.616 +  // close the file
   1.617 +  fclose(f);
   1.618  }
   1.619  
   1.620  extern bool8 cpuIsMultiBoot;
   1.621  
   1.622  bool utilIsGBAImage(const char *file)
   1.623  {
   1.624 -	cpuIsMultiBoot = false;
   1.625 -	if (strlen(file) > 4)
   1.626 +  cpuIsMultiBoot = false;
   1.627 +  if (strlen(file) > 4)
   1.628 +    {
   1.629 +      const char *p = strrchr(file, '.');
   1.630 +
   1.631 +      if (p != NULL)
   1.632  	{
   1.633 -		const char *p = strrchr(file, '.');
   1.634 +	  if (_stricmp(p, ".gba") == 0)
   1.635 +	    return true;
   1.636 +	  if (_stricmp(p, ".agb") == 0)
   1.637 +	    return true;
   1.638 +	  if (_stricmp(p, ".bin") == 0)
   1.639 +	    return true;
   1.640 +	  if (_stricmp(p, ".elf") == 0)
   1.641 +	    return true;
   1.642 +	  if (_stricmp(p, ".mb") == 0)
   1.643 +	    {
   1.644 +	      cpuIsMultiBoot = true;
   1.645 +	      return true;
   1.646 +	    }
   1.647 +	}
   1.648 +    }
   1.649  
   1.650 -		if (p != NULL)
   1.651 -		{
   1.652 -			if (_stricmp(p, ".gba") == 0)
   1.653 -				return true;
   1.654 -			if (_stricmp(p, ".agb") == 0)
   1.655 -				return true;
   1.656 -			if (_stricmp(p, ".bin") == 0)
   1.657 -				return true;
   1.658 -			if (_stricmp(p, ".elf") == 0)
   1.659 -				return true;
   1.660 -			if (_stricmp(p, ".mb") == 0)
   1.661 -			{
   1.662 -				cpuIsMultiBoot = true;
   1.663 -				return true;
   1.664 -			}
   1.665 -		}
   1.666 -	}
   1.667 -
   1.668 -	return false;
   1.669 +  return false;
   1.670  }
   1.671  
   1.672  bool utilIsGBImage(const char *file)
   1.673  {
   1.674 -	if (strlen(file) > 4)
   1.675 +  if (strlen(file) > 4)
   1.676 +    {
   1.677 +      const char *p = strrchr(file, '.');
   1.678 +
   1.679 +      if (p != NULL)
   1.680  	{
   1.681 -		const char *p = strrchr(file, '.');
   1.682 +	  if (_stricmp(p, ".gb") == 0)
   1.683 +	    return true;
   1.684 +	  if (_stricmp(p, ".gbc") == 0)
   1.685 +	    return true;
   1.686 +	  if (_stricmp(p, ".cgb") == 0)
   1.687 +	    return true;
   1.688 +	  if (_stricmp(p, ".sgb") == 0)
   1.689 +	    return true;
   1.690 +	}
   1.691 +    }
   1.692  
   1.693 -		if (p != NULL)
   1.694 -		{
   1.695 -			if (_stricmp(p, ".gb") == 0)
   1.696 -				return true;
   1.697 -			if (_stricmp(p, ".gbc") == 0)
   1.698 -				return true;
   1.699 -			if (_stricmp(p, ".cgb") == 0)
   1.700 -				return true;
   1.701 -			if (_stricmp(p, ".sgb") == 0)
   1.702 -				return true;
   1.703 -		}
   1.704 -	}
   1.705 -
   1.706 -	return false;
   1.707 +  return false;
   1.708  }
   1.709  
   1.710  bool utilIsGBABios(const char *file)
   1.711  {
   1.712 -	if (strlen(file) > 4)
   1.713 +  if (strlen(file) > 4)
   1.714 +    {
   1.715 +      const char *p = strrchr(file, '.');
   1.716 +
   1.717 +      if (p != NULL)
   1.718  	{
   1.719 -		const char *p = strrchr(file, '.');
   1.720 +	  if (_stricmp(p, ".gba") == 0)
   1.721 +	    return true;
   1.722 +	  if (_stricmp(p, ".agb") == 0)
   1.723 +	    return true;
   1.724 +	  if (_stricmp(p, ".bin") == 0)
   1.725 +	    return true;
   1.726 +	  if (_stricmp(p, ".bios") == 0)
   1.727 +	    return true;
   1.728 +	  if (_stricmp(p, ".rom") == 0)
   1.729 +	    return true;
   1.730 +	}
   1.731 +    }
   1.732  
   1.733 -		if (p != NULL)
   1.734 -		{
   1.735 -			if (_stricmp(p, ".gba") == 0)
   1.736 -				return true;
   1.737 -			if (_stricmp(p, ".agb") == 0)
   1.738 -				return true;
   1.739 -			if (_stricmp(p, ".bin") == 0)
   1.740 -				return true;
   1.741 -			if (_stricmp(p, ".bios") == 0)
   1.742 -				return true;
   1.743 -			if (_stricmp(p, ".rom") == 0)
   1.744 -				return true;
   1.745 -		}
   1.746 -	}
   1.747 -
   1.748 -	return false;
   1.749 +  return false;
   1.750  }
   1.751  
   1.752  bool utilIsGBBios(const char *file)
   1.753  {
   1.754 -	if (strlen(file) > 4)
   1.755 +  if (strlen(file) > 4)
   1.756 +    {
   1.757 +      const char *p = strrchr(file, '.');
   1.758 +
   1.759 +      if (p != NULL)
   1.760  	{
   1.761 -		const char *p = strrchr(file, '.');
   1.762 +	  if (_stricmp(p, ".gb") == 0)
   1.763 +	    return true;
   1.764 +	  if (_stricmp(p, ".bin") == 0)
   1.765 +	    return true;
   1.766 +	  if (_stricmp(p, ".bios") == 0)
   1.767 +	    return true;
   1.768 +	  if (_stricmp(p, ".rom") == 0)
   1.769 +	    return true;
   1.770 +	}
   1.771 +    }
   1.772  
   1.773 -		if (p != NULL)
   1.774 -		{
   1.775 -			if (_stricmp(p, ".gb") == 0)
   1.776 -				return true;
   1.777 -			if (_stricmp(p, ".bin") == 0)
   1.778 -				return true;
   1.779 -			if (_stricmp(p, ".bios") == 0)
   1.780 -				return true;
   1.781 -			if (_stricmp(p, ".rom") == 0)
   1.782 -				return true;
   1.783 -		}
   1.784 -	}
   1.785 -
   1.786 -	return false;
   1.787 +  return false;
   1.788  }
   1.789  
   1.790  bool utilIsELF(const char *file)
   1.791  {
   1.792 -	if (strlen(file) > 4)
   1.793 +  if (strlen(file) > 4)
   1.794 +    {
   1.795 +      const char *p = strrchr(file, '.');
   1.796 +
   1.797 +      if (p != NULL)
   1.798  	{
   1.799 -		const char *p = strrchr(file, '.');
   1.800 -
   1.801 -		if (p != NULL)
   1.802 -		{
   1.803 -			if (_stricmp(p, ".elf") == 0)
   1.804 -				return true;
   1.805 -		}
   1.806 +	  if (_stricmp(p, ".elf") == 0)
   1.807 +	    return true;
   1.808  	}
   1.809 -	return false;
   1.810 +    }
   1.811 +  return false;
   1.812  }
   1.813  
   1.814  bool utilIsZipFile(const char *file)
   1.815  {
   1.816 -	if (strlen(file) > 4)
   1.817 +  if (strlen(file) > 4)
   1.818 +    {
   1.819 +      const char *p = strrchr(file, '.');
   1.820 +
   1.821 +      if (p != NULL)
   1.822  	{
   1.823 -		const char *p = strrchr(file, '.');
   1.824 +	  if (_stricmp(p, ".zip") == 0)
   1.825 +	    return true;
   1.826 +	}
   1.827 +    }
   1.828  
   1.829 -		if (p != NULL)
   1.830 -		{
   1.831 -			if (_stricmp(p, ".zip") == 0)
   1.832 -				return true;
   1.833 -		}
   1.834 -	}
   1.835 -
   1.836 -	return false;
   1.837 +  return false;
   1.838  }
   1.839  
   1.840  #if 0
   1.841  bool utilIsRarFile(const char *file)
   1.842  {
   1.843 -	if (strlen(file) > 4)
   1.844 +  if (strlen(file) > 4)
   1.845 +    {
   1.846 +      char *p = strrchr(file, '.');
   1.847 +
   1.848 +      if (p != NULL)
   1.849  	{
   1.850 -		char *p = strrchr(file, '.');
   1.851 +	  if (_stricmp(p, ".rar") == 0)
   1.852 +	    return true;
   1.853 +	}
   1.854 +    }
   1.855  
   1.856 -		if (p != NULL)
   1.857 -		{
   1.858 -			if (_stricmp(p, ".rar") == 0)
   1.859 -				return true;
   1.860 -		}
   1.861 -	}
   1.862 -
   1.863 -	return false;
   1.864 +  return false;
   1.865  }
   1.866  
   1.867  #endif
   1.868  
   1.869  bool utilIsGzipFile(const char *file)
   1.870  {
   1.871 -	if (strlen(file) > 3)
   1.872 +  if (strlen(file) > 3)
   1.873 +    {
   1.874 +      const char *p = strrchr(file, '.');
   1.875 +
   1.876 +      if (p != NULL)
   1.877  	{
   1.878 -		const char *p = strrchr(file, '.');
   1.879 +	  if (_stricmp(p, ".gz") == 0)
   1.880 +	    return true;
   1.881 +	  if (_stricmp(p, ".z") == 0)
   1.882 +	    return true;
   1.883 +	}
   1.884 +    }
   1.885  
   1.886 -		if (p != NULL)
   1.887 -		{
   1.888 -			if (_stricmp(p, ".gz") == 0)
   1.889 -				return true;
   1.890 -			if (_stricmp(p, ".z") == 0)
   1.891 -				return true;
   1.892 -		}
   1.893 -	}
   1.894 -
   1.895 -	return false;
   1.896 +  return false;
   1.897  }
   1.898  
   1.899  void utilGetBaseName(const char *file, char *buffer)
   1.900  {
   1.901 -	strcpy(buffer, file);
   1.902 +  strcpy(buffer, file);
   1.903  
   1.904 -	if (utilIsGzipFile(file))
   1.905 -	{
   1.906 -		char *p = strrchr(buffer, '.');
   1.907 +  if (utilIsGzipFile(file))
   1.908 +    {
   1.909 +      char *p = strrchr(buffer, '.');
   1.910  
   1.911 -		if (p)
   1.912 -			*p = 0;
   1.913 -	}
   1.914 +      if (p)
   1.915 +	*p = 0;
   1.916 +    }
   1.917  }
   1.918  
   1.919  IMAGE_TYPE utilFindType(const char *file)
   1.920  {
   1.921 -	char buffer[2048];
   1.922 +  char buffer[2048];
   1.923  
   1.924 -	if (utilIsZipFile(file))
   1.925 +  if (utilIsZipFile(file))
   1.926 +    {
   1.927 +      unzFile unz = unzOpen(file);
   1.928 +
   1.929 +      if (unz == NULL)
   1.930  	{
   1.931 -		unzFile unz = unzOpen(file);
   1.932 +	  systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file);
   1.933 +	  return IMAGE_UNKNOWN;
   1.934 +	}
   1.935  
   1.936 -		if (unz == NULL)
   1.937 +      int r = unzGoToFirstFile(unz);
   1.938 +
   1.939 +      if (r != UNZ_OK)
   1.940 +	{
   1.941 +	  unzClose(unz);
   1.942 +	  systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
   1.943 +	  return IMAGE_UNKNOWN;
   1.944 +	}
   1.945 +
   1.946 +      IMAGE_TYPE found = IMAGE_UNKNOWN;
   1.947 +
   1.948 +      unz_file_info info;
   1.949 +
   1.950 +      while (true)
   1.951 +	{
   1.952 +	  r = unzGetCurrentFileInfo(unz,
   1.953 +				    &info,
   1.954 +				    buffer,
   1.955 +				    sizeof(buffer),
   1.956 +				    NULL,
   1.957 +				    0,
   1.958 +				    NULL,
   1.959 +				    0);
   1.960 +
   1.961 +	  if (r != UNZ_OK)
   1.962 +	    {
   1.963 +	      unzClose(unz);
   1.964 +	      systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
   1.965 +	      return IMAGE_UNKNOWN;
   1.966 +	    }
   1.967 +
   1.968 +	  if (utilIsGBAImage(buffer))
   1.969 +	    {
   1.970 +	      found = IMAGE_GBA;
   1.971 +	      break;
   1.972 +	    }
   1.973 +
   1.974 +	  if (utilIsGBImage(buffer))
   1.975 +	    {
   1.976 +	      found = IMAGE_GB;
   1.977 +	      break;
   1.978 +	    }
   1.979 +
   1.980 +	  r = unzGoToNextFile(unz);
   1.981 +
   1.982 +	  if (r != UNZ_OK)
   1.983 +	    break;
   1.984 +	}
   1.985 +      unzClose(unz);
   1.986 +
   1.987 +      if (found == IMAGE_UNKNOWN)
   1.988 +	{
   1.989 +	  systemMessage(MSG_NO_IMAGE_ON_ZIP,
   1.990 +			N_("No image found on ZIP file %s"), file);
   1.991 +	  return found;
   1.992 +	}
   1.993 +      return found;
   1.994 +#if 0
   1.995 +    }
   1.996 +  else if (utilIsRarFile(file))
   1.997 +    {
   1.998 +      IMAGE_TYPE found = IMAGE_UNKNOWN;
   1.999 +
  1.1000 +      ArchiveList_struct *rarList = NULL;
  1.1001 +      if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList))
  1.1002 +	{
  1.1003 +	  ArchiveList_struct *p = rarList;
  1.1004 +
  1.1005 +	  while (p)
  1.1006 +	    {
  1.1007 +	      if (utilIsGBAImage(p->item.Name))
  1.1008  		{
  1.1009 -			systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file);
  1.1010 -			return IMAGE_UNKNOWN;
  1.1011 +		  found = IMAGE_GBA;
  1.1012 +		  break;
  1.1013  		}
  1.1014  
  1.1015 -		int r = unzGoToFirstFile(unz);
  1.1016 +	      if (utilIsGBImage(p->item.Name))
  1.1017 +		{
  1.1018 +		  found = IMAGE_GB;
  1.1019 +		  break;
  1.1020 +		}
  1.1021 +	      p = p->next;
  1.1022 +	    }
  1.1023  
  1.1024 -		if (r != UNZ_OK)
  1.1025 -		{
  1.1026 -			unzClose(unz);
  1.1027 -			systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
  1.1028 -			return IMAGE_UNKNOWN;
  1.1029 -		}
  1.1030 +	  urarlib_freelist(rarList);
  1.1031 +	}
  1.1032 +      return found;
  1.1033 +#endif
  1.1034 +    }
  1.1035 +  else
  1.1036 +    {
  1.1037 +      if (utilIsGzipFile(file))
  1.1038 +	utilGetBaseName(file, buffer);
  1.1039 +      else
  1.1040 +	strcpy(buffer, file);
  1.1041  
  1.1042 -		IMAGE_TYPE found = IMAGE_UNKNOWN;
  1.1043 -
  1.1044 -		unz_file_info info;
  1.1045 -
  1.1046 -		while (true)
  1.1047 -		{
  1.1048 -			r = unzGetCurrentFileInfo(unz,
  1.1049 -			                          &info,
  1.1050 -			                          buffer,
  1.1051 -			                          sizeof(buffer),
  1.1052 -			                          NULL,
  1.1053 -			                          0,
  1.1054 -			                          NULL,
  1.1055 -			                          0);
  1.1056 -
  1.1057 -			if (r != UNZ_OK)
  1.1058 -			{
  1.1059 -				unzClose(unz);
  1.1060 -				systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
  1.1061 -				return IMAGE_UNKNOWN;
  1.1062 -			}
  1.1063 -
  1.1064 -			if (utilIsGBAImage(buffer))
  1.1065 -			{
  1.1066 -				found = IMAGE_GBA;
  1.1067 -				break;
  1.1068 -			}
  1.1069 -
  1.1070 -			if (utilIsGBImage(buffer))
  1.1071 -			{
  1.1072 -				found = IMAGE_GB;
  1.1073 -				break;
  1.1074 -			}
  1.1075 -
  1.1076 -			r = unzGoToNextFile(unz);
  1.1077 -
  1.1078 -			if (r != UNZ_OK)
  1.1079 -				break;
  1.1080 -		}
  1.1081 -		unzClose(unz);
  1.1082 -
  1.1083 -		if (found == IMAGE_UNKNOWN)
  1.1084 -		{
  1.1085 -			systemMessage(MSG_NO_IMAGE_ON_ZIP,
  1.1086 -			              N_("No image found on ZIP file %s"), file);
  1.1087 -			return found;
  1.1088 -		}
  1.1089 -		return found;
  1.1090 -#if 0
  1.1091 -	}
  1.1092 -	else if (utilIsRarFile(file))
  1.1093 -	{
  1.1094 -		IMAGE_TYPE found = IMAGE_UNKNOWN;
  1.1095 -
  1.1096 -		ArchiveList_struct *rarList = NULL;
  1.1097 -		if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList))
  1.1098 -		{
  1.1099 -			ArchiveList_struct *p = rarList;
  1.1100 -
  1.1101 -			while (p)
  1.1102 -			{
  1.1103 -				if (utilIsGBAImage(p->item.Name))
  1.1104 -				{
  1.1105 -					found = IMAGE_GBA;
  1.1106 -					break;
  1.1107 -				}
  1.1108 -
  1.1109 -				if (utilIsGBImage(p->item.Name))
  1.1110 -				{
  1.1111 -					found = IMAGE_GB;
  1.1112 -					break;
  1.1113 -				}
  1.1114 -				p = p->next;
  1.1115 -			}
  1.1116 -
  1.1117 -			urarlib_freelist(rarList);
  1.1118 -		}
  1.1119 -		return found;
  1.1120 -#endif
  1.1121 -	}
  1.1122 -	else
  1.1123 -	{
  1.1124 -		if (utilIsGzipFile(file))
  1.1125 -			utilGetBaseName(file, buffer);
  1.1126 -		else
  1.1127 -			strcpy(buffer, file);
  1.1128 -
  1.1129 -		if (utilIsGBAImage(buffer))
  1.1130 -			return IMAGE_GBA;
  1.1131 -		if (utilIsGBImage(buffer))
  1.1132 -			return IMAGE_GB;
  1.1133 -	}
  1.1134 -	return IMAGE_UNKNOWN;
  1.1135 +      if (utilIsGBAImage(buffer))
  1.1136 +	return IMAGE_GBA;
  1.1137 +      if (utilIsGBImage(buffer))
  1.1138 +	return IMAGE_GB;
  1.1139 +    }
  1.1140 +  return IMAGE_UNKNOWN;
  1.1141  }
  1.1142  
  1.1143  static int utilGetSize(int size)
  1.1144  {
  1.1145 -	int res = 1;
  1.1146 -	while (res < size)
  1.1147 -		res <<= 1;
  1.1148 -	return res;
  1.1149 +  int res = 1;
  1.1150 +  while (res < size)
  1.1151 +    res <<= 1;
  1.1152 +  return res;
  1.1153  }
  1.1154  
  1.1155  static u8 *utilLoadFromZip(const char *file,
  1.1156 @@ -830,113 +839,113 @@
  1.1157                             u8 *data,
  1.1158                             int &size)
  1.1159  {
  1.1160 -	char buffer[2048];
  1.1161 +  char buffer[2048];
  1.1162  
  1.1163 -	unzFile unz = unzOpen(file);
  1.1164 +  unzFile unz = unzOpen(file);
  1.1165  
  1.1166 -	if (unz == NULL)
  1.1167 +  if (unz == NULL)
  1.1168 +    {
  1.1169 +      systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file);
  1.1170 +      return NULL;
  1.1171 +    }
  1.1172 +  int r = unzGoToFirstFile(unz);
  1.1173 +
  1.1174 +  if (r != UNZ_OK)
  1.1175 +    {
  1.1176 +      unzClose(unz);
  1.1177 +      systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
  1.1178 +      return NULL;
  1.1179 +    }
  1.1180 +
  1.1181 +  bool found = false;
  1.1182 +
  1.1183 +  unz_file_info info;
  1.1184 +
  1.1185 +  while (true)
  1.1186 +    {
  1.1187 +      r = unzGetCurrentFileInfo(unz,
  1.1188 +				&info,
  1.1189 +				buffer,
  1.1190 +				sizeof(buffer),
  1.1191 +				NULL,
  1.1192 +				0,
  1.1193 +				NULL,
  1.1194 +				0);
  1.1195 +
  1.1196 +      if (r != UNZ_OK)
  1.1197  	{
  1.1198 -		systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file);
  1.1199 -		return NULL;
  1.1200 -	}
  1.1201 -	int r = unzGoToFirstFile(unz);
  1.1202 -
  1.1203 -	if (r != UNZ_OK)
  1.1204 -	{
  1.1205 -		unzClose(unz);
  1.1206 -		systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
  1.1207 -		return NULL;
  1.1208 +	  unzClose(unz);
  1.1209 +	  systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
  1.1210 +	  return NULL;
  1.1211  	}
  1.1212  
  1.1213 -	bool found = false;
  1.1214 -
  1.1215 -	unz_file_info info;
  1.1216 -
  1.1217 -	while (true)
  1.1218 +      if (accept(buffer))
  1.1219  	{
  1.1220 -		r = unzGetCurrentFileInfo(unz,
  1.1221 -		                          &info,
  1.1222 -		                          buffer,
  1.1223 -		                          sizeof(buffer),
  1.1224 -		                          NULL,
  1.1225 -		                          0,
  1.1226 -		                          NULL,
  1.1227 -		                          0);
  1.1228 -
  1.1229 -		if (r != UNZ_OK)
  1.1230 -		{
  1.1231 -			unzClose(unz);
  1.1232 -			systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
  1.1233 -			return NULL;
  1.1234 -		}
  1.1235 -
  1.1236 -		if (accept(buffer))
  1.1237 -		{
  1.1238 -			found = true;
  1.1239 -			break;
  1.1240 -		}
  1.1241 -
  1.1242 -		r = unzGoToNextFile(unz);
  1.1243 -
  1.1244 -		if (r != UNZ_OK)
  1.1245 -			break;
  1.1246 +	  found = true;
  1.1247 +	  break;
  1.1248  	}
  1.1249  
  1.1250 -	if (!found)
  1.1251 +      r = unzGoToNextFile(unz);
  1.1252 +
  1.1253 +      if (r != UNZ_OK)
  1.1254 +	break;
  1.1255 +    }
  1.1256 +
  1.1257 +  if (!found)
  1.1258 +    {
  1.1259 +      unzClose(unz);
  1.1260 +      systemMessage(MSG_NO_IMAGE_ON_ZIP,
  1.1261 +		    N_("No image found on ZIP file %s"), file);
  1.1262 +      return NULL;
  1.1263 +    }
  1.1264 +
  1.1265 +  int fileSize = info.uncompressed_size;
  1.1266 +  if (size == 0)
  1.1267 +    size = fileSize;
  1.1268 +  r = unzOpenCurrentFile(unz);
  1.1269 +
  1.1270 +  if (r != UNZ_OK)
  1.1271 +    {
  1.1272 +      unzClose(unz);
  1.1273 +      systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), buffer);
  1.1274 +      return NULL;
  1.1275 +    }
  1.1276 +
  1.1277 +  u8 *image = data;
  1.1278 +
  1.1279 +  if (image == NULL)
  1.1280 +    {
  1.1281 +      image = (u8 *)malloc(utilGetSize(size));
  1.1282 +      if (image == NULL)
  1.1283  	{
  1.1284 -		unzClose(unz);
  1.1285 -		systemMessage(MSG_NO_IMAGE_ON_ZIP,
  1.1286 -		              N_("No image found on ZIP file %s"), file);
  1.1287 -		return NULL;
  1.1288 +	  unzCloseCurrentFile(unz);
  1.1289 +	  unzClose(unz);
  1.1290 +	  systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1291 +			"data");
  1.1292 +	  return NULL;
  1.1293  	}
  1.1294 +      size = fileSize;
  1.1295 +    }
  1.1296 +  int read = fileSize <= size ? fileSize : size;
  1.1297 +  r = unzReadCurrentFile(unz,
  1.1298 +			 image,
  1.1299 +			 read);
  1.1300  
  1.1301 -	int fileSize = info.uncompressed_size;
  1.1302 -	if (size == 0)
  1.1303 -		size = fileSize;
  1.1304 -	r = unzOpenCurrentFile(unz);
  1.1305 +  unzCloseCurrentFile(unz);
  1.1306 +  unzClose(unz);
  1.1307  
  1.1308 -	if (r != UNZ_OK)
  1.1309 -	{
  1.1310 -		unzClose(unz);
  1.1311 -		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), buffer);
  1.1312 -		return NULL;
  1.1313 -	}
  1.1314 +  if (r != (int)read)
  1.1315 +    {
  1.1316 +      systemMessage(MSG_ERROR_READING_IMAGE,
  1.1317 +		    N_("Error reading image %s"), buffer);
  1.1318 +      if (data == NULL)
  1.1319 +	free(image);
  1.1320 +      return NULL;
  1.1321 +    }
  1.1322  
  1.1323 -	u8 *image = data;
  1.1324 +  size = fileSize;
  1.1325  
  1.1326 -	if (image == NULL)
  1.1327 -	{
  1.1328 -		image = (u8 *)malloc(utilGetSize(size));
  1.1329 -		if (image == NULL)
  1.1330 -		{
  1.1331 -			unzCloseCurrentFile(unz);
  1.1332 -			unzClose(unz);
  1.1333 -			systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1334 -			              "data");
  1.1335 -			return NULL;
  1.1336 -		}
  1.1337 -		size = fileSize;
  1.1338 -	}
  1.1339 -	int read = fileSize <= size ? fileSize : size;
  1.1340 -	r = unzReadCurrentFile(unz,
  1.1341 -	                       image,
  1.1342 -	                       read);
  1.1343 -
  1.1344 -	unzCloseCurrentFile(unz);
  1.1345 -	unzClose(unz);
  1.1346 -
  1.1347 -	if (r != (int)read)
  1.1348 -	{
  1.1349 -		systemMessage(MSG_ERROR_READING_IMAGE,
  1.1350 -		              N_("Error reading image %s"), buffer);
  1.1351 -		if (data == NULL)
  1.1352 -			free(image);
  1.1353 -		return NULL;
  1.1354 -	}
  1.1355 -
  1.1356 -	size = fileSize;
  1.1357 -
  1.1358 -	return image;
  1.1359 +  return image;
  1.1360  }
  1.1361  
  1.1362  static u8 *utilLoadGzipFile(const char *file,
  1.1363 @@ -944,59 +953,59 @@
  1.1364                              u8 *data,
  1.1365                              int &size)
  1.1366  {
  1.1367 -	FILE *f = fopen(file, "rb");
  1.1368 +  FILE *f = fopen(file, "rb");
  1.1369  
  1.1370 -	if (f == NULL)
  1.1371 +  if (f == NULL)
  1.1372 +    {
  1.1373 +      systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1374 +      return NULL;
  1.1375 +    }
  1.1376 +
  1.1377 +  fseek(f, -4, SEEK_END);
  1.1378 +  int fileSize = fgetc(f) | (fgetc(f) << 8) | (fgetc(f) << 16) | (fgetc(f) << 24);
  1.1379 +  fclose(f);
  1.1380 +  if (size == 0)
  1.1381 +    size = fileSize;
  1.1382 +
  1.1383 +  gzFile gz = gzopen(file, "rb");
  1.1384 +
  1.1385 +  if (gz == NULL)
  1.1386 +    {
  1.1387 +      // should not happen, but who knows?
  1.1388 +      systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1389 +      return NULL;
  1.1390 +    }
  1.1391 +
  1.1392 +  u8 *image = data;
  1.1393 +
  1.1394 +  if (image == NULL)
  1.1395 +    {
  1.1396 +      image = (u8 *)malloc(utilGetSize(size));
  1.1397 +      if (image == NULL)
  1.1398  	{
  1.1399 -		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1400 -		return NULL;
  1.1401 +	  systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1402 +			"data");
  1.1403 +	  fclose(f);
  1.1404 +	  return NULL;
  1.1405  	}
  1.1406 +      size = fileSize;
  1.1407 +    }
  1.1408 +  int read = fileSize <= size ? fileSize : size;
  1.1409 +  int r	 = gzread(gz, image, read);
  1.1410 +  gzclose(gz);
  1.1411  
  1.1412 -	fseek(f, -4, SEEK_END);
  1.1413 -	int fileSize = fgetc(f) | (fgetc(f) << 8) | (fgetc(f) << 16) | (fgetc(f) << 24);
  1.1414 -	fclose(f);
  1.1415 -	if (size == 0)
  1.1416 -		size = fileSize;
  1.1417 +  if (r != (int)read)
  1.1418 +    {
  1.1419 +      systemMessage(MSG_ERROR_READING_IMAGE,
  1.1420 +		    N_("Error reading image %s"), file);
  1.1421 +      if (data == NULL)
  1.1422 +	free(image);
  1.1423 +      return NULL;
  1.1424 +    }
  1.1425  
  1.1426 -	gzFile gz = gzopen(file, "rb");
  1.1427 +  size = fileSize;
  1.1428  
  1.1429 -	if (gz == NULL)
  1.1430 -	{
  1.1431 -		// should not happen, but who knows?
  1.1432 -		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1433 -		return NULL;
  1.1434 -	}
  1.1435 -
  1.1436 -	u8 *image = data;
  1.1437 -
  1.1438 -	if (image == NULL)
  1.1439 -	{
  1.1440 -		image = (u8 *)malloc(utilGetSize(size));
  1.1441 -		if (image == NULL)
  1.1442 -		{
  1.1443 -			systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1444 -			              "data");
  1.1445 -			fclose(f);
  1.1446 -			return NULL;
  1.1447 -		}
  1.1448 -		size = fileSize;
  1.1449 -	}
  1.1450 -	int read = fileSize <= size ? fileSize : size;
  1.1451 -	int r	 = gzread(gz, image, read);
  1.1452 -	gzclose(gz);
  1.1453 -
  1.1454 -	if (r != (int)read)
  1.1455 -	{
  1.1456 -		systemMessage(MSG_ERROR_READING_IMAGE,
  1.1457 -		              N_("Error reading image %s"), file);
  1.1458 -		if (data == NULL)
  1.1459 -			free(image);
  1.1460 -		return NULL;
  1.1461 -	}
  1.1462 -
  1.1463 -	size = fileSize;
  1.1464 -
  1.1465 -	return image;
  1.1466 +  return image;
  1.1467  }
  1.1468  
  1.1469  #if 0
  1.1470 @@ -1005,52 +1014,52 @@
  1.1471                             u8 *data,
  1.1472                             int &size)
  1.1473  {
  1.1474 -	char buffer[2048];
  1.1475 +  char buffer[2048];
  1.1476  
  1.1477 -	ArchiveList_struct *rarList = NULL;
  1.1478 -	if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList))
  1.1479 +  ArchiveList_struct *rarList = NULL;
  1.1480 +  if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList))
  1.1481 +    {
  1.1482 +      ArchiveList_struct *p = rarList;
  1.1483 +
  1.1484 +      bool found = false;
  1.1485 +      while (p)
  1.1486  	{
  1.1487 -		ArchiveList_struct *p = rarList;
  1.1488 -
  1.1489 -		bool found = false;
  1.1490 -		while (p)
  1.1491 -		{
  1.1492 -			if (accept(p->item.Name))
  1.1493 -			{
  1.1494 -				strcpy(buffer, p->item.Name);
  1.1495 -				found = true;
  1.1496 -				break;
  1.1497 -			}
  1.1498 -			p = p->next;
  1.1499 -		}
  1.1500 -		if (found)
  1.1501 -		{
  1.1502 -			void *memory		= NULL;
  1.1503 -			unsigned long lsize = 0;
  1.1504 -			size = p->item.UnpSize;
  1.1505 -			int r = urarlib_get((void *)&memory, &lsize, buffer, (void *)file, "");
  1.1506 -			if (!r)
  1.1507 -			{
  1.1508 -				systemMessage(MSG_ERROR_READING_IMAGE,
  1.1509 -				              N_("Error reading image %s"), buffer);
  1.1510 -				urarlib_freelist(rarList);
  1.1511 -				return NULL;
  1.1512 -			}
  1.1513 -			u8 *image = (u8 *)memory;
  1.1514 -			if (data != NULL)
  1.1515 -			{
  1.1516 -				memcpy(image, data, size);
  1.1517 -			}
  1.1518 -			urarlib_freelist(rarList);
  1.1519 -			return image;
  1.1520 -		}
  1.1521 -		systemMessage(MSG_NO_IMAGE_ON_ZIP,
  1.1522 -		              N_("No image found on RAR file %s"), file);
  1.1523 -		urarlib_freelist(rarList);
  1.1524 -		return NULL;
  1.1525 +	  if (accept(p->item.Name))
  1.1526 +	    {
  1.1527 +	      strcpy(buffer, p->item.Name);
  1.1528 +	      found = true;
  1.1529 +	      break;
  1.1530 +	    }
  1.1531 +	  p = p->next;
  1.1532  	}
  1.1533 -	// nothing found
  1.1534 -	return NULL;
  1.1535 +      if (found)
  1.1536 +	{
  1.1537 +	  void *memory		= NULL;
  1.1538 +	  unsigned long lsize = 0;
  1.1539 +	  size = p->item.UnpSize;
  1.1540 +	  int r = urarlib_get((void *)&memory, &lsize, buffer, (void *)file, "");
  1.1541 +	  if (!r)
  1.1542 +	    {
  1.1543 +	      systemMessage(MSG_ERROR_READING_IMAGE,
  1.1544 +			    N_("Error reading image %s"), buffer);
  1.1545 +	      urarlib_freelist(rarList);
  1.1546 +	      return NULL;
  1.1547 +	    }
  1.1548 +	  u8 *image = (u8 *)memory;
  1.1549 +	  if (data != NULL)
  1.1550 +	    {
  1.1551 +	      memcpy(image, data, size);
  1.1552 +	    }
  1.1553 +	  urarlib_freelist(rarList);
  1.1554 +	  return image;
  1.1555 +	}
  1.1556 +      systemMessage(MSG_NO_IMAGE_ON_ZIP,
  1.1557 +		    N_("No image found on RAR file %s"), file);
  1.1558 +      urarlib_freelist(rarList);
  1.1559 +      return NULL;
  1.1560 +    }
  1.1561 +  // nothing found
  1.1562 +  return NULL;
  1.1563  }
  1.1564  
  1.1565  #endif
  1.1566 @@ -1061,245 +1070,245 @@
  1.1567               u8 *data,
  1.1568               int &size)
  1.1569  {
  1.1570 -	if (utilIsZipFile(file))
  1.1571 -	{
  1.1572 -		return utilLoadFromZip(file, accept, data, size);
  1.1573 -	}
  1.1574 -	if (utilIsGzipFile(file))
  1.1575 -	{
  1.1576 -		return utilLoadGzipFile(file, accept, data, size);
  1.1577 -	}
  1.1578 +  if (utilIsZipFile(file))
  1.1579 +    {
  1.1580 +      return utilLoadFromZip(file, accept, data, size);
  1.1581 +    }
  1.1582 +  if (utilIsGzipFile(file))
  1.1583 +    {
  1.1584 +      return utilLoadGzipFile(file, accept, data, size);
  1.1585 +    }
  1.1586  #if 0
  1.1587 -	if (utilIsRarFile(file))
  1.1588 -	{
  1.1589 -		return utilLoadRarFile(file, accept, data, size);
  1.1590 -	}
  1.1591 +  if (utilIsRarFile(file))
  1.1592 +    {
  1.1593 +      return utilLoadRarFile(file, accept, data, size);
  1.1594 +    }
  1.1595  #endif
  1.1596  
  1.1597 -	u8 *image = data;
  1.1598 +  u8 *image = data;
  1.1599  
  1.1600 -	FILE *f = fopen(file, "rb");
  1.1601 +  FILE *f = fopen(file, "rb");
  1.1602  
  1.1603 -	if (!f)
  1.1604 +  if (!f)
  1.1605 +    {
  1.1606 +      systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1607 +      return NULL;
  1.1608 +    }
  1.1609 +
  1.1610 +  fseek(f, 0, SEEK_END);
  1.1611 +  int fileSize = ftell(f);
  1.1612 +  fseek(f, 0, SEEK_SET);
  1.1613 +  if (size == 0)
  1.1614 +    size = fileSize;
  1.1615 +
  1.1616 +  if (image == NULL)
  1.1617 +    {
  1.1618 +      image = (u8 *)malloc(utilGetSize(size));
  1.1619 +      if (image == NULL)
  1.1620  	{
  1.1621 -		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1622 -		return NULL;
  1.1623 +	  systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1624 +			"data");
  1.1625 +	  fclose(f);
  1.1626 +	  return NULL;
  1.1627  	}
  1.1628 +      size = fileSize;
  1.1629 +    }
  1.1630 +  int read = fileSize <= size ? fileSize : size;
  1.1631 +  int r	 = fread(image, 1, read, f);
  1.1632 +  fclose(f);
  1.1633  
  1.1634 -	fseek(f, 0, SEEK_END);
  1.1635 -	int fileSize = ftell(f);
  1.1636 -	fseek(f, 0, SEEK_SET);
  1.1637 -	if (size == 0)
  1.1638 -		size = fileSize;
  1.1639 +  if (r != (int)read)
  1.1640 +    {
  1.1641 +      systemMessage(MSG_ERROR_READING_IMAGE,
  1.1642 +		    N_("Error reading image %s"), file);
  1.1643 +      if (data == NULL)
  1.1644 +	free(image);
  1.1645 +      return NULL;
  1.1646 +    }
  1.1647  
  1.1648 -	if (image == NULL)
  1.1649 -	{
  1.1650 -		image = (u8 *)malloc(utilGetSize(size));
  1.1651 -		if (image == NULL)
  1.1652 -		{
  1.1653 -			systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1654 -			              "data");
  1.1655 -			fclose(f);
  1.1656 -			return NULL;
  1.1657 -		}
  1.1658 -		size = fileSize;
  1.1659 -	}
  1.1660 -	int read = fileSize <= size ? fileSize : size;
  1.1661 -	int r	 = fread(image, 1, read, f);
  1.1662 -	fclose(f);
  1.1663 +  size = fileSize;
  1.1664  
  1.1665 -	if (r != (int)read)
  1.1666 -	{
  1.1667 -		systemMessage(MSG_ERROR_READING_IMAGE,
  1.1668 -		              N_("Error reading image %s"), file);
  1.1669 -		if (data == NULL)
  1.1670 -			free(image);
  1.1671 -		return NULL;
  1.1672 -	}
  1.1673 -
  1.1674 -	size = fileSize;
  1.1675 -
  1.1676 -	return image;
  1.1677 +  return image;
  1.1678  }
  1.1679  
  1.1680  void utilWriteInt(gzFile gzFile, int32 i)
  1.1681  {
  1.1682 -	utilGzWrite(gzFile, &i, sizeof(int32));
  1.1683 +  utilGzWrite(gzFile, &i, sizeof(int32));
  1.1684  }
  1.1685  
  1.1686  int32 utilReadInt(gzFile gzFile)
  1.1687  {
  1.1688 -	int32 i = 0;
  1.1689 -	utilGzRead(gzFile, &i, sizeof(int32));
  1.1690 -	return i;
  1.1691 +  int32 i = 0;
  1.1692 +  utilGzRead(gzFile, &i, sizeof(int32));
  1.1693 +  return i;
  1.1694  }
  1.1695  
  1.1696  void utilReadData(gzFile gzFile, variable_desc *data)
  1.1697  {
  1.1698 -	while (data->address)
  1.1699 -	{
  1.1700 -		utilGzRead(gzFile, data->address, data->size);
  1.1701 -		data++;
  1.1702 -	}
  1.1703 +  while (data->address)
  1.1704 +    {
  1.1705 +      utilGzRead(gzFile, data->address, data->size);
  1.1706 +      data++;
  1.1707 +    }
  1.1708  }
  1.1709  
  1.1710  void utilWriteData(gzFile gzFile, variable_desc *data)
  1.1711  {
  1.1712 -	while (data->address)
  1.1713 -	{
  1.1714 -		utilGzWrite(gzFile, data->address, data->size);
  1.1715 -		data++;
  1.1716 -	}
  1.1717 +  while (data->address)
  1.1718 +    {
  1.1719 +      utilGzWrite(gzFile, data->address, data->size);
  1.1720 +      data++;
  1.1721 +    }
  1.1722  }
  1.1723  
  1.1724  gzFile utilGzOpen(const char *file, const char *mode)
  1.1725  {
  1.1726 -	utilGzWriteFunc = gzWrite;
  1.1727 -	utilGzReadFunc	= gzread;
  1.1728 -	utilGzCloseFunc = gzclose;
  1.1729 -	utilGzSeekFunc	= gzseek;
  1.1730 -	utilGzTellFunc	= gztell;
  1.1731 +  utilGzWriteFunc = gzWrite;
  1.1732 +  utilGzReadFunc	= gzread;
  1.1733 +  utilGzCloseFunc = gzclose;
  1.1734 +  utilGzSeekFunc	= gzseek;
  1.1735 +  utilGzTellFunc	= gztell;
  1.1736  
  1.1737 -	return gzopen(file, mode);
  1.1738 +  return gzopen(file, mode);
  1.1739  }
  1.1740  
  1.1741  gzFile utilGzReopen(int id, const char *mode)
  1.1742  {
  1.1743 -	utilGzWriteFunc = gzWrite;
  1.1744 -	utilGzReadFunc	= gzread;
  1.1745 -	utilGzCloseFunc = gzclose;
  1.1746 -	utilGzSeekFunc	= gzseek;
  1.1747 -	utilGzTellFunc	= gztell;
  1.1748 +  utilGzWriteFunc = gzWrite;
  1.1749 +  utilGzReadFunc	= gzread;
  1.1750 +  utilGzCloseFunc = gzclose;
  1.1751 +  utilGzSeekFunc	= gzseek;
  1.1752 +  utilGzTellFunc	= gztell;
  1.1753  
  1.1754 -	return gzdopen(id, mode);
  1.1755 +  return gzdopen(id, mode);
  1.1756  }
  1.1757  
  1.1758  gzFile utilMemGzOpen(char *memory, int available, char *mode)
  1.1759  {
  1.1760 -	utilGzWriteFunc = memgzwrite;
  1.1761 -	utilGzReadFunc	= memgzread;
  1.1762 -	utilGzCloseFunc = memgzclose;
  1.1763 -	utilGzSeekFunc	= NULL;	// FIXME: not implemented...
  1.1764 -	utilGzTellFunc	= memtell;
  1.1765 +  utilGzWriteFunc = memgzwrite;
  1.1766 +  utilGzReadFunc	= memgzread;
  1.1767 +  utilGzCloseFunc = memgzclose;
  1.1768 +  utilGzSeekFunc	= NULL;	// FIXME: not implemented...
  1.1769 +  utilGzTellFunc	= memtell;
  1.1770  
  1.1771 -	return memgzopen(memory, available, mode);
  1.1772 +  return memgzopen(memory, available, mode);
  1.1773  }
  1.1774  
  1.1775  int utilGzWrite(gzFile file, voidp buffer, unsigned int len)
  1.1776  {
  1.1777 -	return utilGzWriteFunc(file, buffer, len);
  1.1778 +  return utilGzWriteFunc(file, buffer, len);
  1.1779  }
  1.1780  
  1.1781  int utilGzRead(gzFile file, voidp buffer, unsigned int len)
  1.1782  {
  1.1783 -	return utilGzReadFunc(file, buffer, len);
  1.1784 +  return utilGzReadFunc(file, buffer, len);
  1.1785  }
  1.1786  
  1.1787  int utilGzClose(gzFile file)
  1.1788  {
  1.1789 -	return utilGzCloseFunc(file);
  1.1790 +  return utilGzCloseFunc(file);
  1.1791  }
  1.1792  
  1.1793  z_off_t utilGzSeek(gzFile file, z_off_t offset, int whence)
  1.1794  {
  1.1795 -	return utilGzSeekFunc(file, offset, whence);
  1.1796 +  return utilGzSeekFunc(file, offset, whence);
  1.1797  }
  1.1798  
  1.1799  z_off_t utilGzTell(gzFile file)
  1.1800  {
  1.1801 -	return utilGzTellFunc(file);
  1.1802 +  return utilGzTellFunc(file);
  1.1803  }
  1.1804  
  1.1805  void utilGBAFindSave(const u8 *data, const int size)
  1.1806  {
  1.1807 -	u32 *p		   = (u32 *)data;
  1.1808 -	u32 *end	   = (u32 *)(data + size);
  1.1809 -	int	 saveType  = 0;
  1.1810 -	int	 flashSize = 0x10000;
  1.1811 -	bool rtcFound  = false;
  1.1812 +  u32 *p		   = (u32 *)data;
  1.1813 +  u32 *end	   = (u32 *)(data + size);
  1.1814 +  int	 saveType  = 0;
  1.1815 +  int	 flashSize = 0x10000;
  1.1816 +  bool rtcFound  = false;
  1.1817  
  1.1818 -	while (p  < end)
  1.1819 +  while (p  < end)
  1.1820 +    {
  1.1821 +      u32 d = READ32LE(p);
  1.1822 +
  1.1823 +      if (d == 0x52504545)
  1.1824  	{
  1.1825 -		u32 d = READ32LE(p);
  1.1826 -
  1.1827 -		if (d == 0x52504545)
  1.1828 +	  if (memcmp(p, "EEPROM_", 7) == 0)
  1.1829 +	    {
  1.1830 +	      if (saveType == 0)
  1.1831 +		saveType = 1;
  1.1832 +	    }
  1.1833 +	}
  1.1834 +      else if (d == 0x4D415253)
  1.1835 +	{
  1.1836 +	  if (memcmp(p, "SRAM_", 5) == 0)
  1.1837 +	    {
  1.1838 +	      if (saveType == 0)
  1.1839 +		saveType = 2;
  1.1840 +	    }
  1.1841 +	}
  1.1842 +      else if (d == 0x53414C46)
  1.1843 +	{
  1.1844 +	  if (memcmp(p, "FLASH1M_", 8) == 0)
  1.1845 +	    {
  1.1846 +	      if (saveType == 0)
  1.1847  		{
  1.1848 -			if (memcmp(p, "EEPROM_", 7) == 0)
  1.1849 -			{
  1.1850 -				if (saveType == 0)
  1.1851 -					saveType = 1;
  1.1852 -			}
  1.1853 +		  saveType  = 3;
  1.1854 +		  flashSize = 0x20000;
  1.1855  		}
  1.1856 -		else if (d == 0x4D415253)
  1.1857 +	    }
  1.1858 +	  else if (memcmp(p, "FLASH", 5) == 0)
  1.1859 +	    {
  1.1860 +	      if (saveType == 0)
  1.1861  		{
  1.1862 -			if (memcmp(p, "SRAM_", 5) == 0)
  1.1863 -			{
  1.1864 -				if (saveType == 0)
  1.1865 -					saveType = 2;
  1.1866 -			}
  1.1867 +		  saveType  = 3;
  1.1868 +		  flashSize = 0x10000;
  1.1869  		}
  1.1870 -		else if (d == 0x53414C46)
  1.1871 -		{
  1.1872 -			if (memcmp(p, "FLASH1M_", 8) == 0)
  1.1873 -			{
  1.1874 -				if (saveType == 0)
  1.1875 -				{
  1.1876 -					saveType  = 3;
  1.1877 -					flashSize = 0x20000;
  1.1878 -				}
  1.1879 -			}
  1.1880 -			else if (memcmp(p, "FLASH", 5) == 0)
  1.1881 -			{
  1.1882 -				if (saveType == 0)
  1.1883 -				{
  1.1884 -					saveType  = 3;
  1.1885 -					flashSize = 0x10000;
  1.1886 -				}
  1.1887 -			}
  1.1888 -		}
  1.1889 -		else if (d == 0x52494953)
  1.1890 -		{
  1.1891 -			if (memcmp(p, "SIIRTC_V", 8) == 0)
  1.1892 -				rtcFound = true;
  1.1893 -		}
  1.1894 -		p++;
  1.1895 +	    }
  1.1896  	}
  1.1897 -	// if no matches found, then set it to NONE
  1.1898 -	if (saveType == 0)
  1.1899 +      else if (d == 0x52494953)
  1.1900  	{
  1.1901 -		saveType = 5;
  1.1902 +	  if (memcmp(p, "SIIRTC_V", 8) == 0)
  1.1903 +	    rtcFound = true;
  1.1904  	}
  1.1905 -	rtcEnable(rtcFound);
  1.1906 -	cpuSaveType = saveType;
  1.1907 -	flashSetSize(flashSize);
  1.1908 +      p++;
  1.1909 +    }
  1.1910 +  // if no matches found, then set it to NONE
  1.1911 +  if (saveType == 0)
  1.1912 +    {
  1.1913 +      saveType = 5;
  1.1914 +    }
  1.1915 +  rtcEnable(rtcFound);
  1.1916 +  cpuSaveType = saveType;
  1.1917 +  flashSetSize(flashSize);
  1.1918  }
  1.1919  
  1.1920  void utilUpdateSystemColorMaps()
  1.1921  {
  1.1922 -	switch (systemColorDepth)
  1.1923 -	{
  1.1924 -	case 16:
  1.1925 -	{
  1.1926 -		for (int i = 0; i < 0x10000; i++)
  1.1927 -		{
  1.1928 -			systemColorMap16[i] = ((i & 0x1f) << systemRedShift) |
  1.1929 -			                      (((i & 0x3e0) >> 5) << systemGreenShift) |
  1.1930 -			                      (((i & 0x7c00) >> 10) << systemBlueShift);
  1.1931 -		}
  1.1932 -		break;
  1.1933 -	}
  1.1934 -	case 24:
  1.1935 -	case 32:
  1.1936 -	{
  1.1937 -		for (int i = 0; i < 0x10000; i++)
  1.1938 -		{
  1.1939 -			systemColorMap32[i] = ((i & 0x1f) << systemRedShift) |
  1.1940 -			                      (((i & 0x3e0) >> 5) << systemGreenShift) |
  1.1941 -			                      (((i & 0x7c00) >> 10) << systemBlueShift);
  1.1942 -		}
  1.1943 -		break;
  1.1944 -	}
  1.1945 -	}
  1.1946 +  switch (systemColorDepth)
  1.1947 +    {
  1.1948 +    case 16:
  1.1949 +      {
  1.1950 +	for (int i = 0; i < 0x10000; i++)
  1.1951 +	  {
  1.1952 +	    systemColorMap16[i] = ((i & 0x1f) << systemRedShift) |
  1.1953 +	      (((i & 0x3e0) >> 5) << systemGreenShift) |
  1.1954 +	      (((i & 0x7c00) >> 10) << systemBlueShift);
  1.1955 +	  }
  1.1956 +	break;
  1.1957 +      }
  1.1958 +    case 24:
  1.1959 +    case 32:
  1.1960 +      {
  1.1961 +	for (int i = 0; i < 0x10000; i++)
  1.1962 +	  {
  1.1963 +	    systemColorMap32[i] = ((i & 0x1f) << systemRedShift) |
  1.1964 +	      (((i & 0x3e0) >> 5) << systemGreenShift) |
  1.1965 +	      (((i & 0x7c00) >> 10) << systemBlueShift);
  1.1966 +	  }
  1.1967 +	break;
  1.1968 +      }
  1.1969 +    }
  1.1970  }
  1.1971  
  1.1972  //// BIOS stuff
  1.1973 @@ -1307,94 +1316,94 @@
  1.1974  
  1.1975  bool utilLoadBIOS(u8 *bios, const char *biosFileName, int systemType)
  1.1976  {
  1.1977 -	if (bios == NULL || strlen(biosFileName) == 0)
  1.1978 -		return false;
  1.1979 +  if (bios == NULL || strlen(biosFileName) == 0)
  1.1980 +    return false;
  1.1981  
  1.1982 -	if (systemType == 4)
  1.1983 +  if (systemType == 4)
  1.1984 +    {
  1.1985 +      int biosSize = 0x4000;
  1.1986 +      if (utilLoad(biosFileName, utilIsGBABios, bios, biosSize))
  1.1987  	{
  1.1988 -		int biosSize = 0x4000;
  1.1989 -		if (utilLoad(biosFileName, utilIsGBABios, bios, biosSize))
  1.1990 -		{
  1.1991 -			if (biosSize == 0x4000)
  1.1992 -				return true;
  1.1993 -		}
  1.1994 +	  if (biosSize == 0x4000)
  1.1995 +	    return true;
  1.1996  	}
  1.1997 -	else
  1.1998 +    }
  1.1999 +  else
  1.2000 +    {
  1.2001 +      int biosSize = 0x100;
  1.2002 +      if (utilLoad(biosFileName, utilIsGBBios, bios, biosSize))
  1.2003  	{
  1.2004 -		int biosSize = 0x100;
  1.2005 -		if (utilLoad(biosFileName, utilIsGBBios, bios, biosSize))
  1.2006 -		{
  1.2007 -			if (biosSize == 0x100)
  1.2008 -				return true;
  1.2009 -		}
  1.2010 +	  if (biosSize == 0x100)
  1.2011 +	    return true;
  1.2012  	}
  1.2013 +    }
  1.2014  
  1.2015 -	return false;
  1.2016 +  return false;
  1.2017  }
  1.2018  
  1.2019  bool utilCheckBIOS(const char *biosFileName, int systemType)
  1.2020  {
  1.2021 -	if (strlen(biosFileName) == 0)
  1.2022 -		return false;
  1.2023 +  if (strlen(biosFileName) == 0)
  1.2024 +    return false;
  1.2025  
  1.2026 -	u8 * tempBIOS = (u8 *)malloc(systemType == 4 ? 0x4000 : 0x100);
  1.2027 -	bool result	  = utilLoadBIOS(tempBIOS, biosFileName, systemType);
  1.2028 -	free(tempBIOS);
  1.2029 +  u8 * tempBIOS = (u8 *)malloc(systemType == 4 ? 0x4000 : 0x100);
  1.2030 +  bool result	  = utilLoadBIOS(tempBIOS, biosFileName, systemType);
  1.2031 +  free(tempBIOS);
  1.2032  
  1.2033 -	return result;
  1.2034 +  return result;
  1.2035  }
  1.2036  
  1.2037  #if 0
  1.2038  // returns the checksum of the BIOS that will be loaded after the next restart
  1.2039  u16 utilCalcBIOSChecksum(const u8 *bios, int systemType)
  1.2040  {
  1.2041 -	u32	biosChecksum = 0;
  1.2042 -	if (bios)
  1.2043 -	{
  1.2044 -		int biosSize	= (systemType == 4 ? 0x4000 : 0x100);
  1.2045 -		const u16 *data = reinterpret_cast<const u16 *>(bios);
  1.2046 -		for (int i = biosSize; i > 0; i -= 2)
  1.2047 -			biosChecksum += *data++;
  1.2048 -	}
  1.2049 +  u32	biosChecksum = 0;
  1.2050 +  if (bios)
  1.2051 +    {
  1.2052 +      int biosSize	= (systemType == 4 ? 0x4000 : 0x100);
  1.2053 +      const u16 *data = reinterpret_cast<const u16 *>(bios);
  1.2054 +      for (int i = biosSize; i > 0; i -= 2)
  1.2055 +	biosChecksum += *data++;
  1.2056 +    }
  1.2057  
  1.2058 -	while ((biosChecksum >> 16) & 0xFFFF)
  1.2059 -		biosChecksum = (biosChecksum &0xFFFF) + ((biosChecksum >> 16) & 0xFFFF);
  1.2060 +  while ((biosChecksum >> 16) & 0xFFFF)
  1.2061 +    biosChecksum = (biosChecksum &0xFFFF) + ((biosChecksum >> 16) & 0xFFFF);
  1.2062  
  1.2063 -	return biosChecksum & 0xFFFF;
  1.2064 +  return biosChecksum & 0xFFFF;
  1.2065  }
  1.2066  #else
  1.2067  // returns the checksum of the BIOS that will be loaded after the next restart
  1.2068  u16 utilCalcBIOSChecksum(const u8 *bios, int systemType)
  1.2069  {
  1.2070 -	u32	biosChecksum = 0;
  1.2071 -	if (bios)
  1.2072 -	{
  1.2073 -		int biosSize	= (systemType == 4 ? 0x4000 : 0x100);
  1.2074 -		const u32 *data = reinterpret_cast<const u32 *>(bios);
  1.2075 -		for (int i = biosSize; i > 0; i -= 4)
  1.2076 -			biosChecksum += *data++;
  1.2077 -	}
  1.2078 +  u32	biosChecksum = 0;
  1.2079 +  if (bios)
  1.2080 +    {
  1.2081 +      int biosSize	= (systemType == 4 ? 0x4000 : 0x100);
  1.2082 +      const u32 *data = reinterpret_cast<const u32 *>(bios);
  1.2083 +      for (int i = biosSize; i > 0; i -= 4)
  1.2084 +	biosChecksum += *data++;
  1.2085 +    }
  1.2086  
  1.2087 -	return biosChecksum & 0xFFFF;
  1.2088 +  return biosChecksum & 0xFFFF;
  1.2089  }
  1.2090  #endif
  1.2091  
  1.2092  // returns the checksum of the BIOS file
  1.2093  u16 utilCalcBIOSFileChecksum(const char *biosFileName, int systemType)
  1.2094  {
  1.2095 -	if (strlen(biosFileName) == 0)
  1.2096 -		return 0;
  1.2097 +  if (strlen(biosFileName) == 0)
  1.2098 +    return 0;
  1.2099  
  1.2100 -	u16		  biosChecksum = 0;
  1.2101 -	const int biosSize	   = (systemType == 4 ? 0x4000 : 0x100);
  1.2102 -	u8 *	  tempBIOS	   = (u8 *)malloc(biosSize);
  1.2103 -	bool	  hasBIOS	   = utilLoadBIOS(tempBIOS, biosFileName, systemType);
  1.2104 -	if (hasBIOS)
  1.2105 -	{
  1.2106 -		biosChecksum = utilCalcBIOSChecksum(tempBIOS, systemType);
  1.2107 -	}
  1.2108 -	free(tempBIOS);
  1.2109 +  u16		  biosChecksum = 0;
  1.2110 +  const int biosSize	   = (systemType == 4 ? 0x4000 : 0x100);
  1.2111 +  u8 *	  tempBIOS	   = (u8 *)malloc(biosSize);
  1.2112 +  bool	  hasBIOS	   = utilLoadBIOS(tempBIOS, biosFileName, systemType);
  1.2113 +  if (hasBIOS)
  1.2114 +    {
  1.2115 +      biosChecksum = utilCalcBIOSChecksum(tempBIOS, systemType);
  1.2116 +    }
  1.2117 +  free(tempBIOS);
  1.2118  
  1.2119 -	return biosChecksum;
  1.2120 +  return biosChecksum;
  1.2121  }
  1.2122