Path: blob/main/vendor/go.yaml.in/yaml/v3/readerc.go
2872 views
//1// Copyright (c) 2011-2019 Canonical Ltd2// Copyright (c) 2006-2010 Kirill Simonov3//4// Permission is hereby granted, free of charge, to any person obtaining a copy of5// this software and associated documentation files (the "Software"), to deal in6// the Software without restriction, including without limitation the rights to7// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies8// of the Software, and to permit persons to whom the Software is furnished to do9// so, subject to the following conditions:10//11// The above copyright notice and this permission notice shall be included in all12// copies or substantial portions of the Software.13//14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20// SOFTWARE.2122package yaml2324import (25"io"26)2728// Set the reader error and return 0.29func yaml_parser_set_reader_error(parser *yaml_parser_t, problem string, offset int, value int) bool {30parser.error = yaml_READER_ERROR31parser.problem = problem32parser.problem_offset = offset33parser.problem_value = value34return false35}3637// Byte order marks.38const (39bom_UTF8 = "\xef\xbb\xbf"40bom_UTF16LE = "\xff\xfe"41bom_UTF16BE = "\xfe\xff"42)4344// Determine the input stream encoding by checking the BOM symbol. If no BOM is45// found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure.46func yaml_parser_determine_encoding(parser *yaml_parser_t) bool {47// Ensure that we had enough bytes in the raw buffer.48for !parser.eof && len(parser.raw_buffer)-parser.raw_buffer_pos < 3 {49if !yaml_parser_update_raw_buffer(parser) {50return false51}52}5354// Determine the encoding.55buf := parser.raw_buffer56pos := parser.raw_buffer_pos57avail := len(buf) - pos58if avail >= 2 && buf[pos] == bom_UTF16LE[0] && buf[pos+1] == bom_UTF16LE[1] {59parser.encoding = yaml_UTF16LE_ENCODING60parser.raw_buffer_pos += 261parser.offset += 262} else if avail >= 2 && buf[pos] == bom_UTF16BE[0] && buf[pos+1] == bom_UTF16BE[1] {63parser.encoding = yaml_UTF16BE_ENCODING64parser.raw_buffer_pos += 265parser.offset += 266} else if avail >= 3 && buf[pos] == bom_UTF8[0] && buf[pos+1] == bom_UTF8[1] && buf[pos+2] == bom_UTF8[2] {67parser.encoding = yaml_UTF8_ENCODING68parser.raw_buffer_pos += 369parser.offset += 370} else {71parser.encoding = yaml_UTF8_ENCODING72}73return true74}7576// Update the raw buffer.77func yaml_parser_update_raw_buffer(parser *yaml_parser_t) bool {78size_read := 07980// Return if the raw buffer is full.81if parser.raw_buffer_pos == 0 && len(parser.raw_buffer) == cap(parser.raw_buffer) {82return true83}8485// Return on EOF.86if parser.eof {87return true88}8990// Move the remaining bytes in the raw buffer to the beginning.91if parser.raw_buffer_pos > 0 && parser.raw_buffer_pos < len(parser.raw_buffer) {92copy(parser.raw_buffer, parser.raw_buffer[parser.raw_buffer_pos:])93}94parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)-parser.raw_buffer_pos]95parser.raw_buffer_pos = 09697// Call the read handler to fill the buffer.98size_read, err := parser.read_handler(parser, parser.raw_buffer[len(parser.raw_buffer):cap(parser.raw_buffer)])99parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)+size_read]100if err == io.EOF {101parser.eof = true102} else if err != nil {103return yaml_parser_set_reader_error(parser, "input error: "+err.Error(), parser.offset, -1)104}105return true106}107108// Ensure that the buffer contains at least `length` characters.109// Return true on success, false on failure.110//111// The length is supposed to be significantly less that the buffer size.112func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool {113if parser.read_handler == nil {114panic("read handler must be set")115}116117// [Go] This function was changed to guarantee the requested length size at EOF.118// The fact we need to do this is pretty awful, but the description above implies119// for that to be the case, and there are tests120121// If the EOF flag is set and the raw buffer is empty, do nothing.122if parser.eof && parser.raw_buffer_pos == len(parser.raw_buffer) {123// [Go] ACTUALLY! Read the documentation of this function above.124// This is just broken. To return true, we need to have the125// given length in the buffer. Not doing that means every single126// check that calls this function to make sure the buffer has a127// given length is Go) panicking; or C) accessing invalid memory.128//return true129}130131// Return if the buffer contains enough characters.132if parser.unread >= length {133return true134}135136// Determine the input encoding if it is not known yet.137if parser.encoding == yaml_ANY_ENCODING {138if !yaml_parser_determine_encoding(parser) {139return false140}141}142143// Move the unread characters to the beginning of the buffer.144buffer_len := len(parser.buffer)145if parser.buffer_pos > 0 && parser.buffer_pos < buffer_len {146copy(parser.buffer, parser.buffer[parser.buffer_pos:])147buffer_len -= parser.buffer_pos148parser.buffer_pos = 0149} else if parser.buffer_pos == buffer_len {150buffer_len = 0151parser.buffer_pos = 0152}153154// Open the whole buffer for writing, and cut it before returning.155parser.buffer = parser.buffer[:cap(parser.buffer)]156157// Fill the buffer until it has enough characters.158first := true159for parser.unread < length {160161// Fill the raw buffer if necessary.162if !first || parser.raw_buffer_pos == len(parser.raw_buffer) {163if !yaml_parser_update_raw_buffer(parser) {164parser.buffer = parser.buffer[:buffer_len]165return false166}167}168first = false169170// Decode the raw buffer.171inner:172for parser.raw_buffer_pos != len(parser.raw_buffer) {173var value rune174var width int175176raw_unread := len(parser.raw_buffer) - parser.raw_buffer_pos177178// Decode the next character.179switch parser.encoding {180case yaml_UTF8_ENCODING:181// Decode a UTF-8 character. Check RFC 3629182// (http://www.ietf.org/rfc/rfc3629.txt) for more details.183//184// The following table (taken from the RFC) is used for185// decoding.186//187// Char. number range | UTF-8 octet sequence188// (hexadecimal) | (binary)189// --------------------+------------------------------------190// 0000 0000-0000 007F | 0xxxxxxx191// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx192// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx193// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx194//195// Additionally, the characters in the range 0xD800-0xDFFF196// are prohibited as they are reserved for use with UTF-16197// surrogate pairs.198199// Determine the length of the UTF-8 sequence.200octet := parser.raw_buffer[parser.raw_buffer_pos]201switch {202case octet&0x80 == 0x00:203width = 1204case octet&0xE0 == 0xC0:205width = 2206case octet&0xF0 == 0xE0:207width = 3208case octet&0xF8 == 0xF0:209width = 4210default:211// The leading octet is invalid.212return yaml_parser_set_reader_error(parser,213"invalid leading UTF-8 octet",214parser.offset, int(octet))215}216217// Check if the raw buffer contains an incomplete character.218if width > raw_unread {219if parser.eof {220return yaml_parser_set_reader_error(parser,221"incomplete UTF-8 octet sequence",222parser.offset, -1)223}224break inner225}226227// Decode the leading octet.228switch {229case octet&0x80 == 0x00:230value = rune(octet & 0x7F)231case octet&0xE0 == 0xC0:232value = rune(octet & 0x1F)233case octet&0xF0 == 0xE0:234value = rune(octet & 0x0F)235case octet&0xF8 == 0xF0:236value = rune(octet & 0x07)237default:238value = 0239}240241// Check and decode the trailing octets.242for k := 1; k < width; k++ {243octet = parser.raw_buffer[parser.raw_buffer_pos+k]244245// Check if the octet is valid.246if (octet & 0xC0) != 0x80 {247return yaml_parser_set_reader_error(parser,248"invalid trailing UTF-8 octet",249parser.offset+k, int(octet))250}251252// Decode the octet.253value = (value << 6) + rune(octet&0x3F)254}255256// Check the length of the sequence against the value.257switch {258case width == 1:259case width == 2 && value >= 0x80:260case width == 3 && value >= 0x800:261case width == 4 && value >= 0x10000:262default:263return yaml_parser_set_reader_error(parser,264"invalid length of a UTF-8 sequence",265parser.offset, -1)266}267268// Check the range of the value.269if value >= 0xD800 && value <= 0xDFFF || value > 0x10FFFF {270return yaml_parser_set_reader_error(parser,271"invalid Unicode character",272parser.offset, int(value))273}274275case yaml_UTF16LE_ENCODING, yaml_UTF16BE_ENCODING:276var low, high int277if parser.encoding == yaml_UTF16LE_ENCODING {278low, high = 0, 1279} else {280low, high = 1, 0281}282283// The UTF-16 encoding is not as simple as one might284// naively think. Check RFC 2781285// (http://www.ietf.org/rfc/rfc2781.txt).286//287// Normally, two subsequent bytes describe a Unicode288// character. However a special technique (called a289// surrogate pair) is used for specifying character290// values larger than 0xFFFF.291//292// A surrogate pair consists of two pseudo-characters:293// high surrogate area (0xD800-0xDBFF)294// low surrogate area (0xDC00-0xDFFF)295//296// The following formulas are used for decoding297// and encoding characters using surrogate pairs:298//299// U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF)300// U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF)301// W1 = 110110yyyyyyyyyy302// W2 = 110111xxxxxxxxxx303//304// where U is the character value, W1 is the high surrogate305// area, W2 is the low surrogate area.306307// Check for incomplete UTF-16 character.308if raw_unread < 2 {309if parser.eof {310return yaml_parser_set_reader_error(parser,311"incomplete UTF-16 character",312parser.offset, -1)313}314break inner315}316317// Get the character.318value = rune(parser.raw_buffer[parser.raw_buffer_pos+low]) +319(rune(parser.raw_buffer[parser.raw_buffer_pos+high]) << 8)320321// Check for unexpected low surrogate area.322if value&0xFC00 == 0xDC00 {323return yaml_parser_set_reader_error(parser,324"unexpected low surrogate area",325parser.offset, int(value))326}327328// Check for a high surrogate area.329if value&0xFC00 == 0xD800 {330width = 4331332// Check for incomplete surrogate pair.333if raw_unread < 4 {334if parser.eof {335return yaml_parser_set_reader_error(parser,336"incomplete UTF-16 surrogate pair",337parser.offset, -1)338}339break inner340}341342// Get the next character.343value2 := rune(parser.raw_buffer[parser.raw_buffer_pos+low+2]) +344(rune(parser.raw_buffer[parser.raw_buffer_pos+high+2]) << 8)345346// Check for a low surrogate area.347if value2&0xFC00 != 0xDC00 {348return yaml_parser_set_reader_error(parser,349"expected low surrogate area",350parser.offset+2, int(value2))351}352353// Generate the value of the surrogate pair.354value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF)355} else {356width = 2357}358359default:360panic("impossible")361}362363// Check if the character is in the allowed range:364// #x9 | #xA | #xD | [#x20-#x7E] (8 bit)365// | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit)366// | [#x10000-#x10FFFF] (32 bit)367switch {368case value == 0x09:369case value == 0x0A:370case value == 0x0D:371case value >= 0x20 && value <= 0x7E:372case value == 0x85:373case value >= 0xA0 && value <= 0xD7FF:374case value >= 0xE000 && value <= 0xFFFD:375case value >= 0x10000 && value <= 0x10FFFF:376default:377return yaml_parser_set_reader_error(parser,378"control characters are not allowed",379parser.offset, int(value))380}381382// Move the raw pointers.383parser.raw_buffer_pos += width384parser.offset += width385386// Finally put the character into the buffer.387if value <= 0x7F {388// 0000 0000-0000 007F . 0xxxxxxx389parser.buffer[buffer_len+0] = byte(value)390buffer_len += 1391} else if value <= 0x7FF {392// 0000 0080-0000 07FF . 110xxxxx 10xxxxxx393parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6))394parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F))395buffer_len += 2396} else if value <= 0xFFFF {397// 0000 0800-0000 FFFF . 1110xxxx 10xxxxxx 10xxxxxx398parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12))399parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F))400parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F))401buffer_len += 3402} else {403// 0001 0000-0010 FFFF . 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx404parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18))405parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F))406parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F))407parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F))408buffer_len += 4409}410411parser.unread++412}413414// On EOF, put NUL into the buffer and return.415if parser.eof {416parser.buffer[buffer_len] = 0417buffer_len++418parser.unread++419break420}421}422// [Go] Read the documentation of this function above. To return true,423// we need to have the given length in the buffer. Not doing that means424// every single check that calls this function to make sure the buffer425// has a given length is Go) panicking; or C) accessing invalid memory.426// This happens here due to the EOF above breaking early.427for buffer_len < length {428parser.buffer[buffer_len] = 0429buffer_len++430}431parser.buffer = parser.buffer[:buffer_len]432return true433}434435436