1use std::env;
9use std::fs::{self, File};
10use std::io::{self, Write};
11
12const CONFIG: &'static str = "----CONFIG-REF-START----00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
22
23pub fn get_config() -> io::Result<&'static str> {
25 let mut len = None;
26 for (i, c) in CONFIG[24 ..].chars().enumerate() {
27 if c == '0' {
28 if len.is_none() {
29 len = Some(i);
30 }
31 } else if len.is_some() {
32 len = None;
33 }
34 }
35
36 if let Some(i) = len {
37 return Ok(&CONFIG[24..24 + i]);
38 }
39
40 Err(io::Error::new(
41 io::ErrorKind::Other,
42 "Could not locate config.",
43 ))
44}
45
46pub fn new_config(config: &str) -> io::Result<()> {
47 if config.len() > 2024 {
50 return Err(io::Error::new(
51 io::ErrorKind::Other,
52 "Config is too large. It must be no more than 2024 bytes.",
53 ));
54 }
55
56 let conf_text = "----CONFIG-REF-START----";
57
58 if config.contains(conf_text) {
59 return Err(io::Error::new(
60 io::ErrorKind::Other,
61 format!("Config contains invalid string '{}'.", conf_text),
62 ));
63 }
64
65 let exe_loc = env::current_exe()?;
66 let current = fs::read(&exe_loc)?;
67 let mut new = Vec::new();
68 let mut index = None;
69 for i in 0..current.len() {
70 if current[i..i + 24] == *conf_text.as_bytes() {
72 index = Some(i);
73 break;
74 }
75 }
76
77 if let Some(i) = index {
78 new.extend_from_slice(¤t[0..i]);
80 let new_out = conf_text.to_owned()
81 + config
82 + &"0".repeat(2024 - config.len());
83 new.extend_from_slice(new_out.as_bytes());
85 new.extend_from_slice(¤t[i + 2048..]);
86
87 fs::rename(&exe_loc, exe_loc.to_str().unwrap().to_owned() + ".old")?;
92 File::create(exe_loc)?.write_all(&new)?;
93
94 return Ok(());
95 }
96
97 Err(io::Error::new(
98 io::ErrorKind::Other,
99 "Could not locate config.",
100 ))
101}