<commit_before>use std::borrow::Cow;
use std::char;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::env;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;

const RAW_DATA: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/pinyin-data/pinyin.txt"
));

#[cfg(any(
    feature = "plain",
    feature = "with_tone_num",
    feature = "with_tone_num_end"
))]
const PHONETIC_SYMBOL_MAP: &[(char, char, u8)] = &[
    ('ā', 'a', 1),
    ('á', 'a', 2),
    ('ǎ', 'a', 3),
    ('à', 'a', 4),
    ('ē', 'e', 1),
    ('é', 'e', 2),
    ('ě', 'e', 3),
    ('è', 'e', 4),
    ('ế', 'ê', 2),
    ('ề', 'ê', 4),
    ('ō', 'o', 1),
    ('ó', 'o', 2),
    ('ǒ', 'o', 3),
    ('ò', 'o', 4),
    ('ī', 'i', 1),
    ('í', 'i', 2),
    ('ǐ', 'i', 3),
    ('ì', 'i', 4),
    ('ū', 'u', 1),
    ('ú', 'u', 2),
    ('ǔ', 'u', 3),
    ('ù', 'u', 4),
    ('ǘ', 'ü', 2),
    ('ǚ', 'ü', 3),
    ('ǜ', 'ü', 4),
    ('ń', 'n', 2),
    ('ň', 'n', 3),
    ('ǹ', 'n', 4),
    ('ḿ', 'm', 2),
];

#[rustfmt::skip]
const LETTER_TABLE: &[char] = &[
    'b', 'p', 'm', 'f', 'd',
    't', 'n', 'l', 'g', 'k',
    'h', 'j', 'q', 'x', 'r',
    'z', 'c', 's',
                   'w', 'y',
    // 因为数据源里面不会使用 `v` 以及其它的 简写字母，所以这里注释掉
    // 'v', 'ẑ', 'ĉ', 'ŝ', 'ŋ',

    '\u{0304}', '\u{030C}', '\u{0300}', // Unicode 声调连字符
    'a', 'ā', 'á', 'ǎ', 'à',
    'e', 'ē', 'é', 'ě', 'è',
    'i', 'ī', 'í', 'ǐ', 'ì',
    //   "m̄"       "m̌"  "m̀"
    'm',      'ḿ',
    //   "n̄"
    'n',      'ń', 'ň', 'ǹ',
    'o', 'ō', 'ó', 'ǒ', 'ò',
    'u', 'ū', 'ú', 'ǔ', 'ù',
    //   "ê̄"       "ê̌"
    'ê',      'ế',      'ề',
    //   'ǖ'
    'ü',      'ǘ', 'ǚ', 'ǜ',
];

#[cfg(any(feature = "with_tone_num", feature = "with_tone_num_end"))]
const TONE_NUMS: &[char] = &['0', '1', '2', '3', '4'];

type Style = (&'static str, fn(&str) -> Cow<'_, str>);
type InputData = Vec<(u32, Vec<&'static str>)>;
type PinyinDataIndex = HashMap<&'static str, usize>;
type HeteronymDataIndex = HashMap<u32, usize>;

fn main() -> io::Result<()> {
    let data = build_data();
    let pinyin_index = generate_pinyin_data(&data)?;
    let heteronym_index = generate_heteronym_table(&data, &pinyin_index)?;
    generate_char_table(&data, &pinyin_index, &heteronym_index)?;
    // 输出这行以保证改动项目的其他文件不会触发编译脚本重新执行
    println!("cargo:rerun-if-changed=build.rs");
    Ok(())
}

fn build_data() -> InputData {
    let mut input_data = RAW_DATA
        .lines()
        .enumerate()
        // 移除注释和空格
        .map(|(i, mut line)| {
            if let Some(hash_pos) = line.find('#') {
                line = &line[..hash_pos];
            }
            (i, line.trim())
        })
        // 移除空行
        .filter(|(_, line)| !line.is_empty())
        .map(|(i, line)| {
            // Split the line by colon
            let colon_pos = match line.find(':') {
                Some(pos) => pos,
                None => unreachable!("no colon found in line {}", i),
            };
            let code_point = line[..colon_pos].trim();
            let pinyin_list: Vec<_> = line[colon_pos + 1..].trim().split(',').collect();

            // 确保输入数据的字符全部在我们预料之中。
            // 同时也可以提前知道一些被遗弃的码位，如: U+E7C8 和 U+E7C7
            for pinyin in pinyin_list.iter() {
                for ch in pinyin.chars() {
                    let is_known = LETTER_TABLE.contains(&ch);
                    assert!(
                        is_known,
                        "unknown character {:?} at line {}: {}",
                        ch, i, line,
                    );
                }
            }

            // 解析码位
            const CODE_POINT_PREFIX: &str = "U+";
            assert!(code_point.starts_with(CODE_POINT_PREFIX));
            let code = &code_point[CODE_POINT_PREFIX.len()..];
            let code = match u32::from_str_radix(code, 16) {
                Ok(code) => code,
                Err(_) => unreachable!("invalid code point {} at line {}", code, i),
            };
            (code, pinyin_list)
        })
        .collect::<Vec<_>>();
    input_data.sort_by_key(|(code, _)| *code);
    input_data
}

const STYLES: &[Style] = &[
    #[cfg(feature = "plain")]
    ("plain", |input| {
        input.chars().filter_map(|c| get_char_info(c).0).collect()
    }),
    #[cfg(feature = "with_tone")]
    ("with_tone", |input| Cow::from(input)),
    #[cfg(feature = "with_tone_num")]
    ("with_tone_num", |input| {
        let mut result = String::new();
        for ch in input.chars() {
            let (ch, tone) = get_char_info(ch);
            if let Some(ch) = ch {
                result.push(ch);
            }
            if tone > 0 {
                result.push(TONE_NUMS[usize::try_from(tone).unwrap()]);
            }
        }
        result.into()
    }),
    #[cfg(feature = "with_tone_num_end")]
    ("with_tone_num_end", |input| {
        let mut result = String::new();
        let mut output_tone = None;
        for ch in input.chars() {
            let (ch, tone) = get_char_info(ch);
            if let Some(ch) = ch {
                result.push(ch);
            }
            if tone > 0 {
                assert!(output_tone.is_none());
                output_tone = Some(TONE_NUMS[usize::try_from(tone).unwrap()]);
            }
        }
        if let Some(tone) = output_tone {
            result.push(tone);
        }
        result.into()
    }),
];

fn generate_pinyin_data(data: &InputData) -> io::Result<PinyinDataIndex> {
    let mut output = create_out_file("pinyin_data.rs")?;
    let mut pinyin_data = HashMap::new();
    writeln!(output, "&[")?;
    let mut process_pinyin = |pinyin| {
        let index = pinyin_data.len();
        match pinyin_data.entry(pinyin) {
            Entry::Occupied(_) => return Ok(()),
            Entry::Vacant(entry) => {
                entry.insert(index);
            }
        }
        write!(output, "    PinyinData {{ ")?;
        for (field, converter) in STYLES.iter() {
            write!(output, r#"{}: "{}", "#, field, converter(pinyin))?;
        }
        #[cfg(feature = "compat")]
        {
            // 计算切分声母和韵母的位置
            const INITIALS: &[&str] = &[
                "b", "p", "m", "f", "d", "t", "n", "l", "g", "k", "h", "j", "q", "x", "r", "zh",
                "ch", "sh", "z", "c", "s",
            ];
            let split = INITIALS
                .iter()
                .find(|initial| pinyin.starts_with(*initial))
                .map_or(0, |initial| initial.len());
            write!(output, "split: {}, ", split)?;
        }
        writeln!(output, "}},")?;
        Ok(())
    };
    // 插入一个空的拼音数据作为零位
    process_pinyin("")?;
    data.iter()
        .flat_map(|(_, list)| list.iter().map(|s| *s))
        .map(process_pinyin)
        .collect::<io::Result<()>>()?;
    writeln!(output, "]")?;
    Ok(pinyin_data)
}

#[cfg(feature = "heteronym")]
fn generate_heteronym_table(
    data: &InputData,
    index: &PinyinDataIndex,
) -> io::Result<HeteronymDataIndex> {
    let mut heteronym_list_index = HashMap::new();
    let mut heteronym_index = HashMap::new();
    let mut output = create_out_file("heteronym_table.rs")?;
    writeln!(output, "&[")?;
    writeln!(output, "    &[],")?;
    heteronym_list_index.insert(vec![].into_boxed_slice(), 0);
    data.iter()
        .map(|(code, list)| {
            let list = list[1..]
                .iter()
                .map(|pinyin| *index.get(pinyin).unwrap())
                .collect::<Box<[_]>>();
            let new_idx = heteronym_list_index.len();
            let idx = match heteronym_list_index.entry(list) {
                Entry::Occupied(entry) => *entry.get(),
                Entry::Vacant(entry) => {
                    write!(output, "    &[")?;
                    for i in entry.key().iter() {
                        write!(output, "{}, ", i)?;
                    }
                    writeln!(output, "],")?;
                    entry.insert(new_idx);
                    new_idx
                }
            };
            heteronym_index.insert(*code, idx);
            Ok(())
        })
        .collect::<io::Result<()>>()?;
    writeln!(output, "]")?;
    Ok(heteronym_index)
}

#[cfg(not(feature = "heteronym"))]
fn generate_heteronym_table(
    _data: &InputData,
    _index: &PinyinDataIndex,
) -> io::Result<HeteronymDataIndex> {
    Ok(HashMap::new())
}

fn generate_char_table(
    data: &InputData,
    pinyin_index: &PinyinDataIndex,
    heteronym_index: &HeteronymDataIndex,
) -> io::Result<()> {
    // 将码位分入数据块
    const GAP_THRESHOLD: u32 = 2048;
    let mut block_ranges = vec![];
    data.iter()
        .for_each(|(code, _)| match block_ranges.last_mut() {
            Some((_, end)) if *end + GAP_THRESHOLD > *code => *end = *code + 1,
            _ => block_ranges.push((*code, *code + 1)),
        });
    // 当我们允许最大2048个空位时，我们目前会切出3个块。如果这个数字在未来增加了，我们也许会希望调整策略。
    assert_eq!(block_ranges.len(), 4);

    // 输出字符表
    let mut output = create_out_file("char_blocks.rs")?;
    writeln!(output, "&[")?;
    let mut data_iter = data.iter().peekable();
    for (start, end) in block_ranges {
        let len = usize::try_from(end - start).unwrap();
        let mut block = vec![0; len];
        let mut heteronym = vec![0; len];
        while let Some((code, list)) = data_iter.peek() {
            let idx = usize::try_from(*code - start).unwrap();
            if idx >= len {
                break;
            }
            block[idx] = *pinyin_index.get(list[0]).unwrap();
            if cfg!(feature = "heteronym") {
                heteronym[idx] = *heteronym_index.get(code).unwrap();
            }
            data_iter.next();
        }
        write!(output, "    CharBlock {{ start_code: {}, data: &[", start)?;
        for idx in block {
            write!(output, "{}, ", idx)?;
        }
        write!(output, "], ")?;
        if cfg!(feature = "heteronym") {
            write!(output, "heteronym: &[")?;
            for idx in heteronym {
                write!(output, "{}, ", idx)?;
            }
            write!(output, "], ")?;
        }
        writeln!(output, "}},")?;
    }
    writeln!(output, "]")?;
    Ok(())
}

fn create_out_file(name: &str) -> io::Result<impl Write> {
    let path = Path::new(&env::var("OUT_DIR").unwrap()).join(name);
    Ok(BufWriter::new(File::create(&path)?))
}

#[cfg(any(
    feature = "plain",
    feature = "with_tone_num",
    feature = "with_tone_num_end"
))]
fn get_char_info(ch: char) -> (Option<char>, u8) {
    if let Some((_, base, tone)) = PHONETIC_SYMBOL_MAP.iter().find(|(c, _, _)| *c == ch) {
        return (Some(*base), *tone);
    }
    const TONE_MAP: &[(char, u8)] = &[('\u{304}', 1), ('\u{30c}', 3), ('\u{300}', 4)];
    if let Some((_, tone)) = TONE_MAP.iter().find(|(c, _)| *c == ch) {
        return (None, *tone);
    }
    (Some(ch), 0)
}
<commit_msg>Fix build failed on stable version of rust<commit_after>use std::borrow::Cow;
use std::char;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::env;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;

