1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Unified Model Format Detection and Loading
//!
//! Per spec ยง3: Format Support Matrix - auto-detect APR, GGUF, SafeTensors from magic bytes.
//!
//! ## Jidoka (Built-in Quality)
//!
//! - CRC32 verification for APR format
//! - Header size validation for SafeTensors (DOS protection)
//! - Magic byte validation for GGUF
//!
//! ## Supported Formats
//!
//! | Format | Magic | Extension |
//! |--------|-------|-----------|
//! | APR | `APR\0` | `.apr` |
//! | GGUF | `GGUF` | `.gguf` |
//! | SafeTensors | (u64 header size) | `.safetensors` |
use std::path::Path;
/// Detected model format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModelFormat {
/// Aprender native format (first-class support)
Apr,
/// GGUF format (llama.cpp compatible)
Gguf,
/// SafeTensors format (HuggingFace compatible)
SafeTensors,
}
impl std::fmt::Display for ModelFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Apr => write!(f, "APR"),
Self::Gguf => write!(f, "GGUF"),
Self::SafeTensors => write!(f, "SafeTensors"),
}
}
}
/// Errors during format detection
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FormatError {
/// Data too short for format detection (need at least 8 bytes)
TooShort {
/// Actual length
len: usize,
},
/// Unknown format (no magic bytes matched)
UnknownFormat,
/// SafeTensors header too large (DOS protection per spec ยง7.1)
HeaderTooLarge {
/// Header size in bytes
size: u64,
},
/// File extension doesn't match detected format
ExtensionMismatch {
/// Detected format
detected: ModelFormat,
/// Extension from filename
extension: String,
},
}
impl std::fmt::Display for FormatError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TooShort { len } => {
write!(
f,
"Data too short for format detection: {len} bytes (need 8)"
)
},
Self::UnknownFormat => write!(f, "Unknown model format (no magic bytes matched)"),
Self::HeaderTooLarge { size } => write!(
f,
"SafeTensors header too large: {size} bytes (max 100MB for DOS protection)"
),
Self::ExtensionMismatch {
detected,
extension,
} => {
write!(
f,
"Extension mismatch: detected {detected} but file has extension .{extension}"
)
},
}
}
}
impl std::error::Error for FormatError {}
/// APR format magic bytes (first 3 bytes, 4th is version)
///
/// APR v1: `APR1` (0x41505231)
/// APR v2: `APR2` (0x41505232)
/// Legacy: `APR\0` (0x41505200)
pub const APR_MAGIC: &[u8; 3] = b"APR";
/// GGUF format magic bytes
pub const GGUF_MAGIC: &[u8; 4] = b"GGUF";
/// Maximum SafeTensors header size (100MB for DOS protection per spec ยง7.1)
pub const MAX_SAFETENSORS_HEADER: u64 = 100_000_000;
/// Valid APR version bytes
const APR_VERSIONS: [u8; 4] = [b'N', b'1', b'2', 0];
/// Try to detect APR format from magic bytes
#[inline]
fn try_detect_apr(data: &[u8]) -> Option<ModelFormat> {
if data.len() >= 4
&& data.get(0..3).expect("length checked >= 4") == APR_MAGIC
&& APR_VERSIONS.contains(&data[3])
{
return Some(ModelFormat::Apr);
}
None
}
/// Try to detect GGUF format from magic bytes
#[inline]
fn try_detect_gguf(data: &[u8]) -> Option<ModelFormat> {
if data.len() >= 4 && data.get(0..4).expect("length checked >= 4") == GGUF_MAGIC {
return Some(ModelFormat::Gguf);
}
None
}
/// Try to detect SafeTensors format from header size
#[inline]
fn try_detect_safetensors(data: &[u8]) -> Result<Option<ModelFormat>, FormatError> {
let header_size = u64::from_le_bytes(data[0..8].try_into().expect("slice is exactly 8 bytes"));
if header_size > 0 && header_size < MAX_SAFETENSORS_HEADER {
return Ok(Some(ModelFormat::SafeTensors));
}
if header_size >= MAX_SAFETENSORS_HEADER {
return Err(FormatError::HeaderTooLarge { size: header_size });
}
Ok(None)
}
/// Detect model format from magic bytes (Jidoka: fail-fast)
///
/// Per spec ยง3.2: Format Detection
///
/// # Arguments
///
/// * `data` - First 8+ bytes of the model file
///
/// # Returns
///
/// Detected format or error
///
/// # Errors
///
/// Returns error if:
/// - Data is too short (<8 bytes)
/// - No known magic bytes detected
/// - SafeTensors header size exceeds limit (DOS protection)
///
/// # Example
///
/// ```
/// use realizar::format::{detect_format, ModelFormat};
///
/// // APR format
/// let apr_data = b"APR\0xxxxxxxxxxxx";
/// assert_eq!(detect_format(apr_data).expect("test"), ModelFormat::Apr);
///
/// // GGUF format
/// let gguf_data = b"GGUFxxxxxxxxxxxx";
/// assert_eq!(detect_format(gguf_data).expect("test"), ModelFormat::Gguf);
/// ```
pub fn detect_format(data: &[u8]) -> Result<ModelFormat, FormatError> {
if data.len() < 8 {
return Err(FormatError::TooShort { len: data.len() });
}
// Try each format in order of specificity
if let Some(format) = try_detect_apr(data) {
return Ok(format);
}
if let Some(format) = try_detect_gguf(data) {
return Ok(format);
}
if let Some(format) = try_detect_safetensors(data)? {
return Ok(format);
}
Err(FormatError::UnknownFormat)
}
/// Detect format from file path (using extension as hint, then verify magic)
///
/// # Arguments
///
/// * `path` - Path to model file
///
/// # Returns
///
/// Detected format (verified against magic bytes if data provided)
///
/// # Errors
///
/// Returns `FormatError::UnknownFormat` if extension is not recognized.
///
/// # Example
///
/// ```
/// use realizar::format::{detect_format_from_path, ModelFormat};
/// use std::path::Path;
///
/// assert_eq!(
/// detect_format_from_path(Path::new("model.apr")).expect("test"),
/// ModelFormat::Apr
/// );
/// ```
pub fn detect_format_from_path(path: &Path) -> Result<ModelFormat, FormatError> {
let extension = path.extension().and_then(|e| e.to_str()).unwrap_or("");
match extension.to_lowercase().as_str() {
"apr" => Ok(ModelFormat::Apr),
"gguf" => Ok(ModelFormat::Gguf),
"safetensors" => Ok(ModelFormat::SafeTensors),
_ => Err(FormatError::UnknownFormat),
}
}
/// Detect format from path and verify against data magic bytes
///
/// Per Jidoka: stop immediately if extension doesn't match magic
///
/// # Arguments
///
/// * `path` - Path to model file
/// * `data` - First 8+ bytes of model data
///
/// # Returns
///
/// Verified format or error if mismatch
///
/// # Errors
///
/// Returns error if:
/// - Format cannot be detected from magic bytes
/// - File extension doesn't match detected format
pub fn detect_and_verify_format(path: &Path, data: &[u8]) -> Result<ModelFormat, FormatError> {
let from_data = detect_format(data)?;
let from_path = detect_format_from_path(path);
// If path detection succeeded, verify it matches data
if let Ok(path_format) = from_path {
if path_format != from_data {
return Err(FormatError::ExtensionMismatch {
detected: from_data,
extension: path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("unknown")
.to_string(),
});
}
}
// Data-based detection is authoritative
Ok(from_data)
}
include!("format_detect.rs");