Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.crypt > #50120

Re: CHATX - a thread starter that's not as easy as it looks.

From Richard Harnden <richard.nospam@gmail.com>
Newsgroups sci.crypt
Subject Re: CHATX - a thread starter that's not as easy as it looks.
Date 2021-09-25 13:56 +0100
Organization A noiseless patient Spider
Message-ID <sin69v$4i2$1@dont-email.me> (permalink)
References <si9jk4$188$1@dont-email.me>

Show all headers | View raw


On 20/09/2021 10:17, Richard Heathfield wrote:
> lFcrdtwo,econ you itelt nx
> ook ohns  af you sdinliofy
> ttie aegtrxtxmxuxex xoxexcxyt
> hhs tlxo?ixhx xsxdxtx xnxrxpx
> tFar eat apcrexim,tc t semr poexx
> hot cxnra prodita eua doceyctdion
> a-t sa.dxrxtxax xtxlxoxsx xsxIxcn
> t-ehtrxex xhxnxix xoxkx,xax x xax
> eAed danptns yenetxig
> vnr hop'e sah ro.hxnx
> 

Here's my chatx encoder/decoder effort.
I spent far too much time on this.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 1024

typedef void (*chatx_t)(FILE *in);

static int encode_read_pair(FILE *in, char *a, char *b)
{
     int a_length;
     int b_length;
     char *z;

     if ( fgets(a, MAX_LINE, in) == NULL )
         return 0;

     z = strchr(a, '\n');
     if ( z == NULL )
     {
         fprintf(stderr, "Line too long.\n");
         exit(EXIT_FAILURE);
     }

     *z = '\0';
     a_length = z - a;

     if ( fgets(b, MAX_LINE, in) == NULL )
         b_length = 0;
     else
     {
         z = strchr(b, '\n');
         if ( z == NULL )
         {
             fprintf(stderr, "Line too long.\n");
             exit(EXIT_FAILURE);
         }

         *z = '\0';
         b_length = z - b;
     }

     if ( a_length == 0 )
     {
         a_length = 2;
         a[0] = 'x';
         a[1] = 'x';
         a[2] = '\0';
     }
     else
     {
         if ( a_length % 2 == 1 )
         {
             a[a_length++] = 'x';
             a[a_length] = '\0';
         }
     }

     if ( b_length == 0 )
     {
         b_length = 2;
         b[0] = 'x';
         b[1] = 'x';
         b[2] = '\0';
     }
     else
     {
         if ( b_length % 2 == 1 )
         {
             b[b_length++] = 'x';
             b[b_length] = '\0';
         }
     }

     if ( a_length > b_length )
     {
         for (int i=b_length; i<a_length; i++)
             b[i] = 'x';

         b_length = a_length;
         b[b_length] = '\0';
     }

     if ( b_length > a_length )
     {
         for (int i=a_length; i<b_length; i++)
             a[i] = 'x';

         a_length = b_length;
         a[a_length] = '\0';
     }

     return a_length;
}

static void chatx_encode(FILE *in)
{
     char a[MAX_LINE];
     char b[MAX_LINE];
     char c[MAX_LINE];
     char d[MAX_LINE];
     int length;
     char p,q,r,s;
     int c_pos;
     int d_pos;

     while (1)
     {
         c_pos = 0;
         d_pos = 0;

         if ( (length = encode_read_pair(in, a, b)) == 0 )
             break;

         for (int i=0; i<length; i+=2)
         {
             p = a[i];
             q = a[i+1];
             r = b[i];
             s = b[i+1];

             c[c_pos++] = r;
             c[c_pos++] = p;

             d[d_pos++] = s;
             d[d_pos++] = q;
         }

         c[length] = '\0';
         d[length] = '\0';

         puts(c);
         puts(d);
     }
}

static int decode_read_pair(FILE *in, char *a, char *b)
{
     int a_length;
     int b_length;
     char *z;

     if ( fgets(a, MAX_LINE, in) == NULL )
         return 0;

     z = strchr(a, '\n');
     if ( z == NULL )
     {
         fprintf(stderr, "Line too long.\n");
         exit(EXIT_FAILURE);
     }

     *z = '\0';
     a_length = z - a;

     if ( fgets(b, MAX_LINE, in) == NULL )
     {
         fprintf(stderr, "Expected pair of lines.\n");
         exit(EXIT_FAILURE);
     }

     z = strchr(b, '\n');
     if ( z == NULL )
     {
         fprintf(stderr, "Line too long.\n");
         exit(EXIT_FAILURE);
     }

     *z = '\0';
     b_length = z - b;

     if ( a_length != b_length )
     {
         fprintf(stderr, "Line pair has different lengths.");
         exit(EXIT_FAILURE);
     }

     if ( a_length % 2 != 0 )
     {
         fprintf(stderr,
             "Expected lines to be even number of characters.");
         exit(EXIT_FAILURE);
     }

     return a_length;
}

