1use std::sync::Arc;
19
20use unityrs_core::limits::{Charge, Meter, heap_cost};
21
22use super::classify::Class;
23use super::table::{self, Set};
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
27pub enum TemplateKind {
28 Texture,
30 Sprite,
32 AtlasSprite,
34 AtlasJson,
36 Glb,
38}
39
40impl TemplateKind {
41 #[must_use]
44 pub fn name(self) -> &'static str {
45 match self {
46 Self::Texture => "texture",
47 Self::Sprite => "sprite",
48 Self::AtlasSprite => "atlas_sprite",
49 Self::AtlasJson => "atlas_json",
50 Self::Glb => "glb",
51 }
52 }
53}
54
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
57pub enum Var {
58 Input,
60 SafeInput,
62 Type,
64 Name,
66 SafeName,
68 PathId,
70 Atlas,
72 SafeAtlas,
74 Sprite,
76 SafeSprite,
78 ContainerPath,
80 SafeContainerPath,
82}
83
84impl Var {
85 #[must_use]
87 pub fn plain(self) -> Option<Self> {
88 Some(match self {
89 Self::SafeInput => Self::Input,
90 Self::SafeName => Self::Name,
91 Self::SafeAtlas => Self::Atlas,
92 Self::SafeSprite => Self::Sprite,
93 Self::SafeContainerPath => Self::ContainerPath,
94 _ => return None,
95 })
96 }
97
98 fn parse(name: &str) -> Option<Self> {
99 Some(match name {
100 "input" => Self::Input,
101 "safe_input" => Self::SafeInput,
102 "type" => Self::Type,
103 "name" => Self::Name,
104 "safe_name" => Self::SafeName,
105 "path_id" => Self::PathId,
106 "atlas" => Self::Atlas,
107 "safe_atlas" => Self::SafeAtlas,
108 "sprite" => Self::Sprite,
109 "safe_sprite" => Self::SafeSprite,
110 "container_path" => Self::ContainerPath,
111 "safe_container_path" => Self::SafeContainerPath,
112 _ => return None,
113 })
114 }
115
116 #[must_use]
118 pub fn allowed(self, kind: TemplateKind) -> bool {
119 match self {
120 Self::Input | Self::SafeInput | Self::PathId => true,
121 Self::Type | Self::Name | Self::SafeName => {
122 matches!(
123 kind,
124 TemplateKind::Texture | TemplateKind::Sprite | TemplateKind::Glb
125 )
126 }
127 Self::Atlas | Self::SafeAtlas => {
128 matches!(kind, TemplateKind::AtlasSprite | TemplateKind::AtlasJson)
129 }
130 Self::Sprite | Self::SafeSprite => kind == TemplateKind::AtlasSprite,
131 Self::ContainerPath | Self::SafeContainerPath => kind == TemplateKind::Glb,
132 }
133 }
134}
135
136#[derive(Clone, Debug, PartialEq, Eq)]
137enum Piece {
138 Lit(String),
139 Var(Var),
140}
141
142#[derive(Clone, Debug, PartialEq, Eq)]
144pub struct Template {
145 pieces: Vec<Piece>,
146}
147
148#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
150pub enum TemplateError {
151 #[error("an empty template")]
153 Empty,
154 #[error("an unmatched brace at byte {0}")]
156 Brace(usize),
157 #[error("unknown variable {0:?}")]
159 Unknown(String),
160 #[error("variable {0:?} is not allowed here")]
162 NotAllowed(String),
163}
164
165#[derive(Clone, Copy, Debug, PartialEq, Eq)]
169pub enum Token<'t> {
170 Lit(&'t str),
172 Var(Var),
174}
175
176#[must_use]
182pub fn tokens(text: &str, kind: TemplateKind) -> Tokens<'_> {
183 Tokens {
184 text,
185 kind,
186 at: 0,
187 done: false,
188 }
189}
190
191#[derive(Clone, Debug)]
193pub struct Tokens<'t> {
194 text: &'t str,
195 kind: TemplateKind,
196 at: usize,
197 done: bool,
198}
199
200impl<'t> Iterator for Tokens<'t> {
201 type Item = Result<Token<'t>, TemplateError>;
202
203 fn next(&mut self) -> Option<Self::Item> {
204 let at = self.at;
205 let rest = self.text.get(at..).unwrap_or_default();
206 if self.done || rest.is_empty() {
207 return None;
208 }
209 let token = if rest.starts_with("{{") || rest.starts_with("}}") {
210 self.at += 2;
211 Ok(Token::Lit(rest.get(..1).unwrap_or_default()))
212 } else if let Some(after) = rest.strip_prefix('{') {
213 match after.find('}') {
214 None => Err(TemplateError::Brace(at)),
215 Some(end) => {
216 let name = after.get(..end).unwrap_or_default();
217 self.at += end + 2;
218 match Var::parse(name) {
219 None => Err(TemplateError::Unknown(name.to_owned())),
220 Some(var) if !var.allowed(self.kind) => {
221 Err(TemplateError::NotAllowed(name.to_owned()))
222 }
223 Some(var) => Ok(Token::Var(var)),
224 }
225 }
226 }
227 } else if rest.starts_with('}') {
228 Err(TemplateError::Brace(at))
229 } else {
230 let end = rest.find(['{', '}']).unwrap_or(rest.len());
231 self.at += end;
232 Ok(Token::Lit(rest.get(..end).unwrap_or_default()))
233 };
234 self.done = token.is_err();
235 Some(token)
236 }
237}
238
239impl Template {
240 #[cfg(feature = "gltf")]
241 pub(super) fn uses(&self, var: Var) -> bool {
242 self.pieces.iter().any(|piece| match piece {
243 Piece::Var(v) => *v == var || v.plain() == Some(var),
244 Piece::Lit(_) => false,
245 })
246 }
247
248 pub fn parse(text: &str, kind: TemplateKind) -> Result<Self, TemplateError> {
250 if text.is_empty() {
251 return Err(TemplateError::Empty);
252 }
253 let mut pieces = Vec::new();
254 let mut lit = String::new();
255 for token in tokens(text, kind) {
256 match token? {
257 Token::Lit(s) => lit.push_str(s),
258 Token::Var(var) => {
259 if !lit.is_empty() {
260 pieces.push(Piece::Lit(std::mem::take(&mut lit)));
261 }
262 pieces.push(Piece::Var(var));
263 }
264 }
265 }
266 if !lit.is_empty() {
267 pieces.push(Piece::Lit(lit));
268 }
269 Ok(Self { pieces })
270 }
271
272 pub fn expand<'v>(&self, value: impl Fn(Var) -> &'v [u8], out: &mut Vec<u8>) -> usize {
288 out.clear();
289 for (k, p) in self.pieces.iter().enumerate() {
290 let room = STOP.saturating_sub(out.len());
291 if room == 0 {
292 return self.tail(out, &[], k, &value);
293 }
294 let src = match p {
296 Piece::Lit(s) => s.as_bytes(),
297 Piece::Var(v) => match v.plain() {
298 Some(plain) => {
299 safe_into(value(plain), room, out);
300 continue;
301 }
302 None => value(*v),
303 },
304 };
305 out.extend(src.iter().take(room));
306 if src.len() > room {
307 let rest = src.get(room..).unwrap_or_default();
308 return self.tail(out, rest, k + 1, &value);
309 }
310 }
311 0
312 }
313
314 fn tail<'v>(
319 &self,
320 out: &[u8],
321 rest: &[u8],
322 next: usize,
323 value: &impl Fn(Var) -> &'v [u8],
324 ) -> usize {
325 let head = out.get(out.len().saturating_sub(3)..).unwrap_or_default();
326 let later = self
327 .pieces
328 .get(next..)
329 .unwrap_or_default()
330 .iter()
331 .map_while(|p| match p {
332 Piece::Lit(s) => Some(s.as_bytes()),
333 Piece::Var(v) => v.plain().is_none().then(|| value(*v)),
334 })
335 .flatten();
336 let ahead = rest.iter().chain(later).take(3);
337 let mut window = [0u8; 6];
338 let mut len = 0;
339 for (slot, b) in window.iter_mut().zip(head.iter().chain(ahead)) {
340 *slot = *b;
341 len += 1;
342 }
343 cut_tail(window.get(..len).unwrap_or_default(), head.len())
344 }
345}
346
347#[must_use]
351pub fn cut_tail(src: &[u8], at: usize) -> usize {
352 let head = src.get(..at).unwrap_or_default();
353 for back in 1..=head.len().min(3) {
354 let i = head.len() - back;
355 let Some(&b) = head.get(i) else {
356 return 0;
357 };
358 if b & 0xC0 == 0x80 {
359 continue;
360 }
361 let width = match b {
362 0xC2..=0xDF => 2,
363 0xE0..=0xEF => 3,
364 0xF0..=0xF4 => 4,
365 _ => 0,
366 };
367 let whole = src
368 .get(i..i + width)
369 .is_some_and(|c| std::str::from_utf8(c).is_ok());
370 return if width > back && whole { back } else { 0 };
371 }
372 0
373}
374
375pub fn safe_into(bytes: &[u8], room: usize, out: &mut Vec<u8>) {
381 let start = out.len();
382 let lead = bytes.iter().take_while(|b| **b == b'.').count();
383 let rest = bytes.get(lead..).unwrap_or_default();
384 if rest.is_empty() {
385 out.extend(b"unnamed".iter().take(room));
386 return;
387 }
388 let mut dots = 0usize;
389 let put = |c: u8, out: &mut Vec<u8>| {
390 if out.len() - start < room {
391 out.push(c);
392 true
393 } else {
394 false
395 }
396 };
397 for chunk in rest.utf8_chunks() {
398 let valid = chunk.valid().chars().map(Some);
399 let invalid = chunk.invalid().iter().map(|_| None);
400 for c in valid.chain(invalid) {
401 let mapped = match c {
402 Some('.') => {
403 dots += 1;
404 continue;
405 }
406 Some(c) if c.is_ascii_alphanumeric() || matches!(c, '_' | '-') => c as u8,
407 _ => b'_',
408 };
409 for _ in 0..std::mem::take(&mut dots) {
411 if !put(b'.', out) {
412 return;
413 }
414 }
415 if !put(mapped, out) {
416 return;
417 }
418 }
419 }
420}
421
422#[must_use]
426pub fn safe(bytes: &[u8]) -> String {
427 let mut out = Vec::new();
428 safe_into(bytes, usize::MAX, &mut out);
429 String::from_utf8(out).unwrap_or_default()
430}
431
432#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
435pub enum KeyRule {
436 Empty,
438 NotUtf8,
440 ControlChar,
442 LeadingSlash,
444 TrailingSlash,
446 EmptySegment,
448 DotSegment,
450 SegmentTooLong,
452 TooLong,
454 ReservedPrefix,
456 ReservedName,
458}
459
460impl KeyRule {
461 #[must_use]
463 pub fn name(self) -> &'static str {
464 match self {
465 Self::Empty => "empty",
466 Self::NotUtf8 => "not_utf8",
467 Self::ControlChar => "control_char",
468 Self::LeadingSlash => "leading_slash",
469 Self::TrailingSlash => "trailing_slash",
470 Self::EmptySegment => "empty_segment",
471 Self::DotSegment => "dot_segment",
472 Self::SegmentTooLong => "segment_too_long",
473 Self::TooLong => "too_long",
474 Self::ReservedPrefix => "reserved_prefix",
475 Self::ReservedName => "reserved_name",
476 }
477 }
478}
479
480pub const SEGMENT_MAX: usize = 255;
482pub const KEY_MAX: usize = 1024;
484pub const STOP: usize = KEY_MAX + 1;
487
488pub const RESERVED_PREFIX: &str = "_unityrs";
490pub const RESERVED_NAME: &str = ".unityrs-tmp-";
492
493fn dot_segment(segment: &str) -> bool {
495 segment == "." || segment == ".."
496}
497
498#[derive(Clone, Copy, Debug, PartialEq, Eq)]
501enum Rules {
502 Key,
503 Member,
504}
505
506pub fn check(key: &[u8]) -> Result<&str, KeyRule> {
518 rules(key, Rules::Key)
519}
520
521pub fn check_member(path: &[u8]) -> Result<&str, KeyRule> {
524 rules(path, Rules::Member)
525}
526
527pub fn check_prefixed<'k>(
532 key: &'k [u8],
533 prefix: &str,
534 scratch: &mut Vec<u8>,
535) -> Result<&'k str, KeyRule> {
536 let checked = check(key)?;
537 if !prefix.is_empty() {
538 scratch.clear();
539 scratch.extend(prefix.as_bytes().iter().take(STOP));
540 let room = STOP.saturating_sub(scratch.len());
541 scratch.extend(key.iter().take(room));
542 check(scratch)?;
543 }
544 Ok(checked)
545}
546
547fn rules(key: &[u8], which: Rules) -> Result<&str, KeyRule> {
548 if key.is_empty() {
549 return Err(KeyRule::Empty);
550 }
551 let long = key.len() > KEY_MAX;
552 let read = key.get(..STOP).unwrap_or(key);
553 let text = match std::str::from_utf8(read) {
554 Ok(k) => k,
555 Err(e) if long && e.error_len().is_none() => read
557 .get(..e.valid_up_to())
558 .and_then(|v| std::str::from_utf8(v).ok())
559 .ok_or(KeyRule::NotUtf8)?,
560 Err(_) => return Err(KeyRule::NotUtf8),
561 };
562 if text.chars().any(|c| c < ' ' || c == '\u{7f}') {
563 return Err(KeyRule::ControlChar);
564 }
565 if text.starts_with('/') {
566 return Err(KeyRule::LeadingSlash);
567 }
568 if !long && text.ends_with('/') {
569 return Err(KeyRule::TrailingSlash);
570 }
571 if text.contains("//") {
572 return Err(KeyRule::EmptySegment);
573 }
574 let segments = text.split('/');
575 let settled = if long {
576 segments.clone().count().saturating_sub(1)
577 } else {
578 usize::MAX
579 };
580 if segments.take(settled).any(dot_segment) {
581 return Err(KeyRule::DotSegment);
582 }
583 if read.split(|b| *b == b'/').any(|s| s.len() > SEGMENT_MAX) {
585 return Err(KeyRule::SegmentTooLong);
586 }
587 if long {
588 return Err(KeyRule::TooLong);
589 }
590 if which == Rules::Key {
591 if text == RESERVED_PREFIX
592 || text
593 .strip_prefix(RESERVED_PREFIX)
594 .is_some_and(|r| r.starts_with('/'))
595 {
596 return Err(KeyRule::ReservedPrefix);
597 }
598 if text.split('/').any(|s| s.starts_with(RESERVED_NAME)) {
599 return Err(KeyRule::ReservedName);
600 }
601 }
602 Ok(text)
603}
604
605#[must_use]
608pub fn entry_cost(len: usize) -> u64 {
609 heap_cost(u64::try_from(len).unwrap_or(u64::MAX))
610}
611
612#[derive(Debug)]
617pub struct KeyTable {
618 keys: Set<Box<str>>,
619 strings: Charge,
620}
621
622impl KeyTable {
623 #[must_use]
625 pub fn new(meter: &Arc<Meter>) -> Self {
626 Self {
627 keys: Set::new(meter),
628 strings: Charge::zero(meter),
629 }
630 }
631
632 #[must_use]
634 pub fn contains(&self, key: &str) -> bool {
635 self.keys.contains(key)
636 }
637
638 pub fn take(&mut self, key: &str) -> Result<bool, Class> {
641 if self.keys.contains(key) {
642 return Ok(false);
643 }
644 self.keys.reserve_one()?;
645 self.strings
646 .grow(entry_cost(key.len()))
647 .map_err(|e| table::refused(&e))?;
648 self.keys.insert(Box::from(key))
649 }
650
651 #[must_use]
653 pub fn len(&self) -> usize {
654 self.keys.len()
655 }
656
657 #[must_use]
659 pub fn is_empty(&self) -> bool {
660 self.keys.is_empty()
661 }
662}
663
664mod literal;
665
666pub use literal::{TemplateRefusal, check_member_template, check_template};
667
668#[cfg(test)]
669mod tests;