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.
54{
55
56 const char *p, *q;
57 char pct[3] = {0};
58
59 assert(data);
60
61 p = data;
62 q = data + strlen(data);
63
64 /*
65 * Find each instance of "%" in the data and ensure that there are at
66 * least two following characters and that these two characters
67 * represent a hex value.
68 *
69 */
70 while (p != q && (p = strchr(p, '%')) != NULL) {
71
72 if (q - p < 3)
75 (size_t)(p - data),
76 (size_t)(q - p)
77 );
78
79 memcpy(pct, p + 1, 2);
80 if (strspn(pct, "0123456789ABCDEFabcdef") != 2)
83 (size_t)(p - data),
84 3
85 );
86
87 p += 3;
88
89 }
90
92
93}
#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:77
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:62
@ GS1_LINTER_INVALID_PERCENT_SEQUENCE
The input contains an invalid percent hex-encoding "%hh" sequence.
Definition gs1syntaxdictionary.h:122