Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Mandelbrot Set ASCII by Chris M. Thomasson :^)
- ___________________________________________________________________*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #define CT_MIN(mp_0, mp_1) (((mp_0) < (mp_1)) ? (mp_0) : (mp_1))
- #define CT_SQ(mp_n) ((mp_n) * (mp_n))
- #define CT_MBROT_ABET_PUNC \
- " ,./\\;'[]-=<>?:""{}_+`~"
- #define CT_MBROT_ABET_NUM_SHIFT \
- "!@#$%^&*()"
- #define CT_MBROT_ABET_NUM \
- "0123456789"
- #define CT_MBROT_ABET_UPPER \
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- #define CT_MBROT_ABET_LOWER \
- "abcdefghijklmnopqrstuvwxyz"
- #define CT_MBROT_ABET \
- CT_MBROT_ABET_UPPER \
- CT_MBROT_ABET_LOWER \
- CT_MBROT_ABET_NUM \
- CT_MBROT_ABET_NUM_SHIFT \
- CT_MBROT_ABET_PUNC
- #define CT_MBROT_ABET_MOD (sizeof(CT_MBROT_ABET) - 1U)
- #define CT_MBROT_ABET_LOOKUP(mp_char) \
- ((strchr(CT_MBROT_ABET, (mp_char))) - CT_MBROT_ABET)
- struct ct_mbrotcp_pixel
- {
- double x;
- double y;
- };
- struct ct_mbrotcp_plane
- {
- double xmin;
- double xmax;
- double ymin;
- double ymax;
- };
- struct ct_mbrotvp_plane
- {
- unsigned int width;
- unsigned int height;
- };
- unsigned int
- ct_mbrotpixel(
- struct ct_mbrotcp_pixel const* pixel_c,
- unsigned int const imax
- ){
- unsigned int i = 0;
- unsigned int ret = 0;
- struct ct_mbrotcp_pixel pixel_z = *pixel_c;
- struct ct_mbrotcp_pixel pixel_z_sq = {
- CT_SQ(pixel_z.x), CT_SQ(pixel_z.y)
- };
- struct ct_mbrotcp_pixel pixel_cj = { -0.7, 0.0 };
- double cmin = 9999999999999.0;
- for (i = 0; i < imax; ++i)
- {
- pixel_z.y = pixel_z.x * pixel_z.y * 2.0 + pixel_cj.y;
- pixel_z.x = pixel_z_sq.x - pixel_z_sq.y + pixel_cj.x;
- pixel_z_sq.x = CT_SQ(pixel_z.x);
- pixel_z_sq.y = CT_SQ(pixel_z.y);
- cmin = CT_MIN(cmin, CT_SQ(pixel_z_sq.x) + CT_SQ(pixel_z_sq.y));
- if (pixel_z_sq.x + pixel_z_sq.y > 2357)
- {
- ret = (unsigned int)(113747 * fabs(cos(i * cmin * 573)));
- putchar(CT_MBROT_ABET[ret % CT_MBROT_ABET_MOD]);
- return ret;
- }
- }
- {
- unsigned int mc = 0;
- ret = (unsigned int)(234777 * fabs(sin(i * cmin * 3233)));
- mc = ret % CT_MBROT_ABET_MOD;
- putchar(CT_MBROT_ABET[mc]);
- /* uncomment to display points that escape as "blanks" */
- /* putchar(' '); */
- }
- return ret;
- }
- void
- ct_mbrotplane(
- struct ct_mbrotcp_plane const* cp_plane,
- struct ct_mbrotvp_plane const* vp_plane,
- unsigned int const imax,
- unsigned int* icounts
- ){
- unsigned int vpyi = 0;
- struct ct_mbrotcp_pixel cp_pixel = { 0.0, 0.0 };
- double const cp_width = cp_plane->xmax - cp_plane->xmin;
- double const cp_height = cp_plane->ymax - cp_plane->ymin;
- double const xstep = cp_width / vp_plane->width;
- double const ystep = cp_height / vp_plane->height;
- for (vpyi = 0; vpyi < vp_plane->height; ++vpyi)
- {
- unsigned int vpxi = 0;
- cp_pixel.y = cp_plane->ymax - vpyi * ystep;
- for (vpxi = 0; vpxi < vp_plane->width; ++vpxi)
- {
- cp_pixel.x = cp_plane->xmin + vpxi * xstep;
- unsigned int icount = ct_mbrotpixel(&cp_pixel, imax);
- if (icounts)
- {
- unsigned int i = (vpyi * vp_plane->width) + vpxi;
- icounts[i] = icount;
- }
- }
- putchar('\n');
- }
- }
- /* Funny Fractal Encryption
- ___________________________________________________________________*/
- #define CT_FFE_ABET_NUM CT_MBROT_ABET_PUNC
- #define CT_FFE_ABET_UPPER CT_MBROT_ABET_UPPER
- #define CT_FFE_ABET_LOWER CT_MBROT_ABET_LOWER
- #define CT_FFE_ABET CT_MBROT_ABET
- #define CT_FFE_ABET_MOD CT_MBROT_ABET_MOD
- #define CT_FFE_ABET_LOOKUP CT_MBROT_ABET_LOOKUP
- struct ct_ffe
- {
- struct ct_mbrotcp_plane cp_plane;
- struct ct_mbrotvp_plane vp_plane;
- unsigned int* icounts;
- };
- int
- ct_ffe_create(
- struct ct_ffe* const self
- ){
- unsigned int n = self->vp_plane.width * self->vp_plane.height;
- self->icounts = calloc(n, sizeof(*self->icounts));
- if (self->icounts)
- {
- return 1;
- }
- return 0;
- }
- void
- ct_ffe_destroy(
- struct ct_ffe* const self
- ){
- free(self->icounts);
- }
- void
- ct_ffe_encrypt_prepare(
- struct ct_ffe* const self,
- unsigned int const imax
- ){
- ct_mbrotplane(&self->cp_plane, &self->vp_plane, imax, self->icounts);
- }
- int
- ct_ffe_encrypt(
- struct ct_ffe* const self,
- char const* omsg,
- unsigned int omsg_size,
- char* emsg,
- unsigned int emsg_size
- ){
- unsigned int i = 0;
- if (emsg_size < omsg_size)
- {
- return 0;
- }
- for (i = 0; i < omsg_size; ++i)
- {
- unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
- unsigned int cx = CT_FFE_ABET_LOOKUP(omsg[i]);
- unsigned int ec = (cx + icount) % CT_FFE_ABET_MOD;
- emsg[i] = CT_FFE_ABET[ec];
- }
- return 1;
- }
- int
- ct_ffe_decrypt(
- struct ct_ffe* const self,
- char const* emsg,
- unsigned int emsg_size,
- char* dmsg,
- unsigned int dmsg_size
- ){
- unsigned int i = 0;
- if (dmsg_size < emsg_size)
- {
- return 0;
- }
- for (i = 0; i < emsg_size; ++i)
- {
- unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
- unsigned int cx = CT_FFE_ABET_LOOKUP(emsg[i]);
- int ec = cx - icount;
- if (ec < 0)
- {
- ec = cx + CT_FFE_ABET_MOD;
- ec = abs(icount - ec);
- }
- dmsg[i] = CT_FFE_ABET[ec];
- }
- return 1;
- }
- /* Sample FFE Usage Program
- ___________________________________________________________________*/
- int
- main(void)
- {
- char omsg[] = "/Fun\\ <with> [Fractals]! (tm) `Hello'";
- char emsg[sizeof(omsg)] = { '\0' };
- char dmsg[sizeof(omsg)] = { '\0' };
- unsigned int n = sizeof(omsg) - 1;
- unsigned int dim = (((unsigned int)sqrt(n)) + 1);
- unsigned int dim_scale = 3;
- unsigned int gwidth = dim * dim_scale;
- unsigned int gheight = gwidth;
- struct ct_ffe fe = {
- { -1.1, 1.0, -1.0, 1.413 },
- { gwidth, gheight },
- NULL
- };
- if (ct_ffe_create(&fe))
- {
- printf(
- "CT_FFE_ABET_MOD = %u\r\n"
- "________________________________\r\n"
- "Fractal Pad: z^2 + c (Julia Set)\r\n"
- "Grid Dims: %u x %u\r\n"
- "--------------------------------\r\n",
- (unsigned int)CT_FFE_ABET_MOD,
- gwidth,
- gheight
- );
- ct_ffe_encrypt_prepare(&fe, CT_FFE_ABET_MOD * dim_scale);
- printf("________________________________\r\n");
- if (ct_ffe_encrypt(&fe, omsg, n, emsg, n))
- {
- if (ct_ffe_decrypt(&fe, emsg, n, dmsg, n))
- {
- printf("\r\noriginal = %s\r\n\r\n", omsg);
- printf("encrypted = %s\r\n\r\n", emsg);
- printf("decrypted = %s\r\n", dmsg);
- }
- }
- ct_ffe_destroy(&fe);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement