rsword_chirho 0.3.0

Core SWORD module library in pure Rust
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// For God so loved the world that he gave his only begotten Son,
// that whoever believes in him should not perish but have eternal life.
// John 3:16

//! Byte order utilities for reading SWORD module binary files.
//!
//! SWORD stores all binary data in little-endian format. This module provides
//! functions to read and write values in the SWORD format regardless of the
//! host machine's endianness.

use std::io::{self, Read, Write};

/// Read a little-endian u16 from SWORD format.
#[inline]
pub fn read_u16_sword_chirho(data_chirho: &[u8]) -> u16 {
    debug_assert!(data_chirho.len() >= 2);
    u16::from_le_bytes([data_chirho[0], data_chirho[1]])
}

/// Read a little-endian i16 from SWORD format.
#[inline]
pub fn read_i16_sword_chirho(data_chirho: &[u8]) -> i16 {
    debug_assert!(data_chirho.len() >= 2);
    i16::from_le_bytes([data_chirho[0], data_chirho[1]])
}

/// Read a little-endian u32 from SWORD format.
#[inline]
pub fn read_u32_sword_chirho(data_chirho: &[u8]) -> u32 {
    debug_assert!(data_chirho.len() >= 4);
    u32::from_le_bytes([data_chirho[0], data_chirho[1], data_chirho[2], data_chirho[3]])
}

/// Read a little-endian i32 from SWORD format.
#[inline]
pub fn read_i32_sword_chirho(data_chirho: &[u8]) -> i32 {
    debug_assert!(data_chirho.len() >= 4);
    i32::from_le_bytes([data_chirho[0], data_chirho[1], data_chirho[2], data_chirho[3]])
}

/// Read a little-endian u64 from SWORD format.
#[inline]
pub fn read_u64_sword_chirho(data_chirho: &[u8]) -> u64 {
    debug_assert!(data_chirho.len() >= 8);
    u64::from_le_bytes([
        data_chirho[0], data_chirho[1], data_chirho[2], data_chirho[3],
        data_chirho[4], data_chirho[5], data_chirho[6], data_chirho[7],
    ])
}

/// Read a little-endian i64 from SWORD format.
#[inline]
pub fn read_i64_sword_chirho(data_chirho: &[u8]) -> i64 {
    debug_assert!(data_chirho.len() >= 8);
    i64::from_le_bytes([
        data_chirho[0], data_chirho[1], data_chirho[2], data_chirho[3],
        data_chirho[4], data_chirho[5], data_chirho[6], data_chirho[7],
    ])
}

/// Write a u16 in little-endian SWORD format.
#[inline]
pub fn write_u16_sword_chirho(value_chirho: u16) -> [u8; 2] {
    value_chirho.to_le_bytes()
}

/// Write an i16 in little-endian SWORD format.
#[inline]
pub fn write_i16_sword_chirho(value_chirho: i16) -> [u8; 2] {
    value_chirho.to_le_bytes()
}

/// Write a u32 in little-endian SWORD format.
#[inline]
pub fn write_u32_sword_chirho(value_chirho: u32) -> [u8; 4] {
    value_chirho.to_le_bytes()
}

/// Write an i32 in little-endian SWORD format.
#[inline]
pub fn write_i32_sword_chirho(value_chirho: i32) -> [u8; 4] {
    value_chirho.to_le_bytes()
}

/// Write a u64 in little-endian SWORD format.
#[inline]
pub fn write_u64_sword_chirho(value_chirho: u64) -> [u8; 8] {
    value_chirho.to_le_bytes()
}

/// Write an i64 in little-endian SWORD format.
#[inline]
pub fn write_i64_sword_chirho(value_chirho: i64) -> [u8; 8] {
    value_chirho.to_le_bytes()
}

/// Extension trait for reading SWORD format values from a reader.
pub trait ReadSwordChirho: Read {
    /// Read a u16 in SWORD format.
    fn read_u16_sword_chirho(&mut self) -> io::Result<u16> {
        let mut buf_chirho = [0u8; 2];
        self.read_exact(&mut buf_chirho)?;
        Ok(read_u16_sword_chirho(&buf_chirho))
    }

    /// Read an i16 in SWORD format.
    fn read_i16_sword_chirho(&mut self) -> io::Result<i16> {
        let mut buf_chirho = [0u8; 2];
        self.read_exact(&mut buf_chirho)?;
        Ok(read_i16_sword_chirho(&buf_chirho))
    }

    /// Read a u32 in SWORD format.
    fn read_u32_sword_chirho(&mut self) -> io::Result<u32> {
        let mut buf_chirho = [0u8; 4];
        self.read_exact(&mut buf_chirho)?;
        Ok(read_u32_sword_chirho(&buf_chirho))
    }

