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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use std::collections::HashMap;
use std::sync::Arc;
use crate::error::Error;
use crate::result::Result;
use manual_serializer::*;
use crate::utils::*;
use std::fmt;
use crate::resources::Resource;

/// Helper representing structure data header used in 
/// [`VS_VERSIONINFO`](https://learn.microsoft.com/en-us/windows/win32/menurc/vs-versioninfo) 
/// and all related data structures.
#[derive(Debug)]
pub struct Header {
    pub length : usize,
    pub value_length: usize,
    pub data_type : DataType,
    pub key : String,
    pub last : usize,
}

impl Header {
    pub fn new(
        length : usize,
        value_length : usize,
        data_type : DataType,
        key : &str,
    ) -> Header {
        Header {
            length,
            value_length,
            data_type,
            key : key.to_string(),
            last : 0
        }
    }
}

impl TrySerialize for Header {
    type Error = Error;
    fn try_serialize(&self, dest: &mut Serializer) -> Result<()> {
        dest.try_align_u32()?;
        dest.try_store_u16le(self.length as u16)?;
        dest.try_store_u16le(self.value_length as u16)?;
        match self.data_type {
            DataType::Binary => {
                dest.try_store_u16le(0)?;
            },
            DataType::Text => {
                dest.try_store_u16le(1)?;
            }
        };
        dest.try_store_utf16le_sz(&self.key)?;
        dest.try_align_u32()?;
        Ok(())
    }
}

impl TryDeserialize for Header {
    type Error = Error;

    fn try_deserialize(src: &mut Deserializer) -> Result<Header> {
        src.try_align_u32()?;

        let cursor = src.cursor();
        let length = src.try_load_u16le()? as usize;
        let value_length = src.try_load_u16le()? as usize;
        let data_type = src.try_load_u16le()?;
        // println!("@ cursor: {cursor} length: {length} value_length: {value_length} data_type: {data_type}");
        let data_type = match data_type {
            0 => DataType::Binary,
            1 => DataType::Text,
            _ => return Err(format!("Header::try_deserealize(): invalid resource data type (must be 1 or 0) - possible misalignment/corruption").into())
        };
        let key = src.try_load_utf16le_sz()?;

        let padding = src.cursor() % 4;
        src.try_offset(padding)?;
        let last = cursor + length;

        let header = Header {length,value_length,data_type,key,last};
        // println!("{:#?}", header);
        Ok(header)
    }
}


fn try_build_struct(
    key : &str,
    data_type : DataType,
    value_len: usize,
    value : &[u8]
) -> Result<Vec<u8>> {
    let mut dest = Serializer::new(4096);
    let header = Header::new(0,0,data_type,key);
    dest.try_store(&header)?;
    dest.try_store_u8_slice(value)?;
    let mut vec = dest.to_vec();
    store_u16le(&mut vec[0..2], dest.len() as u16);
    store_u16le(&mut vec[2..4], value_len as u16);
    Ok(vec)
}

/// Windows Version value represented as `[u16;4]` array.
/// This version struct is helpful for serialization and 
/// deserialization of the versions packed into a `u64` value
/// represented as 2 LE-encoded `u32` (DWORD) values.
#[derive(Debug, Clone)]
pub struct Version([u16;4]);

impl Default for Version {
    fn default() -> Self {
        Version([0,0,0,0])
    }
}

impl TryDeserialize for Version {
    type Error = Error;
    fn try_deserialize(src:&mut Deserializer) -> Result<Self> {
        let ms = src.try_load_u32le()?;
        let ls = src.try_load_u32le()?;
        Ok(Version([(ms >> 16) as u16, (ms & 0xffff) as u16, (ls >> 16) as u16, (ls & 0xffff) as u16]))
    }
}

impl TrySerialize for Version {
    type Error = Error;
    fn try_serialize(&self, dest:&mut Serializer) -> Result<()> {
        dest.try_store_u32le((self.0[0] as u32) << 16 | (self.0[1] as u32))?;
        dest.try_store_u32le((self.0[2] as u32) << 16 | (self.0[3] as u32))?;
        Ok(())
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f : &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        if self.0[3] == 0 { 
            write!(f,"{}.{}.{}",self.0[0],self.0[1],self.0[2])?;
        } else {
            write!(f,"{}.{}.{}.{}",self.0[0],self.0[1],self.0[2],self.0[3])?;
        }
        Ok(())
    }
}

/// Date helper used for serializing and deserializing 2 LE-encoded u32
/// values into a single `u64` value.
#[derive(Debug, Clone)]
pub struct Date(u64);

