David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 1 | package yaml |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | ) |
| 6 | |
| 7 | // Set the reader error and return 0. |
| 8 | func yaml_parser_set_reader_error(parser *yaml_parser_t, problem string, offset int, value int) bool { |
| 9 | parser.error = yaml_READER_ERROR |
| 10 | parser.problem = problem |
| 11 | parser.problem_offset = offset |
| 12 | parser.problem_value = value |
| 13 | return false |
| 14 | } |
| 15 | |
| 16 | // Byte order marks. |
| 17 | const ( |
| 18 | bom_UTF8 = "\xef\xbb\xbf" |
| 19 | bom_UTF16LE = "\xff\xfe" |
| 20 | bom_UTF16BE = "\xfe\xff" |
| 21 | ) |
| 22 | |
| 23 | // Determine the input stream encoding by checking the BOM symbol. If no BOM is |
| 24 | // found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure. |
| 25 | func yaml_parser_determine_encoding(parser *yaml_parser_t) bool { |
| 26 | // Ensure that we had enough bytes in the raw buffer. |
| 27 | for !parser.eof && len(parser.raw_buffer)-parser.raw_buffer_pos < 3 { |
| 28 | if !yaml_parser_update_raw_buffer(parser) { |
| 29 | return false |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Determine the encoding. |
| 34 | buf := parser.raw_buffer |
| 35 | pos := parser.raw_buffer_pos |
| 36 | avail := len(buf) - pos |
| 37 | if avail >= 2 && buf[pos] == bom_UTF16LE[0] && buf[pos+1] == bom_UTF16LE[1] { |
| 38 | parser.encoding = yaml_UTF16LE_ENCODING |
| 39 | parser.raw_buffer_pos += 2 |
| 40 | parser.offset += 2 |
| 41 | } else if avail >= 2 && buf[pos] == bom_UTF16BE[0] && buf[pos+1] == bom_UTF16BE[1] { |
| 42 | parser.encoding = yaml_UTF16BE_ENCODING |
| 43 | parser.raw_buffer_pos += 2 |
| 44 | parser.offset += 2 |
| 45 | } else if avail >= 3 && buf[pos] == bom_UTF8[0] && buf[pos+1] == bom_UTF8[1] && buf[pos+2] == bom_UTF8[2] { |
| 46 | parser.encoding = yaml_UTF8_ENCODING |
| 47 | parser.raw_buffer_pos += 3 |
| 48 | parser.offset += 3 |
| 49 | } else { |
| 50 | parser.encoding = yaml_UTF8_ENCODING |
| 51 | } |
| 52 | return true |
| 53 | } |
| 54 | |
| 55 | // Update the raw buffer. |
| 56 | func yaml_parser_update_raw_buffer(parser *yaml_parser_t) bool { |
| 57 | size_read := 0 |
| 58 | |
| 59 | // Return if the raw buffer is full. |
| 60 | if parser.raw_buffer_pos == 0 && len(parser.raw_buffer) == cap(parser.raw_buffer) { |
| 61 | return true |
| 62 | } |
| 63 | |
| 64 | // Return on EOF. |
| 65 | if parser.eof { |
| 66 | return true |
| 67 | } |
| 68 | |
| 69 | // Move the remaining bytes in the raw buffer to the beginning. |
| 70 | if parser.raw_buffer_pos > 0 && parser.raw_buffer_pos < len(parser.raw_buffer) { |
| 71 | copy(parser.raw_buffer, parser.raw_buffer[parser.raw_buffer_pos:]) |
| 72 | } |
| 73 | parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)-parser.raw_buffer_pos] |
| 74 | parser.raw_buffer_pos = 0 |
| 75 | |
| 76 | // Call the read handler to fill the buffer. |
| 77 | size_read, err := parser.read_handler(parser, parser.raw_buffer[len(parser.raw_buffer):cap(parser.raw_buffer)]) |
| 78 | parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)+size_read] |
| 79 | if err == io.EOF { |
| 80 | parser.eof = true |
| 81 | } else if err != nil { |
| 82 | return yaml_parser_set_reader_error(parser, "input error: "+err.Error(), parser.offset, -1) |
| 83 | } |
| 84 | return true |
| 85 | } |
| 86 | |
| 87 | // Ensure that the buffer contains at least `length` characters. |
| 88 | // Return true on success, false on failure. |
| 89 | // |
| 90 | // The length is supposed to be significantly less that the buffer size. |
| 91 | func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool { |
| 92 | if parser.read_handler == nil { |
| 93 | panic("read handler must be set") |
| 94 | } |
| 95 | |
| 96 | // If the EOF flag is set and the raw buffer is empty, do nothing. |
| 97 | if parser.eof && parser.raw_buffer_pos == len(parser.raw_buffer) { |
| 98 | return true |
| 99 | } |
| 100 | |
| 101 | // Return if the buffer contains enough characters. |
| 102 | if parser.unread >= length { |
| 103 | return true |
| 104 | } |
| 105 | |
| 106 | // Determine the input encoding if it is not known yet. |
| 107 | if parser.encoding == yaml_ANY_ENCODING { |
| 108 | if !yaml_parser_determine_encoding(parser) { |
| 109 | return false |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Move the unread characters to the beginning of the buffer. |
| 114 | buffer_len := len(parser.buffer) |
| 115 | if parser.buffer_pos > 0 && parser.buffer_pos < buffer_len { |
| 116 | copy(parser.buffer, parser.buffer[parser.buffer_pos:]) |
| 117 | buffer_len -= parser.buffer_pos |
| 118 | parser.buffer_pos = 0 |
| 119 | } else if parser.buffer_pos == buffer_len { |
| 120 | buffer_len = 0 |
| 121 | parser.buffer_pos = 0 |
| 122 | } |
| 123 | |
| 124 | // Open the whole buffer for writing, and cut it before returning. |
| 125 | parser.buffer = parser.buffer[:cap(parser.buffer)] |
| 126 | |
| 127 | // Fill the buffer until it has enough characters. |
| 128 | first := true |
| 129 | for parser.unread < length { |
| 130 | |
| 131 | // Fill the raw buffer if necessary. |
| 132 | if !first || parser.raw_buffer_pos == len(parser.raw_buffer) { |
| 133 | if !yaml_parser_update_raw_buffer(parser) { |
| 134 | parser.buffer = parser.buffer[:buffer_len] |
| 135 | return false |
| 136 | } |
| 137 | } |
| 138 | first = false |
| 139 | |
| 140 | // Decode the raw buffer. |
| 141 | inner: |
| 142 | for parser.raw_buffer_pos != len(parser.raw_buffer) { |
| 143 | var value rune |
| 144 | var width int |
| 145 | |
| 146 | raw_unread := len(parser.raw_buffer) - parser.raw_buffer_pos |
| 147 | |
| 148 | // Decode the next character. |
| 149 | switch parser.encoding { |
| 150 | case yaml_UTF8_ENCODING: |
| 151 | // Decode a UTF-8 character. Check RFC 3629 |
| 152 | // (http://www.ietf.org/rfc/rfc3629.txt) for more details. |
| 153 | // |
| 154 | // The following table (taken from the RFC) is used for |
| 155 | // decoding. |
| 156 | // |
| 157 | // Char. number range | UTF-8 octet sequence |
| 158 | // (hexadecimal) | (binary) |
| 159 | // --------------------+------------------------------------ |
| 160 | // 0000 0000-0000 007F | 0xxxxxxx |
| 161 | // 0000 0080-0000 07FF | 110xxxxx 10xxxxxx |
| 162 | // 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
| 163 | // 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 164 | // |
| 165 | // Additionally, the characters in the range 0xD800-0xDFFF |
| 166 | // are prohibited as they are reserved for use with UTF-16 |
| 167 | // surrogate pairs. |
| 168 | |
| 169 | // Determine the length of the UTF-8 sequence. |
| 170 | octet := parser.raw_buffer[parser.raw_buffer_pos] |
| 171 | switch { |
| 172 | case octet&0x80 == 0x00: |
| 173 | width = 1 |
| 174 | case octet&0xE0 == 0xC0: |
| 175 | width = 2 |
| 176 | case octet&0xF0 == 0xE0: |
| 177 | width = 3 |
| 178 | case octet&0xF8 == 0xF0: |
| 179 | width = 4 |
| 180 | default: |
| 181 | // The leading octet is invalid. |
| 182 | return yaml_parser_set_reader_error(parser, |
| 183 | "invalid leading UTF-8 octet", |
| 184 | parser.offset, int(octet)) |
| 185 | } |
| 186 | |
| 187 | // Check if the raw buffer contains an incomplete character. |
| 188 | if width > raw_unread { |
| 189 | if parser.eof { |
| 190 | return yaml_parser_set_reader_error(parser, |
| 191 | "incomplete UTF-8 octet sequence", |
| 192 | parser.offset, -1) |
| 193 | } |
| 194 | break inner |
| 195 | } |
| 196 | |
| 197 | // Decode the leading octet. |
| 198 | switch { |
| 199 | case octet&0x80 == 0x00: |
| 200 | value = rune(octet & 0x7F) |
| 201 | case octet&0xE0 == 0xC0: |
| 202 | value = rune(octet & 0x1F) |
| 203 | case octet&0xF0 == 0xE0: |
| 204 | value = rune(octet & 0x0F) |
| 205 | case octet&0xF8 == 0xF0: |
| 206 | value = rune(octet & 0x07) |
| 207 | default: |
| 208 | value = 0 |
| 209 | } |
| 210 | |
| 211 | // Check and decode the trailing octets. |
| 212 | for k := 1; k < width; k++ { |
| 213 | octet = parser.raw_buffer[parser.raw_buffer_pos+k] |
| 214 | |
| 215 | // Check if the octet is valid. |
| 216 | if (octet & 0xC0) != 0x80 { |
| 217 | return yaml_parser_set_reader_error(parser, |
| 218 | "invalid trailing UTF-8 octet", |
| 219 | parser.offset+k, int(octet)) |
| 220 | } |
| 221 | |
| 222 | // Decode the octet. |
| 223 | value = (value << 6) + rune(octet&0x3F) |
| 224 | } |
| 225 | |
| 226 | // Check the length of the sequence against the value. |
| 227 | switch { |
| 228 | case width == 1: |
| 229 | case width == 2 && value >= 0x80: |
| 230 | case width == 3 && value >= 0x800: |
| 231 | case width == 4 && value >= 0x10000: |
| 232 | default: |
| 233 | return yaml_parser_set_reader_error(parser, |
| 234 | "invalid length of a UTF-8 sequence", |
| 235 | parser.offset, -1) |
| 236 | } |
| 237 | |
| 238 | // Check the range of the value. |
| 239 | if value >= 0xD800 && value <= 0xDFFF || value > 0x10FFFF { |
| 240 | return yaml_parser_set_reader_error(parser, |
| 241 | "invalid Unicode character", |
| 242 | parser.offset, int(value)) |
| 243 | } |
| 244 | |
| 245 | case yaml_UTF16LE_ENCODING, yaml_UTF16BE_ENCODING: |
| 246 | var low, high int |
| 247 | if parser.encoding == yaml_UTF16LE_ENCODING { |
| 248 | low, high = 0, 1 |
| 249 | } else { |
| 250 | low, high = 1, 0 |
| 251 | } |
| 252 | |
| 253 | // The UTF-16 encoding is not as simple as one might |
| 254 | // naively think. Check RFC 2781 |
| 255 | // (http://www.ietf.org/rfc/rfc2781.txt). |
| 256 | // |
| 257 | // Normally, two subsequent bytes describe a Unicode |
| 258 | // character. However a special technique (called a |
| 259 | // surrogate pair) is used for specifying character |
| 260 | // values larger than 0xFFFF. |
| 261 | // |
| 262 | // A surrogate pair consists of two pseudo-characters: |
| 263 | // high surrogate area (0xD800-0xDBFF) |
| 264 | // low surrogate area (0xDC00-0xDFFF) |
| 265 | // |
| 266 | // The following formulas are used for decoding |
| 267 | // and encoding characters using surrogate pairs: |
| 268 | // |
| 269 | // U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF) |
| 270 | // U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF) |
| 271 | // W1 = 110110yyyyyyyyyy |
| 272 | // W2 = 110111xxxxxxxxxx |
| 273 | // |
| 274 | // where U is the character value, W1 is the high surrogate |
| 275 | // area, W2 is the low surrogate area. |
| 276 | |
| 277 | // Check for incomplete UTF-16 character. |
| 278 | if raw_unread < 2 { |
| 279 | if parser.eof { |
| 280 | return yaml_parser_set_reader_error(parser, |
| 281 | "incomplete UTF-16 character", |
| 282 | parser.offset, -1) |
| 283 | } |
| 284 | break inner |
| 285 | } |
| 286 | |
| 287 | // Get the character. |
| 288 | value = rune(parser.raw_buffer[parser.raw_buffer_pos+low]) + |
| 289 | (rune(parser.raw_buffer[parser.raw_buffer_pos+high]) << 8) |
| 290 | |
| 291 | // Check for unexpected low surrogate area. |
| 292 | if value&0xFC00 == 0xDC00 { |
| 293 | return yaml_parser_set_reader_error(parser, |
| 294 | "unexpected low surrogate area", |
| 295 | parser.offset, int(value)) |
| 296 | } |
| 297 | |
| 298 | // Check for a high surrogate area. |
| 299 | if value&0xFC00 == 0xD800 { |
| 300 | width = 4 |
| 301 | |
| 302 | // Check for incomplete surrogate pair. |
| 303 | if raw_unread < 4 { |
| 304 | if parser.eof { |
| 305 | return yaml_parser_set_reader_error(parser, |
| 306 | "incomplete UTF-16 surrogate pair", |
| 307 | parser.offset, -1) |
| 308 | } |
| 309 | break inner |
| 310 | } |
| 311 | |
| 312 | // Get the next character. |
| 313 | value2 := rune(parser.raw_buffer[parser.raw_buffer_pos+low+2]) + |
| 314 | (rune(parser.raw_buffer[parser.raw_buffer_pos+high+2]) << 8) |
| 315 | |
| 316 | // Check for a low surrogate area. |
| 317 | if value2&0xFC00 != 0xDC00 { |
| 318 | return yaml_parser_set_reader_error(parser, |
| 319 | "expected low surrogate area", |
| 320 | parser.offset+2, int(value2)) |
| 321 | } |
| 322 | |
| 323 | // Generate the value of the surrogate pair. |
| 324 | value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF) |
| 325 | } else { |
| 326 | width = 2 |
| 327 | } |
| 328 | |
| 329 | default: |
| 330 | panic("impossible") |
| 331 | } |
| 332 | |
| 333 | // Check if the character is in the allowed range: |
| 334 | // #x9 | #xA | #xD | [#x20-#x7E] (8 bit) |
| 335 | // | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit) |
| 336 | // | [#x10000-#x10FFFF] (32 bit) |
| 337 | switch { |
| 338 | case value == 0x09: |
| 339 | case value == 0x0A: |
| 340 | case value == 0x0D: |
| 341 | case value >= 0x20 && value <= 0x7E: |
| 342 | case value == 0x85: |
| 343 | case value >= 0xA0 && value <= 0xD7FF: |
| 344 | case value >= 0xE000 && value <= 0xFFFD: |
| 345 | case value >= 0x10000 && value <= 0x10FFFF: |
| 346 | default: |
| 347 | return yaml_parser_set_reader_error(parser, |
| 348 | "control characters are not allowed", |
| 349 | parser.offset, int(value)) |
| 350 | } |
| 351 | |
| 352 | // Move the raw pointers. |
| 353 | parser.raw_buffer_pos += width |
| 354 | parser.offset += width |
| 355 | |
| 356 | // Finally put the character into the buffer. |
| 357 | if value <= 0x7F { |
| 358 | // 0000 0000-0000 007F . 0xxxxxxx |
| 359 | parser.buffer[buffer_len+0] = byte(value) |
| 360 | buffer_len += 1 |
| 361 | } else if value <= 0x7FF { |
| 362 | // 0000 0080-0000 07FF . 110xxxxx 10xxxxxx |
| 363 | parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6)) |
| 364 | parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F)) |
| 365 | buffer_len += 2 |
| 366 | } else if value <= 0xFFFF { |
| 367 | // 0000 0800-0000 FFFF . 1110xxxx 10xxxxxx 10xxxxxx |
| 368 | parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12)) |
| 369 | parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F)) |
| 370 | parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F)) |
| 371 | buffer_len += 3 |
| 372 | } else { |
| 373 | // 0001 0000-0010 FFFF . 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 374 | parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18)) |
| 375 | parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F)) |
| 376 | parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F)) |
| 377 | parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F)) |
| 378 | buffer_len += 4 |
| 379 | } |
| 380 | |
| 381 | parser.unread++ |
| 382 | } |
| 383 | |
| 384 | // On EOF, put NUL into the buffer and return. |
| 385 | if parser.eof { |
| 386 | parser.buffer[buffer_len] = 0 |
| 387 | buffer_len++ |
| 388 | parser.unread++ |
| 389 | break |
| 390 | } |
| 391 | } |
| 392 | parser.buffer = parser.buffer[:buffer_len] |
| 393 | return true |
| 394 | } |