Added new bitstream scrambler/descrambler module that should be compliant.

This commit is contained in:
Morten Rolland 1998-12-07 16:10:34 +00:00
parent a141d20d3c
commit 9e7193cc67
6 changed files with 199 additions and 200 deletions

View File

@ -5,6 +5,8 @@
* Copyright (C) 1998 Morten Rolland [Morten.Rolland@asker.mail.telia.com]
*/
#define CMD_SCRAMBLER_INIT 0x01
#define CMD_SCRAMBLER_INIT 0x01
#define CMD_SCRAMBLER_SCRAM_V29 0x02
#define CMD_SCRAMBLER_DESCR_V29 0x03
int scrambler_construct(ifax_modp self, va_list args);

View File

@ -2,10 +2,10 @@ LDFLAGS=-lm
CFLAGS=-O2 -g -Wall -pedantic -I../include
MODULES = send_to_audio.o pulsegen.o sinegen.o replicate.o \
mod_scrambler.o modulator-V29.o fsk_demod.o fsk_mod.o \
scrambler.o modulator-V29.o fsk_demod.o fsk_mod.o \
decode_serial.o encode_serial.o debug.o rateconvert.o
HELPERS = scrambler17.o
HELPERS =
all: modules.a
@ -13,4 +13,4 @@ modules.a: $(MODULES) $(HELPERS)
$(AR) rcs $@ $^
clean:
rm -f $(MODULES) *~
rm -f $(MODULES) modules.a *~

View File

@ -1,84 +0,0 @@
/* $Id$
******************************************************************************
Fax program for ISDN.
Scrambler.
Copyright (C) 1998 Morten Rolland [Morten.Rolland@asker.mail.telia.com]
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
#include <stdarg.h>
#include <ifax/ifax.h>
#include <ifax/modules/scrambler17.h>
#define MAXBUFFERING 256
typedef struct {
unsigned int state;
unsigned char buffer[MAXBUFFERING];
} scrambler_private;
/* Free the private data
*/
void scrambler_destroy(ifax_modp self)
{
free(self->private);
return;
}
int scrambler_command(ifax_modp self,int cmd,va_list cmds)
{
return 0;
}
int scrambler_handle(ifax_modp self, void *data, size_t length)
{
scrambler_private *priv=(scrambler_private *)self->private;
size_t chunk, remaining=length;
while ( remaining > 0 ) {
chunk = remaining;
if ( chunk > MAXBUFFERING )
chunk = MAXBUFFERING;
scramble17(data,priv->buffer,&priv->state,chunk);
ifax_handle_input(self->sendto,priv->buffer,chunk);
remaining -= chunk;
}
return length;
}
int scrambler_construct(ifax_modp self,va_list args)
{
scrambler_private *priv;
if (NULL==(priv=self->private=malloc(sizeof(scrambler_private))))
return 1;
self->destroy =scrambler_destroy;
self->handle_input =scrambler_handle;
self->command =scrambler_command;
priv->state=0;
return 0;
}

165
modules/scrambler.c Normal file
View File

