Advertisement
image28

File and Program Image Hiding

May 6th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <SDL/SDL.h>
  5. #include <png.h>
  6. #include <SDL/SDL_image.h>
  7.  
  8. int png_save_surface(char *filename, SDL_Surface *surf);
  9. void png_user_error(png_structp ctx, png_const_charp str);
  10. void png_user_warn(png_structp ctx, png_const_charp str);
  11. static int png_colortype_from_surface(SDL_Surface *surface);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     if ( argc < 3 )
  16.     {
  17.         printf("USAGE: %s [file to hide] [image file(s) to hide file in]\n",argv[0]);
  18.         return(1);
  19.     }
  20.     else if ( check(argc,argv) )
  21.     {
  22.         printf("Not Enough Space To Hide File\n");
  23.         return(1);
  24.     }
  25.     else
  26.     {
  27.         printf("Hiding File\n");
  28.         hide(argc,argv);
  29.         return(0);
  30.     }
  31. }
  32.  
  33. int check(int count, char *files[])
  34. {
  35.     FILE *input;
  36.     SDL_Surface *surface;
  37.     unsigned long filesize=0;
  38.     unsigned long pixel_count=0;
  39.     int d=0;
  40.  
  41.     printf("%s %s\n%d\n", files[1], files[2],count);
  42.  
  43.     if ( ( input=fopen(files[1],"rb") ) == NULL ) exit(-1);
  44. printf(".");
  45.     fseek(input,0L,SEEK_END);
  46.     filesize=ftell(input);
  47.     fclose(input);
  48.  
  49.     for(d=2;d<count;d++)
  50.     {
  51.         surface=IMG_Load(files[d]);
  52.         if ( surface == NULL ) exit(-1);
  53.         printf(".");
  54.         pixel_count=pixel_count+((surface->w*surface->h)*3);
  55.         SDL_FreeSurface(surface);
  56.     }
  57.  
  58.     if ( pixel_count > filesize*8 )
  59.     {
  60.         return(0);
  61.     }else{
  62.         return(1);
  63.     }
  64. }
  65.  
  66. int hide(int count, char *files[])
  67. {
  68.     FILE *input;
  69.     SDL_Surface *surface;
  70.     unsigned long filesize=0;
  71.     unsigned long pixel_count=0;
  72.     int start=0;
  73.     int d,e,l;
  74.     int inbyte=0;
  75.     char outfile[32768];
  76.     Uint8 *pixel;
  77.     unsigned long temp=0;
  78.  
  79.  
  80.  
  81.     if ( ( input=fopen(files[1],"rb") ) == NULL ) exit(-1);
  82.     fseek(input,0L,SEEK_END);
  83.     filesize=ftell(input);
  84.     fseek(input,0L,SEEK_SET);
  85.     printf("FILESIZE: %d\n",filesize);
  86.  
  87.     for(d=2;d<count;d++)
  88.     {
  89.         strcpy(outfile,files[d]);
  90.         outfile[strlen(outfile)-4]='\0';
  91.         strcat(outfile,"-2.png");
  92.  
  93.         surface=IMG_Load(files[d]);
  94.         pixel_count=(surface->w*surface->h)*3;
  95.         pixel=(Uint8 *)surface->pixels;
  96.  
  97.         if( SDL_MUSTLOCK( surface ) )
  98.         { //Lock the surface
  99.             SDL_LockSurface( surface );
  100.         }
  101.  
  102.         if ( d == 2 )
  103.         {
  104.             start=32;
  105.  
  106.             for(e=0;e<32;e++)
  107.             {
  108.                 temp=0;
  109.                 temp=filesize%2;
  110.                 if ( temp )
  111.                 {
  112.                     if ( pixel[e]%2 == 0 )
  113.                     {
  114.                         pixel[e]=pixel[e]+1;
  115.                     }
  116.                 }else{
  117.                     if ( pixel[e]%2 == 1 )
  118.                     {
  119.                         pixel[e]=pixel[e]-1;
  120.                     }
  121.                 }
  122.                
  123.                 filesize >>= 1;
  124.                
  125.             }
  126.         }else{
  127.             start=0;
  128.         }
  129.  
  130.         for(l=start;l<pixel_count;l=l+8)
  131.         {
  132.             if ( ! feof(input) )
  133.             {
  134.                 inbyte=0;
  135.                 fread(&inbyte,1,1,input);
  136.                
  137.                 for(e=0;e<8;e++)
  138.                 {
  139.                     temp=0;
  140.                     temp=inbyte%2;
  141.  
  142.                     if ( temp )
  143.                     {
  144.                         if ( pixel[l+e]%2 == 0)
  145.                         {
  146.                             pixel[l+e]=pixel[l+e]+1;
  147.                         }
  148.                     }else{
  149.                         if ( pixel[l+e]%2 == 1)
  150.                         {
  151.                             pixel[l+e]=pixel[l+e]-1;
  152.                         }
  153.                     }
  154.                     inbyte >>= 1;
  155.                 }
  156.             }else{
  157.                 l=pixel_count;
  158.             }
  159.         }
  160.  
  161.         if( SDL_MUSTLOCK( surface ) )
  162.         { //Lock the surface
  163.             SDL_UnlockSurface( surface );
  164.         }
  165.  
  166.         // write new image file
  167.         if( png_save_surface(outfile, surface) < 0 ) exit(-1);
  168.         SDL_FreeSurface(surface);
  169.     }
  170.  
  171.     return(0);
  172. }
  173.  
  174. static int png_colortype_from_surface(SDL_Surface *surface)
  175. {
  176.     int colortype = PNG_COLOR_MASK_COLOR; /* grayscale not supported */
  177.  
  178.     if (surface->format->palette)
  179.         colortype |= PNG_COLOR_MASK_PALETTE;
  180.     else if (surface->format->Amask)
  181.         colortype |= PNG_COLOR_MASK_ALPHA;
  182.        
  183.     return colortype;
  184. }
  185.  
  186.  
  187. void png_user_warn(png_structp ctx, png_const_charp str)
  188. {
  189.     fprintf(stderr, "libpng: warning: %s\n", str);
  190. }
  191.  
  192.  
  193. void png_user_error(png_structp ctx, png_const_charp str)
  194. {
  195.     fprintf(stderr, "libpng: error: %s\n", str);
  196. }
  197.  
  198.  
  199. int png_save_surface(char *filename, SDL_Surface *surf)
  200. {
  201.     FILE *fp;
  202.     png_structp png_ptr;
  203.     png_infop info_ptr;
  204.     int i, colortype;
  205.     png_bytep *row_pointers;
  206.  
  207.     /* Opening output file */
  208.     fp = fopen(filename, "wb");
  209.     if (fp == NULL) {
  210.         perror("fopen error");
  211.         return -1;
  212.     }
  213.  
  214.     /* Initializing png structures and callbacks */
  215.     png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
  216.         NULL, png_user_error, png_user_warn);
  217.     if (png_ptr == NULL) {
  218.         printf("png_create_write_struct error!\n");
  219.         return -1;
  220.     }
  221.  
  222.     info_ptr = png_create_info_struct(png_ptr);
  223.     if (info_ptr == NULL) {
  224.         png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  225.         printf("png_create_info_struct error!\n");
  226.         exit(-1);
  227.     }
  228.  
  229.     if (setjmp(png_jmpbuf(png_ptr))) {
  230.         png_destroy_write_struct(&png_ptr, &info_ptr);
  231.         fclose(fp);
  232.         exit(-1);
  233.     }
  234.  
  235.     png_init_io(png_ptr, fp);
  236.  
  237.     colortype = png_colortype_from_surface(surf);
  238.     png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE,
  239.         PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  240.  
  241.     /* Writing the image */
  242.     png_write_info(png_ptr, info_ptr);
  243.     png_set_packing(png_ptr);
  244.  
  245.     row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surf->h);
  246.     for (i = 0; i < surf->h; i++)
  247.         row_pointers[i] = (png_bytep)(Uint8 *)surf->pixels + i*surf->pitch;
  248.     png_write_image(png_ptr, row_pointers);
  249.     png_write_end(png_ptr, info_ptr);
  250.  
  251.     /* Cleaning out... */
  252.     free(row_pointers);
  253.     png_destroy_write_struct(&png_ptr, &info_ptr);
  254.     fclose(fp);
  255.  
  256.     return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement