> "Chris M. Thomasson" wrote in message
> news:mpgmfd$q02$1...@speranza.aioe.org...
Ummmm.... There is a damn getchar that I forgot to remove! So, the program
completes and hits a damn getchar. Well, you need to hit a key. Anyway,
I forgot to remove this. Sorry!!!!
;^o
Here is the code just in case PasteBin does not work for some reason:
(this one has the getchar commented out)
__________________________________________________________________
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
/*-------------------------*/
#define CT_LB "\r\n"
#define CT_PREC "%.11lf"
#define CT_PREC_EPS "%.13lf"
#define CT_EPS 0.00000000001
#define CT_MIN(mp_0, mp_1) ((mp_0) < (mp_1) ? (mp_0) : (mp_1))
struct main_args
{
int argc;
char** argv;
};
/*-------------------------*/
double
double_to_string_to_double(
double n
){
double nn = 0.0;
double ndif = 0.0;
char nbuf[UCHAR_MAX + 1] = { '\0' };
sprintf(nbuf, CT_PREC, n);
sscanf(nbuf, "%lf", &nn);
ndif = fabs(n - nn);
/* `nn' has lost precision of `n'.*/
printf("lost precision: " CT_PREC_EPS CT_LB, ndif);
return nn;
}
double
string_to_double(
char const* nbuf
){
double n = 0.0;
sscanf(nbuf, "%lf", &n);
return n;
}
unsigned int
string_to_uint(
char const* nbuf
){
unsigned int n = 0;
sscanf(nbuf, "%u", &n);
return n;
}
/*-------------------------*/
#define SQ(mp_0) ((mp_0)*(mp_0))
struct complexn
{
double x;
double y;
};
#define COMPLEXN_SINIT() { 0.0, 0.0 }
struct complexn
complexn_add(
struct complexn self,
struct complexn n
){
struct complexn p = {
self.x + n.x,
self.y + n.y
};
return p;
}
struct complexn
complexn_mul(
struct complexn self,
struct complexn n
){
struct complexn p = {
self.x * n.x - self.y * n.y,
self.x * n.y + self.y * n.x
};
return p;
}
/*--------------------------*/
unsigned long
cantor_packing(
unsigned int n0,
unsigned int n1
){
unsigned long ret = 0.5 * (n0 + n1) * (n0 + n1 + 1) + n1;
return ret;
}
/*-------------------------*/
#define FFE_MARGS_NMIN 7
#define FFE_BUF_NMAX (1024 * 756)
#define FFE_ABETN (UCHAR_MAX + 1U)
struct ffe_buf
{
unsigned char buf[FFE_BUF_NMAX];
size_t bufsz;
};
struct ffe_abet
{
unsigned int abet[UCHAR_MAX + 1];
};
struct ffe_cplane
{
double xmin;
double xmax;
double ymin;
double ymax;
};
unsigned int
ffe_cplane_iterate(
struct complexn c,
struct complexn j,
unsigned int imax
){
unsigned int i = 0;
unsigned int imod = 0;
double dmin = 999999999999.0;
struct complexn z = c;
c = j; /* Julia Mode */
for (i = 0; i < imax; ++i)
{
double zd = 0.0;
z = complexn_mul(z, z);
z = complexn_add(z, c);
zd = SQ(z.x) + SQ(z.y);
dmin = CT_MIN(dmin, zd);
if (zd > 4.0)
{
imod = (unsigned int)((20371 + i) *
fabs(cos(i * i * dmin * 252307))) % FFE_ABETN;
return imod;
}
}
imod = (unsigned int)(40377 *
fabs(sin(dmin * 152307))) % FFE_ABETN;
return imod;
}
size_t
ffe_buf_read(
struct ffe_buf* const self,
FILE* const file
){
self->bufsz = fread(self->buf, 1, FFE_BUF_NMAX, file);
if (self->bufsz == FFE_BUF_NMAX)
{
return self->bufsz;
}
if (! ferror(file))
{
if (feof(file))
{
return self->bufsz;
}
}
printf("fread error!" CT_LB);
return 0;
}
size_t
ffe_buf_write(
struct ffe_buf const* const self,
FILE* const file
){
size_t bufsz = fwrite(self->buf, 1, self->bufsz, file);
if (bufsz == FFE_BUF_NMAX)
{
return bufsz;
}
if (! ferror(file))
{
return bufsz;
}
printf("fwrite error!" CT_LB);
return 0;
}
/*-------------------------*/
struct ffe_encrypt
{
struct ffe_cplane cplane; /* private key: plane */
struct complexn jplane; /* private key: Julia */
struct ffe_cplane pub_cplane; /* public key: plane */
struct ffe_buf* ebuf;
unsigned int flags;
size_t uwidth;
size_t fbytes;
FILE* fctext; /* clear text file */
FILE* fcipher; /* cipher text file */
};
int
ffe_encrypt_main_args_fctext_parse(
struct ffe_encrypt* const self,
struct main_args* const margs
){
if (margs->argc == FFE_MARGS_NMIN + 1)
{
unsigned int base = 1;
/* Try to open the cleartext [in] */
self->fctext = fopen(margs->argv[base], "rb");
if (self->fctext)
{
printf("Opened the cleartext [in]! <%s>" CT_LB, margs->argv[1]);
/* Try to open the ciphertext [out] */
self->fcipher = fopen(margs->argv[base + 1], "wb");
if (self->fcipher)
{
printf("Opened the ciphertext [out]! <%s>" CT_LB,
margs->argv[base + 1]);
return 1;
}
else
{
printf("Cannot open the ciphertext [out]! <%s>" CT_LB,
margs->argv[base + 1]);
}
fclose(self->fctext); /* check for errors!?!? */
}
printf("Cannot open the cleartext [int]! <%s>" CT_LB,
margs->argv[base]);
}
return 0;
}
int
ffe_encrypt_main_args_cplane_parse(
struct ffe_encrypt* const self,
struct main_args* const margs
){
if (margs->argc == FFE_MARGS_NMIN + 1)
{
/* Parse the complex plane dimensions (2d for now) */
unsigned int const icpbase = 3;
self->flags = string_to_uint(margs->argv[icpbase + 0]);
self->cplane.xmin = string_to_double(margs->argv[icpbase + 1]);
self->cplane.xmax = string_to_double(margs->argv[icpbase + 2]);
self->cplane.ymin = string_to_double(margs->argv[icpbase + 3]);
self->cplane.ymax = string_to_double(margs->argv[icpbase + 4]);
return 1;
}
return 0;
}
int
ffe_encrypt_parse(
struct ffe_encrypt* const self,
struct main_args* const margs
){
if (ffe_encrypt_main_args_cplane_parse(self, margs))
{
printf(
"xmin = " CT_PREC CT_LB "xmax = " CT_PREC CT_LB
"ymin = " CT_PREC CT_LB "ymax = " CT_PREC CT_LB,
self->cplane.xmin, self->cplane.xmax,
self->cplane.ymin, self->cplane.ymax
);
if (ffe_encrypt_main_args_fctext_parse(self, margs))
{
return 1;
}
}
return 0;
}
void
ffe_encrypt_first_pass(
struct ffe_encrypt* const self,
struct main_args const* const margs
){
self->fbytes = 0;
double cmean_sum = 0.0;
size_t cpack_msum = 0;
size_t cxor = 0;
for (;;)
{
size_t rbytes = ffe_buf_read(self->ebuf, self->fctext);
if (rbytes != 0)
{
size_t i = 0;
for (i = 0; i < rbytes; i += 2)
{
double cavg_0 = self->ebuf->buf[i] / ((double)UCHAR_MAX);
double cavg_1 = self->ebuf->buf[i + 1] /
((double)UCHAR_MAX);
cmean_sum = cmean_sum + cavg_0 + cavg_1;
cpack_msum =
(cpack_msum + cantor_packing(
self->ebuf->buf[i], self->ebuf->buf[i + 1])) %
1073741823;
cxor = cxor ^ self->ebuf->buf[i];
cxor = cxor ^ self->ebuf->buf[i + 1];
cxor = cxor ^ cpack_msum;
cmean_sum += ((cxor) % (UCHAR_MAX)) / ((double)UCHAR_MAX);
}
printf("ffe_encrypt_first_pass processed: %lu bytes" CT_LB,
(unsigned long)rbytes);
self->fbytes = self->fbytes + rbytes;
continue;
}
break;
}
printf(CT_LB);
{
/* build new location/offset (Public Key) into (Private Key) */
double cavg = cmean_sum / ((double)self->fbytes);
double cfmod = ((cavg + (cpack_msum / cxor)) * cxor) / ((cxor +
cpack_msum) % 1237);
cfmod = fmod(cfmod, 1.0);
/* try to avoid divide-by-zero conditions */
if (! cxor) cxor = 1;
if (! cpack_msum) cpack_msum = 1;
{
/* Generate Public Key Scale */
unsigned int sum_mod = 5;
double fmod_scale = ((1.0 + sqrt(5.0)) / 2.0) / 2.0;
double pub_xmin_scale = fmod(cavg * cfmod + (cxor /
(double)cpack_msum), fmod_scale);
double pub_xmax_scale = fmod((cavg * cfmod) * cxor + (cxor /
(double)cpack_msum), fmod_scale);
double pub_ymin_scale = fmod(cavg + cfmod + (cxor /
(double)cpack_msum), fmod_scale);
double pub_ymax_scale = fmod((cavg + cfmod) * cxor + (cxor /
(double)cpack_msum), fmod_scale);
pub_xmin_scale = pub_xmin_scale / ((cxor % 7) + 3);
pub_xmax_scale = pub_xmax_scale / (((cxor + cpack_msum) % 7) +
1);
pub_ymin_scale = pub_ymin_scale / (((cxor * cpack_msum) % 7) +
1);
pub_ymax_scale = pub_ymax_scale / (((((cxor * 3 + cpack_msum) +
7)) % 7) + 1);
pub_xmin_scale = (fmod(pub_xmin_scale + (cavg / 7), fmod_scale))
+ self->cplane.xmin;
pub_xmax_scale = (fmod(pub_xmax_scale + (cavg / 7), fmod_scale))
+ self->cplane.xmax;
pub_ymin_scale = (fmod(pub_ymin_scale + (cavg / 7), fmod_scale))
+ self->cplane.ymin;
pub_ymax_scale = (fmod(pub_ymax_scale + (cavg / 7), fmod_scale))
+ self->cplane.ymax;
cavg = fmod(cavg, fmod_scale);
cfmod = fmod(cfmod, fmod_scale);
cavg = 1.0 - fmod(cavg + cfmod, fmod_scale);
cpack_msum = cpack_msum * cavg + (cpack_msum % sum_mod) + 1;
cxor = cxor + (cxor % sum_mod) + 1;
self->pub_cplane.xmin =
double_to_string_to_double(pub_xmin_scale);
self->pub_cplane.xmax =
double_to_string_to_double(pub_xmax_scale);
self->pub_cplane.ymin =
double_to_string_to_double(pub_ymin_scale);
self->pub_cplane.ymax =
double_to_string_to_double(pub_ymax_scale);
/* decrypt setting 0 */
if (self->flags == 1)
{
self->pub_cplane = self->cplane;
}
printf("pub_xmin_scale = " CT_PREC CT_LB,
self->pub_cplane.xmin);
printf("pub_xmax_scale = " CT_PREC CT_LB,
self->pub_cplane.xmax);
printf("pub_ymin_scale = " CT_PREC CT_LB,
self->pub_cplane.ymin);
printf("pub_ymax_scale = " CT_PREC CT_LB,
self->pub_cplane.ymax);
}
}
}
int
ffe_encrypt_second_pass(
struct ffe_encrypt* const self,
struct main_args const* const margs
){
double xwidth = self->pub_cplane.xmax - self->pub_cplane.xmin;
double xheight = self->pub_cplane.ymax - self->pub_cplane.ymin;
double xstep = xwidth / self->uwidth;
double ystep = xheight / self->uwidth;
struct complexn c = COMPLEXN_SINIT();
{
size_t i = 0;
rewind(self->fctext);
printf(CT_LB);
for (i = 0; i < self->fbytes;)
{
size_t rbytes = ffe_buf_read(self->ebuf, self->fctext);
if (rbytes != 0)
{
size_t ic = 0;
for (ic = 0; ic < rbytes; ++ic, ++i)
{
/* Apply the cipher */
unsigned int imod = 0;
unsigned int cmod = self->ebuf->buf[ic];
unsigned int yi = i / self->uwidth;
unsigned int xi = i - yi * self->uwidth;
c.x = self->pub_cplane.xmin + xi * xstep;
c.y = self->pub_cplane.ymin + yi * ystep;
imod = ffe_cplane_iterate(c, self->jplane, 503);
/* encrypt/decrypt */
if (self->flags == 0)
{
imod = (imod + cmod) % FFE_ABETN;
}
else
{
int ec = cmod - imod;
if (ec < 0)
{
ec = cmod + FFE_ABETN;
imod = abs(imod - ec);
}
else
{
imod = ec;
}
}
/* commit cipher char to buffer */
self->ebuf->buf[ic] = imod;
if (! (i % 1000))
{
printf("i = %lu\r", (unsigned long)i);
}
}
/* write buffer to cipher file */
{
size_t wbytes = ffe_buf_write(self->ebuf,
self->fcipher);
if (wbytes < rbytes)
{
/* need to loop until the buffer is commited! */
printf("SINGLE WRITE IS FUC%%ED!" CT_LB);
getchar();
}
}
continue;
}
break;
}
if (i == self->fbytes)
{
printf("Passed at %lu bytes!" CT_LB CT_LB, (unsigned long)i);
return 1;
}
}
return 0;
}
/*-------------------------*/
int
main(
int argc,
char* argv[]
){
struct main_args self = { argc, argv };
{
struct ffe_buf ebuf = { { '\0' } };
struct ffe_encrypt fenc = {
{ -2.0, 2.0, -2.0, 2.0, }, /* private key: plane */
/* need to put this in the cmd line damn it! */
{ -0.7, 0.25 }, /* private key: Julia */
{ 0.0, 0.0, 0.0, 0.0, }, /* public key */
&ebuf,
0,
0,
0,
NULL,
NULL
};
if (ffe_encrypt_parse(&fenc, &self))
{
ffe_encrypt_first_pass(&fenc, &self);
printf(CT_LB);
printf("file size: %lu" CT_LB, (unsigned long)fenc.fbytes);
/* gain dims of fractal pad */
fenc.uwidth = (sqrt(fenc.fbytes) + 1.681) * 1.0;
printf("fractal dims: %lu x %lu" CT_LB,
(unsigned long)fenc.uwidth,
(unsigned long)fenc.uwidth
);
if (ffe_encrypt_second_pass(&fenc, &self))
{
if (fenc.flags == 0)
{
unsigned int base = 1;
printf(CT_LB CT_LB "Decrypt Command Line:" CT_LB CT_LB);
printf("%s %s_dc 1 " CT_PREC " " CT_PREC " " CT_PREC " "
CT_PREC CT_LB CT_LB,
self.argv[base + 1],
self.argv[base],
fenc.pub_cplane.xmin,
fenc.pub_cplane.xmax,
fenc.pub_cplane.ymin,
fenc.pub_cplane.ymax);
}
printf(CT_LB "No Errors Reported!" CT_LB CT_LB);
}
/* Check for ERRORS! */
fclose(fenc.fcipher);
printf("Closed the ciphertext [out]! <%s>" CT_LB, self.argv[2]);
fclose(fenc.fctext);
printf("Closed the cleartext [in]! <%s>" CT_LB, self.argv[1]);
}
else
{
printf("failed to parse!" CT_LB);
}
}
puts(CT_LB "Complete!" CT_LB);
/* getchar(); */
return 0;
}
__________________________________________________________________