-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check that bitmap is actually a PNG file
- Loading branch information
1 parent
065d482
commit 6943142
Showing
5 changed files
with
58 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
class PNG(bytes): | ||
|
||
# The first eight bytes of a PNG file always contain the following (decimal) values: | ||
# 137 80 78 71 13 10 26 10 | ||
# https://www.w3.org/TR/PNG-Structure.html | ||
SIGNATURE = b"\x89PNG\r\n\x1a\n" | ||
|
||
def __new__(cls, *args, **kwargs): | ||
self = super().__new__(cls, *args, **kwargs) | ||
header = self[:8] | ||
if header != cls.SIGNATURE: | ||
raise ValueError("Invalid PNG file: bad signature {header!r}") | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from nanoemoji.png import PNG | ||
import pytest | ||
|
||
|
||
def test_good_png_signature(): | ||
# example from https://en.wikipedia.org/wiki/Portable_Network_Graphics | ||
data = bytes( | ||
int(b, 16) | ||
for b in ( | ||
"89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 " | ||
"00 00 00 01 00 00 00 01 08 02 00 00 00 90 77 53 " | ||
"DE 00 00 00 0C 49 44 41 54 08 D7 63 F8 CF C0 00 " | ||
"00 03 01 01 00 18 DD 8D B0 00 00 00 00 49 45 4E " | ||
"44 AE 42 60 82" | ||
).split() | ||
) | ||
assert PNG(data) == data | ||
|
||
|
||
def test_bad_png_signature(): | ||
with pytest.raises(ValueError, match="bad signature"): | ||
PNG(b"<?xml") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters