picqer/php-barcode-generator

Invalid XML when using SVG generator for type Code 93

Bernhard-Krop opened this issue · 2 comments

Hi there!

I've found another error. When using the SVG generator to export a Code 93 barcode as SVG, the exported XML contains error, if the content of the barcode contains extended characters (there is no problem when using the basic characters).

The problem has two sources. Removing one of them would remove the problem.

The first source is the encoding of extended characters in \Picqer\Barcode\Types\TypeCode93::getBarcodeData(). Extended characters are using helping characters defined in the basic character set. These helping characters have been defined in $encoding by using chr() with numbers from 128 to 131. chr() produces (at least in my environment) when concatenating the string "????" (because they are unknown).

The second source is the desc-tag inside the XML. It uses $barcodeData->getBarcode() as content and - in combination with the first source - fills it with the damaged helping characters. When using the created XML, I get an error that the XML is damaged due to these characters.

The first source could be fixed by using other characters as helping characters (E.g. 'a', 'b', 'c' and 'd', of which the character codes are below 128 and would work. Because they are not part of the basic character set for Code 93, they could be used as helping characters).

The second source could be fixed by either removing the desc-tag from XML, or by injecting the encoded string instead of the decoded string of the barcode's content (e.g. 'a' instead of '?A').

I would suggest to remove both problem sources. The first one, because it could make problems in other situations. The second one using the second approach, because it could make problems with other types of barcodes.

Kind regards,

Bernhard

EDIT 1:

I just found the same problem using type Code 128. When a control character is used in the barcode's content, it is injected into the XML's desc-tag (causing errors). Control characters should be stripped from string before injecting it into the desc-tag.

@casperbakker I just created a PR for this issue.

I implemented my suggestion: I've fixed the first source and the second source using the second approach.

After fixing them, I tested the code using each single character (excluding control characters) of the ASCII table. By doing that, I've found some other issues, which I fixed "on the fly". Code 93 Full ASCII (as it was implemented) is now working fine and creating correct barcodes, which can be successfully scanned by my barcode scanner.

@Bernhard-Krop Great. Thank you so much!