const RAW_DATA: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/pinyin-data/pinyin.txt"
));

#[cfg(any(
    feature = "plain",
    feature = "with_tone_num",
    feature = "with_tone_num_end"
))]
const PHONETIC_SYMBOL_MAP: &[(char, char, u8)] = &[
    ('ā', 'a', 1),
    ('á', 'a', 2),
    ('ǎ', 'a', 3),
    ('à', 'a', 4),
    ('ē', 'e', 1),
    ('é', 'e', 2),
    ('ě', 'e', 3),
    ('è', 'e', 4),
    ('ế', 'ê', 2),
    ('ề', 'ê', 4),
    ('ō', 'o', 1),
    ('ó', 'o', 2),
    ('ǒ', 'o', 3),
    ('ò', 'o', 4),
    ('ī', 'i', 1),
    ('í', 'i', 2),
    ('ǐ', 'i', 3),
    ('ì', 'i', 4),
    ('ū', 'u', 1),
    ('ú', 'u', 2),
    ('ǔ', 'u', 3),
    ('ù', 'u', 4),
    ('ǘ', 'ü', 2),
    ('ǚ', 'ü', 3),
    ('ǜ', 'ü', 4),
    ('ń', 'n', 2),
    ('ň', 'n', 3),
    ('ǹ', 'n', 4),
    ('ḿ', 'm', 2),
];

#[rustfmt::skip]
const LETTER_TABLE: &[char] = &[
    'b', 'p', 'm', 'f', 'd',
    't', 'n', 'l', 'g', 'k',
    'h', 'j', 'q', 'x', 'r',
    'z', 'c', 's',
                   'w', 'y',
    // 因为数据源里面不会使用 `v` 以及其它的 简写字母，所以这里注释掉
    // 'v', 'ẑ', 'ĉ', 'ŝ', 'ŋ',

    '\u{0304}', '\u{030C}', '\u{0300}', // Unicode 声调连字符
    'a', 'ā', 'á', 'ǎ', 'à',
    'e', 'ē', 'é', 'ě', 'è',
    'i', 'ī', 'í', 'ǐ', 'ì',
    //   "m̄"       "m̌"  "m̀"
    'm',      'ḿ',
    //   "n̄"
    'n',      'ń', 'ň', 'ǹ',
    'o', 'ō', 'ó', 'ǒ', 'ò',
    'u', 'ū', 'ú', 'ǔ', 'ù',
    //   "ê̄"       "ê̌"
    'ê',      'ế',      'ề',
    //   'ǖ'
    'ü',      'ǘ', 'ǚ', 'ǜ',
];

#[cfg(any(feature = "with_tone_num", feature = "with_tone_num_end"))]
const TONE_NUMS: &[char] = &['0', '1', '2', '3', '4'];

type Style = (&'static str, fn(&str) -> Cow<'_, str>);
type InputData = Vec<(u32, Vec<&'static str>)>;
type PinyinDataIndex = HashMap<&'static str, usize>;
type HeteronymDataIndex = HashMap<u32, usize>;

fn main() -> io::Result<()> {
    let data = build_data();
    let pinyin_index = generate_pinyin_data(&data)?;
    let heteronym_index = generate_heteronym_table(&data, &pinyin_index)?;
    generate_char_table(&data, &pinyin_index, &heteronym_index)?;
    // 输出这行以保证改动项目的其他文件不会触发编译脚本重新执行
    println!("cargo:rerun-if-changed=build.rs");
    Ok(())
}

fn build_data() -> InputData {
    let mut input_data = RAW_DATA
        .lines()
        .enumerate()
        // 移除注释和空格
        .map(|(i, mut line)| {
            if let Some(hash_pos) = line.find('#') {
                line = &line[..hash_pos];
            }
            (i, line.trim())
        })
        // 移除空行
        .filter(|(_, line)| !line.is_empty())
        .map(|(i, line)| {
            // Split the line by colon
            let colon_pos = match line.find(':') {
                Some(pos) => pos,
                None => unreachable!("no colon found in line {}", i),
            };
            let code_point = line[..colon_pos].trim();
            let pinyin_list: Vec<_> = line[colon_pos + 1..].trim().split(',').collect();

            // 确保输入数据的字符全部在我们预料之中。
            // 同时也可以提前知道一些被遗弃的码位，如: U+E7C8 和 U+E7C7
            for pinyin in pinyin_list.iter() {
                for ch in pinyin.chars() {
                    let is_known = LETTER_TABLE.contains(&ch);
                    assert!(
                        is_known,
                        "unknown character {:?} at line {}: {}",
                        ch, i, line,
                    );
                }
            }

            // 解析码位
            const CODE_POINT_PREFIX: &str = "U+";
            assert!(code_point.starts_with(CODE_POINT_PREFIX));
            let code = &code_point[CODE_POINT_PREFIX.len()..];
            let code = match u32::from_str_radix(code, 16) {
                Ok(code) => code,
                Err(_) => unreachable!("invalid code point {} at line {}", code, i),
            };
            (code, pinyin_list)
        })
        .collect::<Vec<_>>();
    input_data.sort_by_key(|(code, _)| *code);
    input_data
}

const STYLES: &[Style] = &[
    #[cfg(feature = "plain")]
    ("plain", |input| {
        input.chars().filter_map(|c| get_char_info(c).0).collect()
    }),
    #[cfg(feature = "with_tone")]
    ("with_tone", |input| Cow::from(input)),
    #[cfg(feature = "with_tone_num")]
    ("with_tone_num", |input| {
        let mut result = String::new();
        for ch in input.chars() {
            let (ch, tone) = get_char_info(ch);
            if let Some(ch) = ch {
                result.push(ch);
            }
            if tone > 0 {
                result.push(TONE_NUMS[usize::try_from(tone).unwrap()]);
            }
        }
        result.into()
    }),
    #[cfg(feature = "with_tone_num_end")]
    ("with_tone_num_end", |input| {
        let mut result = String::new();
        let mut output_tone = None;
        for ch in input.chars() {
            let (ch, tone) = get_char_info(ch);
            if let Some(ch) = ch {
                result.push(ch);
            }
            if tone > 0 {
                assert!(output_tone.is_none());
                output_tone = Some(TONE_NUMS[usize::try_from(tone).unwrap()]);
            }
        }
        if let Some(tone) = output_tone {
            result.push(tone);
        }
        result.into()
    }),
];

fn generate_pinyin_data(data: &InputData) -> io::Result<PinyinDataIndex> {
    let mut output = create_out_file("pinyin_data.rs")?;
    let mut pinyin_data = HashMap::new();
    writeln!(output, "&[")?;
    let mut process_pinyin = |pinyin| {
        let index = pinyin_data.len();
        match pinyin_data.entry(pinyin) {
            Entry::Occupied(_) => return Ok(()),
            Entry::Vacant(entry) => {
                entry.insert(index);
            }
        }
        write!(output, "    PinyinData {{ ")?;
        for (field, converter) in STYLES.iter() {
            write!(output, r#"{}: "{}", "#, field, converter(pinyin))?;
        }
        #[cfg(feature = "compat")]
        {
            // 计算切分声母和韵母的位置
            const INITIALS: &[&str] = &[
                "b", "p", "m", "f", "d", "t", "n", "l", "g", "k", "h", "j", "q", "x", "r", "zh",
                "ch", "sh", "z", "c", "s",
            ];
            let split = INITIALS
                .iter()
                .find(|initial| pinyin.starts_with(*initial))
                .map_or(0, |initial| initial.len());
            write!(output, "split: {}, ", split)?;
        }
        writeln!(output, "}},")?;
        Ok(())
    };
    // 插入一个空的拼音数据作为零位
    process_pinyin("")?;
    data.iter()
        .flat_map(|(_, list)| list.iter().map(|s| *s))
        .map(process_pinyin)
        .collect::<io::Result<()>>()?;
    writeln!(output, "]")?;
    Ok(pinyin_data)
}

#[cfg(feature = "heteronym")]
fn generate_heteronym_table(
    data: &InputData,
    index: &PinyinDataIndex,
) -> io::Result<HeteronymDataIndex> {
    let mut heteronym_list_index = HashMap::new();
    let mut heteronym_index = HashMap::new();
    let mut output = create_out_file("heteronym_table.rs")?;
    writeln!(output, "&[")?;
    writeln!(output, "    &[],")?;
    heteronym_list_index.insert(vec![].into_boxed_slice(), 0);
    data.iter()
        .map(|(code, list)| {
            let list = list[1..]
                .iter()
                .map(|pinyin| *index.get(pinyin).unwrap())
                .collect::<Box<[_]>>();
            let new_idx = heteronym_list_index.len();
            let idx = match heteronym_list_index.entry(list) {
                Entry::Occupied(entry) => *entry.get(),
                Entry::Vacant(entry) => {
                    write!(output, "    &[")?;
                    for i in entry.key().iter() {
                        write!(output, "{}, ", i)?;
                    }
                    writeln!(output, "],")?;
                    entry.insert(new_idx);
                    new_idx
                }
            };
            heteronym_index.insert(*code, idx);
            Ok(())
        })
        .collect::<io::Result<()>>()?;
    writeln!(output, "]")?;
    Ok(heteronym_index)
}

#[cfg(not(feature = "heteronym"))]
fn generate_heteronym_table(
    _data: &InputData,
    _index: &PinyinDataIndex,
) -> io::Result<HeteronymDataIndex> {
    Ok(HashMap::new())
}

fn generate_char_table(
    data: &InputData,
    pinyin_index: &PinyinDataIndex,
    heteronym_index: &HeteronymDataIndex,
) -> io::Result<()> {
    // 将码位分入数据块
    const GAP_THRESHOLD: u32 = 2048;
    let mut block_ranges = vec![];
    data.iter()
        .for_each(|(code, _)| match block_ranges.last_mut() {
            Some((_, end)) if *end + GAP_THRESHOLD > *code => *end = *code + 1,
            _ => block_ranges.push((*code, *code + 1)),
        });
    // 当我们允许最大2048个空位时，我们目前会切出6个块。如果这个数字在未来增加了，我们也许会希望调整策略。
    assert_eq!(block_ranges.len(), 6);

    // 输出字符表
    let mut output = create_out_file("char_blocks.rs")?;
    writeln!(output, "&[")?;
    let mut data_iter = data.iter().peekable();
    for (start, end) in block_ranges {
        let len = usize::try_from(end - start).unwrap();
        let mut block = vec![0; len];
        let mut heteronym = vec![0; len];
        while let Some((code, list)) = data_iter.peek() {
            let idx = usize::try_from(*code - start).unwrap();
            if idx >= len {
                break;
            }
            block[idx] = *pinyin_index.get(list[0]).unwrap();
            if cfg!(feature = "heteronym") {
                heteronym[idx] = *heteronym_index.get(code).unwrap();
            }
            data_iter.next();
        }
        write!(output, "    CharBlock {{ start_code: {}, data: &[", start)?;
        for idx in block {
            write!(output, "{}, ", idx)?;
        }
        write!(output, "], ")?;
        if cfg!(feature = "heteronym") {
            write!(output, "heteronym: &[")?;
            for idx in heteronym {
                write!(output, "{}, ", idx)?;
            }
            write!(output, "], ")?;
        }
        writeln!(output, "}},")?;
    }
    writeln!(output, "]")?;
    Ok(())
}

fn create_out_file(name: &str) -> io::Result<impl Write> {
    let path = Path::new(&env::var("OUT_DIR").unwrap()).join(name);
    Ok(BufWriter::new(File::create(&path)?))
}

#[cfg(any(
    feature = "plain",
    feature = "with_tone_num",
    feature = "with_tone_num_end"
))]
fn get_char_info(ch: char) -> (Option<char>, u8) {
    if let Some((_, base, tone)) = PHONETIC_SYMBOL_MAP.iter().find(|(c, _, _)| *c == ch) {
        return (Some(*base), *tone);
    }
    const TONE_MAP: &[(char, u8)] = &[('\u{304}', 1), ('\u{30c}', 3), ('\u{300}', 4)];
    if let Some((_, tone)) = TONE_MAP.iter().find(|(c, _)| *c == ch) {
        return (None, *tone);
    }
    (Some(ch), 0)
}
<|endoftext|>


