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

Purpose

The pcenc linter ensures that the data has correct percent-encoding.

Remarks
Percent-encoding is defined by RFC 3986: Uniform Resource Identifier (URI): Generic Syntax section "2.1. Percent-Encoding".

Functional Description

◆ gs1_lint_pcenc()

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

Used to ensure that an AI component conforms with correct percent encoding.

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_PERCENT_SEQUENCE if the data contains an invalid percent sequence.
55{
56
57 const char *p;
58
59 assert(data);
60
61 p = data;
62
63 /*
64 * Find each instance of "%" in the data and ensure that there are at
65 * least two following characters and that these two characters
66 * represent a hex value.
67 *
68 */
69 while ((p = strchr(p, '%')) != NULL) {
70
71 if (GS1_LINTER_UNLIKELY(!p[1] || !p[2])) {
72 size_t remaining = 0;
73 if (p[1]) remaining = p[2] ? 2 : 1;
76 (size_t)(p - data),
77 remaining + 1
78 );
79 }
80
81 if (GS1_LINTER_UNLIKELY(!isxdigit(p[1]) || !isxdigit(p[2])))
84 (size_t)(p - data),
85 3
86 );
87
88 p += 3;
89
90 }
91
93
94}
#define GS1_LINTER_UNLIKELY(x)
Implemention 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_INVALID_PERCENT_SEQUENCE
The input contains an invalid percent hex-encoding "%hh" sequence.
Definition gs1syntaxdictionary.h:122