@ -0,0 +1,165 @@
/* $Id$
******************************************************************************
Fax program for ISDN.
Scrambler for ITU-T Recommenedation V.17 and V.29
Copyright (C) 1998 Morten Rolland [Morten.Rolland@asker.mail.telia.com]
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
#include <stdarg.h>
#include <ifax/ifax.h>
#include <ifax/modules/scrambler.h>
#define MAXBUFFER 128
typedef struct {
unsigned int state;
void (*mode)(unsigned char *,unsigned char *, unsigned int *, int);
unsigned char buffer[MAXBUFFER];
} scrambler_private;
/* Function 'scramble_V29' is used by V.29 and V.17 modulation.
*
* Scramble a series of bytes from source (src) to destination (dst)
* buffer. The state is used and updated, and buffer size is
* in bytes. First bit to pass through the scrambling is bit 0
* of src[0], and the last is bit 7 of src[size-1].
* First bit to pop out of the scrambler is bit 0 of dst[0].
*/
static void scramble_V29(unsigned char *src, unsigned char *dst,
unsigned int *state, int size)
{
unsigned char next;
unsigned int st;
st = *state;
while ( size-- ) {
next = (st&0xff) ^ ((st>>5)&0xff) ^ (*src++);
*dst++ = next;
st = (next<<15) | (st>>8);
}
*state = st;
}
/* Function 'descramble_V29' is opposite of above.
*
* A self-syncronizing scrambler is such that, even when the
* scrambler has a state dependent on all previous values,
* the descrambler is only dependent on a sliding window the
* size of the scrambler state. An error in transmission will
* damage a few bits while the replicated state in the descrambler
* is wrong. When the channel is OK again, a correct replicated
* state will be established and communication continues.
*/
static void descramble_V29(unsigned char *src, unsigned char *dst,
unsigned int *state, int size)
{
unsigned char xor, input;
unsigned int st;
st = *state;
while ( size-- ) {
xor = (st&0xff) ^ ((st>>5)&0xff);
input = *src++;
*dst++ = input ^ xor;
st = (input<<15) | (st>>8);
}
*state = st;
}
/* The scrambler modules supports initialization and
* selection of mode (scramble/descramble).
* The command interface takes care of this.
*/
int scrambler_command(ifax_modp self,int cmd,va_list cmds)
{
scrambler_private *priv = self->private;
switch ( cmd ) {
case CMD_SCRAMBLER_INIT:
priv->state = 0;
break;
case CMD_SCRAMBLER_SCRAM_V29:
priv->mode = scramble_V29;
break;
case CMD_SCRAMBLER_DESCR_V29:
priv->mode = descramble_V29;
break;
}
return 0;
}
int scrambler_handle(ifax_modp self, void *data, size_t length)
{
scrambler_private *priv = self->private;
int chunk, remaining = length;
unsigned char *src = data;
while ( remaining > 0 ) {
chunk = remaining;
if ( chunk > MAXBUFFER )
chunk = MAXBUFFER;
(*priv->mode)(src,priv->buffer,&priv->state,chunk);
ifax_handle_input(self->sendto,priv->buffer,chunk);
src += chunk;
remaining -= chunk;
}
return length;
}
void scrambler_destroy(ifax_modp self)
{
free(self->private);
return;
}
int scrambler_construct(ifax_modp self,va_list args)
{
scrambler_private *priv;
if (NULL==(priv=self->private=malloc(sizeof(scrambler_private))))
return 1;
self->destroy = scrambler_destroy;
self->handle_input = scrambler_handle;
self->command = scrambler_command;
priv->state=0;
priv->mode = scramble_V29;
return 0;
}

View File

