T168_Debug222\appl\Barcode\Two\DataMatrix文件:CMPRESSD.C

/******************************************************************************
 *                                                                            *
 *                         M O D U L E   D E F I N E                          *
 *                                                                            *
 ******************************************************************************/

#define CMPRESSD_C

/******************************************************************************
 *                                                                            *
 *        C O M P I L E R   D E F I N E D   I N C L U D E   F I L E S         *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *            U S E R   D E F I N E D   I N C L U D E   F I L E S             *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *                         L O C A L   D E F I N E S                          *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *                        L O C A L   T Y P E D E F S                         *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *             L O C A L   F U N C T I O N   P R O T O T Y P E S              *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *    L O C A L   I N I T I A L I Z E D   D A T A   D E F I N I T I O N S     *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *    L O C A L   U N I T I A L I Z E D   D A T A   D E F I N I T I O N S     *
 *                                                                            *
 ******************************************************************************/

  #include "model.h"
  #include "idmcmpat.h"
  #include "idmmacro.h"
  #include "idmtypes.h"
  #include "cmpressd.h"
  #include "bfrmanag.h"
  #include "string.h"           /* memset() */
  #if defined ( STINGY_MEMORY )
  #include "idmmemmn.h"
  #endif
  #include "crc16.h"
  #include "sentinel.h"

  #if defined( _MSC_VER )
    #if ( _MSC_VER < 700 )
      #pragma optimize("",off )
    #endif
  #endif

  typedef enum
  {
    DF_NUMERIC               , /* -- 0 .. n                    1 .. 9   */
    DF_ALPHABETIC            , /* -- A .. x                    B .. Z   */
    DF_ALPHABETIC_SPACE      , /* -- A .. Z , space            @        */
    DF_ALPHANUMERIC_SPACE    , /* -- A .. Z , 0 .. 9 , space   &        */
    DF_NUMERIC_SPACE         , /* -- 0 .. 9 , space            #        */
    DF_ALPHA_PUNCTUATION     , /* -- A .. Z , 0 .. 9 , " ,.-/" %        */
    DF_SEVEN_BIT             , /* -- any seven bit             ~        */
    DF_EIGHT_BIT             , /* -- any eight bit             *        */
    DF_CRC_16_BIT              /* -- A CRC                     +        */
  } Data_Format ;

  /* Named Numbers                                                       */

  #define       DATA_CODE_CRC_LENGTH            U8CONST ( 16 )
  #define       DATA_CODE_LONG_CRC_LENGTH       U8CONST ( 32 )
  #define       VARIABLE_500_LENGTH             U8CONST (  9 )
  #define       VARIABLE_2000_LENGTH            U8CONST ( 14 )

  #define       CHAR_UC_A        U8CONST( 0X41 ) /*  'A'  */
  #define       CHAR_UC_B        U8CONST( 0X42 ) /*  'B'  */
  #define       CHAR_UC_Z        U8CONST( 0X5A ) /*  'Z'  */
  #define       CHAR_ZERO        U8CONST( 0X30 ) /*  '0'  */
  #define       CHAR_ONE         U8CONST( 0X31 ) /*  '1'  */
  #define       CHAR_NINE        U8CONST( 0X39 ) /*  '9'  */
  #define       CHAR_SPACE       U8CONST( 0X20 ) /*  ' '  */
  #define       CHAR_EXCLAM      U8CONST( 0X21 ) /*  '!'  */
  #define       CHAR_SHARP       U8CONST( 0X23 ) /*  '#'  */
  #define       CHAR_PERCENT     U8CONST( 0X25 ) /*  '%'  */
  #define       CHAR_AMPERSAND   U8CONST( 0X26 ) /*  '&'  */
  #define       CHAR_STAR        U8CONST( 0X2A ) /*  '*'  */
  #define       CHAR_COMMA       U8CONST( 0X2C ) /*  ','  */
  #define       CHAR_PERIOD      U8CONST( 0X2E ) /*  '.'  */
  #define       CHAR_SLASH       U8CONST( 0X2F ) /*  '/'  */
  #define       CHAR_QUESTION    U8CONST( 0X3F ) /*  '?'  */
  #define       CHAR_AT_SIGN     U8CONST( 0X40 ) /*  '@'  */
  #define       CHAR_TILDE       U8CONST( 0X7E ) /*  '~'  */
  #define       CHAR_MINUS       U8CONST( 0X2D ) /*  '-'  */
  #define       CHAR_PLUS        U8CONST( 0X2B ) /*  '+'  */

  /* Format IDs */

  #define       NUMERIC_SPACE_500         ( ( IDM_Format_ID )  1 )
  #define       ALPHABETIC_SPACE_500      ( ( IDM_Format_ID )  2 )
  #define       ALPHA_PUNC_SPACE_500      ( ( IDM_Format_ID )  3 )
  #define       ALPHANUMERIC_SPACE_500    ( ( IDM_Format_ID )  4 )
  #define       SEVEN_BIT_500             ( ( IDM_Format_ID )  5 )
  #define       EIGHT_BIT_500             ( ( IDM_Format_ID )  6 )
  #define       PREPROGRAMMED_500         ( ( IDM_Format_ID )  7 )

  #define       LAST_500_ID           ( ( IDM_Format_ID ) PREPROGRAMMED_500 )

  #define       OFFSET_FOR_2000           ( ( IDM_Format_ID ) 10 )

  #define       FIRST_2000_ID         ( ( IDM_Format_ID ) NUMERIC_SPACE_2000 )

  #define       NUMERIC_SPACE_2000        ( ( IDM_Format_ID ) 11 )
  #define       ALPHABETIC_SPACE_2000     ( ( IDM_Format_ID ) 12 )
  #define       ALPHA_PUNC_SPACE_2000     ( ( IDM_Format_ID ) 13 )
  #define       ALPHANUMERIC_SPACE_2000   ( ( IDM_Format_ID ) 14 )
  #define       SEVEN_BIT_2000            ( ( IDM_Format_ID ) 15 )
  #define       EIGHT_BIT_2000            ( ( IDM_Format_ID ) 16 )
  #define       PREPROGRAMMED_2000        ( ( IDM_Format_ID ) 17 )

  #define       LAST_DOUBLE_ID        ( ( IDM_Format_ID ) PREPROGRAMMED_2000 )


  #define       NUMBER_BITS                     24
  #define       NUMBER_BITS_32                  32

  #define       BIGGEST_NUMBER                U32CONST( 16777216 )
  #define       BIGGEST_NUMBER_32             U32CONST( 4294967295 )


  #define       NUMTAB_FORMAT              DF_NUMERIC
  #define       NUMTAB_SUBTRACT            CHAR_ZERO
                                           /* ( ( UNS_INT_8_BITS ) '0' ) */

  #define       ABCTAB_FORMAT              DF_ALPHABETIC
  #define       ABCTAB_SUBTRACT            CHAR_UC_A
                                           /* ( ( UNS_INT_8_BITS ) 'A' ) */

  #define       NMSTAB_FORMAT              DF_NUMERIC_SPACE
  #define       NMSTAB_POSITIONS_IN_NUMBER ( ( UNS_FST_8_BITS  )  6 )
  #define       NMSTAB_SUBTRACT            ( ( UNS_INT_8_BITS  ) 47 )
  #define       NMSTAB_CHOICES             ( ( UNS_INT_32_BITS ) 11 )
  #define       NMSTAB_MAXNUM              ( BIGGEST_NUMBER / NMSTAB_CHOICES )
  #define       NMSTAB_MAXNUM_32           ( BIGGEST_NUMBER_32/NMSTAB_CHOICES )

  #define       ALPTAB_FORMAT              DF_ALPHABETIC_SPACE
  #define       ALPTAB_POSITIONS_IN_NUMBER ( ( UNS_FST_8_BITS  )  5 )
  #define       ALPTAB_SUBTRACT            ( ( UNS_INT_8_BITS  ) 64 )
  #define       ALPTAB_CHOICES             ( ( UNS_INT_32_BITS ) 27 )
  #define       ALPTAB_MAXNUM              ( BIGGEST_NUMBER / ALPTAB_CHOICES )
  #define       ALPTAB_MAXNUM_32           ( BIGGEST_NUMBER_32/ALPTAB_CHOICES )

  #define       APUTAB_FORMAT              DF_ALPHA_PUNCTUATION
  #define       APUTAB_POSITIONS_IN_NUMBER ( ( UNS_FST_8_BITS  )  4 )
  #define       APUTAB_SUBTRACT            ( ( UNS_INT_8_BITS  ) 64 )
  #define       APUTAB_CHOICES             ( ( UNS_INT_32_BITS ) 41 )
  #define       APUTAB_MAXNUM              ( BIGGEST_NUMBER / APUTAB_CHOICES )
  #define       APUTAB_MAXNUM_32           ( BIGGEST_NUMBER_32/APUTAB_CHOICES )

  #define       ANMTAB_FORMAT              DF_ALPHANUMERIC_SPACE
  #define       ANMTAB_POSITIONS_IN_NUMBER ( ( UNS_FST_8_BITS  )  4 )
  #define       ANMTAB_SUBTRACT            ( ( UNS_INT_8_BITS  ) 64 )
  #define       ANMTAB_CHOICES             ( ( UNS_INT_32_BITS ) 37 )
  #define       ANMTAB_MAXNUM              ( BIGGEST_NUMBER / ANMTAB_CHOICES )
  #define       ANMTAB_MAXNUM_32           ( BIGGEST_NUMBER_32/ANMTAB_CHOICES )

  #define       SVNTAB_FORMAT              DF_SEVEN_BIT
  #define       SVNTAB_CHOICES             ( ( UNS_INT_32_BITS ) 128 )
  #define       SVNTAB_MAXNUM              ( BIGGEST_NUMBER / SVNTAB_CHOICES )
  #define       SVNTAB_MAXNUM_32           ( BIGGEST_NUMBER_32/SVNTAB_CHOICES )

  #define       EGHTAB_FORMAT              DF_EIGHT_BIT
  #define       EGHTAB_CHOICES             ( ( UNS_INT_32_BITS ) 256 )
  #define       EGHTAB_MAXNUM              ( BIGGEST_NUMBER / EGHTAB_CHOICES )
  #define       EGHTAB_MAXNUM_32           ( BIGGEST_NUMBER_32/EGHTAB_CHOICES )

  #define       CRCTAB_FORMAT              DF_CRC_16_BIT
  #define       CRCTAB_CHOICES             U32CONST( 65536 )
  #define       CRCTAB_MAXNUM              ( ( UNS_INT_32_BITS )  1 )


  #define       MAX_POSITIONS_IN_NUMBER    ( ( UNS_FST_8_BITS  )  6 )

  static const UNS_INT_32_BITS      numeric_multiplier[]      =
                                                    { U32CONST ( 1      ) ,
                                                      U32CONST ( 11     ) ,
                                                      U32CONST ( 121    ) ,
                                                      U32CONST ( 1331   ) ,
                                                      U32CONST ( 14641  ) ,
                                                      U32CONST ( 161051 ) } ;

  static const UNS_INT_32_BITS      alphabetic_multiplier[]   =
                                                    { U32CONST ( 1      ) ,
                                                      U32CONST ( 27     ) ,
                                                      U32CONST ( 729    ) ,
                                                      U32CONST ( 19683  ) ,
                                                      U32CONST ( 531441 ) } ;

  static const UNS_INT_32_BITS      alpha_punc_multiplier[]   =
                                                    { U32CONST ( 1      ) ,
                                                      U32CONST ( 41     ) ,
                                                      U32CONST ( 1681   ) ,
                                                      U32CONST ( 68921  ) } ;

  static const UNS_INT_32_BITS      alphanumeric_multiplier[] =
                                                    { U32CONST ( 1      ) ,
                                                      U32CONST ( 37     ) ,
                                                      U32CONST ( 1369   ) ,
                                                      U32CONST ( 50653  ) } ;

  static const UNS_INT_8_BITS       numeric_bits_if_last[]    =
                                                    { U8CONST ( 4  ) ,
                                                      U8CONST ( 7  ) ,
                                                      U8CONST ( 11 ) ,
                                                      U8CONST ( 14 ) ,
                                                      U8CONST ( 18 ) ,
                                                      U8CONST ( 21 ) } ;

  static const UNS_INT_8_BITS       alphabetic_bits_if_last[] =
                                                    { U8CONST ( 5  ) ,
                                                      U8CONST ( 10 ) ,
                                                      U8CONST ( 15 ) ,
                                                      U8CONST ( 20 ) ,
                                                      U8CONST ( 24 ) } ;

  static const UNS_INT_8_BITS       alpha_punc_bits_if_last[] =
                                                    { U8CONST ( 6  ) ,
                                                      U8CONST ( 11 ) ,
                                                      U8CONST ( 17 ) ,
                                                      U8CONST ( 22 ) } ;

  static const UNS_INT_8_BITS       alphanumeric_bits_if_last[]=
                                                    { U8CONST ( 6  ) ,
                                                      U8CONST ( 11 ) ,
                                                      U8CONST ( 16 ) ,
                                                      U8CONST ( 21 ) } ;


  static   IDM_Format_ID      current_format_id       = (IDM_Format_ID ) 0 ;
  static   UNS_INT_16_BITS    current_format_length   = U16CONST(0) ;
  static   UNS_INT_16_BITS    current_data_length     = U16CONST(0) ;


  typedef enum
  {
    NORMAL_PUBLIC_FORMAT    ,
    FIXED_BLOCKS_FORMAT     ,
    PRE_ENCODED_FORMAT      ,
    PRIVATE_FORMAT
  } Format_Type ;

  static   Format_Type        current_format_type               ;
  static   Boolean            current_format_variable_length    ;
  static   Boolean            current_vlen_is_500               ;
  static   Boolean            current_CRC_is_32_bits            ;
  static   UNS_INT_16_BITS    current_dlength_for_CRC           ;


  static   Data_Format        current_data_format               ;

  static   UNS_FST_8_BITS     current_positions_in_number       ;

  static   UNS_INT_32_BITS    current_multiplier[MAX_POSITIONS_IN_NUMBER] ;


  static   UNS_INT_16_BITS    full_numbers_in_code                        ;

  static   UNS_INT_8_BITS     current_bits_in_main                        ;

  static   UNS_INT_8_BITS     current_bits_in_last                        ;


  static   UNS_INT_8_BITS     current_bits_in_byte                        ;

  static   Boolean            special_CRC_work_required         ;

  static   UNS_INT_8_BITS     current_bits_if_last[MAX_POSITIONS_IN_NUMBER] ;

  static   UNS_INT_16_BITS    current_compr_bit_len     ;


  typedef struct
    {
      Data_Format       object_format           ;
      UNS_INT_8_BITS    bits_if_last_in_word    ; /* for encoding */
      UNS_INT_8_BITS    bits_if_first_in_word   ; /* for decoding */
      UNS_INT_8_BITS    highest_allowed         ; /* if B..Z or 1..9 */
      UNS_INT_32_BITS   actual_choices          ; /* for decoding */
      UNS_INT_32_BITS   multiplier              ;
    }  byte_compress_data ;

  #if   defined ( C_MEMORY ) || defined ( ADA_MEMORY )

  static byte_compress_data     each_encoding_algorithm
                                                  [ IDM_MAX_FMT_STR_CHARS ] ;

  static byte_compress_data   * encoding_algorithm =
                                        & ( each_encoding_algorithm [ 0 ] ) ;

  #elif defined ( STINGY_MEMORY )

  static byte_compress_data   * encoding_algorithm ;

  static Boolean        Encoding_Memory_Initialized     = IDM_FALSE ;

  #else

        #error  Someone Screwed Up.  The Memory Model Is not Set!

  #endif

