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
//! Palm database record headers
//!
//! This module contains the [`PdbRecordHeader`] enum, representing either a "record" or a
//! "resource" entry within a Palm database file. See the documentation for that enum for more
//! details.

use core::{fmt::Debug, str};
use std::io::{self, Cursor, Read, Seek, SeekFrom};

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

use super::sealed::DatabaseRecordHelpers;
use crate::{header::DatabaseHeader, record::DatabaseRecord};

/// Attributes used for Palm Database Records (not Resources)
///
/// This is a more human-friendly representation of the attributes byte
/// used by the on-disk record header format. Conversion to/from
/// the attributes byte is handled by the respective `From<u8>`
/// and `From<RecordAttributes>` implementations.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RecordAttributes {
	pub delete: bool,
	pub dirty: bool,
	pub busy: bool,
	pub secret: bool,

	pub category: u8,
}

impl RecordAttributes {
	const SECRET: u8 = 0x10;
	const BUSY: u8 = 0x20;
	const DIRTY: u8 = 0x40;
	const DELETE: u8 = 0x80;

	const CATEGORY_MASK: u8 = 0x0F;
}

impl From<u8> for RecordAttributes {
	fn from(value: u8) -> Self {
		Self {
			delete: (value & Self::DELETE) != 0,
			dirty: (value & Self::DIRTY) != 0,
			busy: (value & Self::BUSY) != 0,
			secret: (value & Self::SECRET) != 0,
			category: value & Self::CATEGORY_MASK,
		}
	}
}

impl From<RecordAttributes> for u8 {
	fn from(value: RecordAttributes) -> Self {
		let RecordAttributes {
			delete,
			dirty,
			busy,
			secret,
			category,
		} = value;

		let mut attributes = category;

		if delete {
			attributes |= RecordAttributes::DELETE
		}
		if dirty {
			attributes |= RecordAttributes::DIRTY
		}
		if busy {
			attributes |= RecordAttributes::BUSY
		}
		if secret {
			attributes |= RecordAttributes::SECRET
		}

		attributes
	}
}

/// Generic Palm database record header
///
/// This type can represent either a "record" or a "resource" within a Palm OS database:
///
/// - Records are potentially-mutable entries that are used in PDB databases to represent content;
/// - Resources are generally-immutable data entries that are used, as the name would suggest, for
///   application resources, and are found within PRC files.
///
/// Which type to use to decode a given record header should be determined by the lowest bit in the
/// `attributes` field of the [`DatabaseHeader`] for this record's containing database - if that
/// bit is cleared, "records" are used; if it is set, "resources" are used.
///
/// These two types are presented as an `enum` for the sake of flexibility, and to allow sharing
/// encoding/decoding code between the two available record header types.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PdbRecordHeader {
	Record {
		attributes: RecordAttributes,
		unique_id: u32,
		data_offset: u32,
		data_len: Option<u32>,
	},

	Resource {
		name: [u8; 4],
		record_id: u16,
		data_offset: u32,
		data_len: Option<u32>,
	},
}

impl DatabaseRecordHelpers for PdbRecordHeader {
	fn struct_len(&self) -> usize {
		match self {
			PdbRecordHeader::Record { .. } => 8,
			PdbRecordHeader::Resource { .. } => 10,
		}
	}

	fn next_entry_data_offset(&self) -> usize {
		match self {
			PdbRecordHeader::Record { .. } => 0,
			PdbRecordHeader::Resource { .. } => 6,
		}
	}

	fn data_offset(&self) -> u32 {
		match self {
			Self::Record { data_offset, .. } => *data_offset,
			Self::Resource { data_offset, .. } => *data_offset,
		}
	}
}

impl DatabaseRecord for PdbRecordHeader {
	fn from_bytes(hdr: &DatabaseHeader, rdr: &mut Cursor<&[u8]>) -> Result<Self, io::Error> {
		let mut this = if hdr.attributes & (1 << 0) == 0 {
			// Type bit clear: construct "records"

			let data_offset = rdr.read_u32::<BigEndian>()?;
			let attributes = rdr.read_u8()?.into();
			let unique_id = rdr.read_u24::<BigEndian>()?;

			Self::Record {
				attributes,
				unique_id,
				data_offset,
				data_len: None,
			}
		} else {
			// Type bit set: construct "resources"

			let name = {
				let mut buf = [0u8; 4];
				rdr.read_exact(&mut buf)?;
				buf
			};

			let record_id = rdr.read_u16::<BigEndian>()?;
			let data_offset = rdr.read_u32::<BigEndian>()?;

			Self::Resource {
				name,
				record_id,
				data_offset,
				data_len: None,
			}
		};

		let actual_len = {
			let data_offset = match this {
				Self::Record { data_offset, .. } => data_offset,
				Self::Resource { data_offset, .. } => data_offset,
			};

			let position = rdr.stream_position()?;
			let data_len = match rdr.seek(SeekFrom::Current(this.next_entry_data_offset() as i64)) {
				Ok(_) => match dbg!(rdr.read_u32::<BigEndian>()) {
					Ok(next_offset) => {
						if next_offset > dbg!(data_offset) {
							std::cmp::min(next_offset, rdr.get_ref().len() as u32) - data_offset
						} else {
							(rdr.get_ref().len() as u32) - data_offset
						}
					}

					Err(_) => (rdr.get_ref().len() as u32) - data_offset,
				},

				Err(_) => (rdr.get_ref().len() as u32) - data_offset,
			};

			rdr.seek(SeekFrom::Start(position))?;
			data_len
		};

		match this {
			Self::Record {
				ref mut data_len, ..
			} => data_len.replace(actual_len),
			Self::Resource {
				ref mut data_len, ..
			} => data_len.replace(actual_len),
		};

		Ok(this)
	}

