GS1 Syntax Dictionary: Linter reference
A reference to the AI component linter routines referred to by the GS1 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 *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 null-terminated data to be linted. Must not be NULL.
[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.
57{
58
59 /*
60 * All characters in "CSET 64", the 64 character alphabet.
61 *
62 */
63 static const char* const cset64 =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65 "abcdefghijklmnopqrstuvwxyz"
66 "0123456789-_";
67
68 size_t pads, len, pos;
69
70 assert(data);
71
72 /*
73 * Count the number of padding characters, which are optional.
74 *
75 */
76 for (pads = 0, len = strlen(data);
77 len > 0 && data[len-1] == '=';
78 pads++, len--);
79
80 if (pads > 2 || (pads > 0 && (len + pads) % 3 != 0)) {
81 if (err_pos) *err_pos = len;
82 if (err_len) *err_len = pads;
84 }
85
86 /*
87 * In what remains, any character outside of CSET 64 is illegal.
88 *
89 */
90 if ((pos = strspn(data, cset64)) < len) {
91 if (err_pos) *err_pos = pos;
92 if (err_len) *err_len = 1;
94 }
95
96 return GS1_LINTER_OK;
97
98}
@ GS1_LINTER_INVALID_CSET64_PADDING
Incorrect number of CSET 64 pad characters.
Definition gs1syntaxdictionary.h:174
@ GS1_LINTER_INVALID_CSET64_CHARACTER
A non-CSET 64 character was found where a CSET 64 character is expected.
Definition gs1syntaxdictionary.h:173
@ GS1_LINTER_OK
No issues were detected by the linter.
Definition gs1syntaxdictionary.h:66