| 1 | #cxx . . $ `<file>/t/opus.cc`/include <opustags.h>cxx . . $ `<file>/src/opustags.h`/ |
| 2 | #include "tap.h"cxx . . $ `<file>/t/tap.h`/ |
| 3 | |
| 4 | #include <string.h> |
| 5 | |
| 6 | static const char standard_OpusTagscxx . . $ standard_OpusTags.[] = |
| 7 | "OpusTags" |
| 8 | "\x14\x00\x00\x00" "opustags test packet" |
| 9 | "\x02\x00\x00\x00" |
| 10 | "\x09\x00\x00\x00" "TITLE=Foo" |
| 11 | "\x0a\x00\x00\x00" "ARTIST=Bar"; |
| 12 | |
| 13 | static void parse_standardcxx . . $ parse_standard(49f6e7a06ebc5aa8).() |
| 14 | { |
| 15 | ogg_packet op; |
| 16 | op.bytes = sizeof(standard_OpusTagscxx . . $ standard_OpusTags.) - 1; |
| 17 | op.packet = (unsigned char*) standard_OpusTagscxx . . $ standard_OpusTags.; |
| 18 | otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# tags = otcxx . . $ ot/::parse_tagscxx . . $ ot/parse_tags(eaf0a27cbd141b32).(op); |
| 19 | if (tags.vendorcxx . . $ ot/opus_tags#vendor. != u8"opustags test packet") |
| 20 | throw failurecxx . . $ tap/failure#("bad vendor string"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 21 | if (tags.commentscxx . . $ ot/opus_tags#comments..sizecxx . . $ std/__cxx11/list#size(c9b1e749b64d5edc).() != 2) |
| 22 | throw failurecxx . . $ tap/failure#("bad number of comments"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 23 | auto it = tags.commentscxx . . $ ot/opus_tags#comments..begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(); |
| 24 | if (*cxx . . $ std/_List_iterator#`operator*`(d426bc7b625886b7).it != u8"TITLE=Foo") |
| 25 | throw failurecxx . . $ tap/failure#("bad title"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 26 | ++cxx . . $ std/_List_iterator#operator++(70483be15bedaca0).it; |
| 27 | if (*cxx . . $ std/_List_iterator#`operator*`(d426bc7b625886b7).it != u8"ARTIST=Bar") |
| 28 | throw failurecxx . . $ tap/failure#("bad artist"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 29 | if (tags.extra_datacxx . . $ ot/opus_tags#extra_data..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() != 0) |
| 30 | throw failurecxx . . $ tap/failure#("found mysterious padding data"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 31 | } |
| 32 | |
| 33 | static otcxx . . $ ot/::statuscxx . . $ ot/status# try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(const ogg_packet& packet) |
| 34 | { |
| 35 | try { |
| 36 | otcxx . . $ ot/::parse_tagscxx . . $ ot/parse_tags(eaf0a27cbd141b32).(packet); |
| 37 | return otcxx . . $ ot/status#status(570d951ce3a7f975).::stcxx . . $ ot/st#::okcxx . . $ ot/st#ok.; |
| 38 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 39 | return rc; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Try parse_tags with packets that should not valid, or that might even |
| 45 | * corrupt the memory. Run this one with valgrind to ensure we're not |
| 46 | * overflowing. |
| 47 | */ |
| 48 | static void parse_corruptedcxx . . $ parse_corrupted(49f6e7a06ebc5aa8).() |
| 49 | { |
| 50 | size_t size = sizeof(standard_OpusTagscxx . . $ standard_OpusTags.); |
| 51 | char packet[size]; |
| 52 | memcpy(packet, standard_OpusTagscxx . . $ standard_OpusTags., size); |
| 53 | otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# tags; |
| 54 | ogg_packet op; |
| 55 | op.packet = (unsigned char*) packet; |
| 56 | op.bytes = size; |
| 57 | |
| 58 | char* header_data = packet; |
| 59 | char* vendor_length = header_data + 8; |
| 60 | char* vendor_string = vendor_length + 4; |
| 61 | char* comment_count = vendor_string + *vendor_length; |
| 62 | char* first_comment_length = comment_count + 4; |
| 63 | char* first_comment_data = first_comment_length + 4; |
| 64 | char* end = packet + size; |
| 65 | |
| 66 | op.bytes = 7; |
| 67 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::cut_magic_numbercxx . . $ ot/st#cut_magic_number.) |
| 68 | throw failurecxx . . $ tap/failure#("did not detect the overflowing magic number"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 69 | op.bytes = 11; |
| 70 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::cut_vendor_lengthcxx . . $ ot/st#cut_vendor_length.) |
| 71 | throw failurecxx . . $ tap/failure#("did not detect the overflowing vendor string length"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 72 | op.bytes = size; |
| 73 | |
| 74 | header_data[0] = 'o'; |
| 75 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::bad_magic_numbercxx . . $ ot/st#bad_magic_number.) |
| 76 | throw failurecxx . . $ tap/failure#("did not detect the bad magic number"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 77 | header_data[0] = 'O'; |
| 78 | |
| 79 | *vendor_length = end - vendor_string + 1; |
| 80 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::cut_vendor_datacxx . . $ ot/st#cut_vendor_data.) |
| 81 | throw failurecxx . . $ tap/failure#("did not detect the overflowing vendor string"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 82 | *vendor_length = end - vendor_string - 3; |
| 83 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::cut_comment_countcxx . . $ ot/st#cut_comment_count.) |
| 84 | throw failurecxx . . $ tap/failure#("did not detect the overflowing comment count"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 85 | *vendor_length = comment_count - vendor_string; |
| 86 | |
| 87 | ++*comment_count; |
| 88 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::cut_comment_lengthcxx . . $ ot/st#cut_comment_length.) |
| 89 | throw failurecxx . . $ tap/failure#("did not detect the overflowing comment length"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 90 | *first_comment_length = end - first_comment_data + 1; |
| 91 | if (try_parse_tagscxx . . $ try_parse_tags(c28f74449640098b).(op) != otcxx . . $ ot/::stcxx . . $ ot/st#::cut_comment_datacxx . . $ ot/st#cut_comment_data.) |
| 92 | throw failurecxx . . $ tap/failure#("did not detect the overflowing comment data"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 93 | } |
| 94 | |
| 95 | static void recode_standardcxx . . $ recode_standard(49f6e7a06ebc5aa8).() |
| 96 | { |
| 97 | ogg_packet op; |
| 98 | op.bytes = sizeof(standard_OpusTagscxx . . $ standard_OpusTags.) - 1; |
| 99 | op.packet = (unsigned char*) standard_OpusTagscxx . . $ standard_OpusTags.; |
| 100 | otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# tags = otcxx . . $ ot/::parse_tagscxx . . $ ot/parse_tags(eaf0a27cbd141b32).(op); |
| 101 | auto packet = otcxx . . $ ot/::render_tagscxx . . $ ot/render_tags(67259994f473118f).(tags); |
| 102 | if (packet.b_o_s != 0) |
| 103 | throw failurecxx . . $ tap/failure#("b_o_s should not be set"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 104 | if (packet.e_o_s != 0) |
| 105 | throw failurecxx . . $ tap/failure#("e_o_s should not be set"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 106 | if (packet.granulepos != 0) |
| 107 | throw failurecxx . . $ tap/failure#("granule_post should be 0"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 108 | if (packet.packetno != 1) |
| 109 | throw failurecxx . . $ tap/failure#("packetno should be 1"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 110 | if (packet.bytes != sizeof(standard_OpusTagscxx . . $ standard_OpusTags.) - 1) |
| 111 | throw failurecxx . . $ tap/failure#("the packet is not the right size"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 112 | if (memcmp(packet.packet, standard_OpusTagscxx . . $ standard_OpusTags., packet.bytes) != 0) |
| 113 | throw failurecxx . . $ tap/failure#("the rendered packet is not what we expected"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 114 | } |
| 115 | |
| 116 | static void recode_paddingcxx . . $ recode_padding(49f6e7a06ebc5aa8).() |
| 117 | { |
| 118 | stdcxx . . $ std/::stringcxx . . $ std/string# padded_OpusTagscxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).(standard_OpusTagscxx . . $ standard_OpusTags., sizeof(standard_OpusTagscxx . . $ standard_OpusTags.)); |
| 119 | // ^ note: padded_OpusTags ends with a null byte here |
| 120 | padded_OpusTags +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "hello"; |
| 121 | ogg_packet op; |
| 122 | op.bytes = padded_OpusTags.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).(); |
| 123 | op.packet = (unsigned char*) padded_OpusTags.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(); |
| 124 | |
| 125 | otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# tags = otcxx . . $ ot/::parse_tagscxx . . $ ot/parse_tags(eaf0a27cbd141b32).(op); |
| 126 | if (tags.extra_datacxx . . $ ot/opus_tags#extra_data. != "\0hello"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367).) |
| 127 | throw failurecxx . . $ tap/failure#("corrupted extra data"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 128 | // recode the packet and ensure it's exactly the same |
| 129 | auto packet = otcxx . . $ ot/::render_tagscxx . . $ ot/render_tags(67259994f473118f).(tags); |
| 130 | if (static_cast<size_t>(packet.bytes) < padded_OpusTags.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).()) |
| 131 | throw failurecxx . . $ tap/failure#("the packet was truncated"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 132 | if (static_cast<size_t>(packet.bytes) > padded_OpusTags.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).()) |
| 133 | throw failurecxx . . $ tap/failure#("the packet got too big"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 134 | if (memcmp(packet.packet, padded_OpusTags.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(), packet.bytes) != 0) |
| 135 | throw failurecxx . . $ tap/failure#("the rendered packet is not what we expected"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 136 | } |
| 137 | |
| 138 | static void extract_covercxx . . $ extract_cover(49f6e7a06ebc5aa8).() |
| 139 | { |
| 140 | otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# picture_data = ""_bsvcxx . . $ `operator""_bsv`(630326791a3f0367). |
| 141 | "\x00\x00\x00\x03" // Picture type 3. |
| 142 | "\x00\x00\x00\x09" "image/foo" // MIME type. |
| 143 | "\x00\x00\x00\x00" "" // Description. |
| 144 | "\x00\x00\x00\x00" // Width. |
| 145 | "\x00\x00\x00\x00" // Height. |
| 146 | "\x00\x00\x00\x00" // Color depth. |
| 147 | "\x00\x00\x00\x00" // Palette size. |
| 148 | "\x00\x00\x00\x0C" "Picture data"; |
| 149 | |
| 150 | otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# tags; |
| 151 | tags.commentscxx . . $ ot/opus_tags#comments. =cxx . . $ std/__cxx11/list#`operator=`(e3262da308648e9d). { u8"METADATA_BLOCK_PICTURE=" +cxx . . $ std/operator+(39ed2c165380f78e). otcxx . . $ ot/::encode_base64cxx . . $ ot/encode_base64(86a0a94dd06bebac).(picture_datacxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).) }; |
| 152 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<otcxx . . $ ot/::picturecxx . . $ ot/picture#> cover = otcxx . . $ ot/::extract_covercxx . . $ ot/extract_cover(757f97eae4ffafe0).(tags); |
| 153 | if (!cover) |
| 154 | throw failurecxx . . $ tap/failure#("could not extract the cover"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 155 | if (covercxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).->cxx . . $ std/optional#`operator->`(258ee1c9e0a18a5c).mime_typecxx . . $ ot/picture#mime_type. != "image/foo"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367).) |
| 156 | throw failurecxx . . $ tap/failure#("bad extracted MIME type"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 157 | if (covercxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).->cxx . . $ std/optional#`operator->`(258ee1c9e0a18a5c).picture_datacxx . . $ ot/picture#picture_data. != "Picture data"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367).) |
| 158 | throw failurecxx . . $ tap/failure#("bad extracted picture data"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 159 | |
| 160 | otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# truncated_data = picture_data.substrcxx . . $ std/basic_string_view#substr(6f25e9449f081971).(0, picture_data.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).() - 1); |
| 161 | tags.commentscxx . . $ ot/opus_tags#comments. =cxx . . $ std/__cxx11/list#`operator=`(e3262da308648e9d). { u8"METADATA_BLOCK_PICTURE=" +cxx . . $ std/operator+(39ed2c165380f78e). otcxx . . $ ot/::encode_base64cxx . . $ ot/encode_base64(86a0a94dd06bebac).(truncated_datacxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).) }; |
| 162 | try { |
| 163 | otcxx . . $ ot/::extract_covercxx . . $ ot/extract_cover(757f97eae4ffafe0).(tags); |
| 164 | throw failurecxx . . $ tap/failure#("accepted a bad picture block"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 165 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) {} |
| 166 | } |
| 167 | |
| 168 | static void make_covercxx . . $ make_cover(49f6e7a06ebc5aa8).() |
| 169 | { |
| 170 | otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# picture_block = ""_bsvcxx . . $ `operator""_bsv`(630326791a3f0367). |
| 171 | "\x00\x00\x00\x03" // Picture type 3. |
| 172 | "\x00\x00\x00\x09" "image/png" // MIME type. |
| 173 | "\x00\x00\x00\x00" "" // Description. |
| 174 | "\x00\x00\x00\x00" // Width. |
| 175 | "\x00\x00\x00\x00" // Height. |
| 176 | "\x00\x00\x00\x00" // Color depth. |
| 177 | "\x00\x00\x00\x00" // Palette size. |
| 178 | "\x00\x00\x00\x11" "\x89PNG Picture data"; |
| 179 | |
| 180 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string# expected = u8"METADATA_BLOCK_PICTURE=" +cxx . . $ std/operator+(39ed2c165380f78e). otcxx . . $ ot/::encode_base64cxx . . $ ot/encode_base64(86a0a94dd06bebac).(picture_blockcxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).); |
| 181 | opaque_iscxx . . $ tap/opaque_is(e56f9d162936dca9).(otcxx . . $ ot/::make_covercxx . . $ ot/make_cover(86a0a94dd06bebac).("\x89PNG Picture data"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367).), expected, "build the picture tag"); |
| 182 | } |
| 183 | |
| 184 | int maincxx . . $ main(b126dc7c1de90089).() |
| 185 | { |
| 186 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "1..6\n"; |
| 187 | runcxx . . $ tap/run(7ee90fd1703d5d92).(parse_standardcxx . . $ parse_standard(49f6e7a06ebc5aa8)., "parse a standard OpusTags packet"); |
| 188 | runcxx . . $ tap/run(7ee90fd1703d5d92).(parse_corruptedcxx . . $ parse_corrupted(49f6e7a06ebc5aa8)., "correctly reject invalid packets"); |
| 189 | runcxx . . $ tap/run(7ee90fd1703d5d92).(recode_standardcxx . . $ recode_standard(49f6e7a06ebc5aa8)., "recode a standard OpusTags packet"); |
| 190 | runcxx . . $ tap/run(7ee90fd1703d5d92).(recode_paddingcxx . . $ recode_padding(49f6e7a06ebc5aa8)., "recode a OpusTags packet with padding"); |
| 191 | runcxx . . $ tap/run(7ee90fd1703d5d92).(extract_covercxx . . $ extract_cover(49f6e7a06ebc5aa8)., "extract the cover art"); |
| 192 | runcxx . . $ tap/run(7ee90fd1703d5d92).(make_covercxx . . $ make_cover(49f6e7a06ebc5aa8)., "encode the cover art"); |
| 193 | return 0; |
| 194 | } |