	fn to_bytes(&self) -> Result<Vec<u8>, io::Error> {
		let mut buf = Cursor::new(Vec::with_capacity(self.struct_len()));

		match self {
			Self::Record {
				data_offset,
				attributes,
				unique_id,
				..
			} => {
				buf.write_u32::<BigEndian>(*data_offset)?;
				buf.write_u8(u8::from(*attributes))?;
				buf.write_u24::<BigEndian>(*unique_id)?;
			}

			Self::Resource {
				name,
				record_id,
				data_offset,
				..
			} => {
				// name
				buf.write_u8(name[0])?;
				buf.write_u8(name[1])?;
				buf.write_u8(name[2])?;
				buf.write_u8(name[3])?;

				// record id & data offset
				buf.write_u16::<BigEndian>(*record_id)?;
				buf.write_u32::<BigEndian>(*data_offset)?;
			}
		}

		Ok(buf.into_inner())
	}

	fn name_str(&self) -> Option<&str> {
		match self {
			Self::Resource { name, .. } => {
				let mut idx = 0;
				while idx < name.len() {
					if name[idx] == 0u8 {
						break;
					}

					idx += 1;
				}

				str::from_utf8(&name[..idx]).ok()
			}

			_ => None,
		}
	}

	fn attributes(&self) -> Option<RecordAttributes> {
		match self {
			Self::Record { attributes, .. } => Some(*attributes),
			_ => None,
		}
	}

	fn data_len(&self) -> Option<u32> {
		match self {
			Self::Record { data_len, .. } => *data_len,
			Self::Resource { data_len, .. } => *data_len,
		}
	}

	fn unique_id(&self) -> Option<u32> {
		match self {
			PdbRecordHeader::Record { unique_id, .. } => Some(*unique_id),
			PdbRecordHeader::Resource { .. } => None,
		}
	}

	fn resource_id(&self) -> Option<u16> {
		match self {
			PdbRecordHeader::Record { .. } => None,
			PdbRecordHeader::Resource { record_id, .. } => Some(*record_id),
		}
	}

	fn construct_record(
		attributes: RecordAttributes,
		unique_id: u32,
		data_offset: u32,
		data_len: Option<u32>,
	) -> Self {
		let mut record_initial = PdbRecordHeader::Record {
			attributes,
			unique_id,
			data_offset,
			data_len,
		};

		let struct_offset = record_initial.struct_len() as u32;
		if let PdbRecordHeader::Record {
			ref mut data_offset,
			..
		} = record_initial
		{
			*data_offset += struct_offset;
		}
		record_initial
	}

	fn construct_resource(
		name: &[u8; 4],
		record_id: u16,
		data_offset: u32,
		data_len: Option<u32>,
	) -> Self {
		let mut initial = PdbRecordHeader::Resource {
			name: *name,
			record_id,
			data_offset,
			data_len,
		};

		let struct_offset = initial.struct_len() as u32;
		if let PdbRecordHeader::Resource {
			ref mut data_offset,
			..
		} = initial
		{
			*data_offset += struct_offset;
		}

		initial
	}
}

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

	use crate::{
		header::DATABASE_HEADER_LENGTH,
		record::{pdb_record::PdbRecordHeader, DatabaseRecord, DatabaseRecordHelpers},
		DatabaseFormat, PalmDatabase, PdbDatabase, PdbWithCategoriesDatabase, PrcDatabase,
	};

	const EXAMPLE_PRC: &'static [u8] = include_bytes!("../../../test-data/hello-v1.prc");
	const EXAMPLE_PDB: &'static [u8] = include_bytes!("../../../test-data/ToDoDB.pdb");
	const MANUAL_PDB: &'static [u8] = include_bytes!("../../../test-data/tWmanual.pdb");

	fn test_db<T: DatabaseFormat>(src_bytes: &'static [u8])
	where
		<T as DatabaseFormat>::RecordHeader: PartialEq<PdbRecordHeader>,
	{
		let database = PalmDatabase::<T>::from_bytes(src_bytes).unwrap();

		eprintln!(
			"Testing records for: {}",
			database.header.name_try_str().unwrap()
		);
		let mut rec_start_offset = DATABASE_HEADER_LENGTH;
		let mut cursor = Cursor::new(database.original_data.as_slice());
		cursor.set_position(rec_start_offset as u64);
		// Test record iteration
		for (_idx, (rec_hdr, rec_data)) in (0..).zip(database.list_records_resources().iter()) {
			assert_eq!(rec_data.len(), rec_hdr.data_len().unwrap_or(0) as usize);

			assert_eq!(
				rec_hdr,
				&PdbRecordHeader::from_bytes(&database.header, &mut cursor).unwrap()
			);
			rec_start_offset += rec_hdr.struct_len();
		}
	}

	#[test]
	fn test_records_all_db_types() {
		test_db::<PrcDatabase>(&EXAMPLE_PRC);
		test_db::<PdbDatabase>(&MANUAL_PDB);
		test_db::<PdbWithCategoriesDatabase>(&EXAMPLE_PDB);
	}
}