
// Steg2Dlg.cpp : implementation file
//
// Written by John A deLaubenfels, https://www.adaptune.com/steg.htm .
// Permission granted to use freely in any non-fraudulent way.
//
//

#include "pch.h"
#include "framework.h"
#include "Steg2.h"
#include "Steg2Dlg.h"
#include "afxdialogex.h"
#include <vector>
#include "mt64.h"
#include "EmbedDlg.h"
#include "ExtractDlg.h"
// #include "rndWrap.h"
#include "ShowtxtDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
   CAboutDlg();

// Dialog Data
#ifdef AFX_DESIGN_TIME
   enum { IDD = IDD_ABOUTBOX };
#endif

   protected:
   virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
   DECLARE_MESSAGE_MAP()
public:
   // afx_msg void OnBnClickedEmbed();
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
   // ON_BN_CLICKED(IDC_BUTTON1, &CAboutDlg::OnBnClickedEmbed)
END_MESSAGE_MAP()


// CSteg2Dlg dialog



CSteg2Dlg::CSteg2Dlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_STEG2_DIALOG, pParent)
{
   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
   m_pCommonInfo = NULL;
   // m_hIcon = AfxGetApp()->LoadIcon(IDB_BITMAP1);       // xptl; probablyl will barf.
   // m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON4);         // xptl; probablyl will barf.
}

void CSteg2Dlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   // DDX_Control(pDX, IDC_MY_GUY, m_cEdt_myGuy);
}

BEGIN_MESSAGE_MAP(CSteg2Dlg, CDialogEx)
   ON_WM_SYSCOMMAND()
   ON_WM_PAINT()
   ON_WM_QUERYDRAGICON()
   ON_BN_CLICKED(IDC_EMBED, &CSteg2Dlg::OnBnClickedEmbed)
   ON_BN_CLICKED(IDC_BUTN_EXTRACT, &CSteg2Dlg::OnClickedButnExtract)
   // ON_BN_CLICKED(IDC_BUTN_GO, &CSteg2Dlg::OnClickedButnGo)
   ON_BN_CLICKED(IDC_BUTN_HELPINFO, &CSteg2Dlg::OnClickedButnHelpinfo)
END_MESSAGE_MAP()


// CSteg2Dlg message handlers

BOOL CSteg2Dlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();

   // Add "About..." menu item to system menu.

   // IDM_ABOUTBOX must be in the system command range.
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
   ASSERT(IDM_ABOUTBOX < 0xF000);

   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != nullptr)
   {
      BOOL bNameValid;
      CString strAboutMenu;
      bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
      ASSERT(bNameValid);
      if (!strAboutMenu.IsEmpty())
      {
         pSysMenu->AppendMenu(MF_SEPARATOR);
         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }
   }

   // Set the icon for this dialog.  The framework does this automatically
   //  when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);       // Set big icon
   SetIcon(m_hIcon, FALSE);      // Set small icon

   // TODO: Add extra initialization here

   return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSteg2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
   if ((nID & 0xFFF0) == IDM_ABOUTBOX)
   {
      CAboutDlg dlgAbout;
      dlgAbout.DoModal();
   }
   else
   {
      CDialogEx::OnSysCommand(nID, lParam);
   }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSteg2Dlg::OnPaint()
{
   if (IsIconic())
   {
      CPaintDC dc(this); // device context for painting

      SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

      // Center icon in client rectangle
      int cxIcon = GetSystemMetrics(SM_CXICON);
      int cyIcon = GetSystemMetrics(SM_CYICON);
      CRect rect;
      GetClientRect(&rect);
      int x = (rect.Width() - cxIcon + 1) / 2;
      int y = (rect.Height() - cyIcon + 1) / 2;

      // Draw the icon
      dc.DrawIcon(x, y, m_hIcon);
   }
   else
   {
      CDialogEx::OnPaint();
   }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSteg2Dlg::OnQueryDragIcon()
{
   return static_cast<HCURSOR>(m_hIcon);
}


#ifdef OOPSIE
void CAboutDlg::OnBnClickedEmbed()
{
   // TODO: Add your control notification handler code here
}
#endif // OOPSIE


void CSteg2Dlg::OnBnClickedEmbed()
{
   // TODO: Add your control notification handler code here


//#ifdef TEST1
//   std::vector<unsigned long long> vcPwdSeed1;
//   MT64::getSeedFromPwd(vcPwdSeed1);
//
//   FILE *fp_out(NULL);
//   if(fopen_s(&fp_out, "blahSeed", "a") != 0)
//      return;
//
//   fprintf(fp_out, "\nDid you expect %016llx ?  (Y/N/Q)   \n", vcPwdSeed1[0]);
//   fclose(fp_out); fp_out=NULL;
//
//
//
//   CFileDialog dlgOpen(TRUE);
//   OPENFILENAME& ofn = dlgOpen.GetOFN();
//   ofn.Flags |= OFN_ALLOWMULTISELECT;
//
//   dlgOpen.DoModal();
//#endif // TEST1
   // doThing("You clicked the button.");

   // rndWrap *pRnd = new rndWrap;
   // pRnd->spitOut("blah17.txt", "Yeah, I spit this out");
   // delete pRnd; pRnd=NULL;

   // int i = rndWrap::getNumber();

   EmbedDlg dlgE;

   dlgE.m_pCommonInfo  = m_pCommonInfo;
   // dlgE.m_vcBackRatios = m_pCommonInfo->vcBackRatEmb;

   long long il = dlgE.DoModal();

}






void CSteg2Dlg::OnClickedButnExtract() {
   // TODO: Add your control notification handler code here
   ExtractDlg dlgX;

   dlgX.m_pCommonInfo  = m_pCommonInfo;
   // dlgX.m_vcBackRatios = m_pCommonInfo->vcBackRatExt;

   long long il = dlgX.DoModal();
}








// void CSteg2Dlg::OnClickedButnGo() {
//    // TODO: Add your control notification handler code here
// }


void CSteg2Dlg::OnClickedButnHelpinfo()
{
   // TODO: Add your control notification handler code here
   ShowtxtDlg dlgShow;
   dlgShow.m_ssBanner    = "Stegmeisther Info / Help";
   dlgShow.m_pCommonInfo  = m_pCommonInfo;
   // dlgShow.m_pcBigString = 
   dlgShow.m_ssBigString = 
// qx
"Stegmeister version 1.11\r\n"
"\r\n"
"Stegmeister hides private information inside images.  Goals are: \r\n"
"   . To make it difficult to tell whether anything is embedded in the output image. \r\n"
"   . To make it all but impossible to recover embedded information without the right password. \r\n"
"\r\n"
"Notice that traditional encryption covers the second goal but not the first: anyone can see that encrypted \r\n"
"information is present even if they can't decrypt it.  Steganography aims to cover both goals. \r\n"
"\r\n"
"\r\n"
"\r\n"
"QUICK START\r\n"
"\r\n"
"Click Embed.  Click each of the \"Click me\" edit boxes, the first with your wrapper file (.jpg, .png, or \r\n"
".bmp), the second with any file(s) you want to embed (any file types).  Click GO.  Type your password and \r\n"
"hit Enter twice.  Your stuffed output file will show in a new Explorer window.\r\n"
"\r\n"
"Click Extract.  Pick the stuffed file you just created.  Type your password; hit Enter twice.  Your \r\n"
"embeded file(s) will extract and show in a new Explorer window.\r\n"
"\r\n"
"\r\n"
"\r\n"
"STILL PRETTY QUICK START\r\n"
"\r\n"
"Click Embed and fill in wrapper and embed files.  Before clicking GO, slide the slider and note how it gives\r\n"
"choices for output file size and detection difficulty.  Also click the Type a Message button and type\r\n"
"or paste text there.  When you type your password and hit Enter once, note the made-up word; now if you ever \r\n"
"type the wrong password you'll see a different word and know to try again.  No more typing a long password \r\n"
"twice to confirm correct entry.\r\n"
"\r\n"
"Click Extract, pick the file you just stuffed, type your password (see familiar made-up word), and you'll\r\n"
"first view your typed text, then, if you also embedded files, an Explorer window showing them.\r\n"
"\r\n"
"\r\n"
"\r\n"
"\r\n"
"Read on for details.\r\n"
"\r\n"
"\r\n"
"\r\n"
"EXTRACTING\r\n"
"\r\n"
"Extracting is pretty straight-forward: pick the stuffed file and type your password.  Stegmeister leaves no \r\n"
"watermarks or other identifying information in its stuffed output files.  This means that it can't distinguish \r\n"
"between an incorrect password and a file which has not been stuffed.  \r\n"
"\r\n"
"\r\n"
"\r\n"
"EMBEDDING\r\n"
"\r\n"
"You can embed one or more files of any extension in a sufficiently large wrapper image file.  Qualities of \r\n"
"the ideal wrapper file are: \r\n"
"\r\n"
"   . Large.  Stegmeister will try to reduce the number of pixels when possible for its stuffed output file, \r\n"
"     but won't make it larger than the input image. \r\n"
"\r\n"
"   . Bright, but not washed out.  Stegmeister won't embed in either black or washed-out white pixels, as doing \r\n"
"     so is a sure \"tell\" that the image has been modified. \r\n"
"\r\n"
"Run Stegmeister and select Embed.  Click the edit box (or \"...\" button) for Input wrapper file and pick your \r\n"
"wrapper, which can be extension .jpg, .png, or .bmp.  The dialog will display the maximum number of bytes the \r\n"
"chosen file can embed.  Next click the edit box (or \"...\" button) for File(s) to embed.  Multiple files must \r\n"
"come from the same folder for the Windows file open dialog to pick them all.  The dialog will now display \r\n"
"Bytes required.  If it's larger than Bytes available, you must either pick a larger wrapper file, or reduce \r\n"
"the number or size of files being embedded. \r\n"
"\r\n"
"Optionally you can click the Type a Message button and type or paste a message which the recipient will see \r\n"
"without the information being written to file at either end. \r\n"
"\r\n"
"Once compatible wrapper and embed files are selected, you'll have a chance to decide how large an output file \r\n"
"you'd like.  Larger files are harder to detect as being stuffed, but they're also, well, large.  If you're \r\n"
"stuffing something small, you can select a very small output file size, or you can favor difficult detection \r\n"
"with a larger output file. \r\n"
"\r\n"
"Detection difficulty reflects how completely the image is stuffed and therefore how difficult or easy it is\r\n"
"to determine it has been modified: 100% of possible gives detection difficulty 0.0; 50% embed ratio gives 1.0; \r\n"
"25% gives 2.0; 12.5% gives 3.0, etc.  Increasing detection difficulty by 1 will tend to double the output \r\n"
"file size.  You can explicitly pick detection difficulty, or output pixel width or height, or pick the size of \r\n"
"the desired output file in bytes [1], or slide the slider.  Anything out of range of possible will be brought \r\n"
"back into range by the program.  Note: the slider is set for what the program considers a reasonable range of \r\n"
"detection difficulty, typically topping out at 4.0, but for higher safety you can use the edit boxes to go \r\n"
"outside slider range (if the wrapper file is large enough to accommodate).\r\n"
"\r\n"
"[1] Size shown is for an uncompressed .bmp file.  Compressed .png file will be slightly smaller.\r\n"
"\r\n"
"Even a heavily stuffed file, where an attacker might be quite certain something has been modified from the \r\n"
"original, will not have its stuffed data any more accessible than a lightly-stuffed file, without the correct \r\n"
"password.  It just will lose some of the sense of \"innocent-looking\".\r\n"
"\r\n"
"Once happy, click the GO button, type your password and verify its sanity check word.  The program will open \r\n"
"an Explorer window on the output file with embed(s).  You can rename the file but don't try to change its \r\n"
"extension or it won't be usable for extraction.  You can switch to Extract and pick the new output file; your \r\n"
"embedded file(s) should be recovered in a folder under the output file. \r\n"
"\r\n"
"\r\n"
"MISCELLANEOUS\r\n"
"\r\n"
"The Stegmeister executable is wrapped (\"boxed\") with Enigma Virtual Box, which bundles normally-external\r\n"
"dependencies with the exe.  This should make it portable: just drop the .exe anywhere (on a 64-bit Windows \r\n"
"10 or later computer) and run it.  Please let me know of any problems.  \r\n"
"\r\n"
"Beginning with version 1.05, Stegmeister stores a hash of the contents of each file it embeds, and checks for \r\n"
"a match on extract.  If mismatch, the user is notified.  Corruption of the stuffed file in transmission could \r\n"
"cause such a problem.\r\n"
"\r\n"
"\r\n"
"SOURCE CODE\r\n"
"\r\n"
"Source for this version (Windows, Visual Studio 2022, 64-bit) is posted at https://www.adaptune.com/steg.htm .\r\n"
"Source for a Linux command-line version is also posted there.\r\n"
"\r\n"
"\r\n"
"CONTACT\r\n"
"\r\n"
"   https://www.adaptune.com/steg.htm \r\n"
"   adaptune@comcast.net \r\n"
"   jdl@adaptune.com \r\n"
"\r\n"
"\r\n"
"CREDITS and DISCLAIMERS\r\n"
"\r\n"
"This software uses the FreeImage open source image library.    See https://freeimage.sourceforge.io for details. \r\n"
"This software applies compression to input files using zlib.   See https://zlib.net for details. \r\n"
"Stegmeister.exe is wrapped (\"boxed\") with Enigma Virtual Box.  See https://enigmaprotector.com/en/downloads.html . \r\n"
"This sofware uses a 64-bit Mersenne Twister.  See http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/emt.html .\r\n"
"\r\n"
"The following applies to the Mersenne Twister code; the disclaimer at the end also applies to Stegmeister.\r\n"
"\r\n"
"   Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved.                          \r\n"
"\r\n"
"   Redistribution and use in source and binary forms, with or without modification, are permitted provided that \r\n"
"   the following conditions are met:\r\n"
"\r\n"
"     1. Redistributions of source code must retain the above copyright notice, this list of conditions and the \r\n"
"        following disclaimer.\r\n"
"\r\n"
"     2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the \r\n"
"        following disclaimer in the documentation and/or other materials provided with the distribution.\r\n"
"\r\n"
"     3. The names of its contributors may not be used to endorse or promote products derived from this software \r\n"
"        without specific prior written permission.\r\n"
"\r\n"
"   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED \r\n"
"   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A \r\n"
"   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY \r\n"
"   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, \r\n"
"   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER \r\n"
"   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR \r\n"
"   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n"
"\r\n"
"\r\n"
"====================================================================================================================\r\n"
"|\r\n"
"|\r\n"
"====================================================================================================================\r\n"
"\r\n"
"ADVANCED SETTINGS\r\n"
"\r\n"
"The preset file someName.txt can be modified for features not in the GUI.\r\n"
"\r\n"
"To have the program create a debug file (blah.dbg), add:\r\n"
"\r\n"
"doDbg                = 1\r\n"
"\r\n"
"\r\n"
"To have the program create .bmp stuffed output file in addition to .png, add:\r\n"
"\r\n"
"doBmp                = 1\r\n"
"\r\n"
"\r\n"
"To have the program create ONLY a .bmp stuffed output file (no .png), add:\r\n"
"\r\n"
"doBmp                = 1\r\n"
"doPng                = 0\r\n"
"\r\n"
"\r\n"
"To seed a particular target stuffed output file pixel width, add:\r\n"
"\r\n"
"widDesired           = [pixels]\r\n"
"\r\n"
"\r\n"
"To seed a particular target stuffed output file pixel height, add:\r\n"
"\r\n"
"hgtDesired           = [pixels]\r\n"
"\r\n"
"\r\n"
"To seed a particular target stuffed output (.bmp) file size, add:\r\n"
"\r\n"
"fszDesired           = [bytes]\r\n"
"\r\n"
"\r\n"
"To seed a particular target stuffed output file detection difficulty, add:\r\n"
"\r\n"
"difDesired           = [value.  must include a decimal point (\"1.0\", etc.)]\r\n"
"\r\n"
"\r\n"
"To suppress the program showing a password hint, add:\r\n"
"\r\n"
"bPassHint            = 0\r\n"
"\r\n"
"\r\n"
"To prevent the program from rewriting someName.txt on exit, add:\r\n"
"\r\n"
"noWrite              = 1\r\n"
"\r\n"
"\r\n"
"To disable the method (bScatter) which prevents phantom colors, add:\r\n"
"\r\n"
"useScatter           = 0\r\n"
"lastScatter          = 0\r\n"
"\r\n"
"(this will bring up a checkbox in the embed dialog, from which you can\r\n"
"turn the feature off and on).\r\n"
"\r\n"
;  // qxheer


   dlgShow.DoModal();
}


