mirror of
https://github.com/zint/zint
synced 2024-11-16 20:57:25 +13:00
Improved GS1 validation
This commit is contained in:
parent
d2d4e30e32
commit
38b8de26e4
@ -26,36 +26,70 @@
|
|||||||
int gs1_verify(struct zint_symbol *symbol, unsigned char source[], char reduced[])
|
int gs1_verify(struct zint_symbol *symbol, unsigned char source[], char reduced[])
|
||||||
{
|
{
|
||||||
int i, j, last_ai, ai_latch;
|
int i, j, last_ai, ai_latch;
|
||||||
char ai_string[4];
|
char ai_string[6];
|
||||||
|
int bracket_level, max_bracket_level, ai_length, max_ai_length, min_ai_length;
|
||||||
|
|
||||||
/* Detect extended ASCII characters */
|
/* Detect extended ASCII characters */
|
||||||
for(i = 0; i < sourcelen; i++) {
|
for(i = 0; i < ustrlen(source); i++) {
|
||||||
if(source[i] >=128) {
|
if(source[i] >=128) {
|
||||||
strcpy(symbol->errtxt, "Extended ASCII characters are not supported by GS1");
|
strcpy(symbol->errtxt, "Extended ASCII characters are not supported by GS1");
|
||||||
return ERROR_INVALID_DATA;
|
return ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start with basic tests */
|
|
||||||
if(source[0] != '[') {
|
if(source[0] != '[') {
|
||||||
strcpy(symbol->errtxt, "Data does not start with an AI");
|
strcpy(symbol->errtxt, "Data does not start with an AI");
|
||||||
return ERROR_INVALID_DATA;
|
return ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i < ustrlen(source) - 1; i++) {
|
/* Check the position of the brackets */
|
||||||
if((source[i] == '[') && (source[i + 1] == '[')) {
|
bracket_level = 0;
|
||||||
/* Can't have nested brackets - Quit */
|
max_bracket_level = 0;
|
||||||
strcpy(symbol->errtxt, "Nested AI detected (two or more open brackets)");
|
ai_length = 0;
|
||||||
return ERROR_INVALID_DATA;
|
max_ai_length = 0;
|
||||||
}
|
min_ai_length = 5;
|
||||||
|
j = 0;
|
||||||
|
for(i = 0; i < ustrlen(source); i++) {
|
||||||
|
ai_length += j;
|
||||||
|
if(source[i] == '[') { bracket_level++; j = 1; }
|
||||||
|
if(source[i] == ']') {
|
||||||
|
bracket_level--;
|
||||||
|
if(ai_length < min_ai_length) { min_ai_length = ai_length; }
|
||||||
|
j = 0;
|
||||||
|
ai_length = 0; }
|
||||||
|
if(bracket_level > max_bracket_level) { max_bracket_level = bracket_level; }
|
||||||
|
if(ai_length > max_ai_length) { max_ai_length = ai_length; }
|
||||||
|
}
|
||||||
|
min_ai_length--;
|
||||||
|
|
||||||
|
if(bracket_level != 0) {
|
||||||
|
/* Not all brackets are closed */
|
||||||
|
strcpy(symbol->errtxt, "Malformed AI in input data (brackets don\'t match)");
|
||||||
|
return ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i < ustrlen(source) - 1; i++) {
|
if(max_bracket_level > 1) {
|
||||||
if((source[i] == ']') && (source[i + 1] == ']')) {
|
/* Nested brackets */
|
||||||
/* Can't have nested brackets - Quit */
|
strcpy(symbol->errtxt, "Found nested brackets in input data");
|
||||||
strcpy(symbol->errtxt, "Nested AI detected (two or more close brackets)");
|
return ERROR_INVALID_DATA;
|
||||||
return ERROR_INVALID_DATA;
|
}
|
||||||
}
|
|
||||||
|
if(max_ai_length > 4) {
|
||||||
|
/* AI is too long */
|
||||||
|
strcpy(symbol->errtxt, "Invalid AI in input data (AI too long)");
|
||||||
|
return ERROR_INVALID_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(min_ai_length <= 1) {
|
||||||
|
/* AI is too short */
|
||||||
|
strcpy(symbol->errtxt, "Invalid AI in input data (AI too short)");
|
||||||
|
return ERROR_INVALID_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ai_latch == 1) {
|
||||||
|
/* Non-numeric data in AI */
|
||||||
|
strcpy(symbol->errtxt, "Invalid AI in input data (non-numeric characters in AI)");
|
||||||
|
return ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Resolve AI data - put resulting string in 'reduced' */
|
/* Resolve AI data - put resulting string in 'reduced' */
|
||||||
|
Loading…
Reference in New Issue
Block a user