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_cset64.c File Reference

Purpose

The cset64 linter ensures that the given data contains only characters within the GS1 AI encodable character set 64 ("CSET 64").

Remarks
CSET 64 is defined in the GS1 General Specifications table "GS1 AI encodable character set 64 (file-safe / URI-safe base64)".

Functional Description

◆ gs1_lint_cset64()

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

Used to validate that an AI component conforms to Z..99 syntax, and has valid padding if provided.

Note: The length of the component is validated by the framework that calls this function.

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_CSET64_CHARACTER if the data contains a non-CSET 64 character.
60{
61
62 /*
63 * CSET 64 characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
64 *
65 */
66 static const uint64_t cset64_bitfield[] = {
67 0x000000000004ffc0, // 0-9, "-"
68 0x7fffffe17fffffe0, // A-Z, "_", a-z
69 0x0000000000000000,
70 0x0000000000000000
71 };
72
73 size_t pads, len, pos;
74
75 assert(data);
76
77
78 /*
79 * Count padding characters from the end
80 *
81 */
82 for (pads = 0; pads < data_len && data[data_len - pads - 1] == '='; pads++);
83 len = data_len - pads;
84
85 if (GS1_LINTER_UNLIKELY(pads > 2 || (pads > 0 && data_len % 3 != 0)))
88 len,
89 pads
90 );
91
92 /*
93 * Validate character set using bitfield lookup
94 *
95 */
96 for (pos = 0; pos < len; pos++) {
97 int valid;
98 GS1_LINTER_BITFIELD_LOOKUP((unsigned char)data[pos], cset64_bitfield, valid);
99 if (GS1_LINTER_UNLIKELY(!valid))
102 pos,
103 1
104 );
105 }
106
108
109}
#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_BITFIELD_LOOKUP(bit, field, valid)
Perform a lookup of a position in a bit field.
Definition gs1syntaxdictionary-utils.h:124
#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_INVALID_CSET64_PADDING
Incorrect number of CSET 64 pad characters.
Definition gs1syntaxdictionary.h:186
@ GS1_LINTER_INVALID_CSET64_CHARACTER
A non-CSET 64 character was found where a CSET 64 character is expected.
Definition gs1syntaxdictionary.h:185