#ifdef INTERNAL_SYSTEM
  static Boolean        encode_is_encode        = IDM_TRUE  ;
#else
  static Boolean        encode_is_encode        = IDM_FALSE ;
#endif


#ifndef INTERNAL_SYSTEM
  #include "dcsysdef.h"
#endif

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  static UNS_INT_16_BITS calc_CRC_kermit
                             ( const IDM_Format_ID           id             ,
                               UNS_INT_8_BITS              * to_compress    ,
                               const UNS_INT_16_BITS         data_length    )
  {
    crc16_init () ;
    crc16_add_char ( ( UNS_INT_8_BITS )
        (   id & ( IDM_Format_ID ) U16CONST ( 0xFF ) ) ) ;
    crc16_add_char ( ( UNS_INT_8_BITS )
        ( ( UNS_INT_16_BITS ) ( id >> 8 ) & U16CONST ( 0xFF ) ) ) ;
    crc16_add_string ( to_compress , (UNS_INT_16_BITS) data_length ) ;
    return crc16_get_CRC () ;
  }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  void cmp_compress_reinit( void )
  {
    current_format_id       =       0 ;
    current_format_length   =       0 ;
    current_data_length     =       0 ;
  }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  extern IDM_Return_Code cmp_compress_init ( void )
  {
    #if   defined ( C_MEMORY ) || defined ( ADA_MEMORY )

    #elif defined ( STINGY_MEMORY )

      if ( IDM_FALSE == Encoding_Memory_Initialized )
      {
        Encoding_Memory_Initialized = IDM_TRUE ;
// TLCS用   97-08-28  TMS ?山 endstart
 //       encoding_algorithm = IDM_malloc ( ( IDM_size_t )
//                ( IDM_MAX_FMT_STR_CHARS * sizeof ( byte_compress_data ) ) ) ;*/
// TLCS用   97-08-28  TMS ?山 end
 
       if ( IDM_NULL == encoding_algorithm )
        {
          return ( IDM_ENC_ALG_OUT_OF_MEM ) ;
        }
      }
    #endif
    cmp_compress_reinit ( ) ;
    return ( IDM_RETURN_SUCCESS ) ;
  }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  static UNS_INT_8_BITS bits_in_number( UNS_INT_32_BITS number )
  {
    UNS_INT_8_BITS    bit_count            ;
    UNS_INT_32_BITS   work_number = number ;
    UNS_INT_8_BITS    work_byte            ;

    if     ( ( ( work_number >> 24 ) & U32CONST ( 0xFF ) ) == U32CONST ( 0 ) )
    {
      if   ( ( ( work_number >> 16 ) & U32CONST ( 0xFF ) ) == U32CONST ( 0 ) )
      {
        if ( ( ( work_number >>  8 ) & U32CONST ( 0xFF ) ) == U32CONST ( 0 ) )
        {
          work_byte = ( UNS_INT_8_BITS ) ( work_number & U32CONST ( 0xFF ) ) ;
          bit_count =  8 ;
        }
        else
        {
          work_byte = ( UNS_INT_8_BITS )
                               ( ( work_number >> 8 ) & U32CONST ( 0xFF ) ) ;
          bit_count = U8CONST ( 16 ) ;
        }
      }
      else
      {
        work_byte = ( UNS_INT_8_BITS )
                              ( ( work_number >> 16 ) & U32CONST ( 0xFF ) ) ;
        bit_count = 24 ;
      }
    }
    else
    {
      work_byte = ( UNS_INT_8_BITS )
                              ( ( work_number >> 24 ) & U32CONST ( 0xFF ) ) ;
      bit_count = 32 ;
    }
    for ( ;; )
    {
      if ( ( work_byte & U8CONST ( 0x80 ) ) == 0 )
      {
        work_byte <<= 1 ;
        bit_count-- ;
      }
      else
      {
        return bit_count ;
      }
    }
  }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  static IDM_Return_Code setup_current_data
                       ( const IDM_Format_ID            work_id             ,
                         const Boolean                  encode_request      )
    {

      UNS_FST_8_BITS    i           ;

      switch ( work_id )
      {
        case NUMERIC_SPACE_500      :
                                 current_format_type =
                                      NORMAL_PUBLIC_FORMAT     ;
                                 current_data_format =
                                      NMSTAB_FORMAT            ;
                                 current_positions_in_number =
                                   ( UNS_FST_8_BITS )
                                   ( QUICK_DONGLE_ENCODE_DECODE
                                   ( 6 ,  927 , 1440 , 978 , 1105 ) ) ;
                                 for ( i=0                            ;
                                       i< current_positions_in_number ;
                                       i++                            )
                                 {
                                   current_multiplier [i]=
                                     numeric_multiplier[i]     ;
                                   current_bits_if_last[i] =
                                     numeric_bits_if_last[i]   ;
                                 }
                                 break ;
        case ALPHABETIC_SPACE_500   :
                                  current_format_type =
                                       NORMAL_PUBLIC_FORMAT     ;
                                  current_data_format =
                                       ALPTAB_FORMAT            ;
                                  current_positions_in_number =
                                    ( UNS_FST_8_BITS )
                                    ( QUICK_DONGLE_ENCODE_DECODE
                                    ( 5 , 354 , 683 , 3919 , 155 ) ) ;
                                  for ( i=0                            ;
                                        i< current_positions_in_number ;
                                        i++                            )
                                  {
                                    current_multiplier [i]=
                                      alphabetic_multiplier[i]   ;
                                    current_bits_if_last[i] =
                                      alphabetic_bits_if_last[i] ;
                                  }
                                  break ;
        case ALPHA_PUNC_SPACE_500   :
                                  current_format_type =
                                       NORMAL_PUBLIC_FORMAT     ;
                                  current_data_format =
                                       APUTAB_FORMAT            ;
                                  current_positions_in_number =
                                          ( UNS_FST_8_BITS )
                                          ( QUICK_DONGLE_ENCODE_DECODE
                                            ( 4 , 803 , 1392 , 27 , 2900 ) ) ;
                                  for ( i=0                            ;
                                        i< current_positions_in_number ;
                                        i++                            )
                                  {
                                    current_multiplier [i]=
                                      alpha_punc_multiplier[i]   ;
                                    current_bits_if_last[i] =
                                      alpha_punc_bits_if_last[i] ;
                                  }
                                  break ;
        case ALPHANUMERIC_SPACE_500 :
                                  current_format_type =
                                       NORMAL_PUBLIC_FORMAT     ;
                                  current_data_format =
                                       ANMTAB_FORMAT            ;
                                  current_positions_in_number =
                                          ( UNS_FST_8_BITS )
                                          ( QUICK_DONGLE_ENCODE_DECODE
                                            ( 4 , 301 , 781 , 3558 , 1370 ) ) ;
                                  for ( i=0                            ;
                                        i< current_positions_in_number ;
                                        i++                            )
                                  {
                                    current_multiplier [i]=
                                      alphanumeric_multiplier[i]   ;
                                    current_bits_if_last[i] =
                                      alphanumeric_bits_if_last[i] ;
                                  }
                                  break ;
        case SEVEN_BIT_500          :
        case PREPROGRAMMED_500      :
                                      current_format_type =
                                           FIXED_BLOCKS_FORMAT      ;
                                      current_bits_in_byte =
                                       ( UNS_INT_8_BITS )
                                         ( QUICK_DONGLE_ENCODE_DECODE
                                         ( 7 , 3092 , 2132 , 461 , 1860 ) ) ;
                                      break ;
        case EIGHT_BIT_500          :
                                      current_format_type =
                                           FIXED_BLOCKS_FORMAT      ;
                                      current_bits_in_byte =
                                       ( UNS_INT_8_BITS )
                                         ( QUICK_DONGLE_ENCODE_DECODE
                                         ( 8 ,  845 , 2359 , 408 , 1472 ) ) ;
                                      break ;
        default                     : if ( encode_request == IDM_TRUE )
                                      {
                                        return IDM_USER_UNDEFINED_ID ;
                                      }
                                      else
                                      {
                                        return IDM_MATRIX_UNDEFINED_ID ;
                                      }
      }  /* end of switch on various double-named format ids */
      return IDM_RETURN_SUCCESS ;
    }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  static IDM_Return_Code set_current_private
                                 ( UNS_INT_8_BITS       * format_string     ,
                                   UNS_INT_16_BITS      * total_info_length )
    {
      UNS_INT_8_BITS       * string_pointer = format_string ;
      UNS_INT_16_BITS     string_counter        = current_data_length   ;
      UNS_INT_16_BITS     format_counter        = current_format_length ;
      UNS_INT_8_BITS      work_byte                                     ;
      UNS_INT_32_BITS     multiplier_to_use     = U32CONST ( 1 )        ;
      UNS_INT_32_BITS     last_multiplier       = U32CONST ( 1 )        ;
      UNS_INT_32_BITS     work_choices                                  ;
      UNS_INT_32_BITS     max_multiplier                                ;
      byte_compress_data  * algorithm =  encoding_algorithm             ;
      byte_compress_data  * word_length_info =   encoding_algorithm     ;
      UNS_INT_8_BITS      temp_bit_length                               ;
      UNS_INT_16_BITS     info_length = 0 ;

      for (                                 ;
            string_counter >  U16CONST(0)   ;
            --string_counter , algorithm++  )
      {
        if ( format_counter-- > U16CONST(1) )
        {
          work_byte = *(string_pointer++) ;
        }
        else
        {
          format_counter = U16CONST(0) ;
          work_byte = *string_pointer ;
        }
        ( * algorithm ) .multiplier = multiplier_to_use ;
        if ( ( work_byte >= CHAR_ONE ) && ( work_byte <= CHAR_NINE ) )
        {
          ( * algorithm ) .object_format = DF_NUMERIC ;
          work_choices = ( UNS_INT_32_BITS ) ( work_byte - CHAR_ZERO+ 1 ) ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;    /* JJL */
          }
          else
          {
            max_multiplier = BIGGEST_NUMBER    / work_choices ;
          }
          ( * algorithm ) .highest_allowed = work_byte ;
        }
        else if ( ( work_byte >= CHAR_UC_B ) && ( work_byte <= CHAR_UC_Z ) )
        {
          ( * algorithm ) .object_format = DF_ALPHABETIC ;
          work_choices = ( UNS_INT_32_BITS ) ( work_byte - CHAR_UC_A+ 1 ) ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = BIGGEST_NUMBER    / work_choices ;
          }
          ( * algorithm ) .highest_allowed = work_byte ;
        }
        else if ( work_byte == CHAR_AT_SIGN )
        {
          ( * algorithm ) .object_format = DF_ALPHABETIC_SPACE ;
          work_choices =  ALPTAB_CHOICES ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = ALPTAB_MAXNUM    ;
          }
        }
        else if ( work_byte == CHAR_AMPERSAND )
        {
          ( * algorithm ) .object_format = DF_ALPHANUMERIC_SPACE ;
          work_choices =  ANMTAB_CHOICES ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = ANMTAB_MAXNUM    ;
          }
        }
        else if ( work_byte == CHAR_SHARP )
        {
          ( * algorithm ) .object_format = DF_NUMERIC_SPACE ;
          work_choices = NMSTAB_CHOICES    ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = NMSTAB_MAXNUM    ;
          }
        }
        else if ( work_byte == CHAR_PERCENT )
        {
          ( * algorithm ) .object_format = DF_ALPHA_PUNCTUATION ;
          work_choices = APUTAB_CHOICES    ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = APUTAB_MAXNUM    ;
          }
        }
        else if ( work_byte == CHAR_TILDE   )
        {
          ( * algorithm ) .object_format = SVNTAB_FORMAT ;
          work_choices = SVNTAB_CHOICES    ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = SVNTAB_MAXNUM    ;
          }
        }
        else if ( work_byte == CHAR_STAR    )
        {
          ( * algorithm ) .object_format = EGHTAB_FORMAT ;
          work_choices = EGHTAB_CHOICES    ;
          if ( current_CRC_is_32_bits == IDM_TRUE )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;  /* JJL */
          }
          else
          {
            max_multiplier = EGHTAB_MAXNUM    ;
          }
        }
        else if ( work_byte == CHAR_PLUS    )
        {
          ( * algorithm ) .object_format = CRCTAB_FORMAT ;
          work_choices   = CRCTAB_CHOICES ;
          max_multiplier = CRCTAB_MAXNUM  ;
          if ( special_CRC_work_required == IDM_FALSE )
          {
            special_CRC_work_required = IDM_TRUE ;
            current_dlength_for_CRC   = ( UNS_INT_16_BITS )
                                 ( current_format_length - string_counter ) ;
          }
          if ( format_counter == U16CONST(0) )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;
          }
          else if ( *string_pointer == CHAR_PLUS )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;
          }
          else if ( IDM_TRUE == current_format_variable_length )
          {
            return IDM_USER_FORMAT_DATA_INVALID ;
          }
        }
        else
        {
          return IDM_USER_FORMAT_DATA_INVALID ;
        }

        ( * algorithm ) .actual_choices  = work_choices          ;
        ( * algorithm ) .bits_if_first_in_word = 0              ;
        ( * algorithm ) .bits_if_last_in_word  = 0              ;
        if ( last_multiplier > max_multiplier )
        {
          temp_bit_length = bits_in_number ( last_multiplier - U32CONST(1) ) ;
          ( * ( algorithm - U16CONST(1) ) ) .bits_if_last_in_word = temp_bit_length ;
          info_length += ( UNS_INT_16_BITS ) temp_bit_length ;
          ( * algorithm ) .multiplier           = U32CONST ( 1 )  ;
          last_multiplier = work_choices ;
          ( * word_length_info ) . bits_if_first_in_word
                                                    = temp_bit_length ;
          word_length_info = algorithm ;
        }
        else
        {
          ( * algorithm ) .multiplier           = last_multiplier ;
          last_multiplier *= work_choices ;
        }
      }  /* end of processing a single format character */
      temp_bit_length = bits_in_number ( last_multiplier - U32CONST(1) ) ;
      ( * ( algorithm - 1 ) ) .bits_if_last_in_word = temp_bit_length ;
      info_length += ( UNS_INT_16_BITS ) temp_bit_length ;
      ( * word_length_info ) . bits_if_first_in_word = temp_bit_length ;
      *total_info_length = info_length ;
      return IDM_RETURN_SUCCESS ;
    } /* end of processing a private format string              */

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*      returns a return code indicating success/failure                  */
  /*------------------------------------------------------------------------*/
  extern IDM_Return_Code cmp_determine_compr_size
                            ( const IDM_Format_ID       id                  ,
                              const Boolean             encode_request      ,
                              UNS_INT_8_BITS          * format_string       ,
                              const UNS_INT_16_BITS     format_length       ,
                              const UNS_INT_16_BITS     data_length         ,
                              UNS_INT_16_BITS         * returned_bit_length )
    {
      IDM_Format_ID     work_id     ;
      UNS_INT_16_BITS   info_length ;
      UNS_FST_8_BITS    i           ;
      IDM_Return_Code return_code ;

      if ( ( id == current_format_id            )
        && ( data_length == current_data_length )
        && ( id != IDM_LAST_PUBLIC_ID           ) )
        /* If it is the same format id and length as before, */
        /* and if it is not our special id...                */
      {
        if ( id == U16CONST(0) )
        {
          /* Here on an initialize followed by an illegal value */
          *returned_bit_length = 0 ;
          if ( encode_request == IDM_TRUE )
          {
            return IDM_USER_FORMAT_OUT_OF_RANGE ;
          }
          else
          {
            return IDM_MATRIX_FORMAT_OUT_OF_RANGE ;
          }
        }
        else
        {
          *returned_bit_length = current_compr_bit_len    ;
          return IDM_RETURN_SUCCESS ;
        }
      }
      else if ( data_length == U16CONST(0) )
      {
        *returned_bit_length = 0 ;
        if ( encode_request == IDM_TRUE )
        {
          return IDM_USER_DATA_ZERO_LENGTH ;
        }
        else
        {
          return IDM_MATRIX_DATA_ZERO_LENGTH ;
        }
      }
      else
      {
        if ( id <= IDM_LAST_PUBLIC_ID )
        {
          if ( id <= LAST_DOUBLE_ID )
          {
            if ( ( id > LAST_500_ID ) && ( id < FIRST_2000_ID ) )
            {
              *returned_bit_length =  0 ; /* Error */
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_UNDEFINED_ID ;
              }
              else
              {
                return IDM_MATRIX_UNDEFINED_ID ;
              }
            }
            else
            {
              current_format_id         = id            ;
              current_data_length       = data_length   ;
              special_CRC_work_required = IDM_FALSE         ;
              current_dlength_for_CRC   = data_length   ;
              current_format_variable_length    = IDM_TRUE  ;
              current_vlen_is_500       = ( Boolean )
                                      ( current_format_id < FIRST_2000_ID ) ;
              current_CRC_is_32_bits    = ( Boolean ) !current_vlen_is_500 ;
              work_id = ( IDM_Format_ID )
                                    ( current_format_id % OFFSET_FOR_2000 ) ;
              return_code = setup_current_data ( work_id , encode_request ) ;
              if ( return_code != IDM_RETURN_SUCCESS )
                return return_code ;
            }    /* end of if on a good public format id in double range */
          }
          else
          {

            if ( id == IDM_LAST_PUBLIC_ID )
            {

              current_format_id         = id            ;
              current_data_length       = data_length   ;
              special_CRC_work_required = IDM_FALSE         ;
              current_dlength_for_CRC   = data_length   ;
              current_format_variable_length    = IDM_TRUE  ;
              current_vlen_is_500       = IDM_TRUE          ;
              current_CRC_is_32_bits    = IDM_FALSE         ;

              if ( ( encode_is_encode == IDM_TRUE ) || ( encode_request == IDM_FALSE ) )
              {

                current_format_type =
                     NORMAL_PUBLIC_FORMAT     ;
                current_data_format =
                     APUTAB_FORMAT            ;
                current_positions_in_number =
                     APUTAB_POSITIONS_IN_NUMBER ;
                for ( i=0 ;
                      i<APUTAB_POSITIONS_IN_NUMBER ;
                      i++ )
                {
                  current_multiplier [i]=
                    alpha_punc_multiplier[i]   ;
                  current_bits_if_last[i] =
                    alpha_punc_bits_if_last[i] ;
                }

              }
              else
              {

                #ifdef INTERNAL_SYSTEM

                return IDM_DESIGN_PROGRAM_ERROR ;

                #else

                current_format_type             = PRE_ENCODED_FORMAT ;
                current_compr_bit_len   = LENGTH_72 ;
                *returned_bit_length = current_compr_bit_len    ;
                return IDM_RETURN_SUCCESS ;

                #endif

              }

            }
            else
            {
              *returned_bit_length =  0 ; /* Error */
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_UNDEFINED_ID ;
              }
              else
              {
                return IDM_MATRIX_UNDEFINED_ID ;
              }
            }

          }
          if ( current_format_type == NORMAL_PUBLIC_FORMAT )
          {
            current_bits_in_main = current_bits_if_last
                                        [ current_positions_in_number - 1 ] ;
            full_numbers_in_code = ( UNS_INT_16_BITS )
                            ( current_data_length
                      / ( ( UNS_INT_16_BITS ) current_positions_in_number ) ) ;
            if ( current_data_length == ( full_numbers_in_code
                      * ( ( UNS_INT_16_BITS ) current_positions_in_number ) ) )
            {
              current_bits_in_last = 0 ;
            }
            else
            {
              current_bits_in_last = ( UNS_INT_8_BITS )
               ( current_bits_if_last [ ( UNS_INT_16_BITS )
                 ( ( current_data_length % ( UNS_INT_16_BITS )
                   current_positions_in_number ) - U16CONST(1) ) ] ) ;
            }
            info_length = ( UNS_INT_16_BITS ) ( ( UNS_INT_16_BITS )
             ( ( UNS_INT_16_BITS ) full_numbers_in_code *
               ( UNS_INT_16_BITS ) current_bits_in_main ) + ( UNS_INT_16_BITS )
                                                      current_bits_in_last ) ;
          }
          else
          {
            info_length = ( UNS_INT_16_BITS )
             ( data_length * ( UNS_INT_16_BITS ) ( current_bits_in_byte ) ) ;
          }
        }
        else
        {
          UNS_INT_8_BITS      last_character    ;

          if ( format_length == U16CONST(0) )
          {
            current_format_id    = 0 ;
            *returned_bit_length = 0 ;
            if ( encode_request == IDM_TRUE )
            {
              return IDM_USER_UNDEFINED_ID ;
            }
            else
            {
              return IDM_MATRIX_UNDEFINED_ID ;
            }
          }
          current_format_id         = id              ;
          current_data_length       = data_length     ;
          current_format_type       = PRIVATE_FORMAT  ;
          last_character            = *( format_string + format_length -
                                                               U16CONST(1) ) ;
          if ( last_character == CHAR_EXCLAM )
          {
            current_format_length   = ( UNS_INT_16_BITS )
                                             ( format_length - U16CONST(1) ) ;
            current_format_variable_length    = IDM_TRUE  ;
            current_vlen_is_500               = IDM_TRUE  ;
            if ( current_format_length > current_data_length )
            {
              current_format_id    = 0 ;
              *returned_bit_length = 0 ;
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_DATA_TOO_SHORT ;
              }
              else
              {
                return IDM_MATRIX_DATA_TOO_SHORT ;
              }
            }
          }
          else if ( last_character == CHAR_QUESTION )
          {
            current_format_length   = ( UNS_INT_16_BITS )
                                                      ( format_length -
                                                               U16CONST(1) ) ;
            current_format_variable_length    = IDM_TRUE  ;
            current_vlen_is_500               = IDM_FALSE ;
            if ( current_format_length > current_data_length )
            {
              current_format_id    = (IDM_Format_ID ) 0 ;
              *returned_bit_length = U16CONST(0) ;
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_DATA_TOO_SHORT ;
              }
              else
              {
                return IDM_MATRIX_DATA_TOO_SHORT ;
              }
            }
          }
          else
          {
            current_format_length   = format_length   ;
            current_format_variable_length    = IDM_FALSE ;
            current_vlen_is_500               = IDM_FALSE ;
            if ( current_format_length != current_data_length )
            {
              current_format_id    = ( IDM_Format_ID ) 0 ;
              *returned_bit_length = U16CONST(0) ;
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_DATA_WRONG_LENGTH ;
              }
              else
              {
                return IDM_MATRIX_DATA_WRONG_LENGTH ;
              }
            }
          }
          if ( current_format_length == U16CONST(0) )
          {
            current_format_id    = ( IDM_Format_ID )0 ;
            *returned_bit_length = U16CONST(0) ;
            return IDM_USER_FORMAT_DATA_INVALID ;
          }
          current_CRC_is_32_bits    = ( Boolean ) ( id >= ( IDM_Format_ID )
                            ( IDM_FIRST_PRIVATE_ID + ( IDM_Format_ID ) 4 ) ) ;
          if ( current_format_length > ( UNS_INT_16_BITS )
                                                       IDM_MAX_FMT_STR_CHARS )
          {
            current_format_id    = ( IDM_Format_ID ) 0 ;
            *returned_bit_length = U16CONST(0) ;
            return IDM_USER_FORMAT_TOO_LONG ;
          }
          special_CRC_work_required = IDM_FALSE         ;
          current_dlength_for_CRC   = data_length   ;
          return_code = set_current_private ( format_string , &info_length ) ;
          if ( return_code != IDM_RETURN_SUCCESS )
            return return_code ;
        }   /* end of private.                                        */


        if ( current_CRC_is_32_bits == IDM_TRUE )
        {
          return IDM_USER_FORMAT_DATA_INVALID ; /* JJL */
        }
        else
        {
          current_compr_bit_len = ( UNS_INT_16_BITS )
                  ( info_length + ( UNS_INT_16_BITS ) DATA_CODE_CRC_LENGTH ) ;
        }
        if ( current_format_variable_length == IDM_TRUE )
        {
          if ( current_vlen_is_500 == IDM_TRUE )
          {
            current_compr_bit_len = ( UNS_INT_16_BITS )
             ( current_compr_bit_len + ( UNS_INT_16_BITS ) VARIABLE_500_LENGTH ) ;
            if ( data_length > U16CONST(500) )
            {
              *returned_bit_length = U16CONST(0) ;
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_DATA_TOO_LONG ;
              }
              else
              {
                return IDM_MATRIX_DATA_TOO_LONG ;
              }
            }
          }
          else
          {
            current_compr_bit_len = ( UNS_INT_16_BITS )
                        ( current_compr_bit_len + ( UNS_INT_16_BITS )
                                                   VARIABLE_2000_LENGTH    ) ;
            if ( data_length > ( UNS_INT_16_BITS ) IDM_MAX_DATA_CHARS )
            {
              *returned_bit_length = U16CONST(0) ;
              if ( encode_request == IDM_TRUE )
              {
                return IDM_USER_DATA_TOO_LONG ;
              }
              else
              {
                return IDM_MATRIX_DATA_TOO_LONG ;
              }
            }
          }
        }
        *returned_bit_length = current_compr_bit_len    ;
        return IDM_RETURN_SUCCESS ;
      }
    }


  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  static IDM_Return_Code cmp_public_verify_enc_string
                                   ( UNS_INT_8_BITS   * incoming_data       ,
                                     UNS_INT_16_BITS    num_in_bytes        ,
                                     UNS_INT_16_BITS  * error_byte          )
    {
      UNS_INT_16_BITS     string_counter       = num_in_bytes   ;
      UNS_INT_8_BITS    * string_pointer       = incoming_data  ;
      UNS_INT_8_BITS      work_byte            ;

      for (                               ;
             string_counter > U16CONST(0) ;
             --string_counter             )
      {
        work_byte = *(string_pointer++) ;
        (*error_byte)++ ;
        switch ( current_data_format )
        {
          case DF_ALPHABETIC_SPACE      :
                 /* space goes to zero. */
                 /* aphabetic goes to 1 .. 26 */
                 if ( work_byte == CHAR_SPACE )
                 {
                   /* ok, do nothing */
                 }
                 else if ( ( work_byte < CHAR_UC_A )
                        || ( work_byte > CHAR_UC_Z ) )
                 {
                   return IDM_USER_INVALID_DATA ;
                 }
                 /* else is A .. Z OK */
                 break ;
          case DF_ALPHANUMERIC_SPACE    :
                 if ( work_byte == CHAR_SPACE )
                 {
                   /* ok, do nothing */
                 }
                 else if ( ( work_byte >= CHAR_UC_A )
                        && ( work_byte <= CHAR_UC_Z ) )
                 {
                   /* ok, do nothing */
                 }
                 else if ( ( work_byte >= CHAR_ZERO )
                        && ( work_byte <= CHAR_NINE ) )
                 {
                   /* ok, do nothing */
                 }
                 else
                 {
                   return IDM_USER_INVALID_DATA ;
                 }
                 break ;
          case DF_NUMERIC_SPACE         :
                 /* space goes to zero. */
                 /* numerics go to 1 .. 10 */
                 if ( work_byte == CHAR_SPACE )
                 {
                   /* ok, do nothing */
                 }
                 else if ( ( work_byte >= CHAR_ZERO )
                        && ( work_byte <= CHAR_NINE ) )
                 {
                   /* ok, do nothing */
                 }
                 else
                 {
                   return IDM_USER_INVALID_DATA ;
                 }
                 break ;
          case DF_ALPHA_PUNCTUATION     :
                 if ( work_byte == CHAR_SPACE )
                 {
                   /* ok, do nothing */
                 }
                 else if ( ( work_byte >= CHAR_UC_A )
                        && ( work_byte <= CHAR_UC_Z ) )
                 {
                   /* ok, do nothing */
                 }
                 else if ( ( work_byte >= CHAR_ZERO )
                        && ( work_byte <= CHAR_NINE ) )
                 {
                   /* ok, do nothing */
                 }
                 else
                 {
                   switch ( work_byte )
                   {
                     case CHAR_PERIOD      :
                     case CHAR_COMMA       :
                     case CHAR_MINUS       :
                     case CHAR_SLASH       : break ;
                     default : return IDM_USER_INVALID_DATA ;
                   }
                 }
                 break ;
          default : return IDM_DESIGN_PROGRAM_ERROR ;
        }
      }
      *error_byte = 0 ;
      return IDM_RETURN_SUCCESS ;
    }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  extern IDM_Return_Code  cmp_verify_enc_string
                       ( const IDM_Format_ID            id                  ,
                               UNS_INT_8_BITS         * incoming_data       ,
                               UNS_INT_16_BITS          num_in_bytes        ,
                               UNS_INT_8_BITS         * format_string       ,
                         const UNS_INT_16_BITS          format_length       ,
                               UNS_INT_16_BITS        * error_byte          )
    {
      IDM_Return_Code external_return_code ;
      UNS_INT_16_BITS     compressed_bits      ;
      UNS_INT_16_BITS     string_counter       = num_in_bytes   ;
      UNS_INT_8_BITS       * string_pointer = incoming_data   ;
      UNS_INT_8_BITS      work_byte            ;

      *error_byte = 0 ;

      external_return_code = cmp_determine_compr_size ( id                   ,
                                                        IDM_TRUE                 ,
                                                        format_string        ,
                                                        format_length        ,
                                                        num_in_bytes         ,
                                                        &compressed_bits     ) ;
      if ( external_return_code != IDM_RETURN_SUCCESS )
      {
        return external_return_code ;
      }
      else
      {

        switch ( current_format_type )
        {
          case NORMAL_PUBLIC_FORMAT    : return cmp_public_verify_enc_string
                            ( incoming_data , num_in_bytes , error_byte ) ;
          case FIXED_BLOCKS_FORMAT     :
                 if ( current_bits_in_byte == 7 )
                 {
                   for (                              ;
                         string_counter > U16CONST(0) ;
                         --string_counter             )
                   {
                     work_byte = *(string_pointer++) ;
                     (*error_byte)++ ;
                     /* that made the pointer to where the error is correct */
                     if ( work_byte >= U8CONST ( 0x80 ) )
                     {
                       return IDM_USER_INVALID_DATA ;
                     }
                   }
                 }
                 break ;

          case PRE_ENCODED_FORMAT      :

          #ifdef INTERNAL_SYSTEM

             return IDM_DESIGN_PROGRAM_ERROR ;

          #else

               break ;

          #endif

          case PRIVATE_FORMAT          :
          {
            /* Private IDs */

            byte_compress_data  * algorithm      =   encoding_algorithm ;

            for (                              ;
                  string_counter > U16CONST(0) ;
                  --string_counter             )
            {
              work_byte = *(string_pointer++) ;
              (*error_byte)++ ;
              switch ( ( * algorithm ) .object_format )
              {
                case DF_NUMERIC               :
                       if ( ( work_byte >= CHAR_ZERO )
                         && ( work_byte <= ( * algorithm ) .highest_allowed ) )
                       {
                         /* ok, do nothing */
                       }
                       else
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       break ;
                case DF_ALPHABETIC            :
                       /* aphabetic goes to 0 .. x - 1 */
                       if ( ( work_byte >= CHAR_UC_A )
                         && ( work_byte <= ( * algorithm ) .highest_allowed ) )
                       {
                         /* ok, do nothing */
                       }
                       else
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       break ;
                case DF_ALPHABETIC_SPACE      :
                       if ( work_byte == CHAR_SPACE )
                       {
                         /* ok, do nothing */
                       }
                       else if ( ( work_byte < CHAR_UC_A )
                              || ( work_byte > CHAR_UC_Z ) )
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       /* else is A .. Z OK */
                       break ;
                case DF_ALPHANUMERIC_SPACE    :
                       /* space goes to zero. */
                       /* aphabetic goes to 1 .. 26 */
                       /* numerics go to 27 .. 36 */
                       if ( work_byte == CHAR_SPACE )
                       {
                         /* ok, do nothing */
                       }
                       else if ( ( work_byte >= CHAR_UC_A )
                              && ( work_byte <= CHAR_UC_Z ) )
                       {
                         /* ok, do nothing */
                       }
                       else if ( ( work_byte >= CHAR_ZERO )
                              && ( work_byte <= CHAR_NINE ) )
                       {
                         /* ok, do nothing */
                       }
                       else
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       break ;
                case DF_NUMERIC_SPACE         :
                       if ( work_byte == CHAR_SPACE )
                       {
                         /* ok, do nothing */
                       }
                       else if ( ( work_byte >= CHAR_ZERO )
                              && ( work_byte <= CHAR_NINE ) )
                       {
                         /* ok, do nothing */
                       }
                       else
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       break ;
                case DF_ALPHA_PUNCTUATION     :
                       if ( work_byte == CHAR_SPACE )
                       {
                         /* ok, do nothing */
                       }
                       else if ( ( work_byte >= CHAR_UC_A )
                              && ( work_byte <= CHAR_UC_Z ) )
                       {
                         /* ok, do nothing */
                       }
                       else if ( ( work_byte >= CHAR_ZERO )
                              && ( work_byte <= CHAR_NINE ) )
                       {
                         /* ok, do nothing */
                       }
                       else
                       {
                         switch ( work_byte )
                         {
                           case CHAR_PERIOD      :
                           case CHAR_COMMA       :
                           case CHAR_MINUS       :
                           case CHAR_SLASH       : break ;
                           default : return IDM_USER_INVALID_DATA ;
                         }
                       }
                       break ;
                case DF_SEVEN_BIT             :
                       if ( work_byte >= U8CONST ( 0x80 ) )
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       break ;
                case DF_EIGHT_BIT             :
                       /* all possible values are good */
                       break ;
                case DF_CRC_16_BIT            :
                       /* A "+" is required */
                       if ( work_byte != CHAR_PLUS )
                       {
                         return IDM_USER_INVALID_DATA ;
                       }
                       break ;
                default : return IDM_DESIGN_PROGRAM_ERROR ;
              }
              algorithm++ ;
            } /* end of string compress loop for privates */
          }   /* end of declare block for privates        */
                 break ;
          default : return IDM_DESIGN_PROGRAM_ERROR ;
        }
        *error_byte = 0 ;
        return IDM_RETURN_SUCCESS ;
      }
    }

  /*^-----------------------------------------------------------------------*/
  /* SYSTEM  :                                                              */
  /* NAME    :                                                              */
  /* DESCRIPTION :                                                          */
  /*------------------------------------------------------------------------*/
  extern IDM_Return_Code cmp_compress_data
                             ( const IDM_Format_ID      id                  ,
                                     UNS_INT_8_BITS   * format_string       ,
                               const UNS_INT_16_BITS    format_length       ,
                                     UNS_INT_8_BITS   * to_compress         ,
                               const UNS_INT_16_BITS    data_length         ,
                                     UNS_INT_16_BITS  * returned_bit_length )
    {
      IDM_Return_Code    external_return_code ;
      UNS_INT_16_BITS     compressed_bits      ;
      UNS_INT_8_BITS       * string_pointer ;
      UNS_INT_16_BITS     string_counter       ;
      UNS_INT_8_BITS      work_byte            ;

      #ifdef INTERNAL_SYSTEM

      encode_is_encode =    IDM_TRUE ;

      #else

      encode_is_encode =   IDM_FALSE ;

      #endif

      external_return_code = cmp_determine_compr_size ( id                   ,
                                                        IDM_TRUE                 ,
                                                        format_string        ,
                                                        format_length        ,
                                                        data_length          ,
                                                        &compressed_bits     ) ;
      if ( external_return_code != IDM_RETURN_SUCCESS )
      {
        *returned_bit_length    =       0 ;
        return external_return_code ;
      }
      else
      {
        *returned_bit_length = compressed_bits ;

        #ifndef INTERNAL_SYSTEM

        if ( id != IDM_LAST_PUBLIC_ID )
        {

        #endif

        if ( current_CRC_is_32_bits == IDM_TRUE )
        {
          return IDM_USER_FORMAT_DATA_INVALID ; /* JJL */
        }
        else
        {
          /* add a 16 bit CRC here */
          UNS_INT_16_BITS       calculated_CRC = calc_CRC_kermit
                             ( id , to_compress , current_dlength_for_CRC ) ;

          #ifdef SPECIAL_DEBUG
            last_crc = ( UNS_INT_32_BITS ) calculated_CRC ;
          #endif

          bfr_add_bits_16_right ( calculated_CRC , U8CONST ( 16 ) ) ;
        }
        if ( current_format_variable_length == IDM_TRUE )
        {
          if ( current_vlen_is_500 == IDM_TRUE )
          {
            bfr_add_bits_16_right ( data_length , ( UNS_INT_8_BITS )
              ( QUICK_DONGLE_ENCODE_W_DECODE
                                         ( 9 , 3323 , 195 , 788 , 816 ) ) ) ;
          }
          else
          {
            bfr_add_bits_16_right ( data_length , ( UNS_INT_8_BITS )
              ( QUICK_DONGLE_ENCODE_W_DECODE
                                         ( 14  , 955 , 81 , 98 , 2265 ) ) ) ;
          }
        }

        #ifndef INTERNAL_SYSTEM

        }

        #endif

        string_counter    = data_length   ;
        string_pointer    = to_compress   ;

        switch ( current_format_type )
        {
          case NORMAL_PUBLIC_FORMAT    :
                 {
                   UNS_FST_8_BITS
                     work_position       =  U8CONST(0) ;
                   UNS_INT_32_BITS      work_number         =  U32CONST(0) ;
                   UNS_INT_8_BITS       work_value                         ;

                   for (                              ;
                         string_counter > U16CONST(0) ;
                         --string_counter             )
                   {
                     work_byte = *(string_pointer++) ;
                     switch ( current_data_format )
                     {
                       case DF_ALPHABETIC_SPACE      :
                              if ( work_byte == CHAR_SPACE )
                              {
                                work_value = U8CONST(0) ;
                              }
                              else
                              {
                                work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - ALPTAB_SUBTRACT ) ;
                              }
                              break ;
                       case DF_ALPHANUMERIC_SPACE    :
                              work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - ANMTAB_SUBTRACT ) ;
                              if ( work_value >= U8CONST(128) ) /* negative */
                              {
                                if ( work_byte == CHAR_SPACE )
                                {
                                  work_value = U8CONST(0) ;
                                }
                                else
                                {
                                  work_value = ( UNS_INT_8_BITS )
                                                  ( work_byte - CHAR_ZERO
                                                + U8CONST(27) ) ;
                                }
                              }
                              break ;
                       case DF_NUMERIC_SPACE         :
                              if ( work_byte == CHAR_SPACE )
                              {
                                work_value = U8CONST(0) ;
                              }
                              else
                              {
                                work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - NMSTAB_SUBTRACT ) ;
                              }
                              break ;
                       case DF_ALPHA_PUNCTUATION     :
                              work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - APUTAB_SUBTRACT ) ;
                              if ( work_value >= U8CONST(128) ) /* negative */
                              {
                                switch ( work_byte )
                                {
                                  case CHAR_SPACE  : work_value = U8CONST(0)  ;
                                                    break ;
                                  case CHAR_PERIOD : work_value = U8CONST(37) ;
                                                    break ;
                                  case CHAR_COMMA  : work_value = U8CONST(38) ;
                                                    break ;
                                  case CHAR_MINUS  : work_value = U8CONST(39) ;
                                                    break ;
                                  case CHAR_SLASH  : work_value = U8CONST(40) ;
                                                    break ;
                                  default          :
                                      work_value = ( UNS_INT_8_BITS )
                                     ( work_byte - CHAR_ZERO + U8CONST(27) ) ;
                                }
                              }
                              break ;
                       default : return IDM_DESIGN_PROGRAM_ERROR ;
                     }
                     work_number += ( ( UNS_INT_32_BITS ) work_value )
                                      * current_multiplier [ work_position++ ] ;
                     if ( work_position == current_positions_in_number )
                     {
                       bfr_add_bits_32_right
                                    ( work_number , current_bits_in_main ) ;
                       work_number   = U32CONST(0) ;
                       work_position = U8CONST(0) ;
                     }
                   }
                   if ( work_position != U8CONST(0) )
                   {
                     bfr_add_bits_32_right
                                   ( work_number , current_bits_in_last ) ;
                   }
                 }
                 break ;
          case FIXED_BLOCKS_FORMAT     :
                 for (                              ;
                       string_counter > U16CONST(0) ;
                       --string_counter             )
                 {
                   work_byte = *(string_pointer++) ;
                   bfr_add_bits_8_right ( work_byte , current_bits_in_byte ) ;
                 }
                 break ;

          case PRE_ENCODED_FORMAT      :

          #ifdef INTERNAL_SYSTEM

             return IDM_DESIGN_PROGRAM_ERROR ;

          #else

             /* Move over the pre-compressed bit string                 */

               {
                 const UNS_INT_16_BITS * special_pointer  = data_string_72  ;
                 UNS_INT_16_BITS   remaining_length = LENGTH_72       ;
                 UNS_INT_16_BITS   work_data_word                     ;

                 for (                                  ;
                       remaining_length >= U16CONST(16) ;
                       remaining_length -= U16CONST(16) )
                 {
                   work_data_word = *(special_pointer++) ;
                   bfr_add_bits_16_left ( work_data_word , U8CONST ( 16 ) ) ;
                 }
                 if ( remaining_length > U16CONST(0) )
                 {
                   work_data_word = *special_pointer ;
                   bfr_add_bits_16_left ( work_data_word ,
                                          ( UNS_INT_8_BITS ) remaining_length );
                 }
               }
               break ;

          #endif

          case PRIVATE_FORMAT          :
          {
            /* Private IDs */

            UNS_INT_32_BITS     work_number        =     U32CONST(0)      ;
              /* what is the current value of the number?           */
            UNS_INT_8_BITS      work_value                                ;
            byte_compress_data  * algorithm        =   encoding_algorithm ;

            for (                              ;
                  string_counter > U16CONST(0) ;
                  --string_counter             )
            {
              work_byte = *(string_pointer++) ;
              switch ( ( * algorithm ) .object_format )
              {
                case DF_NUMERIC               :
                       work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - NUMTAB_SUBTRACT ) ;
                       break ;
                case DF_ALPHABETIC            :
                       work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - ABCTAB_SUBTRACT ) ;
                       break ;
                case DF_ALPHABETIC_SPACE      :
                       if ( work_byte == CHAR_SPACE )
                       {
                         work_value = U16CONST(0) ;
                       }
                       else
                       {
                         work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - ALPTAB_SUBTRACT ) ;
                       }
                       break ;
                case DF_ALPHANUMERIC_SPACE    :
                       work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - ANMTAB_SUBTRACT ) ;
                       if ( work_value >= U8CONST(128) ) /* negative */
                       {
                         if ( work_byte == CHAR_SPACE )
                         {
                           work_value = U8CONST(0) ;
                         }
                         else
                         {
                           work_value = ( UNS_INT_8_BITS )
                                           ( work_byte - CHAR_ZERO
                                             + U8CONST(27) ) ;
                         }
                       }
                       break ;
                case DF_NUMERIC_SPACE         :
                       if ( work_byte == CHAR_SPACE )
                       {
                         work_value = U8CONST(0) ;
                       }
                       else
                       {
                         work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - NMSTAB_SUBTRACT ) ;
                       }
                       break ;
                case DF_ALPHA_PUNCTUATION     :
                       work_value = ( UNS_INT_8_BITS )
                                            ( work_byte - APUTAB_SUBTRACT ) ;
                       if ( work_value >= U8CONST(128) ) /* negative */
                       {
                         switch ( work_byte )
                         {
                           case CHAR_SPACE  : work_value = U8CONST(0) ;
                                              break ;
                           case CHAR_PERIOD : work_value = U8CONST(37) ;
                                              break ;
                           case CHAR_COMMA  : work_value = U8CONST(38) ;
                                              break ;
                           case CHAR_MINUS  : work_value = U8CONST(39) ;
                                              break ;
                           case CHAR_SLASH  : work_value = U8CONST(40) ;
                                              break ;
                           default          :
                                work_value =
                                      ( UNS_INT_8_BITS )
                                             ( work_byte - CHAR_ZERO
                                             + U8CONST(27) ) ;
                         }
                       }
                       break ;
                case DF_SEVEN_BIT             :
                       work_value = work_byte ;
                       break ;
                case DF_EIGHT_BIT             :
                       work_value = work_byte ;
                       break ;
                case DF_CRC_16_BIT            :
                       {

                         UNS_INT_16_BITS        crc_remaining_count =
                          ( UNS_INT_16_BITS ) ( string_counter - U16CONST(1) ) ;
                         byte_compress_data   * crc_algorithm =
                                                     algorithm + U16CONST(1) ;
                         UNS_INT_16_BITS        crc_count     =  U16CONST(0) ;


                         while (    ( crc_remaining_count-- !=  U16CONST(0) )
                                 && ( ( * (crc_algorithm++) ) .object_format
                                                            != DF_CRC_16_BIT ) )
                         {
                           crc_count++ ;
                         }

                         crc16_init () ;
                         crc16_add_string
                           ( string_pointer , (UNS_INT_16_BITS) crc_count ) ;
                         work_number = ( UNS_INT_32_BITS ) crc16_get_CRC () ;
                         work_value  = U8CONST(0) ;
                       }
                       break ;
              default : return IDM_DESIGN_PROGRAM_ERROR ;
              }
              work_number += ( ( UNS_INT_32_BITS ) work_value )
                                          * ( ( * algorithm ) .multiplier ) ;
              if ( ( * algorithm ) .bits_if_last_in_word != U8CONST(0) )
              {
                bfr_add_bits_32_right ( work_number ,
                                       ( * algorithm ) .bits_if_last_in_word ) ;
                work_number = U32CONST(0) ;
              }
              algorithm++ ;
            } /* end of string compress loop for privates */
          }   /* end of declare block for privates        */
                 break ;
          default : return IDM_DESIGN_PROGRAM_ERROR ;
        }
        return IDM_RETURN_SUCCESS ;
      }
    }