<commit_before>use std::process::Command;

fn main() {
    let status = Command::new("ld")
        .arg("-lcpupower").status().unwrap();

    if status.success() {
        println!("cargo:rustc-link-lib=cpupower");
    } else {
        println!("cargo:rustc-link-lib=cpufreq");
    }
}
<commit_msg>Skipping linking to the libcpufreq or libcpupower<commit_after>use std::process::Command;

fn main() {
    let mut status = Command::new("ld")
        .arg("-lcpupower").status();

    if status.is_ok() && status.unwrap().success() {
        println!("cargo:rustc-link-lib=cpupower");
        return;
    }

    status = Command::new("ld")
        .arg("-lcpufreq").status();

    if status.is_ok() && status.unwrap().success() {
        println!("cargo:rustc-link-lib=cpufreq");
        return;
    }
}
<|endoftext|>


<commit_before>//! The following environment variables affect the build:
//!
//! * `UPDATE_CAPSTONE_BINDINGS`: setting indicates that the pre-generated `capstone.rs` should be
//!   updated with the output bindgen
//!
//! # Bindgen enum mapping
//!
//! Bindgen can convert C enums in several ways:
//!
//! 1. **"Rustified" enum**: Bindgen creates a Rust enum, which provides the most "type safety" and
//!    reduces the chance of confusing variants for a different type. For variants whose
//!    discriminant values are not distinct, bindgen defines constants.
//! 2. **"Constified" enum**: Bindgen defines constants for each enum variant.
//! 3. **"Constified" enum module**: Bindgen defines constants for each enum variant in a separate
//!    module.
//!
//! # Rationale for enum types
//!
//! Rustified enum: these have distinct variant discriminants
//!
//! * `cs_arch`
//! * `cs_op_type`
//! * `cs_opt_type`
//!
//! Constified enum module:
//!
//! * `cs_err`: avoid undefined behavior in case an error is instantiated with an invalid value; the
//!   compiler could make false assumptions that the value is only within a certain range.
//! * `cs_group_type`/`ARCH_insn_group`: each architecture adds group types to the `cs_group_type`,
//!   so we constify to avoid needing to transmute.
//! * `cs_mode`: used as a bitmask; when values are OR'd together, they are not a valid discriminant
//!   value
//! * `cs_opt_value`/`ARCH_reg`: variant discriminants are not unique
//!
//! Bitfield enum: fields are OR'd together to form new values
//! * `cs_mode`

#[cfg(feature = "use_bindgen")]
extern crate bindgen;

#[cfg(feature = "use_system_capstone")]
extern crate pkg_config;

#[cfg(feature = "build_capstone_cmake")]
extern crate cmake;

#[cfg(any(windows, feature = "build_capstone_cc"))]
extern crate cc;

use std::fs::copy;
use std::path::PathBuf;
use std::env;

include!("common.rs");

const CAPSTONE_DIR: &'static str = "capstone";

/// Indicates how capstone library should be linked
#[allow(dead_code)]
enum LinkType {
    Dynamic,
    Static,
}

/// Build capstone with cmake
#[cfg(all(not(windows), feature = "build_capstone_cmake"))]
fn build_capstone_cmake() {
    let mut cfg = cmake::Config::new("capstone");
    let dst = cfg.build();
    println!("cargo:rustc-link-search=native={}/lib", dst.display());
}

/// Build capstone with gmake
#[cfg(not(windows))]
fn build_capstone_gmake() {
    use std::process::Command;

    // In BSDs, `make` does not refer to GNU make
    let target_os = env_var("CARGO_CFG_TARGET_OS");
    let make_cmd = if target_os.contains("bsd") || target_os == "dragonfly" {
        "gmake"
    } else {
        "make"
    };

    let out_dir = env_var("OUT_DIR");
    Command::new(make_cmd)
        .current_dir(CAPSTONE_DIR)
        .status()
        .expect("Failed to build Capstone library");
    let capstone = "libcapstone.a";
    Command::new("cp")
        .current_dir(CAPSTONE_DIR)
        .arg(&capstone)
        .arg(&out_dir)
        .status()
        .expect("Failed to copy capstone library to OUT_DIR");

    println!("cargo:rustc-link-search=native={}", out_dir);
}

/// Build capstone using the cc crate
#[cfg(any(windows, feature = "build_capstone_cc"))]
fn build_capstone_cc() {
    use std::fs::DirEntry;

    fn read_dir_and_filter<F: Fn(&DirEntry) -> bool>(dir: &str, filter: F) -> Vec<String> {
        use std::fs::read_dir;

        read_dir(dir)
            .expect("Failed to read capstone source directory")
            .into_iter()
            .map(|e| e.expect("Failed to read capstone source directory"))
            .filter(|e| filter(e))
            .map(|e| format!("{}/{}", dir, e.file_name().to_str().expect("Invalid filename")))
            .collect()
    }

    fn find_c_source_files(dir: &str) -> Vec<String> {
        read_dir_and_filter(dir, |e| {
            let file_type = e.file_type().expect("Failed to read capstone source directory");
            let file_name = e.file_name().into_string().expect("Invalid filename");
            file_type.is_file() && (file_name.ends_with(".c") || file_name.ends_with(".C"))
        })
    }

    fn find_arch_dirs() -> Vec<String> {
        read_dir_and_filter(&format!("{}/{}", CAPSTONE_DIR, "arch"), |e| {
            let file_type = e.file_type().expect("Failed to read capstone source directory");
            file_type.is_dir()
        })
    }

    let mut files = find_c_source_files(CAPSTONE_DIR);
    for arch_dir in find_arch_dirs().into_iter() {
        files.append(&mut find_c_source_files(&arch_dir));
    }

    let use_static_crt = {
        let target_features = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or(String::new());
        target_features.split(",").any(|f| f == "crt-static")
    };

    cc::Build::new()
        .files(files)
        .include(format!("{}/{}", CAPSTONE_DIR, "include"))
        .define("CAPSTONE_X86_ATT_DISABLE_NO", None)
        .define("CAPSTONE_DIET_NO", None)
        .define("CAPSTONE_X86_REDUCE_NO", None)
        .define("CAPSTONE_USE_SYS_DYN_MEM", None)
        .define("CAPSTONE_HAS_ARM", None)
        .define("CAPSTONE_HAS_ARM64", None)
        .define("CAPSTONE_HAS_MIPS", None)
        .define("CAPSTONE_HAS_POWERPC", None)
        .define("CAPSTONE_HAS_SPARC", None)
        .define("CAPSTONE_HAS_SYSZ", None)
        .define("CAPSTONE_HAS_X86", None)
        .define("CAPSTONE_HAS_XCORE", None)
        .flag_if_supported("-Wno-unused-function")
        .flag_if_supported("-Wno-unused-parameter")
        .flag_if_supported("-Wno-unknown-pragmas")
        .flag_if_supported("-Wno-sign-compare")
        .flag_if_supported("-Wno-return-type")
        .static_crt(use_static_crt)
        .compile("capstone");
}

/// Search for header in search paths
#[cfg(feature = "use_bindgen")]
fn find_capstone_header(header_search_paths: &Vec<PathBuf>, name: &str) -> Option<PathBuf> {
    for search_path in header_search_paths.iter() {
        let potential_file = search_path.join(name);
        if potential_file.is_file() {
            return Some(potential_file);
        }
    }
    None
}

/// Gets environment variable value. Panics if variable is not set.
fn env_var(var: &str) -> String {
    env::var(var).expect(&format!("Environment variable {} is not set", var))
}

/// Create bindings using bindgen
#[cfg(feature = "use_bindgen")]
fn write_bindgen_bindings(
    header_search_paths: &Vec<PathBuf>,
    update_pregenerated_bindings: bool,
    pregenerated_bindgen_header: PathBuf,
    out_path: PathBuf,
) {
    let mut builder = bindgen::Builder::default()
        .rust_target(bindgen::RustTarget::Stable_1_19)
        .header(
            find_capstone_header(header_search_paths, "capstone.h")
                .expect("Could not find header")
                .to_str()
                .unwrap(),
        )
        .disable_name_namespacing()
        .prepend_enum_name(false)
        .generate_comments(true)
        .impl_debug(true)
        // Avoid overlapping regex to work around rust-bindgen bug:
        // https://github.com/rust-lang-nursery/rust-bindgen/issues/1198
        .constified_enum_module("cs_err|cs_group_type|cs_opt_value")
        .bitfield_enum("cs_mode")
        .rustified_enum(".*");

    // Whitelist cs_.* functions and types
    let pattern = String::from("cs_.*");
    builder = builder
        .whitelist_function(&pattern)
        .whitelist_type(&pattern);

    // Whitelist types with architectures
    for arch in ARCH_INCLUDES {
        // .*(^|_)ARCH(_|$).*
        let arch_type_pattern = format!(".*(^|_){}(_|$).*", arch.cs_name);
        let const_mod_pattern = format!("^{}_(reg|insn_group)$", arch.cs_name);
        builder = builder
            .whitelist_type(&arch_type_pattern)
            .constified_enum_module(&const_mod_pattern);
    }

    let bindings = builder.generate().expect("Unable to generate bindings");

    // Write bindings to $OUT_DIR/bindings.rs
    bindings.write_to_file(out_path.clone()).expect(
        "Unable to write bindings",
    );

    if update_pregenerated_bindings {
        copy(out_path, pregenerated_bindgen_header).expect("Unable to update capstone bindings");
    }
}