impl Default for Date {
    fn default() -> Self {
        Date(0)
    }
}

impl TryDeserialize for Date {
    type Error = Error;
    fn try_deserialize(src:&mut Deserializer) -> Result<Self> {
        let ms = src.try_load_u32le()? as u64;
        let ls = src.try_load_u32le()? as u64;
        Ok(Date(ms << 32 | ls))
    }
}

impl TrySerialize for Date {
    type Error = Error;
    fn try_serialize(&self, dest:&mut Serializer) -> Result<()> {
        dest.try_store_u32le((self.0 >> 32) as u32)?;
        dest.try_store_u32le((self.0 & 0xffffffff) as u32)?;
        Ok(())
    }
}

impl fmt::Display for Date {
    fn fmt(&self, f : &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        write!(f,"{}",self.0)?;
        Ok(())
    }
}


/// Rust representation of [`VS_FIXEDFILEINFO`](https://learn.microsoft.com/en-us/windows/win32/api/VerRsrc/ns-verrsrc-vs_fixedfileinfo)
/// structure.
#[derive(Debug, Clone)]
pub struct FileInfo {
    pub signature : u32,
    pub struc_version : u32,
    pub file_version : Version,
    pub product_version : Version,
    pub file_flags_mask : u32,
    pub file_flags : u32,
    pub file_os : u32,
    pub file_type : u32,
    pub file_subtype : u32,
    pub file_date : Date,
}

impl Default for FileInfo {
    fn default() -> Self {
        FileInfo {
            signature: 0xfeef04bd,
            struc_version: 0,
            file_version: Version::default(),
            product_version: Version::default(),
            file_flags_mask: 0,
            file_flags: 0,
            file_os: 0,
            file_type: 0,
            file_subtype: 0,
            file_date: Date::default(),
        }
    }
}

impl FileInfo {
    pub fn print(&self) {
        println!("signature: 0x{:x}", self.signature);
        println!("struc_version: 0x{:x}", self.struc_version);
        println!("file_version: {}", self.file_version);
        println!("product_version: {}", self.product_version);
        println!("file_flags_mask: 0x{:x}", self.file_flags_mask);
        println!("file_flags: 0x{:x}", self.file_flags);
        println!("file_os: 0x{:x}", self.file_os);
        println!("file_type: 0x{:x}", self.file_type);
        println!("file_subtype: 0x{:x}", self.file_subtype);
        println!("file_date: {}", self.file_date);
    }

}
// impl TryFrom<&mut Deserializer<'_>> for FileInfo {
impl TryDeserialize for FileInfo {
    type Error = Error;
    fn try_deserialize(src: &mut Deserializer) -> Result<FileInfo> {
        // let src = Deserializer::new(data);

        let info = FileInfo {
            signature : src.try_load_u32le()?,
            struc_version : src.try_load_u32le()?,
            file_version : src.try_load()?,
            product_version : src.try_load()?,
            file_flags_mask : src.try_load_u32le()?,
            file_flags : src.try_load_u32le()?,
            file_os : src.try_load_u32le()?,
            file_type : src.try_load_u32le()?,
            file_subtype : src.try_load_u32le()?,
            file_date : src.try_load()?,
        };

        if info.signature != 0xfeef04bd {
            return Err(format!("FileInfo: invalid signature 0x{:8x}", info.signature).into());
        }

        Ok(info)
    }
}

impl TrySerialize for FileInfo {
    type Error = Error;
    fn try_serialize(&self, dest: &mut Serializer) -> Result<()> {
        dest
            .try_store_u32le(self.signature)?
            .try_store_u32le(self.struc_version)?
            .try_store(&self.file_version)?
            .try_store(&self.product_version)?
            .try_store_u32le(self.file_flags_mask)?
            .try_store_u32le(self.file_flags)?
            .try_store_u32le(self.file_os)?
            .try_store_u32le(self.file_type)?
            .try_store_u32le(self.file_subtype)?
            .try_store(&self.file_date)?;

        Ok(())
    }
}

/// Helper enum for serializing and deserializing StringFileInfo and VarFileInfo child structures.
#[derive(Debug, Clone)]
pub enum VersionInfoChild {
    StringFileInfo {
        tables : HashMap<String, HashMap<String,Data>>
    },
    VarFileInfo {
        vars : HashMap<String,Vec<u32>>    
    },
}

/// Wrapper of the underlying data that may be represented as a binary buffer or a text string.
#[derive(Debug, Clone)]
pub enum Data {
    Binary(Vec<u8>),
    Text(String)
}

