home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / NSIS.EXE / Source / util.cpp < prev    next >
C/C++ Source or Header  |  2001-05-30  |  5KB  |  174 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <conio.h>
  5. #include "exehead/fileform.h"
  6. #include "util.h"
  7. #include "strlist.h"
  8.  
  9. int g_dopause=0;
  10.  
  11.  
  12. void dopause(void)
  13. {
  14.   if (g_dopause)
  15.   {
  16.     printf("MakeNSIS done - hit enter to close...");
  17.     fflush(stdout);
  18.     int a;
  19.     while ((a=_getch()) != '\r' && a != 27/*esc*/);
  20.   }
  21. }
  22.  
  23. int find_data_offset(char *hdr, int hdr_len, char *srch, int startoffs, int srchlen)
  24. {
  25.   int offs=0;
  26.   hdr_len-=srchlen;
  27.   while (hdr_len>=0 && memcmp(hdr+offs+startoffs,srch+startoffs,srchlen-startoffs))
  28.   {
  29.     offs++;
  30.     hdr_len--;
  31.   }
  32.   if (hdr_len<0)
  33.   {
  34.     printf("find_data_offset: error searching data -- failing!\n");
  35.     return -1;
  36.   }
  37.   return offs;
  38. }
  39.  
  40. // will update out+ICO_HDRSKIP with a 766-ICO_HDRSKIP bytes of 32x32x16 icon
  41. int replace_icon(char *out, char *filename)
  42. {
  43.   char obuf[766];
  44.   int success=0;
  45.   FILE *fp=fopen(filename,"rb");
  46.   if (!fp)
  47.   {
  48.     printf("replace_icon: error: error opening file \"%s\" -- failing!\n",filename);
  49.     return -1;
  50.   }
  51.  
  52.   if (!fgetc(fp) && !fgetc(fp) && fgetc(fp)==1 && !fgetc(fp))
  53.   {
  54.     int num_images=fgetc(fp);
  55.     num_images|=fgetc(fp)<<8;
  56.     int x;
  57.     for (x = 0; x < num_images; x++)
  58.     {
  59.       fseek(fp,6+x*16,SEEK_SET);
  60.       int w=fgetc(fp);
  61.       int h=fgetc(fp);
  62.       int colors=fgetc(fp);
  63.       int res=fgetc(fp);
  64.       int planes=fgetc(fp); planes|=fgetc(fp)<<8;
  65.       int bitcnt=fgetc(fp); bitcnt|=fgetc(fp)<<8;
  66.       int size=fgetc(fp); size|=fgetc(fp)<<8; size|=fgetc(fp)<<16; size|=fgetc(fp)<<24;
  67.       int offs=fgetc(fp); offs|=fgetc(fp)<<8; offs|=fgetc(fp)<<16; offs|=fgetc(fp)<<24;
  68.       if (w == 32 && h == 32 && ((planes == 1 && bitcnt == 4) || colors == 16) && size <= 766-6-16) 
  69.       {
  70.         fseek(fp,offs,SEEK_SET);
  71.         if (fgetc(fp) == 40 && !fgetc(fp) && !fgetc(fp) && !fgetc(fp))
  72.         {
  73.           memset(obuf,0,sizeof(obuf));
  74.           obuf[2]=1;
  75.           obuf[4]=1;
  76.           fseek(fp,6+x*16,SEEK_SET);
  77.           fread(obuf+6,1,12,fp);
  78.           obuf[6+12]=6+16;
  79.           fseek(fp,offs,SEEK_SET);
  80.           fread(obuf+6+16,1,size,fp);
  81.           success=1;
  82.           break;
  83.         }
  84.       }
  85.     }
  86.   }
  87.   
  88.   fclose(fp);
  89.   if (!success)
  90.   {
  91.     printf("replace_icon: error: icon file \"%s\" has no 32x32x16 icon -- failing!\n",filename);
  92.     return 1;
  93.   }
  94.   else
  95.     memcpy(out+ICO_HDRSKIP,obuf+ICO_HDRSKIP,766-ICO_HDRSKIP);
  96.   return 0;
  97. }
  98.  
  99.  
  100. int replace_bitmap(char *out, char *filename)
  101. {
  102.  
  103.   static unsigned char header_4bpp[54] = {    
  104.     0x42, 0x4d, 0x66, 0x01, 0x00, 0x00, 0x00, 0x00, 
  105.     0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x28, 0x00,
  106.     0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x14, 0x00,
  107.     0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
  108.     0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xc4, 0x0e,
  109.     0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
  110.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  111.     // note: input file must be 16 colors;
  112.     //         can be any width and height, though
  113.  
  114.     HANDLE            hbmp;
  115.     unsigned char    header[54];
  116.     unsigned char    colors[64];
  117.  
  118.     FILE *fIn = fopen(filename, "rb");
  119.     if (!fIn) 
  120.   {
  121.     printf("replace_bitmap: error: bitmap file not found \"%s\" -- failing!\n",filename);
  122.         return 1;
  123.   }
  124.  
  125.     fread(header, 54, 1, fIn);    // read the header (ignore)
  126.     //int w = header[18] + header[19]*256;
  127.     //int h = header[22] + header[23]*256;
  128.     //int bpp = header[28];
  129.     fread(colors, 64, 1, fIn);    // read the colors
  130.     fclose(fIn);
  131.     
  132.     hbmp = LoadImage(NULL, filename, IMAGE_BITMAP, 20, 20, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
  133.     if (!hbmp) 
  134.     {
  135.     printf("replace_bitmap: error: invalid bitmap file \"%s\" -- failing!\n",filename);
  136.         return 1;
  137.     }
  138.  
  139.     BITMAP bm;
  140.     GetObject(hbmp, sizeof(bm), (LPSTR)&bm);
  141.     int w = bm.bmWidth;
  142.     int h = bm.bmHeight;
  143.     int bpp = bm.bmBitsPixel;
  144.  
  145.     if (w == 20 && h == 20 && bpp == 4 && bm.bmWidthBytes*bm.bmHeight + 64 + 54 <= 358) 
  146.     {
  147.       // update & write header
  148.       header_4bpp[18] = w % 256;
  149.       header_4bpp[19] = w / 256;
  150.       header_4bpp[22] = h % 256;
  151.       header_4bpp[23] = h / 256;
  152.  
  153.     char obuf[358];
  154.  
  155.     memcpy(obuf,header_4bpp,54);
  156.     memcpy(obuf+54,colors,64);
  157.       
  158.     bm.bmWidthBytes+=3;
  159.     bm.bmWidthBytes&=~3;
  160.  
  161.     memcpy(obuf+54+64,bm.bmBits,bm.bmWidthBytes*bm.bmHeight);
  162.     memcpy(out+BMP_HDRSKIP,obuf+BMP_HDRSKIP,358-BMP_HDRSKIP);
  163.   }
  164.   else
  165.   {
  166.     DeleteObject(hbmp);
  167.     printf("replace_bitmap: error: bitmap error in \"%s\" -- failing!\n",filename);
  168.     return 1;
  169.   }
  170.   DeleteObject(hbmp);
  171.     
  172.   return 0;
  173. }
  174.