/// Find system capstone library and return link type
#[cfg(all(not(windows), feature = "use_system_capstone"))]
fn find_system_capstone(header_search_paths: &mut Vec<PathBuf>) -> Option<LinkType> {
    assert!(
        !cfg!(feature = "build_capstone_cmake"),
        "build_capstone_cmake feature is only valid when using bundled cmake"
    );

    let capstone_lib =
        pkg_config::find_library("capstone").expect("Could not find system capstone");
    header_search_paths.append(&mut capstone_lib.include_paths.clone());
    Some(LinkType::Dynamic)
}

fn main() {
    #[allow(unused_assignments)]
    let mut link_type: Option<LinkType> = None;

    // C header search paths
    let mut header_search_paths: Vec<PathBuf> = Vec::new();

    if cfg!(feature = "use_system_capstone") {
        #[cfg(windows)] panic!("Feature 'use_system_capstone' is not supported on Windows");
        #[cfg(all(not(windows), feature = "use_system_capstone"))]
        {
            link_type = find_system_capstone(&mut header_search_paths);
        }
    } else {
        if cfg!(feature = "build_capstone_cmake") {
            #[cfg(windows)] panic!("Feature 'build_capstone_cmake' is not supported on Windows");
            #[cfg(all(not(windows), feature = "build_capstone_cmake"))] build_capstone_cmake();
        } else if cfg!(any(windows, feature = "build_capstone_cc")) {
            #[cfg(any(windows, feature = "build_capstone_cc"))] build_capstone_cc();
        } else {
            #[cfg(not(windows))] build_capstone_gmake();
        }

        header_search_paths.push([CAPSTONE_DIR, "include"].iter().collect());
        link_type = Some(LinkType::Static);
    }

    match link_type.expect("Must specify link type") {
        LinkType::Dynamic => {
            println!("cargo:rustc-link-lib=dylib=capstone");
        }
        LinkType::Static => {
            println!("cargo:rustc-link-lib=static=capstone");
        }
    }

    // If UPDATE_CAPSTONE_BINDINGS is set, then updated the pre-generated capstone bindings
    let update_pregenerated_bindings = env::var("UPDATE_CAPSTONE_BINDINGS").is_ok();
    if update_pregenerated_bindings {
        assert!(
            cfg!(feature = "use_bindgen"),
            concat!(
                "Setting UPDATE_CAPSTONE_BINDINGS only makes ",
                "sense when enabling feature \"use_bindgen\""
            )
        );
    }

    let pregenerated_bindgen_header: PathBuf = [
        env_var("CARGO_MANIFEST_DIR"),
        "pre_generated".into(),
        BINDINGS_FILE.into(),
    ].iter()
        .collect();
    let out_path = PathBuf::from(env_var("OUT_DIR")).join(BINDINGS_FILE);

    // Only run bindgen if we are *not* using the bundled capstone bindings
    #[cfg(feature = "use_bindgen")]
    write_bindgen_bindings(
        &header_search_paths,
        update_pregenerated_bindings,
        pregenerated_bindgen_header,
        out_path,
    );

    // Otherwise, copy the pregenerated bindings
    #[cfg(not(feature = "use_bindgen"))]
    copy(&pregenerated_bindgen_header, &out_path).expect("Unable to update capstone bindings");
}
<commit_msg>Remove outdated comment<commit_after>//! The following environment variables affect the build:
//!
//! * `UPDATE_CAPSTONE_BINDINGS`: setting indicates that the pre-generated `capstone.rs` should be
//!   updated with the output bindgen
//!
//! # Bindgen enum mapping
//!
//! Bindgen can convert C enums in several ways:
//!
//! 1. **"Rustified" enum**: Bindgen creates a Rust enum, which provides the most "type safety" and
//!    reduces the chance of confusing variants for a different type. For variants whose
//!    discriminant values are not distinct, bindgen defines constants.
//! 2. **"Constified" enum**: Bindgen defines constants for each enum variant.
//! 3. **"Constified" enum module**: Bindgen defines constants for each enum variant in a separate
//!    module.
//!
//! # Rationale for enum types
//!
//! Rustified enum: these have distinct variant discriminants
//!
//! * `cs_arch`
//! * `cs_op_type`
//! * `cs_opt_type`
//!
//! Constified enum module:
//!
//! * `cs_err`: avoid undefined behavior in case an error is instantiated with an invalid value; the
//!   compiler could make false assumptions that the value is only within a certain range.
//! * `cs_group_type`/`ARCH_insn_group`: each architecture adds group types to the `cs_group_type`,
//!   so we constify to avoid needing to transmute.
//! * `cs_mode`: used as a bitmask; when values are OR'd together, they are not a valid discriminant
//!   value
//! * `cs_opt_value`/`ARCH_reg`: variant discriminants are not unique
//!
//! Bitfield enum: fields are OR'd together to form new values
//! * `cs_mode`

#[cfg(feature = "use_bindgen")]
extern crate bindgen;

#[cfg(feature = "use_system_capstone")]
extern crate pkg_config;

#[cfg(feature = "build_capstone_cmake")]
extern crate cmake;

#[cfg(any(windows, feature = "build_capstone_cc"))]
extern crate cc;

use std::fs::copy;
use std::path::PathBuf;
use std::env;

include!("common.rs");

const CAPSTONE_DIR: &'static str = "capstone";

/// Indicates how capstone library should be linked
#[allow(dead_code)]
enum LinkType {
    Dynamic,
    Static,
}

/// Build capstone with cmake
#[cfg(all(not(windows), feature = "build_capstone_cmake"))]
fn build_capstone_cmake() {
    let mut cfg = cmake::Config::new("capstone");
    let dst = cfg.build();
    println!("cargo:rustc-link-search=native={}/lib", dst.display());
}

/// Build capstone with gmake
#[cfg(not(windows))]
fn build_capstone_gmake() {
    use std::process::Command;

    // In BSDs, `make` does not refer to GNU make
    let target_os = env_var("CARGO_CFG_TARGET_OS");
    let make_cmd = if target_os.contains("bsd") || target_os == "dragonfly" {
        "gmake"
    } else {
        "make"
    };

    let out_dir = env_var("OUT_DIR");
    Command::new(make_cmd)
        .current_dir(CAPSTONE_DIR)
        .status()
        .expect("Failed to build Capstone library");
    let capstone = "libcapstone.a";
    Command::new("cp")
        .current_dir(CAPSTONE_DIR)
        .arg(&capstone)
        .arg(&out_dir)
        .status()
        .expect("Failed to copy capstone library to OUT_DIR");

    println!("cargo:rustc-link-search=native={}", out_dir);
}

/// Build capstone using the cc crate
#[cfg(any(windows, feature = "build_capstone_cc"))]
fn build_capstone_cc() {
    use std::fs::DirEntry;

    fn read_dir_and_filter<F: Fn(&DirEntry) -> bool>(dir: &str, filter: F) -> Vec<String> {
        use std::fs::read_dir;

        read_dir(dir)
            .expect("Failed to read capstone source directory")
            .into_iter()
            .map(|e| e.expect("Failed to read capstone source directory"))
            .filter(|e| filter(e))
            .map(|e| format!("{}/{}", dir, e.file_name().to_str().expect("Invalid filename")))
            .collect()
    }

    fn find_c_source_files(dir: &str) -> Vec<String> {
        read_dir_and_filter(dir, |e| {
            let file_type = e.file_type().expect("Failed to read capstone source directory");
            let file_name = e.file_name().into_string().expect("Invalid filename");
            file_type.is_file() && (file_name.ends_with(".c") || file_name.ends_with(".C"))
        })
    }

    fn find_arch_dirs() -> Vec<String> {
        read_dir_and_filter(&format!("{}/{}", CAPSTONE_DIR, "arch"), |e| {
            let file_type = e.file_type().expect("Failed to read capstone source directory");
            file_type.is_dir()
        })
    }

    let mut files = find_c_source_files(CAPSTONE_DIR);
    for arch_dir in find_arch_dirs().into_iter() {
        files.append(&mut find_c_source_files(&arch_dir));
    }

    let use_static_crt = {
        let target_features = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or(String::new());
        target_features.split(",").any(|f| f == "crt-static")
    };

    cc::Build::new()
        .files(files)
        .include(format!("{}/{}", CAPSTONE_DIR, "include"))
        .define("CAPSTONE_X86_ATT_DISABLE_NO", None)
        .define("CAPSTONE_DIET_NO", None)
        .define("CAPSTONE_X86_REDUCE_NO", None)
        .define("CAPSTONE_USE_SYS_DYN_MEM", None)
        .define("CAPSTONE_HAS_ARM", None)
        .define("CAPSTONE_HAS_ARM64", None)
        .define("CAPSTONE_HAS_MIPS", None)
        .define("CAPSTONE_HAS_POWERPC", None)
        .define("CAPSTONE_HAS_SPARC", None)
        .define("CAPSTONE_HAS_SYSZ", None)
        .define("CAPSTONE_HAS_X86", None)
        .define("CAPSTONE_HAS_XCORE", None)
        .flag_if_supported("-Wno-unused-function")
        .flag_if_supported("-Wno-unused-parameter")
        .flag_if_supported("-Wno-unknown-pragmas")
        .flag_if_supported("-Wno-sign-compare")
        .flag_if_supported("-Wno-return-type")
        .static_crt(use_static_crt)
        .compile("capstone");
}

/// Search for header in search paths
#[cfg(feature = "use_bindgen")]
fn find_capstone_header(header_search_paths: &Vec<PathBuf>, name: &str) -> Option<PathBuf> {
    for search_path in header_search_paths.iter() {
        let potential_file = search_path.join(name);
        if potential_file.is_file() {
            return Some(potential_file);
        }
    }
    None
}

/// Gets environment variable value. Panics if variable is not set.
fn env_var(var: &str) -> String {
    env::var(var).expect(&format!("Environment variable {} is not set", var))
}

/// Create bindings using bindgen
#[cfg(feature = "use_bindgen")]
fn write_bindgen_bindings(
    header_search_paths: &Vec<PathBuf>,
    update_pregenerated_bindings: bool,
    pregenerated_bindgen_header: PathBuf,
    out_path: PathBuf,
) {
    let mut builder = bindgen::Builder::default()
        .rust_target(bindgen::RustTarget::Stable_1_19)
        .header(
            find_capstone_header(header_search_paths, "capstone.h")
                .expect("Could not find header")
                .to_str()
                .unwrap(),
        )
        .disable_name_namespacing()
        .prepend_enum_name(false)
        .generate_comments(true)
        .impl_debug(true)
        .constified_enum_module("cs_err|cs_group_type|cs_opt_value")
        .bitfield_enum("cs_mode")
        .rustified_enum(".*");

    // Whitelist cs_.* functions and types
    let pattern = String::from("cs_.*");
    builder = builder
        .whitelist_function(&pattern)
        .whitelist_type(&pattern);

    // Whitelist types with architectures
    for arch in ARCH_INCLUDES {
        // .*(^|_)ARCH(_|$).*
        let arch_type_pattern = format!(".*(^|_){}(_|$).*", arch.cs_name);
        let const_mod_pattern = format!("^{}_(reg|insn_group)$", arch.cs_name);
        builder = builder
            .whitelist_type(&arch_type_pattern)
            .constified_enum_module(&const_mod_pattern);
    }

    let bindings = builder.generate().expect("Unable to generate bindings");

    // Write bindings to $OUT_DIR/bindings.rs
    bindings.write_to_file(out_path.clone()).expect(
        "Unable to write bindings",
    );

    if update_pregenerated_bindings {
        copy(out_path, pregenerated_bindgen_header).expect("Unable to update capstone bindings");
    }
}