impl TrySerialize for VersionInfoChild {
    type Error = Error;
    fn try_serialize(&self, dest: &mut Serializer) -> Result<()> {

        match self {
            VersionInfoChild::StringFileInfo { tables } => {
                for (key_lang,map) in tables {
                    let mut lang_records = Serializer::default();
                    for (key_record, data) in map {
                        let (data_type,data) = match data {
                            Data::Binary(data) => {
                                (DataType::Binary,data.clone())
                            },
                            Data::Text(text) => {
                                (DataType::Text,string_to_u8vec_sz(text))
                            },
                        };

                        let string_record = try_build_struct(key_record,data_type,data.len()/2,&data)?;
                        lang_records.try_align_u32()?;
                        lang_records.try_store_u8_slice(&string_record)?;
                    }
                    
                    let string_table = try_build_struct(key_lang,DataType::Binary,0,&lang_records.to_vec())?;
                    let string_file_info = try_build_struct("StringFileInfo",DataType::Binary,0,&string_table)?;
                    dest.try_align_u32()?;
                    dest.try_store_u8_slice(&string_file_info)?;
                }
            },
            VersionInfoChild::VarFileInfo { vars } => {
                let mut var_records = Serializer::default();
                for (k,data) in vars {
                    let var_record = try_build_struct(k,DataType::Binary,data.len()/2,&u32slice_to_u8vec(data))?;
                    var_records.try_align_u32()?;
                    var_records.try_store_u8_slice(&var_record)?;
                }
                let var_file_info = try_build_struct("VarFileInfo",DataType::Binary,0,&var_records.to_vec())?;
                dest.try_align_u32()?;
                dest.try_store_u8_slice(&var_file_info)?;
            }
        }

        Ok(())
    }
}

impl TryDeserialize for VersionInfoChild {
    type Error = Error;
    fn try_deserialize(src: &mut Deserializer) -> Result<VersionInfoChild> {

        let header: Header = src.try_load()?;

        let data = match header.key.as_str() {
            "StringFileInfo" => {
                let mut tables = HashMap::new();
                while src.cursor() < header.last {

                    let string_table_header: Header = src.try_load()?;
                    let lang = string_table_header.key;
                    let mut data = HashMap::new();
        
                    while src.cursor() < string_table_header.last {
                        let string_header: Header = src.try_load()?;
                        match string_header.data_type {
                            DataType::Binary => {
                                let len = string_header.value_length*2;
                                let vec = src.try_load_u8_vec(len)?;
                                data.insert(string_header.key, Data::Binary(vec));
                            },
                            DataType::Text => {
                                let text = src.try_load_utf16le_sz()?;
                                data.insert(string_header.key, Data::Text(text));
                            }
                        };
                    }
        
                    tables.insert(lang, data);
                }
    
                VersionInfoChild::StringFileInfo { tables }
            },
            "VarFileInfo" => {

                let mut vars = HashMap::new();
                while src.cursor() < header.last {
                    // let var_header = Header::try_from(&mut *src)?;
                    let var_header: Header = src.try_load()?;
        
                    let mut values = Vec::new();
                    while src.cursor() < var_header.last {
                        values.push(src.try_load_u32le()?);
                    }
        
                    vars.insert(var_header.key, values);
                }

                VersionInfoChild::VarFileInfo { vars }
            },
            _ => return Err(format!("Unknown child type: {}", header.key).into())
        };

        Ok(data)

    }

}


/// Data type flag indicating if data is encoded as a binary or a text string.
#[derive(Debug, Clone)]
pub enum DataType {
    Binary,
    Text,
}

/// [`VS_VERSIONINFO`](https://learn.microsoft.com/en-us/windows/win32/menurc/vs-versioninfo) 
/// resource structure representation.
#[derive(Debug, Clone)]
pub struct VersionInfo {
    /// Associated [`Resource`]
    pub resource : Arc<Resource>,
    /// Binary or text data type
    pub data_type : DataType,
    /// resource key string (language code-page hex string)
    pub key : String,
    /// VS_FIXEDFILEINFO representation.
    /// [https://learn.microsoft.com/en-us/windows/win32/api/VerRsrc/ns-verrsrc-vs_fixedfileinfo](https://learn.microsoft.com/en-us/windows/win32/api/VerRsrc/ns-verrsrc-vs_fixedfileinfo)
    pub info : FileInfo,
    /// VS_VERSIONINFO child structures. One or multiple of:
    /// - [StringFileInfo](https://learn.microsoft.com/en-us/windows/win32/menurc/stringfileinfo)
    /// - [VarFileInfo](https://learn.microsoft.com/en-us/windows/win32/menurc/varfileinfo)
    pub children : Vec<VersionInfoChild>,
}

