Example #1
0
PHB_ZEBRA hb_zebra_create_upca( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        i, iLen = ( int ) nLen;

   HB_SYMBOL_UNUSED( iFlags );

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_UPCA;

   if( iLen != 11 && iLen != 12 )
   {
      pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
      return pZebra;
   }

   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] < '0' || szCode[ i ] > '9' )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
   }

   if( iLen == 11 )
   {
      pZebra->szCode = ( char * ) hb_xgrab( 13 );
      hb_xmemcpy( pZebra->szCode, szCode, 11 );
      pZebra->szCode[ 11 ] = _upca_checksum( szCode );
      pZebra->szCode[ 12 ] = '\0';
   }
   else
   {
      if( szCode[ 11 ] != _upca_checksum( szCode ) )
      {
         pZebra->iError = HB_ZEBRA_ERROR_BADCHECKSUM;
         return pZebra;
      }
      pZebra->szCode = ( char * ) hb_xgrab( 13 );
      hb_xmemcpy( pZebra->szCode, szCode, 12 );
      pZebra->szCode[ 12 ] = '\0';
   }
   szCode = pZebra->szCode;

   pZebra->pBits = hb_bitbuffer_create();

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* start */

   for( i = 0; i < 6; i++ )
      hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ] ^ 0x7F, 7 );

   hb_bitbuffer_cat_int( pZebra->pBits, 10, 5 );  /* middle */

   for( i = 6; i < 12; i++ )
      hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ], 7 );

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* stop */
   return pZebra;
}
Example #2
0
PHB_ZEBRA hb_zebra_create_ean8( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        i, iLen = ( int ) nLen;

   HB_SYMBOL_UNUSED( iFlags );

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_EAN8;

   if( iLen != 7 && iLen != 8 )
   {
      pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
      return pZebra;
   }

   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] < '0' || szCode[ i ] > '9' )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
   }

   if( iLen == 7 )
   {
      pZebra->szCode = ( char * ) hb_xgrab( 9 );
      hb_xmemcpy( pZebra->szCode, szCode, 7 );
      pZebra->szCode[ 7 ] = _ean8_checksum( szCode );
      pZebra->szCode[ 8 ] = '\0';
   }
   else
   {
      if( szCode[ 7 ] != _ean8_checksum( szCode ) )
      {
         pZebra->iError = HB_ZEBRA_ERROR_BADCHECKSUM;
         return pZebra;
      }
      pZebra->szCode = ( char * ) hb_xgrab( 9 );
      hb_xmemcpy( pZebra->szCode, szCode, 8 );
      pZebra->szCode[ 8 ] = '\0';
   }
   szCode = pZebra->szCode;

   pZebra->pBits = hb_bitbuffer_create();

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* start */

   for( i = 0; i < 4; i++ )
      hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ] ^ 0x7F, 7 );

   hb_bitbuffer_cat_int( pZebra->pBits, 10, 5 );  /* middle */

   for( i = 4; i < 8; i++ )
      hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ], 7 );

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* stop */
   return pZebra;
}
Example #3
0
static PHB_BITBUFFER _qr_interlace( PHB_BITBUFFER pData, unsigned char * pECC, int iVersion, int iLevel )
{
   const QRVERSION * pVersion = &s_version[ iVersion - 1 ];
   const QRLEVEL * pLevel = &( pVersion->level[ iLevel ] );
   PHB_BITBUFFER pRet;
   HB_BYTE * pDataBuf, * pRetBuf;
   unsigned int uiDst, uiSrc, uiPos, uiBlock;

   pRet = hb_bitbuffer_create();
   hb_bitbuffer_set( pRet, pVersion->uiTotal * 8, HB_FALSE ); /* Allocate */

   pRetBuf = hb_bitbuffer_buffer( pRet );
   pDataBuf = hb_bitbuffer_buffer( pData );

   uiDst = 0;
   for( uiPos = 0; uiPos < ( unsigned int ) pLevel->block[ 0 ].uiData || uiPos < ( unsigned int ) pLevel->block[ 1 ].uiData; uiPos++ )
   {
      uiSrc = 0;
      for( uiBlock = 0; uiBlock < ( unsigned int ) pLevel->block[ 0 ].uiCount; uiBlock++ )
      {
         if( uiPos < ( unsigned int ) pLevel->block[ 0 ].uiData )
         {
            pRetBuf[ uiDst++ ] = pDataBuf[ uiPos + uiSrc ];
         }
         uiSrc += pLevel->block[ 0 ].uiData;
      }
      if( pLevel->block[ 1 ].uiCount )
      {
         for( uiBlock = 0; uiBlock < ( unsigned int ) pLevel->block[ 1 ].uiCount; uiBlock++ )
         {
            pRetBuf[ uiDst++ ] = pDataBuf[ uiPos + uiSrc ];
            uiSrc += pLevel->block[ 1 ].uiData;
         }
      }
   }

   for( uiPos = 0; uiPos < ( unsigned int ) pLevel->block[ 0 ].uiECC; uiPos++ )
   {
      uiSrc = 0;
      for( uiBlock = 0; uiBlock < ( unsigned int ) pLevel->block[ 0 ].uiCount; uiBlock++ )
      {
         if( uiPos < ( unsigned int ) pLevel->block[ 0 ].uiECC )
         {
            pRetBuf[ uiDst++ ] = s_rev[ pECC[ uiPos + uiSrc ] ];
         }
         uiSrc += pLevel->block[ 0 ].uiECC;
      }
      if( pLevel->block[ 1 ].uiCount )
      {
         for( uiBlock = 0; uiBlock < ( unsigned int ) pLevel->block[ 1 ].uiCount; uiBlock++ )
         {
            pRetBuf[ uiDst++ ] = s_rev[ pECC[ uiPos + uiSrc ] ];
            uiSrc += pLevel->block[ 1 ].uiECC;
         }
      }
   }

#ifdef DEBUG_CODE
   if( uiDst != pVersion->uiTotal )
      HB_TRACE( HB_TR_ALWAYS, ( "ERROR!!! uiDst:%d  pVersion->uiTotal:%d", uiDst, pVersion->uiTotal ) );

   for( uiPos = 0; uiPos < pVersion->uiTotal; uiPos++ )
      HB_TRACE( HB_TR_ALWAYS, ( "interlaced:%3d %02X", ( int ) s_rev[ ( unsigned char ) pRetBuf[ uiPos ] ], ( int ) s_rev[ ( unsigned char ) pRetBuf[ uiPos ] ] ) );
#endif
   return pRet;
}
Example #4
0
PHB_ZEBRA hb_zebra_create_qrcode( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA       pZebra;
   PHB_BITBUFFER   pData, pFinal;
   unsigned char * pECC;
   int             iVersion, iLevel, iMask;

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_QRCODE;

   if( nLen > 7089 )
   {
      pZebra->iError = HB_ZEBRA_ERROR_TOOLARGE;
      return pZebra;
   }

   switch( iFlags & HB_ZEBRA_FLAG_QR_LEVEL_MASK )
   {
      case HB_ZEBRA_FLAG_QR_LEVEL_M:
         iLevel = 1;
         break;
      case HB_ZEBRA_FLAG_QR_LEVEL_Q:
         iLevel = 2;
         break;
      case HB_ZEBRA_FLAG_QR_LEVEL_H:
         iLevel = 3;
         break;
      default:
         iLevel = 0;
         break;
   }

#ifdef DEBUG_CODE
   HB_TRACE( HB_TR_ALWAYS, ( "qr1 iLevel:%d", iLevel ) );
#endif

   pData = hb_bitbuffer_create();
   iVersion = _qr_dataencode( szCode, nLen, pData, iLevel );

#ifdef DEBUG_CODE
   HB_TRACE( HB_TR_ALWAYS, ( "qr3 iVersion:%d", iVersion ) );
#endif

   if( iVersion == 0 )
   {
      hb_bitbuffer_destroy( pData );
      pZebra->iError = HB_ZEBRA_ERROR_TOOLARGE;
      return pZebra;
   }

   pZebra->iCol = _qr_versionlength( iVersion );

   pZebra->szCode = hb_strdup( szCode );

   pECC = _qr_checksum( pData, iVersion, iLevel );

   pFinal = _qr_interlace( pData, pECC, iVersion, iLevel );
   hb_bitbuffer_destroy( pData );
   hb_xfree( pECC );

   pZebra->pBits = hb_bitbuffer_create();
   _qr_draw( pZebra->pBits, pFinal, iVersion );
   hb_bitbuffer_destroy( pFinal );

   iMask = _qr_mask( pZebra->pBits, iVersion );

   _qr_draw_version_format( pZebra->pBits, iVersion, iLevel, iMask );
   return pZebra;
}
Example #5
0
File: msi.c Project: NaldoDj/core
PHB_ZEBRA hb_zebra_create_msi( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        i, j, iN, iW, iLen = ( int ) nLen;

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_MSI;

   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] < '0' || szCode[ i ] > '9' )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
   }

   pZebra->szCode = ( char * ) hb_xgrab( iLen + 1 );
   hb_xmemcpy( pZebra->szCode, szCode, iLen );
   pZebra->szCode[ iLen ] = '\0';
   szCode = pZebra->szCode;

   pZebra->pBits = hb_bitbuffer_create();

   if( iFlags & HB_ZEBRA_FLAG_WIDE2_5 )
   {
      iN = 2;
      iW = 5;
   }
   else if( iFlags & HB_ZEBRA_FLAG_WIDE3 )
   {
      iN = 1;
      iW = 3;
   }
   else
   {
      iN = 1;
      iW = 2;
   }

   /* start */
   hb_bitbuffer_cat_int( pZebra->pBits, 31, iW );
   hb_bitbuffer_cat_int( pZebra->pBits,  0, iN );
   for( i = 0; i < iLen; i++ )
   {
      char code = szCode[ i ] - '0';
      for( j = 0; j < 4; j++ )
      {
         if( code & 8 )
         {
            hb_bitbuffer_cat_int( pZebra->pBits, 31, iW );
            hb_bitbuffer_cat_int( pZebra->pBits,  0, iN );
         }
         else
         {
            hb_bitbuffer_cat_int( pZebra->pBits, 3, iN );
            hb_bitbuffer_cat_int( pZebra->pBits, 0, iW );
         }
         code <<= 1;
      }
   }
   if( iFlags & HB_ZEBRA_FLAG_CHECKSUM )
   {
      char code = _msi_checksum( szCode );
      code -= '0';
      for( j = 0; j < 4; j++ )
      {
         if( code & 8 )
         {
            hb_bitbuffer_cat_int( pZebra->pBits, 31, iW );
            hb_bitbuffer_cat_int( pZebra->pBits,  0, iN );
         }
         else
         {
            hb_bitbuffer_cat_int( pZebra->pBits, 3, iN );
            hb_bitbuffer_cat_int( pZebra->pBits, 0, iW );
         }
         code <<= 1;
      }
   }
   /* stop */
   hb_bitbuffer_cat_int( pZebra->pBits, 3, iN );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, iW );
   hb_bitbuffer_cat_int( pZebra->pBits, 3, iN );
   return pZebra;
}
Example #6
0
PHB_ZEBRA hb_zebra_create_itf( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        i, iN, iW, iLen = ( int ) nLen;
   char       csum;

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_ITF;

   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] < '0' || szCode[ i ] > '9' )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
   }
   if( ( iLen + ( iFlags & HB_ZEBRA_FLAG_CHECKSUM ? 1 : 0 ) ) & 1 )
   {
      pZebra->szCode = ( char * ) hb_xgrab( iLen + 2 );
      pZebra->szCode[ 0 ] = '0';
      hb_xmemcpy( pZebra->szCode + 1, szCode, iLen );
      pZebra->szCode[ iLen + 1 ] = '\0';
   }
   else
   {
      pZebra->szCode = ( char * ) hb_xgrab( iLen + 1 );
      hb_xmemcpy( pZebra->szCode, szCode, iLen );
      pZebra->szCode[ iLen ] = '\0';
   }

   szCode = pZebra->szCode;
   if( iFlags & HB_ZEBRA_FLAG_CHECKSUM )
      csum = _itf_checksum( pZebra->szCode );
   else
      csum = 0;

   if( iFlags & HB_ZEBRA_FLAG_WIDE2_5 )
   {
      iN = 2;
      iW = 5;
   }
   else if( iFlags & HB_ZEBRA_FLAG_WIDE3 )
   {
      iN = 1;
      iW = 3;
   }
   else
   {
      iN = 1;
      iW = 2;
   }

   pZebra->pBits = hb_bitbuffer_create();

   /* start */
   hb_bitbuffer_cat_int( pZebra->pBits, 3, iN );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, iN );
   hb_bitbuffer_cat_int( pZebra->pBits, 3, iN );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, iN );

   for( i = 0; szCode[ i ]; i += 2 )
   {
      char c1 = s_code[ szCode[ i ] - '0' ], c2 = szCode[ i + 1 ] ? s_code[ szCode[ i + 1 ] - '0' ] : csum;
      hb_bitbuffer_cat_int( pZebra->pBits, 31, c1 & 1 ? iW : iN );  hb_bitbuffer_cat_int( pZebra->pBits, 0, c2 & 1 ? iW : iN );  c1 >>= 1;  c2 >>= 1;
      hb_bitbuffer_cat_int( pZebra->pBits, 31, c1 & 1 ? iW : iN );  hb_bitbuffer_cat_int( pZebra->pBits, 0, c2 & 1 ? iW : iN );  c1 >>= 1;  c2 >>= 1;
      hb_bitbuffer_cat_int( pZebra->pBits, 31, c1 & 1 ? iW : iN );  hb_bitbuffer_cat_int( pZebra->pBits, 0, c2 & 1 ? iW : iN );  c1 >>= 1;  c2 >>= 1;
      hb_bitbuffer_cat_int( pZebra->pBits, 31, c1 & 1 ? iW : iN );  hb_bitbuffer_cat_int( pZebra->pBits, 0, c2 & 1 ? iW : iN );  c1 >>= 1;  c2 >>= 1;
      hb_bitbuffer_cat_int( pZebra->pBits, 31, c1 & 1 ? iW : iN );  hb_bitbuffer_cat_int( pZebra->pBits, 0, c2 & 1 ? iW : iN );
      if( ! szCode[ i + 1 ] )
         break;
   }

   /* stop */
   hb_bitbuffer_cat_int( pZebra->pBits, 31, iW );
   hb_bitbuffer_cat_int( pZebra->pBits,  0, iN );
   hb_bitbuffer_cat_int( pZebra->pBits,  3, iN );
   return pZebra;
}
Example #7
0
PHB_ZEBRA hb_zebra_create_upce( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        i, iLen = ( int ) nLen;
   char       sumcode;

   HB_SYMBOL_UNUSED( iFlags );

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_UPCE;

   if( iLen != 6 && iLen != 7 )
   {
      pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
      return pZebra;
   }

   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] < '0' || szCode[ i ] > '9' )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
   }

   if( iLen == 6 )
   {
      pZebra->szCode = ( char * ) hb_xgrab( 8 );
      hb_xmemcpy( pZebra->szCode, szCode, 6 );
      pZebra->szCode[ 6 ] = _upce_checksum( szCode );
      pZebra->szCode[ 7 ] = '\0';
   }
   else
   {
      if( szCode[ 6 ] != _upce_checksum( szCode ) )
      {
         pZebra->iError = HB_ZEBRA_ERROR_BADCHECKSUM;
         return pZebra;
      }
      pZebra->szCode = ( char * ) hb_xgrab( 8 );
      hb_xmemcpy( pZebra->szCode, szCode, 7 );
      pZebra->szCode[ 7 ] = '\0';
   }
   szCode = pZebra->szCode;

   sumcode = szCode[ 6 ] == '0' ? 0x38 : s_first[ szCode[ 6 ] - '0' ];
   pZebra->pBits = hb_bitbuffer_create();

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* start */

   for( i = 0; i < 6; i++ )
   {
      if( sumcode & ( 1 << i ) )
         hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ] ^ 0x7F, 7 );
      else
         hb_bitbuffer_cat_int( pZebra->pBits, s_gcode[ szCode[ i ] - '0' ], 7 );
   }
   hb_bitbuffer_cat_int( pZebra->pBits, 42, 6 );   /* stop */
   return pZebra;
}
Example #8
0
PHB_ZEBRA hb_zebra_create_ean13( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA pZebra;
   int       i, iLen = ( int ) nLen;

   HB_SYMBOL_UNUSED( iFlags );

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_EAN13;

   if( iLen != 12 && iLen != 13 )
   {
      pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
      return pZebra;
   }

   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] < '0' || szCode[ i ] > '9' )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
   }

   if( iLen == 12 )
   {
      pZebra->szCode = ( char * ) hb_xgrab( 14 );
      hb_xmemcpy( pZebra->szCode, szCode, 12 );
      pZebra->szCode[ 12 ] = _ean13_checksum( szCode );
      pZebra->szCode[ 13 ] = '\0';
   }
   else
   {
      if( szCode[ 12 ] != _ean13_checksum( szCode ) )
      {
         pZebra->iError = HB_ZEBRA_ERROR_BADCHECKSUM;
         return pZebra;
      }
      pZebra->szCode = ( char * ) hb_xgrab( 14 );
      hb_xmemcpy( pZebra->szCode, szCode, 13 );
      pZebra->szCode[ 13 ] = '\0';
   }
   szCode = pZebra->szCode;

   pZebra->pBits = hb_bitbuffer_create();

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* start */

   for( i = 1; i <= 6; i++ )
   {
      if( s_first[ szCode[ 0 ] - '0' ] & ( 1 << ( i - 1 ) ) )
         hb_bitbuffer_cat_int( pZebra->pBits, s_gcode[ szCode[ i ] - '0' ], 7 );
      else
         hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ] ^ 0x7F, 7 );
   }

   hb_bitbuffer_cat_int( pZebra->pBits, 10, 5 );  /* middle */

   for( i = 7; i <= 12; i++ )
      hb_bitbuffer_cat_int( pZebra->pBits, s_rcode[ szCode[ i ] - '0' ], 7 );

   hb_bitbuffer_cat_int( pZebra->pBits, 5, 3 );   /* stop */

   return pZebra;
}
Example #9
0
PHB_ZEBRA hb_zebra_create_code128( const char * szCode, HB_SIZE nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        i, j, k, csum, iCodeSet, iCodeLen, iLen = ( int ) nLen;
   int *      pCode;

   HB_SYMBOL_UNUSED( iFlags );

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_CODE128;

   j = 0;
   for( i = 0; i < iLen; i++ )
   {
      if( ( unsigned char ) szCode[ i ] >= 128 )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
      if( szCode[ i ] >= ' ' && szCode[ i ] <= 126 )
         j++;
   }

   /* make print string */
   pZebra->szCode = ( char * ) hb_xgrab( j + 1 );
   j = 0;
   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] >= 32 && szCode[ i ] <= 126 )
         pZebra->szCode[ j++ ] = szCode[ i ];
   }
   pZebra->szCode[ j ] = '\0';

   /* generate code set switch characters */
   pCode = ( int * ) hb_xgrab( sizeof( int ) * iLen * 2 );
   iCodeSet = CODESET_B; /* to pacify MSVC warning only. It will be assigned later */
   iCodeLen = 0;
   /* determine the first optimal codeset */
   for( i = 0; i < iLen; i++ )
   {
      if( _code128_charno( szCode[ i ], CODESET_A ) == -1 )
      {
         iCodeSet = CODESET_B;
         pCode[ iCodeLen++ ] = START_B;
         break;
      }
      else if( _code128_charno( szCode[ i ], CODESET_B ) == -1 )
      {
         iCodeSet = CODESET_A;
         pCode[ iCodeLen++ ] = START_A;
         break;
      }
   }
   if( iCodeLen == 0 )
   {
      iCodeSet = CODESET_B;
      pCode[ iCodeLen++ ] = START_B;
   }

   /* encode source data */
   /* Warning: digit optimizer works in optimal way with this encoder code. Be careful
      if you'll change encoder code, digit optimizer can require adjustment also [Mindaugas] */
   for( i = 0; i < iLen; i++ )
   {
      int iCode = _code128_charno( szCode[ i ], iCodeSet );

      if( iCode != -1 )
         pCode[ iCodeLen++ ] = iCode;
      else
      {
         /* We should generate codeset switch instead of shift for the last character.
            This will help digit optimizer to do its job [Mindaugas] */
         if( i + 1 < iLen &&
             _code128_charno( szCode[ i + 1 ], iCodeSet == CODESET_A ? CODESET_B : CODESET_A ) == -1 )
         {
            pCode[ iCodeLen++ ] = SHIFT_AB;
            pCode[ iCodeLen++ ] = _code128_charno( szCode[ i ], iCodeSet == CODESET_A ? CODESET_B : CODESET_A );
         }
         else
         {
            if( iCodeSet == CODESET_A )
            {
               iCodeSet = CODESET_B;
               pCode[ iCodeLen++ ] = SELECT_B;
            }
            else
            {
               iCodeSet = CODESET_A;
               pCode[ iCodeLen++ ] = SELECT_A;
            }
            pCode[ iCodeLen++ ] = _code128_charno( szCode[ i ], iCodeSet );
         }
      }
   }

   /* optimize digits */
   iCodeSet = pCode[ 0 ] == START_A ? CODESET_A : CODESET_B;
   for( i = 1; i < iCodeLen; i++ )
   {
      if( iCodeSet == CODESET_A && pCode[ i ] == SELECT_B )
         iCodeSet = CODESET_B;
      else if( iCodeSet == CODESET_B && pCode[ i ] == SELECT_A )
         iCodeSet = CODESET_A;

      if( 16 <= pCode[ i ] && pCode[ i ] <= 25 )
      {
         for( j = i + 1; j < iCodeLen && 16 <= pCode[ j ] && pCode[ j ] <= 25; j++ )
            ;

         if( j - i == 2 && i == 1 && j == iCodeLen )
         {
            /* [StartB] 1 2  -->  [StartC] [12] */
            pCode[ 0 ] = START_C;
            pCode[ 1 ] = ( pCode[ 1 ] - 16 ) * 10 + pCode[ 2 ] - 16;
            iCodeLen = 2;
            break;
         }
         else if( ( j - i >= 4 && ( i == 1 || j == iCodeLen || pCode[ j ] == SELECT_A || pCode[ j ] == SELECT_B ) ) ||
                  j - i >= 6 )
         {
            if( i == 1 )
            {
               /* [StartN] 1 2 3 4  -->  [StartC] [12] [34] */
               /* [StartN] 1 2 3 4 5  -->  [StartC] [12] [34] [CodeN] 5 */
               /* [StartN] 1 2 3 4 X ... -->  [StartC] [12] [34] [CodeN] X ... */
               /* [StartN] 1 2 3 4 5 X ... -->  [StartC] [12] [34] [CodeN] 5 X ... */
               pCode[ 0 ] = START_C;
               for( k = 1; k < j - 1; k += 2 )
                  pCode[ i++ ] = ( pCode[ k ] - 16 ) * 10 + pCode[ k + 1 ] - 16;

               if( k < iCodeLen )
               {
                  j = i;
                  pCode[ i++ ] = iCodeSet == CODESET_A ? SELECT_A : SELECT_B;
                  for( ; k < iCodeLen; k++ )
                     pCode[ i++ ] = pCode[ k ];
               }
               iCodeLen = i;
            }
            else
            {
               /* ... X 1 2 3 4  -->  ... X [CodeC] [12] [34] */
               /* ... X 1 2 3 4 [CodeN] ... -->  ... X [CodeC] [12] [34] [CodeN] ... */
               /* ... X 1 2 3 4 5  -->  ... X 1 [CodeC] [23] [45] */
               /* ... X 1 2 3 4 5 [CodeN] ...  -->  ... X 1 [CodeC] [23] [45] [CodeN] ... */
               /* ... X 1 2 3 4 5 6 Y ...  -->  ... X [CodeC] [12] [34] [56] [CodeN] Y ... */
               /* ... X 1 2 3 4 5 6 7 Y ...  -->  ... X 1 [CodeC] [23] [45] [67] [CodeN] Y ... */

               if( ( j - i ) & 1 )
               {
                  /* digit count is odd */
                  i++;
               }

               pCode[ i + 1 ] = ( pCode[ i ] - 16 ) * 10 + pCode[ i + 1 ] - 16;
               pCode[ i ] = SELECT_C;
               i += 2;
               for( k = i; k < j; k += 2 )
                  pCode[ i++ ] = ( pCode[ k ] - 16 ) * 10 + pCode[ k + 1 ] - 16;
               j = i;
               if( k < iCodeLen )
               {
                  if( pCode[ k ] != SELECT_A && pCode[ k ] != SELECT_B )
                     pCode[ i++ ] = iCodeSet == CODESET_A ? SELECT_A : SELECT_B;

                  for( ; k < iCodeLen; k++ )
                     pCode[ i++ ] = pCode[ k ];
               }
               iCodeLen = i;
            }
         }
         i = j - 1;
      }
   }

   pZebra->pBits = hb_bitbuffer_create();
   csum = pCode[ 0 ];
   for( i = 0; i < iCodeLen; i++ )
   {
      hb_bitbuffer_cat_int( pZebra->pBits, s_code[ pCode[ i ] ], 11 );
      csum += i * pCode[ i ];
   }

   hb_xfree( pCode );
   /* checksum */
   hb_bitbuffer_cat_int( pZebra->pBits, s_code[ csum % 103 ], 11 );

   hb_bitbuffer_cat_int( pZebra->pBits, 0x1AE3, 13 );
   return pZebra;
}
Example #10
0
PHB_ZEBRA hb_zebra_create_code93( const char * szCode, ULONG nLen, int iFlags )
{
   PHB_ZEBRA  pZebra;
   int        csum, ksum, k, i, j, iLen = ( int ) nLen;

   HB_SYMBOL_UNUSED( iFlags );

   pZebra = hb_zebra_create();
   pZebra->iType = HB_ZEBRA_TYPE_CODE93;

   j = 0;
   k = 0;
   for( i = 0; i < iLen; i++ )
   {
      if( ( unsigned char ) szCode[ i ] >= 128 )
      {
         pZebra->iError = HB_ZEBRA_ERROR_INVALIDCODE;
         return pZebra;
      }
      if( szCode[ i ] >= ' ' && szCode[ i ] <= 126 )
         j++;

      k += _code93_charno( szCode[ i ] ) >= 0 ? 1 : 2;
   }

   pZebra->szCode = ( char * ) hb_xgrab( j + 1 );
   j = 0;
   for( i = 0; i < iLen; i++ )
   {
      if( szCode[ i ] >= 32 && szCode[ i ] <= 126 )
         pZebra->szCode[ j++ ] = szCode[ i ];
   }
   pZebra->szCode[ j ] = '\0';

   pZebra->pBits = hb_bitbuffer_create();

   /* start */
   hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
   hb_bitbuffer_cat_int( pZebra->pBits, s_code[ 47 ], 7 );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );

   csum = 0;
   ksum = 0;
   k++;
   for( i = 0; i < iLen; i++ )
   {
      int no = _code93_charno( szCode[ i ] );
      if( no >= 0 )
      {
         hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
         hb_bitbuffer_cat_int( pZebra->pBits, s_code[ no ], 7 );
         hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );
         ksum += ( k % 15 ? k % 15 : 15 ) * no;  k--;
         csum += ( k % 20 ? k % 20 : 20 ) * no;
      }
      else
      {
         int no1 = 0, no2 = 0;
         if( szCode[ i ] >= 1 && szCode[ i ] <= 26 )
         {
            no1 = 43; /* ($) */
            no2 = szCode[ i ] - 1 + 10;
         }
         else if( szCode[ i ] >= '!' && szCode[ i ] <= ':' )
         {
            no1 = 45; /* (/) */
            no2 = szCode[ i ] - '!' + 10;
         }
         else if( szCode[ i ] >= 'a' && szCode[ i ] <= 'z' )
         {
            no1 = 46; /* (+) */
            no2 = szCode[ i ] - 'a' + 10;
         }
         else if( szCode[ i ] >= 27 && szCode[ i ] <= 31 )
         {
            no1 = 44; /* (%) */
            no2 = szCode[ i ] - 27 + 10;
         }
         else if( szCode[ i ] >= '[' && szCode[ i ] <= '_' )
         {
            no1 = 44; /* (%) */
            no2 = szCode[ i ] - '[' + 15;
         }
         else if( szCode[ i ] >= '{' && ( unsigned char ) szCode[ i ] <= 127 )
         {
            no1 = 44; /* (%) */
            no2 = szCode[ i ] - '{' + 20;
         }
         else if( szCode[ i ] == '\0' )
         {
            no1 = 44; /* (%) */
            no2 = 30; /* U */
         }
         else if( szCode[ i ] == '@' )
         {
            no1 = 44; /* (%) */
            no2 = 31; /* V */
         }
         else if( szCode[ i ] == '`' )
         {
            no1 = 44; /* (%) */
            no2 = 32; /* W */
         }
         hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
         hb_bitbuffer_cat_int( pZebra->pBits, s_code[ no1 ], 7 );
         hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );
         hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
         hb_bitbuffer_cat_int( pZebra->pBits, s_code[ no2 ], 7 );
         hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );
         ksum += ( k % 15 ? k % 15 : 15 ) * no1;  k--;
         csum += ( k % 20 ? k % 20 : 20 ) * no1;
         ksum += ( k % 15 ? k % 15 : 15 ) * no2;  k--;
         csum += ( k % 20 ? k % 20 : 20 ) * no2;
      }
   }

   /* checksum */
   hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
   hb_bitbuffer_cat_int( pZebra->pBits, s_code[ csum % 47 ], 7 );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );
   ksum += csum % 47;
   hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
   hb_bitbuffer_cat_int( pZebra->pBits, s_code[ ksum % 47 ], 7 );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );

   /* stop */
   hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );
   hb_bitbuffer_cat_int( pZebra->pBits, s_code[ 47 ], 7 );
   hb_bitbuffer_cat_int( pZebra->pBits, 0, 1 );
   hb_bitbuffer_cat_int( pZebra->pBits, 1, 1 );

   return pZebra;
}