/// Find system capstone library and return link type
#[cfg(all(not(windows), feature = "use_system_capstone"))]
fn find_system_capstone(header_search_paths: &mut Vec<PathBuf>) -> Option<LinkType> {
    assert!(
        !cfg!(feature = "build_capstone_cmake"),
        "build_capstone_cmake feature is only valid when using bundled cmake"
    );

    let capstone_lib =
        pkg_config::find_library("capstone").expect("Could not find system capstone");
    header_search_paths.append(&mut capstone_lib.include_paths.clone());
    Some(LinkType::Dynamic)
}

fn main() {
    #[allow(unused_assignments)]
    let mut link_type: Option<LinkType> = None;

    // C header search paths
    let mut header_search_paths: Vec<PathBuf> = Vec::new();

    if cfg!(feature = "use_system_capstone") {
        #[cfg(windows)] panic!("Feature 'use_system_capstone' is not supported on Windows");
        #[cfg(all(not(windows), feature = "use_system_capstone"))]
        {
            link_type = find_system_capstone(&mut header_search_paths);
        }
    } else {
        if cfg!(feature = "build_capstone_cmake") {
            #[cfg(windows)] panic!("Feature 'build_capstone_cmake' is not supported on Windows");
            #[cfg(all(not(windows), feature = "build_capstone_cmake"))] build_capstone_cmake();
        } else if cfg!(any(windows, feature = "build_capstone_cc")) {
            #[cfg(any(windows, feature = "build_capstone_cc"))] build_capstone_cc();
        } else {
            #[cfg(not(windows))] build_capstone_gmake();
        }

        header_search_paths.push([CAPSTONE_DIR, "include"].iter().collect());
        link_type = Some(LinkType::Static);
    }

    match link_type.expect("Must specify link type") {
        LinkType::Dynamic => {
            println!("cargo:rustc-link-lib=dylib=capstone");
        }
        LinkType::Static => {
            println!("cargo:rustc-link-lib=static=capstone");
        }
    }

    // If UPDATE_CAPSTONE_BINDINGS is set, then updated the pre-generated capstone bindings
    let update_pregenerated_bindings = env::var("UPDATE_CAPSTONE_BINDINGS").is_ok();
    if update_pregenerated_bindings {
        assert!(
            cfg!(feature = "use_bindgen"),
            concat!(
                "Setting UPDATE_CAPSTONE_BINDINGS only makes ",
                "sense when enabling feature \"use_bindgen\""
            )
        );
    }

    let pregenerated_bindgen_header: PathBuf = [
        env_var("CARGO_MANIFEST_DIR"),
        "pre_generated".into(),
        BINDINGS_FILE.into(),
    ].iter()
        .collect();
    let out_path = PathBuf::from(env_var("OUT_DIR")).join(BINDINGS_FILE);

    // Only run bindgen if we are *not* using the bundled capstone bindings
    #[cfg(feature = "use_bindgen")]
    write_bindgen_bindings(
        &header_search_paths,
        update_pregenerated_bindings,
        pregenerated_bindgen_header,
        out_path,
    );

    // Otherwise, copy the pregenerated bindings
    #[cfg(not(feature = "use_bindgen"))]
    copy(&pregenerated_bindgen_header, &out_path).expect("Unable to update capstone bindings");
}
<|endoftext|>


<commit_before>extern crate cc;

use std::env;

fn main() {

    match env::var("CGAL_DIR") {
        Ok(cgal_dir) => {
            let cgal_lib_dir = format!("{}/lib", cgal_dir);
            let cgal_include_dir = format!("{}/include", cgal_dir);

            cc::Build::new()
                .file("src/cgal_wrapper.cpp")
                .cpp(true)
                .include(cgal_include_dir)
                .compile("libcgal.a");

            println!("cargo:rustc-flags=-L {} -l CGAL_Core -l CGAL -l gmp", cgal_lib_dir);
        },
        Err(_) => {
            cc::Build::new()
                .file("src/cgal_wrapper.cpp")
                .cpp(true)
                .compile("libcgal.a");

            println!("cargo:rustc-flags=-l CGAL_Core -l CGAL -l gmp");
        }
    }
}
<commit_msg>add -frounding-math flag<commit_after>extern crate cc;

use std::env;

fn main() {

    match env::var("CGAL_DIR") {
        Ok(cgal_dir) => {
            let cgal_lib_dir = format!("{}/lib", cgal_dir);
            let cgal_include_dir = format!("{}/include", cgal_dir);

            cc::Build::new()
                .file("src/cgal_wrapper.cpp")
                .cpp(true)
                .flag("-frounding-math")
                .include(cgal_include_dir)
                .compile("libcgal.a");

            println!("cargo:rustc-flags=-L {} -l CGAL_Core -l CGAL -l gmp", cgal_lib_dir);
        },
        Err(_) => {
            cc::Build::new()
                .file("src/cgal_wrapper.cpp")
                .cpp(true)
                .flag("-frounding-math")
                .compile("libcgal.a");

            println!("cargo:rustc-flags=-l CGAL_Core -l CGAL -l gmp");
        }
    }
}
<|endoftext|>


<commit_before>// build.rs
// Copyright 2016 Alexander Altman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(feature = "c-api")]
extern crate cheddar;

fn main() { handle_c_api(); }

#[cfg(not(feature = "c-api"))]
fn handle_c_api() {}

#[cfg(feature = "c-api")]
fn handle_c_api() {
  let out_dir: std::path::PathBuf =
    std::env::var_os("OUT_DIR").expect("could not find output directory").into();
  cheddar::Cheddar::new()
    .expect("unable to read cargo manifest")
    .module("c_api")
    .expect("malformed header path")
    .run_build(out_dir.join("include").join("konane.h"));
  // std::fs::copy(out_dir.join("include").join("konane.h"), )
}
<commit_msg>Improve build script<commit_after>// build.rs
// Copyright 2016 Alexander Altman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(feature = "c-api")]
extern crate cheddar;

fn main() { handle_c_api(); }

#[cfg(not(feature = "c-api"))]
fn handle_c_api() {}

#[cfg(feature = "c-api")]
fn handle_c_api() {
  let top_dir: std::path::PathBuf =
    std::env::var_os("CARGO_MANIFEST_DIR").expect("could not find cargo manifest directory").into();
  cheddar::Cheddar::new()
    .expect("unable to read cargo manifest")
    .module("c_api")
    .expect("malformed header path")
    .run_build(top_dir.join("gen").join("include").join("konane.h"));
}
<|endoftext|>


<commit_before>//! This data structure emits messages to maintain its surrounding blocks in a desired
//! loaded state (e.g. to keep player surroundings loaded, or to keep unloaded blocks
//! solid near the player).

use cgmath::Point3;
use std::cmp::max;
use std::collections::VecDeque;
use stopwatch;

use cube_shell::cube_diff;

mod surroundings_iter {
  use cgmath::{Point3};
  use std;

  use cube_shell::cube_shell;

  pub struct CubeShellClosure {
    center: Point3<i32>,
  }

  impl FnOnce<(i32,)> for CubeShellClosure {
    type Output = std::vec::IntoIter<Point3<i32>>;
    extern "rust-call" fn call_once(self, radius: (i32,)) -> Self::Output {
      cube_shell(&self.center, radius.0).into_iter()
    }
  }

  impl FnMut<(i32,)> for CubeShellClosure {
    extern "rust-call" fn call_mut(&mut self, radius: (i32,)) -> Self::Output {
      cube_shell(&self.center, radius.0).into_iter()
    }
  }

  pub type T =
    std::iter::FlatMap<
      std::ops::Range<i32>,
      std::vec::IntoIter<Point3<i32>>,
      CubeShellClosure,
    >;

  pub fn new(center: &Point3<i32>, max_distance: i32) -> T {
    (0 .. max_distance).flat_map(CubeShellClosure {
      center: *center,
    })
  }
}

#[allow(missing_docs)]
/// The type of message emitted by `SurroundingsLoader`. This stream of messages maintains
/// an owner's desired surroundings.
pub enum LoadType {
  Load,
  Unload,
  /// Load only if already loaded
  Update,
}

/// Iteratively load point3 in cube-shaped layers around the some point.
/// That point can be updated with calls to `update`.
/// What "load" exactly means depends on the closures provided.
pub struct SurroundingsLoader {
  last_position: Option<Point3<i32>>,

  max_load_distance: i32,
  to_load: Option<surroundings_iter::T>,

  to_recheck: VecDeque<Point3<i32>>,
  // The distances to the switches between LODs.
  lod_thresholds: Vec<i32>,
}

impl SurroundingsLoader {
  #[allow(missing_docs)]
  pub fn new(
    max_load_distance: i32,
    lod_thresholds: Vec<i32>,
  ) -> SurroundingsLoader {
    assert!(max_load_distance >= 0);

    SurroundingsLoader {
      last_position: None,

      to_load: None,
      max_load_distance: max_load_distance,

      to_recheck: VecDeque::new(),
      lod_thresholds: lod_thresholds,
    }
  }

  /// Update the center point around which we load, and load some more blocks.
  pub fn updates(&mut self, position: &Point3<i32>) -> Updates {
    let position_changed = self.last_position != Some(*position);
    if position_changed {
      stopwatch::time("surroundings_loader.extend", || {
        self.to_load = Some(surroundings_iter::new(&position, self.max_load_distance));
        self.last_position.map(|last_position| {
          for &distance in &self.lod_thresholds {
            self.to_recheck.extend(
              cube_diff(&last_position, &position, distance).into_iter()
            );
          }
          self.to_recheck.extend(
            cube_diff(&last_position, &position, self.max_load_distance).into_iter()
          );
        });

        self.last_position = Some(*position);
      })
    }

    Updates {
      loader: self,
      position: *position,
    }
  }
}

unsafe impl Send for SurroundingsLoader {}

/// Iterator for the updates from a SurroundingsLoader.
pub struct Updates<'a> {
  loader: &'a mut SurroundingsLoader,
  position: Point3<i32>,
}

/// Find the minimum cube shell radius it would take from one point to intersect the other.
pub fn distance_between(p1: &Point3<i32>, p2: &Point3<i32>) -> i32 {
  let dx = (p1.x - p2.x).abs();
  let dy = (p1.y - p2.y).abs();
  let dz = (p1.z - p2.z).abs();
  max(max(dx, dy), dz)
}

impl<'a> Iterator for Updates<'a> {
  type Item = (Point3<i32>, LoadType);

  fn next(&mut self) -> Option<Self::Item> {
    stopwatch::time("surroundings_loader.next", || {
      if let Some(position) = self.loader.to_recheck.pop_front() {
        let distance = distance_between(&self.position, &position);
        if distance > self.loader.max_load_distance {
          Some((position, LoadType::Unload))
        } else {
          Some((position, LoadType::Update))
        }
      } else {
        self.loader.to_load.as_mut().unwrap().next()
          .map(|position| (position, LoadType::Load))
      }
    })
  }
}
<commit_msg>minor change<commit_after>//! This data structure emits messages to maintain its surrounding blocks in a desired
//! loaded state (e.g. to keep player surroundings loaded, or to keep unloaded blocks
//! solid near the player).