@ -1,111 +0,0 @@
/* $Id$
******************************************************************************
Fax program for ISDN.
Helper for scrambler module.
Copyright (C) 1998 Morten Rolland [Morten.Rolland@asker.mail.telia.com]
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
/* scrambler17.c
*
* Scramble/descramble a bitstream using a polynomial of
*
* 1 + x-18 + x-23
*
* As defined by the V.17 modulation standard.
*
* (C) 1998 Morten Rolland
*/
#include <ifax/ifax.h>
static unsigned char rotate_self[64] = {
0x00,0xfa,0x0f,0xf5,0x1f,0xe5,0x10,0xea,
0x3f,0xc5,0x30,0xca,0x20,0xda,0x2f,0xd5,
0x7e,0x84,0x71,0x8b,0x61,0x9b,0x6e,0x94,
0x41,0xbb,0x4e,0xb4,0x5e,0xa4,0x51,0xab,
0xfd,0x07,0xf2,0x08,0xe2,0x18,0xed,0x17,
0xc2,0x38,0xcd,0x37,0xdd,0x27,0xd2,0x28,
0x83,0x79,0x8c,0x76,0x9c,0x66,0x93,0x69,
0xbc,0x46,0xb3,0x49,0xa3,0x59,0xac,0x56
};
static unsigned char rotate_byte[256] = {
0x00,0xfd,0x7e,0x83,0x3f,0xc2,0x41,0xbc,
0x1f,0xe2,0x61,0x9c,0x20,0xdd,0x5e,0xa3,
0x0f,0xf2,0x71,0x8c,0x30,0xcd,0x4e,0xb3,
0x10,0xed,0x6e,0x93,0x2f,0xd2,0x51,0xac,
0x07,0xfa,0x79,0x84,0x38,0xc5,0x46,0xbb,
0x18,0xe5,0x66,0x9b,0x27,0xda,0x59,0xa4,
0x08,0xf5,0x76,0x8b,0x37,0xca,0x49,0xb4,
0x17,0xea,0x69,0x94,0x28,0xd5,0x56,0xab,
0x03,0xfe,0x7d,0x80,0x3c,0xc1,0x42,0xbf,
0x1c,0xe1,0x62,0x9f,0x23,0xde,0x5d,0xa0,
0x0c,0xf1,0x72,0x8f,0x33,0xce,0x4d,0xb0,
0x13,0xee,0x6d,0x90,0x2c,0xd1,0x52,0xaf,
0x04,0xf9,0x7a,0x87,0x3b,0xc6,0x45,0xb8,
0x1b,0xe6,0x65,0x98,0x24,0xd9,0x5a,0xa7,
0x0b,0xf6,0x75,0x88,0x34,0xc9,0x4a,0xb7,
0x14,0xe9,0x6a,0x97,0x2b,0xd6,0x55,0xa8,
0x01,0xfc,0x7f,0x82,0x3e,0xc3,0x40,0xbd,
0x1e,0xe3,0x60,0x9d,0x21,0xdc,0x5f,0xa2,
0x0e,0xf3,0x70,0x8d,0x31,0xcc,0x4f,0xb2,
0x11,0xec,0x6f,0x92,0x2e,0xd3,0x50,0xad,
0x06,0xfb,0x78,0x85,0x39,0xc4,0x47,0xba,
0x19,0xe4,0x67,0x9a,0x26,0xdb,0x58,0xa5,
0x09,0xf4,0x77,0x8a,0x36,0xcb,0x48,0xb5,
0x16,0xeb,0x68,0x95,0x29,0xd4,0x57,0xaa,
0x02,0xff,0x7c,0x81,0x3d,0xc0,0x43,0xbe,
0x1d,0xe0,0x63,0x9e,0x22,0xdf,0x5c,0xa1,
0x0d,0xf0,0x73,0x8e,0x32,0xcf,0x4c,0xb1,
0x12,0xef,0x6c,0x91,0x2d,0xd0,0x53,0xae,
0x05,0xf8,0x7b,0x86,0x3a,0xc7,0x44,0xb9,
0x1a,0xe7,0x64,0x99,0x25,0xd8,0x5b,0xa6,
0x0a,0xf7,0x74,0x89,0x35,0xc8,0x4b,0xb6,
0x15,0xe8,0x6b,0x96,0x2a,0xd7,0x54,0xa9
};
/* scramble a series of bytes from source (s) to destination (d) buffer.
* The state is used and updated, and buffer size is in *bytes*.
* First bit to pass through the scrambling is bit 0 of s[0], and
* the last is bit 7 of s[size-1].
* First bit to pop out of the scrambling is bit 0 of d[0] etc.
*/
void scramble17(unsigned char *s, unsigned char *d,
unsigned int *state, int size)
{
unsigned char next;
unsigned int st;
st = *state;
while ( size-- ) {
next = rotate_self[st&0x3f] ^
rotate_byte[*s] ^
rotate_byte[bitrev((st>>16)&0xff)];
st = (st<<8) | next;
*d = bitrev(next);
s++, d++;
}
*state = st;
}

29
test.c
View File

@ -166,11 +166,38 @@ void test_modulator_V29(void)
*/
}
void test_scrambler(void)
{
/* The new scrambler/descrambler in modules/scrambler.c should be
* compliant with V.29. Run a small empirical test here of
* encding/decoding.
*/
int t;
ifax_modp scrambler, descrambler, debug;
unsigned char source[200];
debug = ifax_create_module(IFAX_DEBUG,1);
scrambler = ifax_create_module(IFAX_SCRAMBLER);
descrambler = ifax_create_module(IFAX_SCRAMBLER);
ifax_command(descrambler,CMD_SCRAMBLER_DESCR_V29);
scrambler->sendto = descrambler;
descrambler->sendto = debug;
for ( t=0; t < 200; t++ )
source[t] = t;
ifax_handle_input(scrambler,source,200);
}
void main(int argc,char **argv)
{
setup_all_modules();
/* transmit_carrier(); */
test_modulator_V29();
/* test_modulator_V29(); */
test_scrambler();
}