use crate::{
Charset,
CharsetDecodeError,
CharsetDecodeErrorKind,
CharsetDecodeResult,
CharsetEncodeError,
CharsetEncodeErrorKind,
CharsetEncodeResult,
DecodeStatus,
Unicode,
Utf8,
};
pub(crate) fn decode_prefix(input: &[u8], index: usize) -> CharsetDecodeResult<DecodeStatus> {
if index > input.len() {
let kind = CharsetDecodeErrorKind::MalformedSequence { value: None };
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index));
}
if index == input.len() {
return Ok(DecodeStatus::NeedMore {
required: index + 1,
available: 0,
});
}
let first = input[index];
let length = match Utf8::byte_len_from_leading_byte(first) {
Some(length) => length,
None => {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(first as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index));
}
};
if input.len() < index + length {
validate_partial(input, index)?;
return Ok(DecodeStatus::NeedMore {
required: index + length,
available: input.len() - index,
});
}
let code_point = match length {
1 => first as u32,
2 => decode_two(input, index)?,
3 => decode_three(input, index)?,
4 => decode_four(input, index)?,
_ => unreachable!("UTF-8 sequence length is limited to four bytes"),
};
let ch = Unicode::to_char(code_point).expect("well-formed UTF-8 decodes to a Unicode scalar");
Ok(DecodeStatus::Complete {
value: ch,
consumed: length,
})
}
pub(crate) fn encode_char(ch: char, output: &mut [u8], index: usize) -> CharsetEncodeResult<usize> {
if index > output.len() {
let kind = CharsetEncodeErrorKind::BufferTooSmall {
required: index + 1,
available: 0,
};
return Err(CharsetEncodeError::new(Charset::UTF_8, kind, index));
}
let length = Utf8::byte_len(ch);
let available = output.len() - index;
if available < length {
let kind = CharsetEncodeErrorKind::BufferTooSmall {
required: index + length,
available,
};
return Err(CharsetEncodeError::new(Charset::UTF_8, kind, index));
}
let mut scratch = [0_u8; Utf8::MAX_BYTES_PER_CHAR];
let encoded = ch.encode_utf8(&mut scratch);
output[index..index + length].copy_from_slice(encoded.as_bytes());
Ok(length)
}
pub(crate) fn decode_two(input: &[u8], index: usize) -> CharsetDecodeResult<u32> {
let second = input[index + 1];
if !Utf8::is_continuation_byte(second) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(second as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 1));
}
Ok((((input[index] & 0x1f) as u32) << 6) | ((second & 0x3f) as u32))
}
pub(crate) fn validate_partial(input: &[u8], index: usize) -> CharsetDecodeResult<()> {
if input.len() >= index + 2 && !is_valid_second_byte(input[index], input[index + 1]) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(input[index + 1] as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 1));
}
if input.len() >= index + 3 && !Utf8::is_continuation_byte(input[index + 2]) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(input[index + 2] as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 2));
}
Ok(())
}
pub(crate) fn is_valid_second_byte(first: u8, second: u8) -> bool {
match first {
0xc2..=0xdf => Utf8::is_continuation_byte(second),
0xe0 => (0xa0..=0xbf).contains(&second),
0xed => (0x80..=0x9f).contains(&second),
0xe1..=0xec | 0xee..=0xef => Utf8::is_continuation_byte(second),
0xf0 => (0x90..=0xbf).contains(&second),
0xf1..=0xf3 => Utf8::is_continuation_byte(second),
0xf4 => (0x80..=0x8f).contains(&second),
_ => false,
}
}
pub(crate) fn decode_three(input: &[u8], index: usize) -> CharsetDecodeResult<u32> {
let first = input[index];
let second = input[index + 1];
let third = input[index + 2];
if !is_valid_second_byte(first, second) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(second as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 1));
}
if !Utf8::is_continuation_byte(third) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(third as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 2));
}
Ok((((first & 0x0f) as u32) << 12) | (((second & 0x3f) as u32) << 6) | ((third & 0x3f) as u32))
}
pub(crate) fn decode_four(input: &[u8], index: usize) -> CharsetDecodeResult<u32> {
let first = input[index];
let second = input[index + 1];
let third = input[index + 2];
let fourth = input[index + 3];
if !is_valid_second_byte(first, second) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(second as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 1));
}
if !Utf8::is_continuation_byte(third) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(third as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 2));
}
if !Utf8::is_continuation_byte(fourth) {
let kind = CharsetDecodeErrorKind::MalformedSequence {
value: Some(fourth as u32),
};
return Err(CharsetDecodeError::new(Charset::UTF_8, kind, index + 3));
}
Ok((((first & 0x07) as u32) << 18)
| (((second & 0x3f) as u32) << 12)
| (((third & 0x3f) as u32) << 6)
| ((fourth & 0x3f) as u32))
}