<commit_before>#include <fitz.h>
#include <mupdf.h>
#include "pdfapp.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#include <shellapi.h>

#define ID_ABOUT	0x1000
#define ID_DOCINFO	0x1001

static HWND hwndframe = NULL;
static HWND hwndview = NULL;
static HDC hdc;
static HBRUSH bgbrush;
static HBRUSH shbrush;
static BITMAPINFO *dibinf;
static HCURSOR arrowcurs, handcurs, waitcurs;
static LRESULT CALLBACK frameproc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK viewproc(HWND, UINT, WPARAM, LPARAM);

static int bmpstride = 0;
static char *bmpdata = NULL;
static int justcopied = 0;

static pdfapp_t gapp;

/*
 * Associate Apparition with PDF files.
 */

void associate(char *argv0)
{
    char tmp[256];
    char *name = "Adobe PDF Document";
    HKEY key, kicon, kshell, kopen, kcmd;
    DWORD disp;

    /* HKEY_CLASSES_ROOT\.pdf */

    if (RegCreateKeyEx(HKEY_CLASSES_ROOT,
		".pdf", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &key, &disp))
	return;

    if (RegSetValueEx(key, "", 0, REG_SZ, "Apparition", strlen("Apparition")+1))
	return;

    RegCloseKey(key);

    /* HKEY_CLASSES_ROOT\Apparition */

    if (RegCreateKeyEx(HKEY_CLASSES_ROOT,
		"Apparition", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &key, &disp))
	return;

    if (RegSetValueEx(key, "", 0, REG_SZ, name, strlen(name)+1))
	return;

    /* HKEY_CLASSES_ROOT\Apparition\DefaultIcon */

    if (RegCreateKeyEx(key,
		"DefaultIcon", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kicon, &disp))
	return;

    sprintf(tmp, "%s,1", argv0);
    if (RegSetValueEx(kicon, "", 0, REG_SZ, tmp, strlen(tmp)+1))
	return;

    RegCloseKey(kicon);

    /* HKEY_CLASSES_ROOT\Apparition\Shell\Open\Command */

    if (RegCreateKeyEx(key,
		"shell", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kshell, &disp))
	return;
    if (RegCreateKeyEx(kshell,
		"open", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kopen, &disp))
	return;
    if (RegCreateKeyEx(kopen,
		"command", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kcmd, &disp))
	return;

    sprintf(tmp, "\"%s\" \"%%1\"", argv0);
    if (RegSetValueEx(kcmd, "", 0, REG_SZ, tmp, strlen(tmp)+1))
	return;

    RegCloseKey(kcmd);
    RegCloseKey(kopen);
    RegCloseKey(kshell);

    RegCloseKey(key);
}

/*
 * Dialog boxes
 */

void winwarn(pdfapp_t *app, char *msg)
{
    MessageBoxA(hwndframe, msg, "Apparition: Warning", MB_ICONWARNING);
}

void winerror(pdfapp_t *app, fz_error *error)
{
    MessageBoxA(hwndframe, error->msg, "Apparition: Error", MB_ICONERROR);
    exit(1);
}

int winfilename(char *buf, int len)
{
    OPENFILENAME ofn;
    strcpy(buf, "");
    memset(&ofn, 0, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwndframe;
    ofn.lpstrFile = buf;
    ofn.nMaxFile = len;
    ofn.lpstrInitialDir = NULL;
    ofn.lpstrTitle = "Apparition: Open PDF file";
    ofn.lpstrFilter = "PDF Files (*.pdf)\0*.pdf\0All Files\0*\0\0";
    ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
    return GetOpenFileName(&ofn);
}

static char pd_filename[256] = "The file is encrypted.";
static char pd_password[256] = "";
static int pd_okay = 0;

INT CALLBACK
dlogpassproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
	SetDlgItemText(hwnd, 4, pd_filename);
	return TRUE;
    case WM_COMMAND:
	switch(wParam)
	{
	case 1:
	    pd_okay = 1;
	    GetDlgItemText(hwnd, 3, pd_password, sizeof pd_password);
	    EndDialog(hwnd, 0);
	    return TRUE;
	case 2:
	    pd_okay = 0;
	    EndDialog(hwnd, 0);
	    return TRUE;
	}
	break;
    }
    return FALSE;
}

