2008-07-14 09:15:55 +12:00
/* library.c - external functions of libzint
libzint - the open source barcode library
2009-05-10 23:47:20 +12:00
Copyright ( C ) 2009 Robin Stuart < robin @ zint . org . uk >
2008-07-14 09:15:55 +12:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License along
with this program ; if not , write to the Free Software Foundation , Inc . ,
51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA .
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
2009-06-03 08:23:38 +12:00
# ifdef _MSC_VER
# include <malloc.h>
# endif
2008-07-14 09:15:55 +12:00
# include "common.h"
2009-01-11 22:11:03 +13:00
# include "gs1.h"
2009-02-23 03:13:32 +13:00
# include "sjis.h"
2008-07-14 09:15:55 +12:00
2009-02-01 11:03:46 +13:00
# define HIBCSET "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $ / +%"
2008-07-14 09:15:55 +12:00
struct zint_symbol * ZBarcode_Create ( )
{
struct zint_symbol * symbol ;
int i , j ;
2008-09-03 07:41:14 +12:00
2009-06-03 08:23:38 +12:00
symbol = ( struct zint_symbol * ) malloc ( sizeof ( * symbol ) ) ;
2008-07-14 09:15:55 +12:00
if ( ! symbol ) return NULL ;
memset ( symbol , 0 , sizeof ( * symbol ) ) ;
symbol - > symbology = BARCODE_CODE128 ;
symbol - > height = 0 ;
symbol - > whitespace_width = 0 ;
symbol - > border_width = 0 ;
symbol - > output_options = 0 ;
symbol - > rows = 0 ;
symbol - > width = 0 ;
strcpy ( symbol - > fgcolour , " 000000 " ) ;
strcpy ( symbol - > bgcolour , " ffffff " ) ;
strcpy ( symbol - > outfile , " out.png " ) ;
2008-12-21 04:21:04 +13:00
symbol - > scale = 1.0 ;
2008-07-14 09:15:55 +12:00
symbol - > option_1 = - 1 ;
symbol - > option_2 = 0 ;
2008-09-03 07:41:14 +12:00
symbol - > option_3 = 928 ; // PDF_MAX
2009-01-11 22:11:03 +13:00
symbol - > input_mode = DATA_MODE ;
2008-07-14 09:15:55 +12:00
strcpy ( symbol - > primary , " " ) ;
2008-10-16 10:00:10 +13:00
for ( i = 0 ; i < 178 ; i + + ) {
2008-07-14 09:15:55 +12:00
for ( j = 0 ; j < 1000 ; j + + ) {
2009-06-01 08:33:54 +12:00
unset_module ( symbol , i , j ) ;
2008-07-14 09:15:55 +12:00
}
symbol - > row_height [ i ] = 0 ;
}
return symbol ;
2009-01-03 10:09:16 +13:00
symbol - > nullchar = 0x00 ;
2008-07-14 09:15:55 +12:00
}
int ZBarcode_Delete ( struct zint_symbol * symbol )
{
free ( symbol ) ;
return 0 ;
}
extern int eanx ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* EAN system barcodes */
extern int c39 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 3 from 9 (or Code 39) */
extern int pharmazentral ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Pharmazentral Nummer (PZN) */
extern int ec39 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Extended Code 3 from 9 (or Code 39+) */
extern int codabar ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Codabar - a simple substitution cipher */
extern int matrix_two_of_five ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 2 of 5 Standard (& Matrix) */
extern int industrial_two_of_five ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 2 of 5 Industrial */
extern int iata_two_of_five ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 2 of 5 IATA */
extern int interleaved_two_of_five ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 2 of 5 Interleaved */
extern int logic_two_of_five ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 2 of 5 Data Logic */
extern int itf14 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* ITF-14 */
extern int dpleit ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Deutsche Post Leitcode */
extern int dpident ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Deutsche Post Identcode */
extern int c93 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 93 - a re-working of Code 39+, generates 2 check digits */
extern int code_128 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 128 and NVE-18 */
extern int ean_128 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* EAN-128 (GS1-128) */
extern int code_11 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 11 */
2008-09-16 19:39:47 +12:00
extern int msi_handle ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* MSI Plessey */
2008-07-14 09:15:55 +12:00
extern int telepen ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Telepen ASCII */
extern int telepen_num ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Telepen Numeric */
extern int plessey ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Plessey Code */
extern int pharma_one ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Pharmacode One Track */
extern int flattermarken ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Flattermarken */
extern int fim ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Facing Identification Mark */
extern int pharma_two ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Pharmacode Two Track */
extern int post_plot ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Postnet */
extern int planet_plot ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* PLANET */
extern int imail ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Intelligent Mail (aka USPS OneCode) */
extern int royal_plot ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* RM4SCC */
extern int australia_post ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Australia Post 4-state */
extern int code16k ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 16k */
extern int pdf417enc ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* PDF417 */
extern int dmatrix ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Data Matrix (IEC16022) */
extern int qr_code ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* QR Code */
extern int micro_pdf417 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Micro PDF417 */
extern int maxicode ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Maxicode */
extern int rss14 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* RSS-14 */
extern int rsslimited ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* RSS Limited */
extern int rssexpanded ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* RSS Expanded */
extern int composite ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Composite Symbology */
2008-09-03 07:41:14 +12:00
extern int kix_code ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* TNT KIX Code */
extern int aztec ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Aztec Code */
extern int code32 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Italian Pharmacode */
2008-09-16 19:39:47 +12:00
extern int codablock ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Codablock F */
extern int daft_code ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* DAFT Code */
2008-09-19 21:02:52 +12:00
extern int ean_14 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* EAN-14 */
2008-10-13 10:05:53 +13:00
extern int nve_18 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* NVE-18 */
2008-11-05 21:43:13 +13:00
extern int microqr ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Micro QR Code */
2008-11-09 23:06:05 +13:00
extern int aztec_runes ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Aztec Runes */
2008-12-08 09:11:50 +13:00
extern int korea_post ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Korea Post */
2009-03-15 00:04:52 +13:00
extern int japan_post ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Japanese Post */
2009-05-10 23:47:20 +12:00
extern int code_49 ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code 49 */
2009-05-17 02:19:43 +12:00
extern int channel_code ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Channel Code */
2009-07-05 08:48:42 +12:00
extern int code_one ( struct zint_symbol * symbol , unsigned char source [ ] ) ; /* Code One */
2008-07-14 09:15:55 +12:00
2008-09-26 23:39:06 +12:00
# ifndef NO_PNG
2008-10-03 21:07:05 +13:00
int png_handle ( struct zint_symbol * symbol , int rotate_angle ) ;
2008-09-26 23:39:06 +12:00
# endif
2008-07-14 09:15:55 +12:00
extern int ps_plot ( struct zint_symbol * symbol ) ;
2009-02-18 06:48:39 +13:00
extern int svg_plot ( struct zint_symbol * symbol ) ;
2008-07-14 09:15:55 +12:00
2008-10-05 18:51:58 +13:00
void error_tag ( char error_string [ ] , int error_number )
{
char error_buffer [ 100 ] ;
if ( error_number ! = 0 ) {
strcpy ( error_buffer , error_string ) ;
if ( error_number > 4 ) {
strcpy ( error_string , " error: " ) ;
} else {
strcpy ( error_string , " warning: " ) ;
}
concat ( error_string , error_buffer ) ;
}
}
2009-02-01 11:03:46 +13:00
int hibc ( struct zint_symbol * symbol , unsigned char source [ ] )
{
int counter , srclen , error_number , i ;
char to_process [ 40 ] , temp [ 3 ] , check_digit ;
srclen = ustrlen ( source ) ;
strcpy ( temp , " " ) ;
to_upper ( source ) ;
if ( srclen > 36 ) {
strcpy ( symbol - > errtxt , " Data too long for HIBC LIC " ) ;
return ERROR_TOO_LONG ;
}
error_number = is_sane ( HIBCSET , source ) ;
if ( error_number = = ERROR_INVALID_DATA ) {
2009-05-10 23:47:20 +12:00
strcpy ( symbol - > errtxt , " Invalid characters in data " ) ;
2009-02-01 11:03:46 +13:00
return error_number ;
}
strcpy ( to_process , " + " ) ;
counter = 41 ;
for ( i = 0 ; i < ustrlen ( source ) ; i + + ) {
counter + = posn ( HIBCSET , source [ i ] ) ;
}
counter = counter % 43 ;
if ( counter < 10 ) {
check_digit = itoc ( counter ) ;
} else {
if ( counter < 36 ) {
check_digit = ( counter - 10 ) + ' A ' ;
} else {
switch ( counter ) {
case 36 : check_digit = ' - ' ; break ;
case 37 : check_digit = ' . ' ; break ;
case 38 : check_digit = ' ' ; break ;
case 39 : check_digit = ' $ ' ; break ;
case 40 : check_digit = ' / ' ; break ;
case 41 : check_digit = ' + ' ; break ;
case 42 : check_digit = 37 ; break ;
default : check_digit = ' ' ; break ; /* Keep compiler happy */
}
}
}
temp [ 0 ] = check_digit ;
temp [ 1 ] = ' \0 ' ;
concat ( to_process , ( char * ) source ) ;
concat ( to_process , temp ) ;
switch ( symbol - > symbology ) {
case BARCODE_HIBC_128 :
error_number = code_128 ( symbol , ( unsigned char * ) to_process ) ;
2009-02-20 08:09:57 +13:00
ustrcpy ( symbol - > text , ( unsigned char * ) " * " ) ;
uconcat ( symbol - > text , ( unsigned char * ) to_process ) ;
uconcat ( symbol - > text , ( unsigned char * ) " * " ) ;
2009-02-01 11:03:46 +13:00
break ;
case BARCODE_HIBC_39 :
2009-02-11 11:48:00 +13:00
symbol - > option_2 = 0 ;
2009-02-01 11:03:46 +13:00
error_number = c39 ( symbol , ( unsigned char * ) to_process ) ;
2009-02-20 08:09:57 +13:00
ustrcpy ( symbol - > text , ( unsigned char * ) " * " ) ;
uconcat ( symbol - > text , ( unsigned char * ) to_process ) ;
uconcat ( symbol - > text , ( unsigned char * ) " * " ) ;
2009-02-01 11:03:46 +13:00
break ;
case BARCODE_HIBC_DM :
error_number = dmatrix ( symbol , ( unsigned char * ) to_process ) ;
break ;
case BARCODE_HIBC_QR :
error_number = qr_code ( symbol , ( unsigned char * ) to_process ) ;
break ;
case BARCODE_HIBC_PDF :
error_number = pdf417enc ( symbol , ( unsigned char * ) to_process ) ;
break ;
case BARCODE_HIBC_MICPDF :
error_number = micro_pdf417 ( symbol , ( unsigned char * ) to_process ) ;
break ;
case BARCODE_HIBC_BLOCKF :
error_number = codablock ( symbol , ( unsigned char * ) to_process ) ;
break ;
}
return error_number ;
}
2009-01-11 22:11:03 +13:00
int eci_process ( struct zint_symbol * symbol , unsigned char source [ ] , unsigned char preprocessed [ ] )
{
int j , i , next , input_length ;
input_length = ustrlen ( source ) ;
/* Supports UTF-8 input by converting it to Latin-1 Extended ASCII */
/* Currently only supports Latin-1 characters but expect this to be expanded
to the full ECI spectrum */
j = 0 ;
i = 0 ;
do {
next = - 1 ;
if ( source [ i ] < 128 ) {
preprocessed [ j ] = source [ i ] ;
j + + ;
next = i + 1 ;
} else {
if ( source [ i ] = = 0xC2 ) {
preprocessed [ j ] = source [ i + 1 ] ;
j + + ;
next = i + 2 ;
}
if ( source [ i ] = = 0xC3 ) {
preprocessed [ j ] = source [ i + 1 ] + 64 ;
j + + ;
next = i + 2 ;
}
}
if ( next = = - 1 ) {
strcpy ( symbol - > errtxt , " error: Invalid character in input string (only Latin-1 characters supported) " ) ;
return ERROR_INVALID_DATA ;
}
i = next ;
} while ( i < input_length ) ;
preprocessed [ j ] = ' \0 ' ;
return 0 ;
}
2009-02-23 03:13:32 +13:00
int unicode2shift_jis ( struct zint_symbol * symbol , unsigned char source [ ] , unsigned char preprocessed [ ] )
{ /* QR Code supports compression of Shift-JIS data using "Kanji" mode - this function
attempts to convert Unicode characters to Shift - JIS to allow this */
int bpos , jpos , len , error_number , i ;
int next ;
unsigned long int uval , jval ;
len = ustrlen ( source ) ;
bpos = 0 ;
jpos = 0 ;
error_number = 0 ;
next = 0 ;
do {
uval = 0 ;
jval = 0 ;
if ( source [ bpos ] < = 0x7f ) {
/* 1 byte mode */
uval = source [ bpos ] ;
next = bpos + 1 ;
}
if ( ( source [ bpos ] > = 0x80 ) & & ( source [ bpos ] < = 0xbf ) ) {
strcpy ( symbol - > errtxt , " Corrupt Unicode data " ) ;
return ERROR_INVALID_DATA ;
}
if ( ( source [ bpos ] > = 0xc0 ) & & ( source [ bpos ] < = 0xc1 ) ) {
strcpy ( symbol - > errtxt , " Overlong encoding not supported " ) ;
return ERROR_INVALID_DATA ;
}
if ( ( source [ bpos ] > = 0xc2 ) & & ( source [ bpos ] < = 0xdf ) ) {
/* 2 byte mode */
uval = ( ( source [ bpos ] & 0x1f ) < < 6 ) + ( source [ bpos + 1 ] & 0x3f ) ;
next = bpos + 2 ;
}
if ( ( source [ bpos ] > = 0xe0 ) & & ( source [ bpos ] < = 0xef ) ) {
/* 3 byte mode */
uval = ( ( source [ bpos ] & 0x0f ) < < 12 ) + ( ( source [ bpos + 1 ] & 0x3f ) < < 6 ) + ( source [ bpos + 2 ] & 0x3f ) ;
next = bpos + 3 ;
}
if ( source [ bpos ] > = 0xf0 ) {
strcpy ( symbol - > errtxt , " Unicode sequences of more than 3 bytes not supported " ) ;
return ERROR_INVALID_DATA ;
}
for ( i = 0 ; i < 6843 ; i + + ) {
if ( sjis_lookup [ i * 2 ] = = uval ) {
jval = sjis_lookup [ ( i * 2 ) + 1 ] ;
}
}
if ( jval = = 0 ) {
strcpy ( symbol - > errtxt , " Invalid Shift JIS character " ) ;
return ERROR_INVALID_DATA ;
}
preprocessed [ jpos ] = ( jval & 0xff00 ) > > 8 ;
preprocessed [ jpos + 1 ] = ( jval & 0xff ) ;
/* printf("Unicode value U+%04X = Shift JIS value 0x%04X\n", uval, jval); */
jpos + = 2 ;
bpos = next ;
} while ( bpos < len ) ;
return error_number ;
}
2009-01-11 22:11:03 +13:00
int gs1_compliant ( int symbology )
{
/* Returns 1 if symbology supports GS1 data */
int result = 0 ;
switch ( symbology ) {
case BARCODE_EAN128 :
case BARCODE_RSS_EXP :
case BARCODE_RSS_EXPSTACK :
case BARCODE_EANX_CC :
case BARCODE_EAN128_CC :
case BARCODE_RSS14_CC :
case BARCODE_RSS_LTD_CC :
case BARCODE_RSS_EXP_CC :
case BARCODE_UPCA_CC :
case BARCODE_UPCE_CC :
case BARCODE_RSS14STACK_CC :
case BARCODE_RSS14_OMNI_CC :
case BARCODE_RSS_EXPSTACK_CC :
2009-01-14 06:49:00 +13:00
case BARCODE_CODE16K :
2009-01-16 18:50:22 +13:00
case BARCODE_AZTEC :
2009-01-25 01:22:10 +13:00
case BARCODE_DATAMATRIX :
case BARCODE_CODABLOCKF :
2009-07-09 20:28:00 +12:00
case BARCODE_CODEONE :
case BARCODE_CODE49 :
2009-08-11 10:33:53 +12:00
case BARCODE_QRCODE :
2009-01-11 22:11:03 +13:00
result = 1 ;
break ;
}
return result ;
}
2009-08-26 10:40:11 +12:00
int ZBarcode_Check_Supported ( int symbol_id )
{
/* Checks whether a symbology is supported */
int value ;
switch ( symbol_id ) {
case 5 :
case 10 :
case 11 :
case 12 :
case 14 :
case 15 :
case 17 :
case 19 :
case 26 :
case 27 :
case 33 :
case 35 :
case 36 :
case 38 :
case 39 :
case 48 :
case 54 :
# ifdef NO_QR
case 58 :
# endif
case 59 :
case 61 :
case 62 :
case 64 :
case 65 :
case 73 :
case 78 :
case 83 :
case 88 :
case 91 :
case 100 :
case 101 :
case 103 :
case 105 :
case 107 :
case 109 :
value = 0 ; break ;
default :
value = 1 ; break ;
}
if ( symbol_id < 1 ) { value = 0 ; }
if ( symbol_id > 141 ) { value = 0 ; }
if ( ( symbol_id > = 41 ) & & ( symbol_id < = 46 ) ) { value = 0 ; }
if ( ( symbol_id > = 94 ) & & ( symbol_id < = 96 ) ) { value = 0 ; }
if ( ( symbol_id > = 111 ) & & ( symbol_id < = 127 ) ) { value = 0 ; }
return value ;
}
2009-01-11 22:11:03 +13:00
int ZBarcode_Encode ( struct zint_symbol * symbol , unsigned char * source )
2008-07-14 09:15:55 +12:00
{
2008-10-05 18:51:58 +13:00
int error_number , error_buffer ;
2008-12-25 10:29:54 +13:00
int input_length ;
2009-08-07 06:48:42 +12:00
# ifdef _MSC_VER
unsigned char * preprocessed ;
# endif
2009-01-11 22:11:03 +13:00
input_length = ustrlen ( source ) ;
2009-08-07 06:48:42 +12:00
error_number = 0 ;
2009-06-03 08:23:38 +12:00
# ifndef _MSC_VER
2009-01-11 22:11:03 +13:00
unsigned char preprocessed [ input_length ] ;
2009-06-03 08:23:38 +12:00
# else
2009-08-07 06:48:42 +12:00
preprocessed = ( unsigned char * ) _alloca ( input_length + 1 ) ;
2009-06-03 08:23:38 +12:00
# endif
2008-07-14 09:15:55 +12:00
2009-01-11 22:11:03 +13:00
if ( ustrlen ( source ) = = 0 ) {
2009-05-15 10:22:01 +12:00
strcpy ( symbol - > errtxt , " No input data " ) ;
2008-10-10 00:29:09 +13:00
error_tag ( symbol - > errtxt , ERROR_INVALID_DATA ) ;
return ERROR_INVALID_DATA ;
}
2008-09-03 07:41:14 +12:00
2008-10-10 00:29:09 +13:00
/* First check the symbology field */
2009-05-10 23:47:20 +12:00
if ( symbol - > symbology < 1 ) { strcpy ( symbol - > errtxt , " Symbology out of range, using Code 128 " ) ; symbol - > symbology = BARCODE_CODE128 ; error_number = WARN_INVALID_OPTION ; }
2008-07-14 09:15:55 +12:00
/* symbol->symbologys 1 to 86 are defined by tbarcode */
if ( symbol - > symbology = = 5 ) { symbol - > symbology = BARCODE_C25MATRIX ; }
if ( ( symbol - > symbology > = 10 ) & & ( symbol - > symbology < = 12 ) ) { symbol - > symbology = BARCODE_EANX ; }
if ( ( symbol - > symbology = = 14 ) | | ( symbol - > symbology = = 15 ) ) { symbol - > symbology = BARCODE_EANX ; }
if ( symbol - > symbology = = 17 ) { symbol - > symbology = BARCODE_UPCA ; }
2009-05-10 23:47:20 +12:00
if ( symbol - > symbology = = 19 ) { strcpy ( symbol - > errtxt , " Codabar 18 not supported, using Codabar " ) ; symbol - > symbology = BARCODE_CODABAR ; error_number = WARN_INVALID_OPTION ; }
2008-07-14 09:15:55 +12:00
if ( symbol - > symbology = = 26 ) { symbol - > symbology = BARCODE_UPCA ; }
2009-05-10 23:47:20 +12:00
if ( symbol - > symbology = = 27 ) { strcpy ( symbol - > errtxt , " UPCD1 not supported " ) ; error_number = ERROR_INVALID_OPTION ; }
2008-07-14 09:15:55 +12:00
if ( symbol - > symbology = = 33 ) { symbol - > symbology = BARCODE_EAN128 ; }
if ( ( symbol - > symbology = = 35 ) | | ( symbol - > symbology = = 36 ) ) { symbol - > symbology = BARCODE_UPCA ; }
if ( ( symbol - > symbology = = 38 ) | | ( symbol - > symbology = = 39 ) ) { symbol - > symbology = BARCODE_UPCE ; }
if ( ( symbol - > symbology > = 41 ) & & ( symbol - > symbology < = 45 ) ) { symbol - > symbology = BARCODE_POSTNET ; }
if ( symbol - > symbology = = 46 ) { symbol - > symbology = BARCODE_PLESSEY ; }
if ( symbol - > symbology = = 48 ) { symbol - > symbology = BARCODE_NVE18 ; }
2009-05-10 23:47:20 +12:00
if ( symbol - > symbology = = 54 ) { strcpy ( symbol - > errtxt , " General Parcel Code not supported, using Code 128 " ) ; symbol - > symbology = BARCODE_CODE128 ; error_number = WARN_INVALID_OPTION ; }
2008-07-14 09:15:55 +12:00
if ( ( symbol - > symbology = = 59 ) | | ( symbol - > symbology = = 61 ) ) { symbol - > symbology = BARCODE_CODE128 ; }
if ( symbol - > symbology = = 62 ) { symbol - > symbology = BARCODE_CODE93 ; }
if ( ( symbol - > symbology = = 64 ) | | ( symbol - > symbology = = 65 ) ) { symbol - > symbology = BARCODE_AUSPOST ; }
2009-05-10 23:47:20 +12:00
if ( symbol - > symbology = = 73 ) { strcpy ( symbol - > errtxt , " Codablock E not supported " ) ; error_number = ERROR_INVALID_OPTION ; }
2008-07-14 09:15:55 +12:00
if ( symbol - > symbology = = 78 ) { symbol - > symbology = BARCODE_RSS14 ; }
if ( symbol - > symbology = = 83 ) { symbol - > symbology = BARCODE_PLANET ; }
2008-09-19 21:02:52 +12:00
if ( symbol - > symbology = = 88 ) { symbol - > symbology = BARCODE_EAN128 ; }
2009-05-10 23:47:20 +12:00
if ( symbol - > symbology = = 91 ) { strcpy ( symbol - > errtxt , " Symbology out of range, using Code 128 " ) ; symbol - > symbology = BARCODE_CODE128 ; error_number = WARN_INVALID_OPTION ; }
if ( ( symbol - > symbology > = 94 ) & & ( symbol - > symbology < = 96 ) ) { strcpy ( symbol - > errtxt , " Symbology out of range, using Code 128 " ) ; symbol - > symbology = BARCODE_CODE128 ; error_number = WARN_INVALID_OPTION ; }
2009-02-01 11:03:46 +13:00
if ( symbol - > symbology = = 100 ) { symbol - > symbology = BARCODE_HIBC_128 ; }
if ( symbol - > symbology = = 101 ) { symbol - > symbology = BARCODE_HIBC_39 ; }
if ( symbol - > symbology = = 103 ) { symbol - > symbology = BARCODE_HIBC_DM ; }
if ( symbol - > symbology = = 105 ) { symbol - > symbology = BARCODE_HIBC_QR ; }
if ( symbol - > symbology = = 107 ) { symbol - > symbology = BARCODE_HIBC_PDF ; }
if ( symbol - > symbology = = 109 ) { symbol - > symbology = BARCODE_HIBC_MICPDF ; }
if ( symbol - > symbology = = 111 ) { symbol - > symbology = BARCODE_HIBC_BLOCKF ; }
2009-05-10 23:47:20 +12:00
if ( ( symbol - > symbology > = 112 ) & & ( symbol - > symbology < = 127 ) ) { strcpy ( symbol - > errtxt , " Symbology out of range, using Code 128 " ) ; symbol - > symbology = BARCODE_CODE128 ; error_number = WARN_INVALID_OPTION ; }
2008-11-05 21:43:13 +13:00
/* Everything from 128 up is Zint-specific */
2009-07-05 08:48:42 +12:00
if ( symbol - > symbology > = 142 ) { strcpy ( symbol - > errtxt , " Symbology out of range, using Code 128 " ) ; symbol - > symbology = BARCODE_CODE128 ; error_number = WARN_INVALID_OPTION ; }
2009-04-27 10:04:01 +12:00
2008-09-16 19:39:47 +12:00
if ( error_number > 4 ) {
2008-10-05 18:51:58 +13:00
error_tag ( symbol - > errtxt , error_number ) ;
2008-09-16 19:39:47 +12:00
return error_number ;
2008-10-05 18:51:58 +13:00
} else {
error_buffer = error_number ;
2008-07-14 09:15:55 +12:00
}
2008-09-03 07:41:14 +12:00
2009-01-11 22:11:03 +13:00
if ( ( symbol - > input_mode < 0 ) | | ( symbol - > input_mode > 3 ) ) { symbol - > input_mode = DATA_MODE ; }
switch ( symbol - > input_mode ) {
case DATA_MODE :
ustrcpy ( preprocessed , source ) ; break ;
case UNICODE_MODE :
error_number = eci_process ( symbol , source , preprocessed ) ;
if ( error_number ! = 0 ) { return error_number ; }
break ;
case GS1_MODE :
if ( gs1_compliant ( symbol - > symbology ) = = 1 ) {
2009-01-14 09:42:45 +13:00
error_number = ugs1_verify ( symbol , source , preprocessed ) ;
if ( error_number ! = 0 ) { return error_number ; }
2009-01-11 22:11:03 +13:00
} else {
strcpy ( symbol - > errtxt , " Selected symbology does not support GS1 mode " ) ;
return ERROR_INVALID_OPTION ;
2008-12-25 10:29:54 +13:00
}
2009-01-11 22:11:03 +13:00
break ;
case KANJI_MODE :
2009-02-23 03:13:32 +13:00
if ( ( symbol - > symbology ! = BARCODE_QRCODE ) & & ( symbol - > symbology ! = BARCODE_MICROQR ) ) {
2009-01-11 22:11:03 +13:00
strcpy ( symbol - > errtxt , " Selected symbology does not support Kanji mode " ) ;
return ERROR_INVALID_OPTION ;
2008-12-25 10:29:54 +13:00
}
2009-02-23 03:13:32 +13:00
error_number = unicode2shift_jis ( symbol , source , preprocessed ) ;
if ( error_number ! = 0 ) { return error_number ; }
break ;
case SJIS_MODE :
if ( ( symbol - > symbology ! = BARCODE_QRCODE ) & & ( symbol - > symbology ! = BARCODE_MICROQR ) ) {
strcpy ( symbol - > errtxt , " Selected symbology does not support Kanji mode " ) ;
return ERROR_INVALID_OPTION ;
}
ustrcpy ( preprocessed , source ) ;
break ;
2009-01-11 22:11:03 +13:00
}
2008-12-25 10:29:54 +13:00
2008-07-14 09:15:55 +12:00
if ( symbol - > symbology = = BARCODE_CODE16K ) {
symbol - > whitespace_width = 16 ;
symbol - > border_width = 2 ;
symbol - > output_options = BARCODE_BIND ;
}
2008-09-03 07:41:14 +12:00
2008-07-14 09:15:55 +12:00
if ( symbol - > symbology = = BARCODE_ITF14 ) {
symbol - > whitespace_width = 20 ;
symbol - > border_width = 8 ;
symbol - > output_options = BARCODE_BOX ;
}
2008-10-10 00:29:09 +13:00
2008-07-14 09:15:55 +12:00
switch ( symbol - > symbology ) {
2009-01-11 22:11:03 +13:00
case BARCODE_C25MATRIX : error_number = matrix_two_of_five ( symbol , preprocessed ) ; break ;
case BARCODE_C25IND : error_number = industrial_two_of_five ( symbol , preprocessed ) ; break ;
case BARCODE_C25INTER : error_number = interleaved_two_of_five ( symbol , preprocessed ) ; break ;
case BARCODE_C25IATA : error_number = iata_two_of_five ( symbol , preprocessed ) ; break ;
case BARCODE_C25LOGIC : error_number = logic_two_of_five ( symbol , preprocessed ) ; break ;
case BARCODE_DPLEIT : error_number = dpleit ( symbol , preprocessed ) ; break ;
case BARCODE_DPIDENT : error_number = dpident ( symbol , preprocessed ) ; break ;
case BARCODE_UPCA : error_number = eanx ( symbol , preprocessed ) ; break ;
case BARCODE_UPCE : error_number = eanx ( symbol , preprocessed ) ; break ;
case BARCODE_EANX : error_number = eanx ( symbol , preprocessed ) ; break ;
case BARCODE_EAN128 : error_number = ean_128 ( symbol , preprocessed ) ; break ;
case BARCODE_CODE39 : error_number = c39 ( symbol , preprocessed ) ; break ;
case BARCODE_PZN : error_number = pharmazentral ( symbol , preprocessed ) ; break ;
case BARCODE_EXCODE39 : error_number = ec39 ( symbol , preprocessed ) ; break ;
case BARCODE_CODABAR : error_number = codabar ( symbol , preprocessed ) ; break ;
case BARCODE_CODE93 : error_number = c93 ( symbol , preprocessed ) ; break ;
case BARCODE_LOGMARS : error_number = c39 ( symbol , preprocessed ) ; break ;
case BARCODE_CODE128 : error_number = code_128 ( symbol , preprocessed ) ; break ;
case BARCODE_CODE128B : error_number = code_128 ( symbol , preprocessed ) ; break ;
case BARCODE_NVE18 : error_number = nve_18 ( symbol , preprocessed ) ; break ;
case BARCODE_CODE11 : error_number = code_11 ( symbol , preprocessed ) ; break ;
case BARCODE_MSI_PLESSEY : error_number = msi_handle ( symbol , preprocessed ) ; break ;
case BARCODE_TELEPEN : error_number = telepen ( symbol , preprocessed ) ; break ;
case BARCODE_TELEPEN_NUM : error_number = telepen_num ( symbol , preprocessed ) ; break ;
case BARCODE_PHARMA : error_number = pharma_one ( symbol , preprocessed ) ; break ;
case BARCODE_PLESSEY : error_number = plessey ( symbol , preprocessed ) ; break ;
case BARCODE_ITF14 : error_number = itf14 ( symbol , preprocessed ) ; break ;
case BARCODE_FLAT : error_number = flattermarken ( symbol , preprocessed ) ; break ;
case BARCODE_FIM : error_number = fim ( symbol , preprocessed ) ; break ;
case BARCODE_POSTNET : error_number = post_plot ( symbol , preprocessed ) ; break ;
case BARCODE_PLANET : error_number = planet_plot ( symbol , preprocessed ) ; break ;
case BARCODE_RM4SCC : error_number = royal_plot ( symbol , preprocessed ) ; break ;
case BARCODE_AUSPOST : error_number = australia_post ( symbol , preprocessed ) ; break ;
case BARCODE_AUSREPLY : error_number = australia_post ( symbol , preprocessed ) ; break ;
case BARCODE_AUSROUTE : error_number = australia_post ( symbol , preprocessed ) ; break ;
case BARCODE_AUSREDIRECT : error_number = australia_post ( symbol , preprocessed ) ; break ;
case BARCODE_CODE16K : error_number = code16k ( symbol , preprocessed ) ; break ;
case BARCODE_PHARMA_TWO : error_number = pharma_two ( symbol , preprocessed ) ; break ;
case BARCODE_ONECODE : error_number = imail ( symbol , preprocessed ) ; break ;
case BARCODE_DATAMATRIX : error_number = dmatrix ( symbol , preprocessed ) ; break ;
case BARCODE_PDF417 : error_number = pdf417enc ( symbol , preprocessed ) ; break ;
case BARCODE_PDF417TRUNC : error_number = pdf417enc ( symbol , preprocessed ) ; break ;
case BARCODE_QRCODE : error_number = qr_code ( symbol , preprocessed ) ; break ;
case BARCODE_MICROPDF417 : error_number = micro_pdf417 ( symbol , preprocessed ) ; break ;
case BARCODE_ISBNX : error_number = eanx ( symbol , preprocessed ) ; break ;
case BARCODE_MAXICODE : error_number = maxicode ( symbol , preprocessed ) ; break ;
case BARCODE_RSS14 : error_number = rss14 ( symbol , preprocessed ) ; break ;
case BARCODE_RSS14STACK : error_number = rss14 ( symbol , preprocessed ) ; break ;
case BARCODE_RSS14STACK_OMNI : error_number = rss14 ( symbol , preprocessed ) ; break ;
case BARCODE_RSS_LTD : error_number = rsslimited ( symbol , preprocessed ) ; break ;
case BARCODE_RSS_EXP : error_number = rssexpanded ( symbol , preprocessed ) ; break ;
case BARCODE_RSS_EXPSTACK : error_number = rssexpanded ( symbol , preprocessed ) ; break ;
case BARCODE_EANX_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_EAN128_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_RSS14_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_RSS_LTD_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_RSS_EXP_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_UPCA_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_UPCE_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_RSS14STACK_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_RSS14_OMNI_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_RSS_EXPSTACK_CC : error_number = composite ( symbol , preprocessed ) ; break ;
case BARCODE_AZTEC : error_number = aztec ( symbol , preprocessed ) ; break ;
case BARCODE_KIX : error_number = kix_code ( symbol , preprocessed ) ; break ;
case BARCODE_CODE32 : error_number = code32 ( symbol , preprocessed ) ; break ;
case BARCODE_CODABLOCKF : error_number = codablock ( symbol , preprocessed ) ; break ;
case BARCODE_DAFT : error_number = daft_code ( symbol , preprocessed ) ; break ;
case BARCODE_EAN14 : error_number = ean_14 ( symbol , preprocessed ) ; break ;
case BARCODE_MICROQR : error_number = microqr ( symbol , preprocessed ) ; break ;
case BARCODE_AZRUNE : error_number = aztec_runes ( symbol , preprocessed ) ; break ;
case BARCODE_KOREAPOST : error_number = korea_post ( symbol , preprocessed ) ; break ;
2009-02-01 11:03:46 +13:00
case BARCODE_HIBC_128 : error_number = hibc ( symbol , preprocessed ) ; break ;
case BARCODE_HIBC_39 : error_number = hibc ( symbol , preprocessed ) ; break ;
case BARCODE_HIBC_DM : error_number = hibc ( symbol , preprocessed ) ; break ;
case BARCODE_HIBC_QR : error_number = hibc ( symbol , preprocessed ) ; break ;
case BARCODE_HIBC_PDF : error_number = hibc ( symbol , preprocessed ) ; break ;
case BARCODE_HIBC_MICPDF : error_number = hibc ( symbol , preprocessed ) ; break ;
case BARCODE_HIBC_BLOCKF : error_number = hibc ( symbol , preprocessed ) ; break ;
2009-03-15 00:04:52 +13:00
case BARCODE_JAPANPOST : error_number = japan_post ( symbol , preprocessed ) ; break ;
2009-05-10 23:47:20 +12:00
case BARCODE_CODE49 : error_number = code_49 ( symbol , preprocessed ) ; break ;
2009-05-17 02:19:43 +12:00
case BARCODE_CHANNEL : error_number = channel_code ( symbol , preprocessed ) ; break ;
2009-07-05 08:48:42 +12:00
case BARCODE_CODEONE : error_number = code_one ( symbol , preprocessed ) ; break ;
2008-07-14 09:15:55 +12:00
}
2009-02-20 08:09:57 +13:00
if ( ( symbol - > symbology = = BARCODE_CODE128 ) | | ( symbol - > symbology = = BARCODE_CODE128B ) ) {
ustrcpy ( symbol - > text , source ) ;
}
2008-10-05 18:51:58 +13:00
if ( error_number = = 0 ) {
error_number = error_buffer ;
}
error_tag ( symbol - > errtxt , error_number ) ;
2008-10-19 21:56:47 +13:00
/*printf("%s\n",symbol->text);*/
2008-09-16 19:39:47 +12:00
return error_number ;
2008-07-14 09:15:55 +12:00
}
int ZBarcode_Print ( struct zint_symbol * symbol )
{
2008-09-16 19:39:47 +12:00
int error_number ;
2008-07-14 09:15:55 +12:00
char output [ 4 ] ;
2008-10-10 00:29:09 +13:00
2009-07-09 20:28:00 +12:00
/*int i, j;
2009-06-01 08:33:54 +12:00
for ( i = 0 ; i < symbol - > rows ; i + + ) {
2009-06-18 22:20:23 +12:00
for ( j = 0 ; j < = symbol - > width / 7 ; j + + ) {
2009-06-01 08:33:54 +12:00
printf ( " %2.2X " , symbol - > encoded_data [ i ] [ j ] ) ;
}
printf ( " \n " ) ;
2009-07-09 20:28:00 +12:00
} */
2009-06-01 08:33:54 +12:00
2008-07-14 09:15:55 +12:00
if ( strlen ( symbol - > outfile ) > 3 ) {
output [ 0 ] = symbol - > outfile [ strlen ( symbol - > outfile ) - 3 ] ;
output [ 1 ] = symbol - > outfile [ strlen ( symbol - > outfile ) - 2 ] ;
output [ 2 ] = symbol - > outfile [ strlen ( symbol - > outfile ) - 1 ] ;
output [ 3 ] = ' \0 ' ;
2008-10-03 21:07:05 +13:00
to_upper ( ( unsigned char * ) output ) ;
2008-07-19 02:43:30 +12:00
# ifndef NO_PNG
2008-07-14 09:15:55 +12:00
if ( ! ( strcmp ( output , " PNG " ) ) ) {
2008-09-16 19:39:47 +12:00
error_number = png_handle ( symbol , 0 ) ;
2008-07-14 09:15:55 +12:00
} else {
2008-07-19 02:43:30 +12:00
# endif
2008-07-14 09:15:55 +12:00
if ( ! ( strcmp ( output , " EPS " ) ) ) {
2008-09-16 19:39:47 +12:00
error_number = ps_plot ( symbol ) ;
2008-07-14 09:15:55 +12:00
} else {
2009-02-18 06:48:39 +13:00
if ( ! ( strcmp ( output , " SVG " ) ) ) {
error_number = svg_plot ( symbol ) ;
} else {
2009-05-10 23:47:20 +12:00
strcpy ( symbol - > errtxt , " Unknown output format " ) ;
2009-02-18 06:48:39 +13:00
error_tag ( symbol - > errtxt , ERROR_INVALID_OPTION ) ;
return ERROR_INVALID_OPTION ;
}
2008-07-14 09:15:55 +12:00
}
2008-07-19 02:43:30 +12:00
# ifndef NO_PNG
2008-07-14 09:15:55 +12:00
}
2008-07-19 02:43:30 +12:00
# endif
2008-07-14 09:15:55 +12:00
} else {
2009-05-10 23:47:20 +12:00
strcpy ( symbol - > errtxt , " Unknown output format " ) ;
2008-10-05 18:51:58 +13:00
error_tag ( symbol - > errtxt , ERROR_INVALID_OPTION ) ;
2008-07-14 09:15:55 +12:00
return ERROR_INVALID_OPTION ;
}
2008-10-05 18:51:58 +13:00
error_tag ( symbol - > errtxt , error_number ) ;
2008-09-16 19:39:47 +12:00
return error_number ;
2008-07-14 09:15:55 +12:00
}
2008-09-03 07:41:14 +12:00
int ZBarcode_Print_Rotated ( struct zint_symbol * symbol , int rotate_angle )
2008-07-14 09:15:55 +12:00
{
2008-09-16 19:39:47 +12:00
int error_number ;
2008-09-03 07:41:14 +12:00
char output [ 4 ] ;
if ( strlen ( symbol - > outfile ) > 3 ) {
output [ 0 ] = symbol - > outfile [ strlen ( symbol - > outfile ) - 3 ] ;
output [ 1 ] = symbol - > outfile [ strlen ( symbol - > outfile ) - 2 ] ;
output [ 2 ] = symbol - > outfile [ strlen ( symbol - > outfile ) - 1 ] ;
output [ 3 ] = ' \0 ' ;
2008-10-03 21:07:05 +13:00
to_upper ( ( unsigned char * ) output ) ;
2008-10-10 00:29:09 +13:00
2008-09-18 06:24:15 +12:00
# ifndef NO_PNG
2008-09-03 07:41:14 +12:00
if ( ! ( strcmp ( output , " PNG " ) ) ) {
2008-09-16 19:39:47 +12:00
error_number = png_handle ( symbol , rotate_angle ) ;
2008-09-03 07:41:14 +12:00
} else {
2008-09-18 06:24:15 +12:00
# endif
2008-09-03 07:41:14 +12:00
if ( ! ( strcmp ( output , " EPS " ) ) ) {
2008-09-16 19:39:47 +12:00
error_number = ps_plot ( symbol ) ;
2008-09-03 07:41:14 +12:00
} else {
2009-05-20 02:04:19 +12:00
if ( ! ( strcmp ( output , " SVG " ) ) ) {
error_number = svg_plot ( symbol ) ;
} else {
strcpy ( symbol - > errtxt , " Unknown output format " ) ;
error_tag ( symbol - > errtxt , ERROR_INVALID_OPTION ) ;
return ERROR_INVALID_OPTION ;
}
2008-09-03 07:41:14 +12:00
}
2008-09-18 06:24:15 +12:00
# ifndef NO_PNG
2008-09-03 07:41:14 +12:00
}
2008-09-18 06:24:15 +12:00
# endif
2008-09-03 07:41:14 +12:00
} else {
2009-05-10 23:47:20 +12:00
strcpy ( symbol - > errtxt , " Unknown output format " ) ;
2008-10-05 18:51:58 +13:00
error_tag ( symbol - > errtxt , ERROR_INVALID_OPTION ) ;
2008-09-03 07:41:14 +12:00
return ERROR_INVALID_OPTION ;
}
2008-07-19 02:43:30 +12:00
2008-10-05 18:51:58 +13:00
error_tag ( symbol - > errtxt , error_number ) ;
2008-09-16 19:39:47 +12:00
return error_number ;
2008-09-03 07:41:14 +12:00
}
2008-07-19 02:43:30 +12:00
2008-09-03 07:41:14 +12:00
int ZBarcode_Encode_and_Print ( struct zint_symbol * symbol , unsigned char * input )
{
2008-09-16 19:39:47 +12:00
int error_number ;
2008-09-03 07:41:14 +12:00
2008-09-16 19:39:47 +12:00
error_number = 0 ;
2008-09-03 07:41:14 +12:00
2008-09-16 19:39:47 +12:00
error_number = ZBarcode_Encode ( symbol , input ) ;
if ( error_number ! = 0 ) {
return error_number ;
2008-07-14 09:15:55 +12:00
}
2008-09-16 19:39:47 +12:00
error_number = ZBarcode_Print ( symbol ) ;
return error_number ;
2008-07-14 09:15:55 +12:00
}
2008-09-03 07:41:14 +12:00
int ZBarcode_Encode_and_Print_Rotated ( struct zint_symbol * symbol , unsigned char * input , int rotate_angle )
{
2008-09-16 19:39:47 +12:00
int error_number ;
2008-09-03 07:41:14 +12:00
2008-09-16 19:39:47 +12:00
error_number = 0 ;
2008-09-03 07:41:14 +12:00
2008-09-16 19:39:47 +12:00
error_number = ZBarcode_Encode ( symbol , input ) ;
if ( error_number ! = 0 ) {
return error_number ;
2008-09-03 07:41:14 +12:00
}
2008-09-16 19:39:47 +12:00
error_number = ZBarcode_Print_Rotated ( symbol , rotate_angle ) ;
return error_number ;
2008-09-03 07:41:14 +12:00
}
2009-08-21 01:20:59 +12:00
int ZBarcode_Encode_from_File ( struct zint_symbol * symbol , char * filename )
{
int i ;
FILE * file ;
unsigned char * buffer ;
unsigned long fileLen ;
2009-08-27 07:06:19 +12:00
unsigned char used_characters [ 256 ] ;
2009-08-21 01:20:59 +12:00
file = fopen ( filename , " rb " ) ;
if ( ! file ) {
strcpy ( symbol - > errtxt , " Unable to read input file " ) ;
return ERROR_INVALID_DATA ;
}
/* Get file length */
fseek ( file , 0 , SEEK_END ) ;
fileLen = ftell ( file ) ;
fseek ( file , 0 , SEEK_SET ) ;
if ( fileLen > 7100 ) {
/* The largest amount of data that can be encoded is 7089 numeric digits in QR Code */
strcpy ( symbol - > errtxt , " Input file too long " ) ;
fclose ( file ) ;
return ERROR_INVALID_DATA ;
}
/* Allocate memory */
buffer = ( unsigned char * ) malloc ( ( fileLen + 1 ) * sizeof ( unsigned char ) ) ;
if ( ! buffer ) {
strcpy ( symbol - > errtxt , " Internal memory error " ) ;
fclose ( file ) ;
return ERROR_MEMORY ;
}
/* Read file contents into buffer */
fread ( buffer , fileLen , 1 , file ) ;
fclose ( file ) ;
if ( symbol - > input_mode = = DATA_MODE ) {
/* Look for and correctly handle NULL characters */
for ( i = 0 ; i < = 255 ; i + + ) { used_characters [ i ] = 0 ; }
for ( i = 0 ; i < ( int ) fileLen ; i + + ) { used_characters [ ( int ) buffer [ i ] ] = 1 ; }
if ( used_characters [ 0 ] = = 1 ) { /* only if data contains a NULL */
/* determine first unused character > 0 */
i = 1 ;
while ( i < 255 ) {
if ( used_characters [ i ] = = 0 ) {
symbol - > nullchar = ( char ) i ;
break ;
}
i + + ;
}
if ( symbol - > nullchar = = 0x00 ) {
/* No candidate character could be found. Quit */
strcpy ( symbol - > errtxt , " Could not encode NULL character " ) ;
return ERROR_INVALID_OPTION ;
}
for ( i = 0 ; i < fileLen ; i + + ) {
if ( buffer [ i ] = = 0x00 ) {
buffer [ i ] = symbol - > nullchar ;
}
}
}
}
return ZBarcode_Encode ( symbol , buffer ) ;
}
int ZBarcode_Encode_from_File_and_Print ( struct zint_symbol * symbol , char * filename , int rotate_angle )
{
int error_number ;
error_number = 0 ;
error_number = ZBarcode_Encode_from_File ( symbol , filename ) ;
if ( error_number ! = 0 ) {
return error_number ;
}
return ZBarcode_Print_Rotated ( symbol , rotate_angle ) ;
}