/* Hide v0.0.3 Ver.:OK (c) 06/09 - Ch Iossif */
/*
   Name:         Hide
   Version:      0.0.3
   Author:       Ch Iossif <chiossif@yahoo.com>
   Date:         27/06/09 09:12
   Modified:     20090627091247-20090627093457
                 20090627094411-20090627102037 Coding finished in 58'36" - v0.0.1 ??
                 20090704121900-20090704124700 Coding finished in 28' - v0.0.3 OK
   Description:  Hides a BIP/BIL/BSQ multiband image in a BIP/BIL/BSQ much bigger image
   Usage:        $Hide bigimage image mode [0 on every byte, 1 on 1st byte, 2 on 2nd byte]
   Compile line: $gcc -o Hide Hide.c

   Copyright (C) June 2009 Ch Iossif <chiossif@yahoo.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <stdlib.h>

void hide(int,char *,FILE *,int);
void error(const char *);

int main(int argc, char *argv[]) {
    FILE *fp1, *fp2;
    int filesize1, filesize2, mode, sint, bit, imbuff;
    register int i,j;

    if (argc!=4)
        error("Usage:\t$Hide bigimage image mode [0 on every byte, 1 on 1st byte, 2 on 2nd byte]");

    if ((fp1=fopen(argv[1],"r+b"))==NULL)
        error("Bad filename 1");
    if (fseek(fp1,0,SEEK_END))
        error("Bad seek 1");
    if ((filesize1=ftell(fp1))<0)
        error("Bad filesize");
    if (fseek(fp1,0,SEEK_SET))
        error("Bad seek 2");

    if ((fp2=fopen(argv[2],"rb"))==NULL)
        error("Bad filename 2");
    if (fseek(fp2,0,SEEK_END))
        error("Bad seek 3");
    if ((filesize2=ftell(fp2))<0)
        error("Bad filesize");
    if (fseek(fp2,0,SEEK_SET))
        error("Bad seek 4");

    if ((mode=atoi(argv[3]))<0 || mode>2)
        error("Bad mode value");
    if ((!mode&&filesize1<filesize2*8)||(filesize1<filesize2*16))
        error("Image is too large to be hidden");

    if (mode==1) {
        error("NYI - Not Yet Implemented");
    } else if (mode==2) {
        error("NYI - Not Yet Implemented");
    } else {
        for (i=0;i<filesize2;i++) {
            if ((sint=fgetc(fp2))<0)
                error("Bad read 2");
            for (j=0;j<8;j++) {
                if ((imbuff=fgetc(fp1))<0)
                    error("Bad read 1");
                bit=sint&128;
                sint<<=1;
                if (bit)
                    imbuff|=0x01;
                else
                    imbuff&=0xFE;
                if (fseek(fp1,-1,SEEK_CUR))
                    error("Bad seek 5");
                if ((fputc(imbuff,fp1))<0)
                    error("Bad write 1");
            }
        }
    }

    fclose(fp2);
    fclose(fp1);
    fprintf(stderr,"Hide v0.0.3 Ver.:OK -(C) June 2009 Ch Iossif <chiossif@yahoo.com> - GPLv3\n");
    return 0;
}

void error(const char *s) {
    printf("Hide reports: Error: %s.\n",s);
    exit(1);
}