use cgmath::Point3;
use std::cmp::max;
use std::collections::VecDeque;
use stopwatch;

use cube_shell::cube_diff;

mod surroundings_iter {
  use cgmath::{Point3};
  use std;

  use cube_shell::cube_shell;

  pub struct CubeShellClosure {
    center: Point3<i32>,
  }

  impl FnOnce<(i32,)> for CubeShellClosure {
    type Output = std::vec::IntoIter<Point3<i32>>;
    extern "rust-call" fn call_once(self, radius: (i32,)) -> Self::Output {
      cube_shell(&self.center, radius.0).into_iter()
    }
  }

  impl FnMut<(i32,)> for CubeShellClosure {
    extern "rust-call" fn call_mut(&mut self, radius: (i32,)) -> Self::Output {
      cube_shell(&self.center, radius.0).into_iter()
    }
  }

  pub type T =
    std::iter::FlatMap<
      std::ops::Range<i32>,
      std::vec::IntoIter<Point3<i32>>,
      CubeShellClosure,
    >;

  pub fn new(center: &Point3<i32>, max_distance: i32) -> T {
    (0 .. max_distance).flat_map(CubeShellClosure {
      center: *center,
    })
  }
}

#[allow(missing_docs)]
/// The type of message emitted by `SurroundingsLoader`. This stream of messages maintains
/// an owner's desired surroundings.
pub enum LoadType {
  Load,
  Unload,
  /// Load only if already loaded
  Update,
}

/// Iteratively load points cube-shaped layers around the some point.
/// That point can be updated with calls to `update`.
/// What "load" exactly means depends on the closures provided.
pub struct SurroundingsLoader {
  last_position: Option<Point3<i32>>,

  max_load_distance: i32,
  to_load: Option<surroundings_iter::T>,

  to_recheck: VecDeque<Point3<i32>>,
  // The distances to the switches between LODs.
  lod_thresholds: Vec<i32>,
}

impl SurroundingsLoader {
  #[allow(missing_docs)]
  pub fn new(
    max_load_distance: i32,
    lod_thresholds: Vec<i32>,
  ) -> SurroundingsLoader {
    assert!(max_load_distance >= 0);

    SurroundingsLoader {
      last_position: None,

      to_load: None,
      max_load_distance: max_load_distance,

      to_recheck: VecDeque::new(),
      lod_thresholds: lod_thresholds,
    }
  }

  /// Update the center point around which we load, and load some more blocks.
  pub fn updates(&mut self, position: &Point3<i32>) -> Updates {
    let position_changed = self.last_position != Some(*position);
    if position_changed {
      stopwatch::time("surroundings_loader.extend", || {
        self.to_load = Some(surroundings_iter::new(&position, self.max_load_distance));
        self.last_position.map(|last_position| {
          for &distance in &self.lod_thresholds {
            self.to_recheck.extend(
              cube_diff(&last_position, &position, distance).into_iter()
            );
          }
          self.to_recheck.extend(
            cube_diff(&last_position, &position, self.max_load_distance).into_iter()
          );
        });

        self.last_position = Some(*position);
      })
    }

    Updates {
      loader: self,
      position: *position,
    }
  }
}

unsafe impl Send for SurroundingsLoader {}

/// Iterator for the updates from a SurroundingsLoader.
pub struct Updates<'a> {
  loader: &'a mut SurroundingsLoader,
  position: Point3<i32>,
}

/// Find the minimum cube shell radius it would take from one point to intersect the other.
pub fn distance_between(p1: &Point3<i32>, p2: &Point3<i32>) -> i32 {
  let dx = (p1.x - p2.x).abs();
  let dy = (p1.y - p2.y).abs();
  let dz = (p1.z - p2.z).abs();
  max(max(dx, dy), dz)
}

impl<'a> Iterator for Updates<'a> {
  type Item = (Point3<i32>, LoadType);

  fn next(&mut self) -> Option<Self::Item> {
    stopwatch::time("surroundings_loader.next", || {
      if let Some(position) = self.loader.to_recheck.pop_front() {
        let distance = distance_between(&self.position, &position);
        if distance > self.loader.max_load_distance {
          Some((position, LoadType::Unload))
        } else {
          Some((position, LoadType::Update))
        }
      } else {
        self.loader.to_load.as_mut().unwrap().next()
          .map(|position| (position, LoadType::Load))
      }
    })
  }
}
<|endoftext|>


<commit_before>//! A not-quite-trivial HTTP server which responds to requests by showing the request and response
//! headers and any other information it has.

extern mod extra;
extern mod http;

use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
use std::rt::io::Writer;
use extra::time;

use http::server::{Config, Server, ServerUtil, Request, ResponseWriter};
use http::headers::HeaderEnum;
use http::headers::test_utils::to_stream_into_str;

#[deriving(Clone)]
struct InfoServer;

