GS1 Barcode Syntax Tests reference
A reference to the AI component "linter" routines referred to by the GS1 Barcode Syntax Dictionary. Copyright (c) 2022-2024 GS1 AISBL.
lint_pieceoftotal.c File Reference

Purpose

The pieceoftotal linter ensures that the data represents a meaningful piece of total, i.e. a concatenation of non-zero piece number follow by an equal-width, non-zero total number of pieces.

Functional Description

◆ gs1_lint_pieceoftotal()

GS1_SYNTAX_DICTIONARY_API gs1_lint_err_t gs1_lint_pieceoftotal ( const char *const  data,
size_t  data_len,
size_t *const  err_pos,
size_t *const  err_len 
)

Used to ensure that an AI component conforms to a PPTT format, where PP and TT have equal width.

Parameters
[in]dataPointer to the data to be linted. Must not be NULL.
[in]data_lenLength of the data to be linted.
[out]err_posTo facilitate error highlighting, the start position of the bad data is written to this pointer, if not NULL.
[out]err_lenThe length of the bad data is written to this pointer, if not NULL.
Returns
GS1_LINTER_OK if okay.
GS1_LINTER_INVALID_LENGTH_FOR_PIECE_OF_TOTAL if the data is not an even length.
GS1_LINTER_NON_DIGIT_CHARACTER if the data contains a non-digit character.
GS1_LINTER_ZERO_PIECE_NUMBER if the data contains a piece number with a zero value.
GS1_LINTER_ZERO_TOTAL_PIECES if the data contains a total piece count with a zero value.
GS1_LINTER_PIECE_NUMBER_EXCEEDS_TOTAL if the data contains a piece number that is larger than the total piece count.
56{
57
58/// \cond
59#define P(i) data[i]
60#define T(i) data[data_len / 2 + i]
61/// \endcond
62
63 size_t pos, i;
64 int pieceiszero, totaliszero, compare;
65
66 assert(data);
67
68 /*
69 * Data must consist of all digits.
70 *
71 */
72 for (pos = 0; pos < data_len; pos++)
73 if (GS1_LINTER_UNLIKELY(data[pos] < '0' || data[pos] > '9'))
76 pos,
77 1
78 );
79
80 /*
81 * Data must be a non-zero, even number of characters.
82 *
83 */
84 if (GS1_LINTER_UNLIKELY(data_len == 0 || data_len % 2 != 0))
87 0,
88 data_len
89 );
90
91 /*
92 * Determine whether either the piece number or total piece count is
93 * zero and whether the piece number exceeds the total piece count.
94 *
95 */
96 pieceiszero = totaliszero = 1;
97 compare = 0; /* -1:P<T ; 0:P==T ; 1:P>T */
98 for (i = 0; i < data_len / 2; i++) {
99 if (pieceiszero && P(i) != '0') pieceiszero = 0;
100 if (totaliszero && T(i) != '0') totaliszero = 0;
101 if (!compare && P(i) != T(i)) compare = P(i) < T(i) ? -1 : 1;
102 }
103
104 /*
105 * Neither piece nor total may be zero.
106 *
107 */
108 if (GS1_LINTER_UNLIKELY(pieceiszero || totaliszero))
111 pieceiszero ? 0 : data_len / 2,
112 data_len / 2
113 );
114
115 /*
116 * The piece number must not exceed the total piece count.
117 *
118 */
119 if (GS1_LINTER_UNLIKELY(compare == 1))
122 0,
123 data_len
124 );
125
127
128}
#define GS1_LINTER_UNLIKELY(x)
Implementation may provide hint to the compiler that the expression is likely to be false.
Definition gs1syntaxdictionary-utils.h:76
#define GS1_LINTER_RETURN_ERROR(error, position, length)
Return from a linter indicating that a problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:103
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:88
@ GS1_LINTER_PIECE_NUMBER_EXCEEDS_TOTAL
The piece number must not exceed the piece total.
Definition gs1syntaxdictionary.h:121
@ GS1_LINTER_ZERO_PIECE_NUMBER
The piece number must not have a value of zero.
Definition gs1syntaxdictionary.h:119
@ GS1_LINTER_INVALID_LENGTH_FOR_PIECE_OF_TOTAL
The piece with total must have an even length, having equal-length components.
Definition gs1syntaxdictionary.h:118
@ GS1_LINTER_ZERO_TOTAL_PIECES
The piece total must not have a value of zero.
Definition gs1syntaxdictionary.h:120
@ GS1_LINTER_NON_DIGIT_CHARACTER
A non-digit character was found where a digit is expected.
Definition gs1syntaxdictionary.h:78