Skip to content

Commit cb5b152

Browse files
committed
Files missing from the previous commit added
1 parent 3f5a315 commit cb5b152

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

src/quickRdfIo/StreamSkipBomTrait.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* The MIT License
5+
*
6+
* Copyright 2024 zozlak.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
namespace quickRdfIo;
28+
29+
use Psr\Http\Message\StreamInterface;
30+
31+
/**
32+
* Description of StreamSkipBomTrait
33+
*
34+
* @author zozlak
35+
*/
36+
trait StreamSkipBomTrait {
37+
38+
private $invalidBoms2B = [
39+
"\xEF\xFF" => "UTF-16 BE",
40+
"\xFF\xFE" => "UTF-16 LE",
41+
];
42+
private $invalidBoms3B = [
43+
"\x2B\x2F\x76" => "UTF-7",
44+
"\xF7\x64\x4C" => "UTF-1",
45+
"\x0E\xFE\xFF" => "SCSU",
46+
"\xFB\xEE\x28" => "BOCU-1",
47+
];
48+
private $invalidBoms4B = [
49+
"\x00\x00\xFE\xFF" => "UTF-32 BE",
50+
"\xFF\xFE\x00\x00" => "UTF-32 LE",
51+
"\xDD\x73\x66\x73" => "UTF-EBCDIC",
52+
"\x84\x31\x95\x33" => "GB18030",
53+
];
54+
private $bomUtf8 = "\xEF\xBB\xBF";
55+
56+
private function skipBom(StreamInterface $stream): void {
57+
if ($stream->isSeekable()) {
58+
$bom = $stream->read(4);
59+
if (isset($this->invalidBoms4B[$bom])) {
60+
throw new RdfIoException("Input stream has wrong encoding " . $this->invalidBoms4B[$bom]);
61+
}
62+
$bom = substr($bom, 0, 3);
63+
if ($bom === $this->bomUtf8) {
64+
$stream->seek(-1, SEEK_CUR);
65+
return;
66+
}
67+
if (isset($this->invalidBoms3B[$bom])) {
68+
throw new RdfIoException("Input stream has wrong encoding " . $this->invalidBoms3B[$bom]);
69+
}
70+
$bom = substr($bom, 0, 2);
71+
if (isset($this->invalidBoms2B[$bom])) {
72+
throw new RdfIoException("Input stream has wrong encoding " . $this->invalidBoms2B[$bom]);
73+
}
74+
// no BOM recognized - rewind
75+
$stream->seek(-4, SEEK_CUR);
76+
}
77+
}
78+
}

tests/files/issue10_utf16be.nq

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��<http://foo> <http://bar> <http://baz> .

tests/files/issue10_utf32le.nq

44 Bytes
Binary file not shown.

tests/files/issue10_utf7.nq

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+/v<http://foo> <http://bar> <http://baz> .

tests/files/issue10_utf8.nq

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<http://foo> <http://bar> <http://baz> .

0 commit comments

Comments
 (0)