impl TryFrom<Arc<Resource>> for VersionInfo {
    type Error = Error;
    fn try_from(resource: Arc<Resource>) -> Result<VersionInfo> {
        // let clone = resource.clone();
        let data = resource.encoded.lock().unwrap();
        let mut src = Deserializer::new(&data);

        let header: Header = src.try_load()?;
        let info :FileInfo = src.try_load()?;
        let skip = src.cursor() % 4;
        src.try_offset(skip)?;

        let mut children = Vec::new();
        let mut remaining = src.remaining();
        while remaining > 0 {
            let child: VersionInfoChild = src.try_load()?;
            children.push(child);
            remaining = src.remaining();
        }

        let info = VersionInfo {
            resource : resource.clone(),
            data_type : header.data_type,
            key : header.key,
            info,
            children
        };

        Ok(info)

    }
}

impl VersionInfo {
    pub fn try_to_vec(&self) -> Result<Vec<u8>> {
        let mut dest = Serializer::default();

        let mut child_data = Serializer::default();
        for child in &self.children {
            child_data.try_store(child)?;
            child_data.try_align_u32()?;
        }
        let child_data = child_data.to_vec();

        let file_info_data = Serializer::default()
            .try_store(&self.info)?
            .to_vec();

        let data = Serializer::default()
            .try_store_u8_slice(&file_info_data)?
            .try_align_u32()?
            .try_store_u8_slice(&child_data)?
            .to_vec();

        let version_info = try_build_struct("VS_VERSION_INFO",DataType::Binary,file_info_data.len(),&data)?;
        dest.try_store_u8_slice(&version_info)?;

        Ok(dest.to_vec())
    }

    pub fn set_file_version(&mut self, v : &[u16;4]) -> &mut Self {
        self.info.file_version = Version(*v);
        self.insert_string("FileVersion", &Version(*v).to_string());
        self
    }
    
    pub fn set_product_version(&mut self, v : &[u16;4]) -> &mut Self {
        self.info.product_version = Version(*v);
        self.insert_string("ProductVersion", &Version(*v).to_string());
        self
    }

    pub fn set_version(&mut self, v: &[u16;4]) -> &mut Self {
        self.set_file_version(v);
        self.set_product_version(v);
        self
    }

    pub fn replace_string(&mut self, key: &str, text: &str) -> &mut Self {
        for child in self.children.iter_mut() {
            match child {
                VersionInfoChild::StringFileInfo { tables } => {
                    for (_, table) in tables {
                        if let Some(_) = table.get(key) {
                            table.insert(key.to_string(), Data::Text(text.to_string()));
                        }
                    }
                },
                _ => { }
            }
        }
        self
    }

    pub fn insert_string(&mut self, key: &str, text: &str) -> &mut Self {
        for child in self.children.iter_mut() {
            match child {
                VersionInfoChild::StringFileInfo { tables } => {
                    for (_, table) in tables {
                        table.insert(key.to_string(), Data::Text(text.to_string()));
                    }
                },
                _ => { }
            }
        }
        self
    }

    pub fn insert_strings(&mut self, tuples: &[(&str,&str)]) -> &mut Self {
        for child in self.children.iter_mut() {
            match child {
                VersionInfoChild::StringFileInfo { tables } => {
                    for (_, table) in tables {
                        for (key, text) in tuples {
                            table.insert(key.to_string(), Data::Text(text.to_string()));
                        }
                    }
                },
                _ => { }
            }
        }
        self
    }

    pub fn remove_string(&mut self, key: &str) -> &mut Self {
        for child in self.children.iter_mut() {
            match child {
                VersionInfoChild::StringFileInfo { tables } => {
                    for (_, table) in tables {
                        table.remove(key);
                    }
                },
                _ => { }
            }
        }
        self
    }

    pub fn ensure_language(&mut self, lang: &str) -> &mut Self {
        for child in self.children.iter_mut() {
            match child {
                VersionInfoChild::StringFileInfo { tables } => {
                    if tables.get(lang).is_none() {
                        tables.insert(lang.to_string(), HashMap::new());
                    }
                },
                _ => { }
            }
        }
        self
    }
    

    pub fn update(&mut self) -> Result<()> {
        self.resource.replace(&self.try_to_vec()?)?.update()?;
        Ok(())
    }
}