以下为.h文件  CMPRESSD.H  ///

#ifndef CMPRESSD_H

/******************************************************************************
 *                                                                            *
 *                         M O D U L E   D E F I N E                          *
 *                                                                            *
 ******************************************************************************/

#define CMPRESSD_H

/******************************************************************************
 *                                                                            *
 *        C O M P I L E R   D E F I N E D   I N C L U D E   F I L E S         *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *            U S E R   D E F I N E D   I N C L U D E   F I L E S             *
 *                                                                            *
 ******************************************************************************/

#ifdef __cplusplus
extern "C" {            /* Assume C declarations for C++ */
#endif    /* __cplusplus */

/******************************************************************************
 *                                                                            *
 *                        G L O B A L   D E F I N E S                         *
 *                                                                            *
 ******************************************************************************/
 
/* None */

/******************************************************************************
 *                                                                            *
 *                 S T R U C T U R E   D E F I N I T I O N S                  *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *    G L O B A L   V A R I A B L E S   -   N O   I N I T I A L I Z E R S     *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *       G L O B A L   V A R I A B L E S   -   I N I T I A L I Z E R S        *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *                   F U N C T I O N   P R O T O T Y P E S                    *
 *                                                                            *
 ******************************************************************************/

IDM_Return_Code cmp_compress_init ( void ) ;
void cmp_compress_reinit( void ) ;
IDM_Return_Code cmp_determine_compr_size( const IDM_Format_ID,
            const Boolean,
            UNS_INT_8_BITS *,
            const UNS_INT_16_BITS,
            const UNS_INT_16_BITS,
            UNS_INT_16_BITS * ) ;
IDM_Return_Code  cmp_verify_enc_string( const IDM_Format_ID,
            UNS_INT_8_BITS *,
            UNS_INT_16_BITS,
            UNS_INT_8_BITS *,
            const UNS_INT_16_BITS,
            UNS_INT_16_BITS * );
IDM_Return_Code cmp_compress_data( const IDM_Format_ID,
            UNS_INT_8_BITS *,
            const UNS_INT_16_BITS,
            UNS_INT_8_BITS *,
            const UNS_INT_16_BITS,
            UNS_INT_16_BITS * ) ;

#ifdef __cplusplus
}                       /* End of extern "C" { */
#endif    /* __cplusplus */

#endif

  • 16
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值