    /// Read an i32 in SWORD format.
    fn read_i32_sword_chirho(&mut self) -> io::Result<i32> {
        let mut buf_chirho = [0u8; 4];
        self.read_exact(&mut buf_chirho)?;
        Ok(read_i32_sword_chirho(&buf_chirho))
    }

    /// Read a u64 in SWORD format.
    fn read_u64_sword_chirho(&mut self) -> io::Result<u64> {
        let mut buf_chirho = [0u8; 8];
        self.read_exact(&mut buf_chirho)?;
        Ok(read_u64_sword_chirho(&buf_chirho))
    }

    /// Read an i64 in SWORD format.
    fn read_i64_sword_chirho(&mut self) -> io::Result<i64> {
        let mut buf_chirho = [0u8; 8];
        self.read_exact(&mut buf_chirho)?;
        Ok(read_i64_sword_chirho(&buf_chirho))
    }
}

impl<R: Read + ?Sized> ReadSwordChirho for R {}

/// Extension trait for writing SWORD format values to a writer.
pub trait WriteSwordChirho: Write {
    /// Write a u16 in SWORD format.
    fn write_u16_sword_chirho(&mut self, value_chirho: u16) -> io::Result<()> {
        self.write_all(&write_u16_sword_chirho(value_chirho))
    }

    /// Write an i16 in SWORD format.
    fn write_i16_sword_chirho(&mut self, value_chirho: i16) -> io::Result<()> {
        self.write_all(&write_i16_sword_chirho(value_chirho))
    }

    /// Write a u32 in SWORD format.
    fn write_u32_sword_chirho(&mut self, value_chirho: u32) -> io::Result<()> {
        self.write_all(&write_u32_sword_chirho(value_chirho))
    }

    /// Write an i32 in SWORD format.
    fn write_i32_sword_chirho(&mut self, value_chirho: i32) -> io::Result<()> {
        self.write_all(&write_i32_sword_chirho(value_chirho))
    }

    /// Write a u64 in SWORD format.
    fn write_u64_sword_chirho(&mut self, value_chirho: u64) -> io::Result<()> {
        self.write_all(&write_u64_sword_chirho(value_chirho))
    }

    /// Write an i64 in SWORD format.
    fn write_i64_sword_chirho(&mut self, value_chirho: i64) -> io::Result<()> {
        self.write_all(&write_i64_sword_chirho(value_chirho))
    }
}

impl<W: Write + ?Sized> WriteSwordChirho for W {}

/// Index entry for RawVerse format (6 bytes).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RawVerseIndexChirho {
    /// Offset into the text file.
    pub offset_chirho: u32,
    /// Size of the verse text.
    pub size_chirho: u16,
}

impl RawVerseIndexChirho {
    /// Size of a RawVerse index entry in bytes.
    pub const SIZE_CHIRHO: usize = 6;

    /// Read from a byte slice.
    pub fn from_bytes_chirho(data_chirho: &[u8]) -> Self {
        debug_assert!(data_chirho.len() >= Self::SIZE_CHIRHO);
        Self {
            offset_chirho: read_u32_sword_chirho(&data_chirho[0..4]),
            size_chirho: read_u16_sword_chirho(&data_chirho[4..6]),
        }
    }

    /// Write to a byte array.
    pub fn to_bytes_chirho(&self) -> [u8; 6] {
        let mut buf_chirho = [0u8; 6];
        buf_chirho[0..4].copy_from_slice(&write_u32_sword_chirho(self.offset_chirho));
        buf_chirho[4..6].copy_from_slice(&write_u16_sword_chirho(self.size_chirho));
        buf_chirho
    }

    /// Check if this entry is empty (no text).
    pub fn is_empty_chirho(&self) -> bool {
        self.size_chirho == 0
    }
}

/// Index entry for RawVerse4 format (8 bytes).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RawVerse4IndexChirho {
    /// Offset into the text file.
    pub offset_chirho: u32,
    /// Size of the verse text.
    pub size_chirho: u32,
}

impl RawVerse4IndexChirho {
    /// Size of a RawVerse4 index entry in bytes.
    pub const SIZE_CHIRHO: usize = 8;

    /// Read from a byte slice.
    pub fn from_bytes_chirho(data_chirho: &[u8]) -> Self {
        debug_assert!(data_chirho.len() >= Self::SIZE_CHIRHO);
        Self {
            offset_chirho: read_u32_sword_chirho(&data_chirho[0..4]),
            size_chirho: read_u32_sword_chirho(&data_chirho[4..8]),
        }
    }

    /// Write to a byte array.
    pub fn to_bytes_chirho(&self) -> [u8; 8] {
        let mut buf_chirho = [0u8; 8];
        buf_chirho[0..4].copy_from_slice(&write_u32_sword_chirho(self.offset_chirho));
        buf_chirho[4..8].copy_from_slice(&write_u32_sword_chirho(self.size_chirho));
        buf_chirho
    }