static void chatx_decode(FILE *in)
{
     char a[MAX_LINE];
     char b[MAX_LINE];
     char c[MAX_LINE];
     char d[MAX_LINE];
     int length;
     char p,q,r,s;
     int c_pos;
     int d_pos;

     while (1)
     {
         c_pos = 0;
         d_pos = 0;

         if ( (length = decode_read_pair(in, a, b)) == 0 )
             break;

         for (int i=0; i<length; i+=2)
         {
             p = a[i];
             q = a[i+1];
             r = b[i];
             s = b[i+1];

             c[c_pos++] = q;
             c[c_pos++] = s;

             d[d_pos++] = p;
             d[d_pos++] = r;
         }

         c[length] = '\0';
         d[length] = '\0';

         puts(c);
         puts(d);
     }
}

int main(int argc, char *argv[])
{
     chatx_t chatx = chatx_encode;

     if ( argc > 2 )
     {
         fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
         return EXIT_FAILURE;
     }

     if ( argc == 2 )
     {
         if ( strcmp(argv[1], "-d") == 0 )
             chatx = chatx_decode;
         else
         {
             if ( strcmp(argv[1], "-e") == 0 )
                 chatx = chatx_encode;
             else
             {
                 fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
                 return EXIT_FAILURE;
             }
         }
     }

     chatx(stdin);

     return 0;
}

Back to sci.crypt | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-20 10:17 +0100
  Re: CHATX - a thread starter that's not as easy as it looks. MM <mrvmurray@gmail.com> - 2021-09-20 07:00 -0700
    Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-20 15:17 +0100
      Re: CHATX - a thread starter that's not as easy as it looks. MM <mrvmurray@gmail.com> - 2021-09-20 10:52 -0700
        Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-20 19:10 +0100
  Re: CHATX - a thread starter that's not as easy as it looks. Max <maxturv26@gmx.net> - 2021-09-20 20:36 +0200
    Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-20 19:42 +0100
      Re: CHATX - a thread starter that's not as easy as it looks. Max <maxturv26@gmx.net> - 2021-09-20 20:50 +0200
        Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-20 21:02 +0100
          Re: CHATX - a thread starter that's not as easy as it looks. Max <maxturv26@gmx.net> - 2021-09-20 22:24 +0200
            Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-20 22:08 +0100
      Re: CHATX - a thread starter that's not as easy as it looks. wizzofozz <oxxxxxxxxxxxs@gmail.com> - 2021-09-20 21:43 +0200
        Re: CHATX - a thread starter that's not as easy as it looks. Max <maxturv26@gmx.net> - 2021-09-20 21:50 +0200
      Re: CHATX - a thread starter that's not as easy as it looks. Phil Carmody <pc+usenet@asdf.org> - 2021-09-21 17:00 +0300
        Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-21 15:22 +0100
        Re: CHATX - a thread starter that's not as easy as it looks. Max <maxturv26@gmx.net> - 2021-09-21 23:31 +0200
          Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-21 23:03 +0100
            Re: CHATX - a thread starter that's not as easy as it looks. Max <maxturv26@gmx.net> - 2021-09-22 00:22 +0200
        Re: CHATX - a thread starter that's not as easy as it looks. wizzofozz <oxxxxxxxxxxxs@gmail.com> - 2021-09-25 13:29 +0200
          Re: CHATX - a thread starter that's not as easy as it looks. Phil Carmody <pc+usenet@asdf.org> - 2021-09-26 14:39 +0300
  Re: CHATX - a thread starter that's not as easy as it looks. Richard Harnden <richard.nospam@gmail.com> - 2021-09-21 15:07 +0100
    Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-21 15:18 +0100
      Re: CHATX - a thread starter that's not as easy as it looks. Phil Carmody <pc+usenet@asdf.org> - 2021-09-21 21:43 +0300
        Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-21 20:41 +0100
  Re: CHATX - a thread starter that's not as easy as it looks. wizzofozz <oxxxxxxxxxxxs@gmail.com> - 2021-09-25 02:24 +0200
    Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-25 05:24 +0100
  Re: CHATX - a thread starter that's not as easy as it looks. Richard Harnden <richard.nospam@gmail.com> - 2021-09-25 13:56 +0100
    Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-25 14:21 +0100
      Re: CHATX - a thread starter that's not as easy as it looks. Richard Heathfield <rjh@cpax.org.uk> - 2021-09-25 14:28 +0100
    Re: CHATX - a thread starter that's not as easy as it looks. wizzofozz <oxxxxxxxxxxxs@gmail.com> - 2021-09-25 16:11 +0200

csiph-web