char *winpassword(pdfapp_t *app, char *filename)
{
    char buf[124], *s;
    strcpy(buf, filename);
    s = buf;
    if (strrchr(s, '\\')) s = strrchr(s, '\\') + 1;
    if (strrchr(s, '/')) s = strrchr(s, '/') + 1;
    if (strlen(s) > 32)
	strcpy(s + 30, "...");
    sprintf(pd_filename, "The file \"%s\" is encrypted.", s);
    DialogBox(NULL, "IDD_DLOGPASS", hwndframe, dlogpassproc);
    if (pd_okay)
	return pd_password;
    return NULL;
}

INT CALLBACK
dloginfoproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    char buf[256];
    pdf_xref *xref = gapp.xref;
    fz_obj *obj;

    switch(message)
    {
    case WM_INITDIALOG:

	SetDlgItemTextA(hwnd, 0x10, gapp.filename);

	sprintf(buf, "PDF %g", xref->version);
	SetDlgItemTextA(hwnd, 0x11, buf);

	if (xref->crypt)
	{
	    sprintf(buf, "Standard %d bit RC4", xref->crypt->n * 8);
	    SetDlgItemTextA(hwnd, 0x12, buf);
	    strcpy(buf, "");
	    if (xref->crypt->p & (1 << 2))
		strcat(buf, "print, ");
	    if (xref->crypt->p & (1 << 3))
		strcat(buf, "modify, ");
	    if (xref->crypt->p & (1 << 4))
		strcat(buf, "copy, ");
	    if (xref->crypt->p & (1 << 5))
		strcat(buf, "annotate, ");
	    if (strlen(buf) > 2)
		buf[strlen(buf)-2] = 0;
	    else
		strcpy(buf, "none");
	    SetDlgItemTextA(hwnd, 0x13, buf);<commit_msg>fix winmain.c compilation: pdf_crypt.n was renamed to pdf_crypt.len (kjk)<commit_after>#include <fitz.h>
#include <mupdf.h>
#include "pdfapp.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#include <shellapi.h>

#define ID_ABOUT	0x1000
#define ID_DOCINFO	0x1001

static HWND hwndframe = NULL;
static HWND hwndview = NULL;
static HDC hdc;
static HBRUSH bgbrush;
static HBRUSH shbrush;
static BITMAPINFO *dibinf;
static HCURSOR arrowcurs, handcurs, waitcurs;
static LRESULT CALLBACK frameproc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK viewproc(HWND, UINT, WPARAM, LPARAM);

static int bmpstride = 0;
static char *bmpdata = NULL;
static int justcopied = 0;

static pdfapp_t gapp;

/*
 * Associate Apparition with PDF files.
 */

void associate(char *argv0)
{
    char tmp[256];
    char *name = "Adobe PDF Document";
    HKEY key, kicon, kshell, kopen, kcmd;
    DWORD disp;

    /* HKEY_CLASSES_ROOT\.pdf */

    if (RegCreateKeyEx(HKEY_CLASSES_ROOT,
		".pdf", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &key, &disp))
	return;

    if (RegSetValueEx(key, "", 0, REG_SZ, "Apparition", strlen("Apparition")+1))
	return;

    RegCloseKey(key);

    /* HKEY_CLASSES_ROOT\Apparition */

    if (RegCreateKeyEx(HKEY_CLASSES_ROOT,
		"Apparition", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &key, &disp))
	return;

    if (RegSetValueEx(key, "", 0, REG_SZ, name, strlen(name)+1))
	return;

    /* HKEY_CLASSES_ROOT\Apparition\DefaultIcon */

    if (RegCreateKeyEx(key,
		"DefaultIcon", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kicon, &disp))
	return;

    sprintf(tmp, "%s,1", argv0);
    if (RegSetValueEx(kicon, "", 0, REG_SZ, tmp, strlen(tmp)+1))
	return;

    RegCloseKey(kicon);

    /* HKEY_CLASSES_ROOT\Apparition\Shell\Open\Command */

    if (RegCreateKeyEx(key,
		"shell", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kshell, &disp))
	return;
    if (RegCreateKeyEx(kshell,
		"open", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kopen, &disp))
	return;
    if (RegCreateKeyEx(kopen,
		"command", 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &kcmd, &disp))
	return;

    sprintf(tmp, "\"%s\" \"%%1\"", argv0);
    if (RegSetValueEx(kcmd, "", 0, REG_SZ, tmp, strlen(tmp)+1))
	return;

    RegCloseKey(kcmd);
    RegCloseKey(kopen);
    RegCloseKey(kshell);

    RegCloseKey(key);
}

/*
 * Dialog boxes
 */

void winwarn(pdfapp_t *app, char *msg)
{
    MessageBoxA(hwndframe, msg, "Apparition: Warning", MB_ICONWARNING);
}

void winerror(pdfapp_t *app, fz_error *error)
{
    MessageBoxA(hwndframe, error->msg, "Apparition: Error", MB_ICONERROR);
    exit(1);
}

int winfilename(char *buf, int len)
{
    OPENFILENAME ofn;
    strcpy(buf, "");
    memset(&ofn, 0, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwndframe;
    ofn.lpstrFile = buf;
    ofn.nMaxFile = len;
    ofn.lpstrInitialDir = NULL;
    ofn.lpstrTitle = "Apparition: Open PDF file";
    ofn.lpstrFilter = "PDF Files (*.pdf)\0*.pdf\0All Files\0*\0\0";
    ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
    return GetOpenFileName(&ofn);
}

static char pd_filename[256] = "The file is encrypted.";
static char pd_password[256] = "";
static int pd_okay = 0;

INT CALLBACK
dlogpassproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
	SetDlgItemText(hwnd, 4, pd_filename);
	return TRUE;
    case WM_COMMAND:
	switch(wParam)
	{
	case 1:
	    pd_okay = 1;
	    GetDlgItemText(hwnd, 3, pd_password, sizeof pd_password);
	    EndDialog(hwnd, 0);
	    return TRUE;
	case 2:
	    pd_okay = 0;
	    EndDialog(hwnd, 0);
	    return TRUE;
	}
	break;
    }
    return FALSE;
}

char *winpassword(pdfapp_t *app, char *filename)
{
    char buf[124], *s;
    strcpy(buf, filename);
    s = buf;
    if (strrchr(s, '\\')) s = strrchr(s, '\\') + 1;
    if (strrchr(s, '/')) s = strrchr(s, '/') + 1;
    if (strlen(s) > 32)
	strcpy(s + 30, "...");
    sprintf(pd_filename, "The file \"%s\" is encrypted.", s);
    DialogBox(NULL, "IDD_DLOGPASS", hwndframe, dlogpassproc);
    if (pd_okay)
	return pd_password;
    return NULL;
}

INT CALLBACK
dloginfoproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    char buf[256];
    pdf_xref *xref = gapp.xref;
    fz_obj *obj;

    switch(message)
    {
    case WM_INITDIALOG:

	SetDlgItemTextA(hwnd, 0x10, gapp.filename);

	sprintf(buf, "PDF %g", xref->version);
	SetDlgItemTextA(hwnd, 0x11, buf);

	if (xref->crypt)
	{
	    sprintf(buf, "Standard %d bit RC4", xref->crypt->len * 8);
	    SetDlgItemTextA(hwnd, 0x12, buf);
	    strcpy(buf, "");
	    if (xref->crypt->p & (1 << 2))
		strcat(buf, "print, ");
	    if (xref->crypt->p & (1 << 3))
		strcat(buf, "modify, ");
	    if (xref->crypt->p & (1 << 4))
		strcat(buf, "copy, ");
	    if (xref->crypt->p & (1 << 5))
		strcat(buf, "annotate, ");
	    if (strlen(buf) > 2)
		buf[strlen(buf)-2] = 0;
	    else
		strcpy(buf, "none");
	    SetDlgItemTextA(hwnd, 0x13, buf);
{'fraction_non_alphanumeric': 0.15216519647153168, 'fraction_numerical': 0.01904570970328789, 'mean_word_length': 3.7374169040835707, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3356442', 'n_tokens_mistral': 4300, 'n_tokens_neox': 4072, 'n_words': 901}