    /// Check if this entry is empty (no text).
    pub fn is_empty_chirho(&self) -> bool {
        self.size_chirho == 0
    }
}

/// Index entry for zText compressed verse format (10 bytes).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ZVerseIndexChirho {
    /// Block number containing this verse.
    pub block_num_chirho: u32,
    /// Offset within the uncompressed block.
    pub offset_in_block_chirho: u32,
    /// Size of the verse in uncompressed form.
    pub size_chirho: u16,
}

impl ZVerseIndexChirho {
    /// Size of a zVerse index entry in bytes.
    pub const SIZE_CHIRHO: usize = 10;

    /// Read from a byte slice.
    pub fn from_bytes_chirho(data_chirho: &[u8]) -> Self {
        debug_assert!(data_chirho.len() >= Self::SIZE_CHIRHO);
        Self {
            block_num_chirho: read_u32_sword_chirho(&data_chirho[0..4]),
            offset_in_block_chirho: read_u32_sword_chirho(&data_chirho[4..8]),
            size_chirho: read_u16_sword_chirho(&data_chirho[8..10]),
        }
    }

    /// Write to a byte array.
    pub fn to_bytes_chirho(&self) -> [u8; 10] {
        let mut buf_chirho = [0u8; 10];
        buf_chirho[0..4].copy_from_slice(&write_u32_sword_chirho(self.block_num_chirho));
        buf_chirho[4..8].copy_from_slice(&write_u32_sword_chirho(self.offset_in_block_chirho));
        buf_chirho[8..10].copy_from_slice(&write_u16_sword_chirho(self.size_chirho));
        buf_chirho
    }

    /// Check if this entry is empty (no text).
    pub fn is_empty_chirho(&self) -> bool {
        self.size_chirho == 0
    }
}

/// Block index entry for zText compressed format (12 bytes).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ZBlockIndexChirho {
    /// Offset of compressed data in the .czz file.
    pub comp_offset_chirho: u32,
    /// Size of compressed data.
    pub comp_size_chirho: u32,
    /// Size of uncompressed data.
    pub uncomp_size_chirho: u32,
}

impl ZBlockIndexChirho {
    /// Size of a zBlock index entry in bytes.
    pub const SIZE_CHIRHO: usize = 12;

    /// Read from a byte slice.
    pub fn from_bytes_chirho(data_chirho: &[u8]) -> Self {
        debug_assert!(data_chirho.len() >= Self::SIZE_CHIRHO);
        Self {
            comp_offset_chirho: read_u32_sword_chirho(&data_chirho[0..4]),
            comp_size_chirho: read_u32_sword_chirho(&data_chirho[4..8]),
            uncomp_size_chirho: read_u32_sword_chirho(&data_chirho[8..12]),
        }
    }

    /// Write to a byte array.
    pub fn to_bytes_chirho(&self) -> [u8; 12] {
        let mut buf_chirho = [0u8; 12];
        buf_chirho[0..4].copy_from_slice(&write_u32_sword_chirho(self.comp_offset_chirho));
        buf_chirho[4..8].copy_from_slice(&write_u32_sword_chirho(self.comp_size_chirho));
        buf_chirho[8..12].copy_from_slice(&write_u32_sword_chirho(self.uncomp_size_chirho));
        buf_chirho
    }

    /// Check if this block is empty.
    pub fn is_empty_chirho(&self) -> bool {
        self.comp_size_chirho == 0
    }
}

/// Index entry for zText4 compressed verse format (12 bytes).
/// Uses 4-byte size field instead of 2-byte to support larger entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ZVerse4IndexChirho {
    /// Block number containing this verse.
    pub block_num_chirho: u32,
    /// Offset within the uncompressed block.
    pub offset_in_block_chirho: u32,
    /// Size of the verse in uncompressed form (4-byte).
    pub size_chirho: u32,
}

impl ZVerse4IndexChirho {
    /// Size of a zVerse4 index entry in bytes.
    pub const SIZE_CHIRHO: usize = 12;

    /// Read from a byte slice.
    pub fn from_bytes_chirho(data_chirho: &[u8]) -> Self {
        debug_assert!(data_chirho.len() >= Self::SIZE_CHIRHO);
        Self {
            block_num_chirho: read_u32_sword_chirho(&data_chirho[0..4]),
            offset_in_block_chirho: read_u32_sword_chirho(&data_chirho[4..8]),
            size_chirho: read_u32_sword_chirho(&data_chirho[8..12]),
        }
    }