impl Server for InfoServer {
    fn get_config(&self) -> Config {
        Config { bind_address: SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 } }
    }

    fn handle_request(&self, r: &Request, w: &mut ResponseWriter) {
        w.headers.insert(~"Date", to_stream_into_str(&time::now_utc()));
        w.headers.insert(~"Content-Type", ~"text/html");
        w.headers.insert(~"Server", ~"Rust Thingummy/0.0-pre");
        w.write(bytes!("<!DOCTYPE html><title>Rust HTTP server</title>"));

        w.write(bytes!("
Request
"));
        let s = fmt!("

Method

%s

Host

%?

Request URI

%?

HTTP version

%?

Close connection

%?

",
            r.method.to_str(),
            r.headers.host,
            r.request_uri,
            r.version,
            r.close_connection);
        w.write(s.as_bytes().to_owned());
        w.write(bytes!("
Extension headers
"));
        w.write(bytes!("

Name	Value

"));
        for header in r.headers.iter() {
            let line = fmt!("
%s	%s
",
                            header.header_name(),
                            header.header_value());
            w.write(line.as_bytes().to_owned());
        }
        w.write(bytes!("

"));
        w.write(bytes!("
Body

"));
        w.write(r.body.as_bytes().to_owned());
        w.write(bytes!("
"));

        w.write(bytes!("
Response
"));
        let s = fmt!("

Status

%s

", w.status.to_str());
        w.write(s.as_bytes().to_owned());
        w.write(bytes!("
Headers
"));
        w.write(bytes!("

Name	Value

"));
        {
            let h = w.headers.clone();
            for (k, v) in h.iter() {
                let line = fmt!("
%s	%s
", *k, *v);
                w.write(line.as_bytes().to_owned());
            }
        }
        w.write(bytes!("

"));
        // Server doesn't yet support the chunked transfer coding, nor was Content-Length specified,
        // so we'd better close the connection to ensure the page finishes loading.
        // DON'T COPY THIS WAY OF DOING THINGS. EVER. OR ELSE.
        unsafe { ::std::cast::transmute::<&Request, &mut Request>(r).close_connection = true; }
    }
}

fn main() {
    InfoServer.serve_forever();
}
<commit_msg>Remove unnecessary allocation.<commit_after>//! A not-quite-trivial HTTP server which responds to requests by showing the request and response
//! headers and any other information it has.

extern mod extra;
extern mod http;

use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
use std::rt::io::Writer;
use extra::time;

use http::server::{Config, Server, ServerUtil, Request, ResponseWriter};
use http::headers::HeaderEnum;
use http::headers::test_utils::to_stream_into_str;

#[deriving(Clone)]
struct InfoServer;

impl Server for InfoServer {
    fn get_config(&self) -> Config {
        Config { bind_address: SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 } }
    }

    fn handle_request(&self, r: &Request, w: &mut ResponseWriter) {
        w.headers.insert(~"Date", to_stream_into_str(&time::now_utc()));
        w.headers.insert(~"Content-Type", ~"text/html");
        w.headers.insert(~"Server", ~"Rust Thingummy/0.0-pre");
        w.write(bytes!("<!DOCTYPE html><title>Rust HTTP server</title>"));

        w.write(bytes!("
Request
"));
        let s = fmt!("

Method

%s

Host

%?

Request URI

%?

HTTP version

%?

Close connection

%?

",
            r.method.to_str(),
            r.headers.host,
            r.request_uri,
            r.version,
            r.close_connection);
        w.write(s.as_bytes());
        w.write(bytes!("
Extension headers
"));
        w.write(bytes!("

Name	Value

"));
        for header in r.headers.iter() {
            let line = fmt!("
%s	%s
",
                            header.header_name(),
                            header.header_value());
            w.write(line.as_bytes());
        }
        w.write(bytes!("

"));
        w.write(bytes!("
Body

"));
        w.write(r.body.as_bytes());
        w.write(bytes!("
"));

        w.write(bytes!("
Response
"));
        let s = fmt!("

Status

%s

", w.status.to_str());
        w.write(s.as_bytes());
        w.write(bytes!("
Headers
"));
        w.write(bytes!("

Name	Value

"));
        {
            let h = w.headers.clone();
            for (k, v) in h.iter() {
                let line = fmt!("
%s	%s
", *k, *v);
                w.write(line.as_bytes());
            }
        }
        w.write(bytes!("

"));
        // Server doesn't yet support the chunked transfer coding, nor was Content-Length specified,
        // so we'd better close the connection to ensure the page finishes loading.
        // DON'T COPY THIS WAY OF DOING THINGS. EVER. OR ELSE.
        unsafe { ::std::cast::transmute::<&Request, &mut Request>(r).close_connection = true; }
    }
}

fn main() {
    InfoServer.serve_forever();
}
<|endoftext|>


<commit_before>use crate::convert::*;
use core::hash::{Hasher};

///This constant come from Kunth's prng (Empirically it works better than those from splitmix32).
const MULTIPLE: u64 = crate::MULTIPLE;
const INCREMENT: u64 = 1442695040888963407;
const ROT: u32 = 23; //17

/// A `Hasher` for hashing an arbitrary stream of bytes.
///
/// Instances of [`AHasher`] represent state that is updated while hashing data.
///
/// Each method updates the internal state based on the new data provided. Once
/// all of the data has been provided, the resulting hash can be obtained by calling
/// `finish()`
///
/// [Clone] is also provided in case you wish to calculate hashes for two different items that
/// start with the same data.
///
#[derive(Debug, Clone)]
pub struct AHasher {
    buffer: u64
}

impl AHasher {

    /// Creates a new hasher keyed to the provided key.
    #[inline]
    pub fn new_with_key(key: u64) -> AHasher {
        AHasher { buffer: key }
    }

    /// Creates a new hasher keyed to the provided keys.
    #[inline]
    pub(crate) fn new_with_keys(key1: u64, key2: u64) -> AHasher {
        AHasher { buffer: key1 ^ (key2.rotate_left(ROT)) }
    }

    /// This update function has the goal of updating the buffer with a single multiply
    /// FxHash does this but is venerable to attack. To avoid this input needs to be masked to with an
    /// unpredictable value. Other hashes such as murmurhash have taken this approach but were found venerable
    /// to attack. The attack was based on the idea of reversing the pre-mixing (Which is necessarily
    /// reversible otherwise bits would be lost) then placing a difference in the highest bit before the
    /// multiply used to mix the data. Because a multiply can never affect the bits to the right of it, a
    /// subsequent update that also differed in this bit could result in a predictable collision.
    ///
    /// This version avoids this vulnerability while still only using a single multiply. It takes advantage
    /// of the fact that when a 64 bit multiply is performed the upper 64 bits are usually computed and thrown
    /// away. Instead it creates two 128 bit values where the upper 64 bits are zeros and multiplies them.
    /// (The compiler is smart enough to turn this into a 64 bit multiplication in the assembly)
    /// Then the upper bits are added to the lower bits to produce a single 64 bit result.
    ///
    /// To understand why this is a good scrambling function it helps to understand multiply-with-carry PRNGs:
    /// https://en.wikipedia.org/wiki/Multiply-with-carry_pseudorandom_number_generator
    /// If the multiple is chosen well, this creates a long period, decent quality PRNG.
    /// Notice that this function is equivalent to this except the `buffer`/`state` is being xored with each
    /// new block of data. In the event that data is all zeros, it is exactly equivalent to a MWC PRNG.
    ///
    /// This is impervious to attack because every bit buffer at the end is dependent on every bit in
    /// `new_data ^ buffer`. For example suppose two inputs differed in only the 5th bit. Then when the
    /// multiplication is performed the `result` will differ in bits 5-69. More specifically it will differ by
    /// 2^5 * MULTIPLE. However in the next step bits 65-128 are turned into a separate 64 bit value. So the
    /// differing bits will be in the lower 6 bits of this value. The two intermediate values that differ in
    /// bits 5-63 and in bits 0-5 respectively get added together. Producing an output that differs in every
    /// bit. The addition carries in the multiplication and at the end additionally mean that the even if an
    /// attacker somehow knew part of (but not all) the contents of the buffer before hand,
    /// they would not be able to predict any of the bits in the buffer at the end.
    #[inline(always)]
    fn update(&mut self, new_data: u64) {
        let result: [u64;2] = ((new_data ^ self.buffer) as u128).wrapping_mul(MULTIPLE as u128).convert();
        self.buffer = result[0].wrapping_add(result[1]);
    }

    /// This update function updates the buffer with the new information in a way that can't be canceled
    /// with a subsequent update without knowledge of the content of the buffer prior to the update.
    ///
    /// To achieve this the input needs to be modified in an unpredictable (to an attacker) way before it is
    /// combined with the value in the buffer. This is done by xoring it with `key`.
    ///
    /// Other hashes such as murmurhash have taken that approach but were found venerable to attack.
    /// The attack was based on the idea of reversing any pre-mixing (Which is necessarily reversible otherwise
    /// bits would be lost) then placing a difference in the highest bit before the multiply. Because a multiply
    /// can never affect the bits to the right of it, a subsequent update that also only differed in the high
    /// order bit could cancel out the change to `buffer` from the first update. This allowed murmurhash to be
    /// attacked. In this update function aHash avoids this vulnerability by rotating and performing a second
    /// multiply.
    ///
    /// This makes it impossible for an attacker to place a single bit difference between
    /// two blocks so as to cancel each other. (While the transform is still reversible if you know the key)
    ///
    /// This is similar to the above update function but is designed to run in a loop
    /// that will be unrolled and vectorized. So instead of using the buffer, it uses a 'key' that it updates
    /// and returns. The buffer is only xored at the end. This structure is so that when the method is inlined,
    /// the compiler will unroll any loop this gets placed in and the loop can be automatically vectorized
    /// and the rotates, xors, and multiplies can be paralleled.
    ///
    /// The key needs to be incremented between consecutive calls to prevent (a,b) from hashing the same as (b,a).
    /// The adding of the increment is moved to the bottom rather than the top. This allows one less add to be
    /// performed overall, but more importantly, it follows the multiply, which is expensive. So the CPU can
    /// run another operation afterwords if does not depend on the output of the multiply operation.
    #[inline(always)]
    fn ordered_update(&mut self, new_data: u64, key: u64) -> u64 {
        self.buffer ^= (new_data ^ key).wrapping_mul(MULTIPLE).rotate_left(ROT).wrapping_mul(MULTIPLE);
        key.wrapping_add(INCREMENT)
    }
}

#[inline(never)]
#[no_mangle]
fn hash_test(input: &[u8]) -> u64 {
    let mut a = AHasher::new_with_key(67);
    a.write(input);
    a.finish()
}

/// Provides methods to hash all of the primitive types.
impl Hasher for AHasher {

    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u16(&mut self, i: u16) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u128(&mut self, i: u128) {
        let data: [u64;2] = i.convert();
        self.update(data[0]);
        self.update(data[1]);
    }

    #[inline]
    fn write_usize(&mut self, i: usize) {
        self.write_u64(i as u64);
    }

    #[inline]
    fn write(&mut self, input: &[u8]) {
        let mut data = input;
        let length = data.len() as u64;
        //Needs to be an add rather than an xor because otherwise it could be canceled with carefully formed input.
        self.buffer = self.buffer.wrapping_add(length.wrapping_mul(MULTIPLE));
        //A 'binary search' on sizes reduces the number of comparisons.
        if data.len() > 8 {
            if data.len() > 16 {
                let tail = data.read_last_u64();
                let mut key: u64 = self.buffer;
                while data.len() > 8 {
                    let (val, rest) = data.read_u64();
                    key = self.ordered_update(val, key);
                    data = rest;
                }
                self.update(tail);
            } else {
                self.update(data.read_u64().0);
                self.update(data.read_last_u64());
            }
        } else {
            if data.len() >= 2 {
                if data.len() >= 4 {
                    let block: [u32; 2] = [data.read_u32().0, data.read_last_u32()];
                    self.update(block.convert());
                } else {
                    let block: [u16; 2] = [data.read_u16().0, data.read_last_u16()];
                    let val: u32 = block.convert();
                    self.update(val as u64);
                }
            } else {
                let value = if data.len() > 0 {
                    data[0] //len 1
                } else {
                    0
                };
                self.update(value as u64);
            }
        }
    }
    #[inline]
    fn finish(&self) -> u64 {
        self.buffer
    }
}

#[cfg(test)]
mod tests {
    use crate::convert::Convert;
    use crate::fallback_hash::*;

    #[test]
    fn test_hash() {
        let mut hasher = AHasher::new_with_keys(0,0);
        let value: u64 = 1 << 32;
        hasher.update(value);
        let result = hasher.buffer;
        let mut hasher = AHasher::new_with_keys(0,0);
        let value2: u64 = 1;
        hasher.update(value2);
        let result2 = hasher.buffer;
        let result: [u8; 8] = result.convert();
        let result2: [u8; 8] = result2.convert();
        assert_ne!(hex::encode(result), hex::encode(result2));
    }

    #[test]
    fn test_conversion() {
        let input: &[u8] = "dddddddd".as_bytes();
        let bytes: u64 = as_array!(input, 8).convert();
        assert_eq!(bytes, 0x6464646464646464);
    }
}
<commit_msg>Fix typo<commit_after>use crate::convert::*;
use core::hash::{Hasher};

///This constant come from Kunth's prng (Empirically it works better than those from splitmix32).
const MULTIPLE: u64 = crate::MULTIPLE;
const INCREMENT: u64 = 1442695040888963407;
const ROT: u32 = 23; //17

/// A `Hasher` for hashing an arbitrary stream of bytes.
///
/// Instances of [`AHasher`] represent state that is updated while hashing data.
///
/// Each method updates the internal state based on the new data provided. Once
/// all of the data has been provided, the resulting hash can be obtained by calling
/// `finish()`
///
/// [Clone] is also provided in case you wish to calculate hashes for two different items that
/// start with the same data.
///
#[derive(Debug, Clone)]
pub struct AHasher {
    buffer: u64
}

impl AHasher {

    /// Creates a new hasher keyed to the provided key.
    #[inline]
    pub fn new_with_key(key: u64) -> AHasher {
        AHasher { buffer: key }
    }

    /// Creates a new hasher keyed to the provided keys.
    #[inline]
    pub(crate) fn new_with_keys(key1: u64, key2: u64) -> AHasher {
        AHasher { buffer: key1 ^ (key2.rotate_left(ROT)) }
    }

    /// This update function has the goal of updating the buffer with a single multiply
    /// FxHash does this but is vulnerable to attack. To avoid this input needs to be masked to with an
    /// unpredictable value. Other hashes such as murmurhash have taken this approach but were found vulnerable
    /// to attack. The attack was based on the idea of reversing the pre-mixing (Which is necessarily
    /// reversible otherwise bits would be lost) then placing a difference in the highest bit before the
    /// multiply used to mix the data. Because a multiply can never affect the bits to the right of it, a
    /// subsequent update that also differed in this bit could result in a predictable collision.
    ///
    /// This version avoids this vulnerability while still only using a single multiply. It takes advantage
    /// of the fact that when a 64 bit multiply is performed the upper 64 bits are usually computed and thrown
    /// away. Instead it creates two 128 bit values where the upper 64 bits are zeros and multiplies them.
    /// (The compiler is smart enough to turn this into a 64 bit multiplication in the assembly)
    /// Then the upper bits are added to the lower bits to produce a single 64 bit result.
    ///
    /// To understand why this is a good scrambling function it helps to understand multiply-with-carry PRNGs:
    /// https://en.wikipedia.org/wiki/Multiply-with-carry_pseudorandom_number_generator
    /// If the multiple is chosen well, this creates a long period, decent quality PRNG.
    /// Notice that this function is equivalent to this except the `buffer`/`state` is being xored with each
    /// new block of data. In the event that data is all zeros, it is exactly equivalent to a MWC PRNG.
    ///
    /// This is impervious to attack because every bit buffer at the end is dependent on every bit in
    /// `new_data ^ buffer`. For example suppose two inputs differed in only the 5th bit. Then when the
    /// multiplication is performed the `result` will differ in bits 5-69. More specifically it will differ by
    /// 2^5 * MULTIPLE. However in the next step bits 65-128 are turned into a separate 64 bit value. So the
    /// differing bits will be in the lower 6 bits of this value. The two intermediate values that differ in
    /// bits 5-63 and in bits 0-5 respectively get added together. Producing an output that differs in every
    /// bit. The addition carries in the multiplication and at the end additionally mean that the even if an
    /// attacker somehow knew part of (but not all) the contents of the buffer before hand,
    /// they would not be able to predict any of the bits in the buffer at the end.
    #[inline(always)]
    fn update(&mut self, new_data: u64) {
        let result: [u64;2] = ((new_data ^ self.buffer) as u128).wrapping_mul(MULTIPLE as u128).convert();
        self.buffer = result[0].wrapping_add(result[1]);
    }

    /// This update function updates the buffer with the new information in a way that can't be canceled
    /// with a subsequent update without knowledge of the content of the buffer prior to the update.
    ///
    /// To achieve this the input needs to be modified in an unpredictable (to an attacker) way before it is
    /// combined with the value in the buffer. This is done by xoring it with `key`.
    ///
    /// Other hashes such as murmurhash have taken that approach but were found vulnerable to attack.
    /// The attack was based on the idea of reversing any pre-mixing (Which is necessarily reversible otherwise
    /// bits would be lost) then placing a difference in the highest bit before the multiply. Because a multiply
    /// can never affect the bits to the right of it, a subsequent update that also only differed in the high
    /// order bit could cancel out the change to `buffer` from the first update. This allowed murmurhash to be
    /// attacked. In this update function aHash avoids this vulnerability by rotating and performing a second
    /// multiply.
    ///
    /// This makes it impossible for an attacker to place a single bit difference between
    /// two blocks so as to cancel each other. (While the transform is still reversible if you know the key)
    ///
    /// This is similar to the above update function but is designed to run in a loop
    /// that will be unrolled and vectorized. So instead of using the buffer, it uses a 'key' that it updates
    /// and returns. The buffer is only xored at the end. This structure is so that when the method is inlined,
    /// the compiler will unroll any loop this gets placed in and the loop can be automatically vectorized
    /// and the rotates, xors, and multiplies can be paralleled.
    ///
    /// The key needs to be incremented between consecutive calls to prevent (a,b) from hashing the same as (b,a).
    /// The adding of the increment is moved to the bottom rather than the top. This allows one less add to be
    /// performed overall, but more importantly, it follows the multiply, which is expensive. So the CPU can
    /// run another operation afterwords if does not depend on the output of the multiply operation.
    #[inline(always)]
    fn ordered_update(&mut self, new_data: u64, key: u64) -> u64 {
        self.buffer ^= (new_data ^ key).wrapping_mul(MULTIPLE).rotate_left(ROT).wrapping_mul(MULTIPLE);
        key.wrapping_add(INCREMENT)
    }
}

#[inline(never)]
#[no_mangle]
fn hash_test(input: &[u8]) -> u64 {
    let mut a = AHasher::new_with_key(67);
    a.write(input);
    a.finish()
}

/// Provides methods to hash all of the primitive types.
impl Hasher for AHasher {

    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u16(&mut self, i: u16) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.update(i as u64);
    }

    #[inline]
    fn write_u128(&mut self, i: u128) {
        let data: [u64;2] = i.convert();
        self.update(data[0]);
        self.update(data[1]);
    }

    #[inline]
    fn write_usize(&mut self, i: usize) {
        self.write_u64(i as u64);
    }

    #[inline]
    fn write(&mut self, input: &[u8]) {
        let mut data = input;
        let length = data.len() as u64;
        //Needs to be an add rather than an xor because otherwise it could be canceled with carefully formed input.
        self.buffer = self.buffer.wrapping_add(length.wrapping_mul(MULTIPLE));
        //A 'binary search' on sizes reduces the number of comparisons.
        if data.len() > 8 {
            if data.len() > 16 {
                let tail = data.read_last_u64();
                let mut key: u64 = self.buffer;
                while data.len() > 8 {
                    let (val, rest) = data.read_u64();
                    key = self.ordered_update(val, key);
                    data = rest;
                }
                self.update(tail);
            } else {
                self.update(data.read_u64().0);
                self.update(data.read_last_u64());
            }
        } else {
            if data.len() >= 2 {
                if data.len() >= 4 {
                    let block: [u32; 2] = [data.read_u32().0, data.read_last_u32()];
                    self.update(block.convert());
                } else {
                    let block: [u16; 2] = [data.read_u16().0, data.read_last_u16()];
                    let val: u32 = block.convert();
                    self.update(val as u64);
                }
            } else {
                let value = if data.len() > 0 {
                    data[0] //len 1
                } else {
                    0
                };
                self.update(value as u64);
            }
        }
    }
    #[inline]
    fn finish(&self) -> u64 {
        self.buffer
    }
}

#[cfg(test)]
mod tests {
    use crate::convert::Convert;
    use crate::fallback_hash::*;

    #[test]
    fn test_hash() {
        let mut hasher = AHasher::new_with_keys(0,0);
        let value: u64 = 1 << 32;
        hasher.update(value);
        let result = hasher.buffer;
        let mut hasher = AHasher::new_with_keys(0,0);
        let value2: u64 = 1;
        hasher.update(value2);
        let result2 = hasher.buffer;
        let result: [u8; 8] = result.convert();
        let result2: [u8; 8] = result2.convert();
        assert_ne!(hex::encode(result), hex::encode(result2));
    }

    #[test]
    fn test_conversion() {
        let input: &[u8] = "dddddddd".as_bytes();
        let bytes: u64 = as_array!(input, 8).convert();
        assert_eq!(bytes, 0x6464646464646464);
    }
}
<|endoftext|>


<commit_before>use self::routes::*;
use self::response::*;
use self::settings::*;
use self::context::*;

use http::server::Request;
use http::*;
use knob::Settings;

pub mod routes;
pub mod response;
pub mod settings;
pub mod context;
pub mod params;

#[deriving(Clone)]
pub struct Application<T> {
  routes: Routes<T>,
  settings: Settings
}

impl<T: ToResponse> Application<T> {
  pub fn new(create: &fn (&mut Application<T>)) -> Application<T> {
    let mut app = ~Application { routes: Routes::new(), settings: Settings::new() };
    create(app);
    app.settings.load_os_args();
    *app
  }

  pub fn settings<'a>(&'a mut self, config: &fn(&'a mut Settings)) {
    config(&'a mut self.settings);
  }

  pub fn routes<'a>(&'a mut self, draw: &fn(&'a mut Routes<T>)) {
    draw(&'a mut self.routes)
  }

  pub fn call(&self, request: &Request) -> Response {
    match self.routes.find(request) {
      Ok(route) => {
        match route {
          MatchedRoute { method, params, f } => {
            let ctx = Context { settings: &self.settings, params: params, request: request };
            let result = f(ctx);
            result.to_response()
          }
        }

      },
      Err(error) => {
        match error {
          NotFoundError => { Response::new(status::NotFound, ~"Not Found") },
          MethodNotAllowedError => { Response::new(status::MethodNotAllowed, ~"Method not allowed") }
        }
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use super::context::*;

  use http::method::*;
  use http::status::*;
  use http::server::request::*;

  use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
  use http::headers::request::HeaderCollection;

  fn get(path: ~str) -> Request {
    Request {
        remote_addr: Some(SocketAddr { ip: Ipv4Addr(127,0,0,1), port:4000 }),
        headers: ~HeaderCollection::new(),
        body: ~"",
        method: Get,
        request_uri: AbsolutePath(path),
        close_connection: true,
        version: (1, 1),
    }
  }

  fn dummy(_ctx: Context) {

  }

  #[test]
  fn test_full_match() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/foo"));
    assert_eq!(res.status, Ok);
  }

  #[test]
  fn test_longer_url() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/foobar"));
    assert_eq!(res.status, NotFound);
  }

  #[test]
  fn test_url_prefix() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/fo"));
    assert_eq!(res.status, NotFound);
  }

  #[test]
  fn test_wrong_method() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.post(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/foo"));
    assert_eq!(res.status, MethodNotAllowed);
  }

  fn assert_id(context: Context) {
    let params = context.params;
    let id: Option<~str> = params.fetch(~"id");

    assert_eq!(id, Some(~"123"))
  }

  #[test]
  fn test_parameters() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo/(?<id>\\d+)", dummy)
      }
    };
    let res = app.call(&get(~"/foo/123"));
    assert_eq!(res.status, Ok);
  }
}<commit_msg>Add more tests<commit_after>use self::routes::*;
use self::response::*;
use self::settings::*;
use self::context::*;

use http::server::Request;
use http::*;
use knob::Settings;

pub mod routes;
pub mod response;
pub mod settings;
pub mod context;
pub mod params;

#[deriving(Clone)]
pub struct Application<T> {
  routes: Routes<T>,
  settings: Settings
}

impl<T: ToResponse> Application<T> {
  pub fn new(create: &fn (&mut Application<T>)) -> Application<T> {
    let mut app = ~Application { routes: Routes::new(), settings: Settings::new() };
    create(app);
    app.settings.load_os_args();
    *app
  }

  pub fn settings<'a>(&'a mut self, config: &fn(&'a mut Settings)) {
    config(&'a mut self.settings);
  }

  pub fn routes<'a>(&'a mut self, draw: &fn(&'a mut Routes<T>)) {
    draw(&'a mut self.routes)
  }

  pub fn call(&self, request: &Request) -> Response {
    match self.routes.find(request) {
      Ok(route) => {
        match route {
          MatchedRoute { method, params, f } => {
            let ctx = Context { settings: &self.settings, params: params, request: request };
            let result = f(ctx);
            result.to_response()
          }
        }

      },
      Err(error) => {
        match error {
          NotFoundError => { Response::new(status::NotFound, ~"Not Found") },
          MethodNotAllowedError => { Response::new(status::MethodNotAllowed, ~"Method not allowed") }
        }
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use super::context::*;

  use http::method::*;
  use http::status::*;
  use http::server::request::*;

  use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
  use http::headers::request::HeaderCollection;

  fn get(path: ~str) -> Request {
    Request {
        remote_addr: Some(SocketAddr { ip: Ipv4Addr(127,0,0,1), port:4000 }),
        headers: ~HeaderCollection::new(),
        body: ~"",
        method: Get,
        request_uri: AbsolutePath(path),
        close_connection: true,
        version: (1, 1),
    }
  }

  fn dummy(_ctx: Context) {

  }

  #[test]
  fn test_full_match() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/foo"));
    assert_eq!(res.status, Ok);
  }

  #[test]
  fn test_longer_url() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/foobar"));
    assert_eq!(res.status, NotFound);
  }

  #[test]
  fn test_url_prefix() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/fo"));
    assert_eq!(res.status, NotFound);
  }

  #[test]
  fn test_wrong_method() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.post(~"/foo", dummy)
      }
    };
    let res = app.call(&get(~"/foo"));
    assert_eq!(res.status, MethodNotAllowed);
  }

  fn assert_id(context: Context) {
    let params = context.params;
    let id: Option<~str> = params.fetch(~"id");

    assert_eq!(id, Some(~"123"))
  }

  #[test]
  fn test_parameters() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo/(?<id>\\d+)", assert_id)
      }
    };
    let res = app.call(&get(~"/foo/123"));
    assert_eq!(res.status, Ok);
    let res = app.call(&get(~"/foo"));
    assert_eq!(res.status, NotFound);
  }

  fn assert_ids_given(context: Context) {
    let params = context.params;
    let id: Option<~str> = params.fetch("id");
    let bar_id: Option<~str> = params.fetch("bar_id");

    assert_eq!(id, Some(~"123"))
    assert_eq!(bar_id, Some(~"456"))
  }

  #[test]
  fn test_optional_parameters_given() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo/(?<id>\\d+)(?:/bar/(?<bar_id>\\d+))?", assert_ids_given)
      }
    };
    let res = app.call(&get(~"/foo/123/bar/456"));
    assert_eq!(res.status, Ok);
  }

  fn assert_ids_missing(context: Context) {
    let params = context.params;
    let id: Option<~str> = params.fetch("id");
    let bar_id: Option<~str> = params.fetch("bar_id");

    assert_eq!(id, Some(~"123"))
    assert_eq!(bar_id, None)
  }

  #[test]
  fn test_optional_parameters_missing() {
    let app = do Application::new |app| {
      do app.routes |routes| {
        routes.get(~"/foo/(?<id>\\d+)(?:/bar/(?<bar_id>\\d+))?", assert_ids_missing)
      }
    };
    let res = app.call(&get(~"/foo/123"));
    assert_eq!(res.status, Ok);
  }
}<|endoftext|>