    /// Write to a byte array.
    pub fn to_bytes_chirho(&self) -> [u8; 12] {
        let mut buf_chirho = [0u8; 12];
        buf_chirho[0..4].copy_from_slice(&write_u32_sword_chirho(self.block_num_chirho));
        buf_chirho[4..8].copy_from_slice(&write_u32_sword_chirho(self.offset_in_block_chirho));
        buf_chirho[8..12].copy_from_slice(&write_u32_sword_chirho(self.size_chirho));
        buf_chirho
    }

    /// Check if this entry is empty (no text).
    pub fn is_empty_chirho(&self) -> bool {
        self.size_chirho == 0
    }
}

#[cfg(test)]
mod tests_chirho {
    use super::*;
    use std::io::Cursor;

    #[test]
    fn test_u16_roundtrip_chirho() {
        let value_chirho = 0x1234u16;
        let bytes_chirho = write_u16_sword_chirho(value_chirho);
        assert_eq!(bytes_chirho, [0x34, 0x12]); // Little-endian
        assert_eq!(read_u16_sword_chirho(&bytes_chirho), value_chirho);
    }

    #[test]
    fn test_u32_roundtrip_chirho() {
        let value_chirho = 0x12345678u32;
        let bytes_chirho = write_u32_sword_chirho(value_chirho);
        assert_eq!(bytes_chirho, [0x78, 0x56, 0x34, 0x12]); // Little-endian
        assert_eq!(read_u32_sword_chirho(&bytes_chirho), value_chirho);
    }

    #[test]
    fn test_i32_roundtrip_chirho() {
        let value_chirho = -12345i32;
        let bytes_chirho = write_i32_sword_chirho(value_chirho);
        assert_eq!(read_i32_sword_chirho(&bytes_chirho), value_chirho);
    }

    #[test]
    fn test_raw_verse_index_chirho() {
        let index_chirho = RawVerseIndexChirho {
            offset_chirho: 0x1000,
            size_chirho: 0x0100,
        };
        let bytes_chirho = index_chirho.to_bytes_chirho();
        assert_eq!(bytes_chirho.len(), RawVerseIndexChirho::SIZE_CHIRHO);

        let parsed_chirho = RawVerseIndexChirho::from_bytes_chirho(&bytes_chirho);
        assert_eq!(parsed_chirho, index_chirho);
    }

    #[test]
    fn test_raw_verse4_index_chirho() {
        let index_chirho = RawVerse4IndexChirho {
            offset_chirho: 0x10000000,
            size_chirho: 0x00010000,
        };
        let bytes_chirho = index_chirho.to_bytes_chirho();
        assert_eq!(bytes_chirho.len(), RawVerse4IndexChirho::SIZE_CHIRHO);

        let parsed_chirho = RawVerse4IndexChirho::from_bytes_chirho(&bytes_chirho);
        assert_eq!(parsed_chirho, index_chirho);
    }

    #[test]
    fn test_z_verse_index_chirho() {
        let index_chirho = ZVerseIndexChirho {
            block_num_chirho: 42,
            offset_in_block_chirho: 1024,
            size_chirho: 256,
        };
        let bytes_chirho = index_chirho.to_bytes_chirho();
        assert_eq!(bytes_chirho.len(), ZVerseIndexChirho::SIZE_CHIRHO);

        let parsed_chirho = ZVerseIndexChirho::from_bytes_chirho(&bytes_chirho);
        assert_eq!(parsed_chirho, index_chirho);
    }

    #[test]
    fn test_z_block_index_chirho() {
        let index_chirho = ZBlockIndexChirho {
            comp_offset_chirho: 0x1000,
            comp_size_chirho: 0x0200,
            uncomp_size_chirho: 0x0800,
        };
        let bytes_chirho = index_chirho.to_bytes_chirho();
        assert_eq!(bytes_chirho.len(), ZBlockIndexChirho::SIZE_CHIRHO);

        let parsed_chirho = ZBlockIndexChirho::from_bytes_chirho(&bytes_chirho);
        assert_eq!(parsed_chirho, index_chirho);
    }

    #[test]
    fn test_reader_extension_chirho() {
        let data_chirho = vec![0x34, 0x12, 0x78, 0x56, 0x34, 0x12];
        let mut cursor_chirho = Cursor::new(data_chirho);

        assert_eq!(cursor_chirho.read_u16_sword_chirho().unwrap(), 0x1234);
        assert_eq!(cursor_chirho.read_u32_sword_chirho().unwrap(), 0x12345678);
    }

    #[test]
    fn test_writer_extension_chirho() {
        let mut buf_chirho = Vec::new();
        buf_chirho.write_u16_sword_chirho(0x1234).unwrap();
        buf_chirho.write_u32_sword_chirho(0x12345678).unwrap();

        assert_eq!(buf_chirho, vec![0x34, 0x12, 0x78, 0x56, 0x34, 0x12]);
    }
}