<commit_before>// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


// To avoid problems with Windows UAC installer detection heuristics,
// Rust-produced executables need an application manifest.
// For details, see issue #10512.

// No-op on other platforms.

use driver::session::Session;
use std::path::Path;

#[cfg(not(windows))]
pub fn postprocess_executable(sess: Session, filename: &Path) {}

#[cfg(windows)]
pub fn postprocess_executable(sess: Session, filename: &Path) {

    let default_manifest = concat!(
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>",
        "<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>",
        "  <trustInfo xmlns='urn:schemas-microsoft-com:asm.v3'>",
        "    <security>",
        "      <requestedPrivileges>",
        "        <requestedExecutionLevel level='asInvoker' uiAccess='false' />",
        "      </requestedPrivileges>",
        "    </security>",
        "  </trustInfo>",
        "</assembly>");

    match windows::embed_manifest(filename, default_manifest) {
        Ok(_) => (),
        Err(msg) => sess.err(format!("Could not embed application manifest: {}", msg))
    }
}

#[cfg(windows)]
mod windows {
    use std::libc::types::os::arch::extra::{BOOL,WORD,DWORD,HANDLE,LPCWSTR,LPCVOID};
    use std::libc::consts::os::extra::FALSE;
    use std::cast::transmute;
    use std::os;

    // FIXME #9053: should import as_utf16_p from std rather than re-defining here
    //use std::os::win32::as_utf16_p;
    fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
        let mut t = s.to_utf16();
        // Null terminate before passing on.
        t.push(0u16);
        t.as_imm_buf(|buf, _len| f(buf))
    }

    #[link_name = "kernel32"]
    extern "system" {
        pub fn BeginUpdateResourceW(pFileName: LPCWSTR,
                                    bDeleteExistingResources: BOOL) -> HANDLE;
        pub fn UpdateResourceW(hUpdate: HANDLE,
                               lpType: LPCWSTR,
                               lpName: LPCWSTR,
                               wLanguage: WORD,
                               lpData: LPCVOID,
                               cbData: DWORD) -> BOOL;
        pub fn EndUpdateResourceW(hUpdate: HANDLE,
                                  fDiscard: BOOL) -> BOOL;
    }

    fn MAKEINTRESOURCEW(id: int) -> LPCWSTR {
        unsafe{ transmute(id) }
    }

    pub fn embed_manifest(filename: &Path, 
                          manifest: &str) -> Result<(),~str> {
        unsafe {
            let hUpdate = as_utf16_p(filename.as_str().unwrap(), |path| {
                BeginUpdateResourceW(path, FALSE)
            });
            if hUpdate.is_null() {
                return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error()));
            }

            let ok = manifest.as_imm_buf(|p, len| {
                UpdateResourceW(hUpdate,
                                MAKEINTRESOURCEW(24), // RT_MANIFEST
                                MAKEINTRESOURCEW(1),  // CREATEPROCESS_MANIFEST_RESOURCE_ID<commit_msg>Fixed "unused variable" errors and trailing whitespace.<commit_after>// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


// To avoid problems with Windows UAC installer detection heuristics,
// Rust-produced executables need an application manifest.
// For details, see issue #10512.

// No-op on other platforms.

use driver::session::Session;
use std::path::Path;

#[cfg(not(windows))]
pub fn postprocess_executable(_sess: Session, _filename: &Path) {}

#[cfg(windows)]
pub fn postprocess_executable(sess: Session, filename: &Path) {

    let default_manifest = concat!(
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>",
        "<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>",
        "  <trustInfo xmlns='urn:schemas-microsoft-com:asm.v3'>",
        "    <security>",
        "      <requestedPrivileges>",
        "        <requestedExecutionLevel level='asInvoker' uiAccess='false' />",
        "      </requestedPrivileges>",
        "    </security>",
        "  </trustInfo>",
        "</assembly>");

    match windows::embed_manifest(filename, default_manifest) {
        Ok(_) => (),
        Err(msg) => sess.err(format!("Could not embed application manifest: {}", msg))
    }
}

#[cfg(windows)]
mod windows {
    use std::libc::types::os::arch::extra::{BOOL,WORD,DWORD,HANDLE,LPCWSTR,LPCVOID};
    use std::libc::consts::os::extra::FALSE;
    use std::cast::transmute;
    use std::os;

    // FIXME #9053: should import as_utf16_p from std rather than re-defining here
    //use std::os::win32::as_utf16_p;
    fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
        let mut t = s.to_utf16();
        // Null terminate before passing on.
        t.push(0u16);
        t.as_imm_buf(|buf, _len| f(buf))
    }

    #[link_name = "kernel32"]
    extern "system" {
        pub fn BeginUpdateResourceW(pFileName: LPCWSTR,
                                    bDeleteExistingResources: BOOL) -> HANDLE;
        pub fn UpdateResourceW(hUpdate: HANDLE,
                               lpType: LPCWSTR,
                               lpName: LPCWSTR,
                               wLanguage: WORD,
                               lpData: LPCVOID,
                               cbData: DWORD) -> BOOL;
        pub fn EndUpdateResourceW(hUpdate: HANDLE,
                                  fDiscard: BOOL) -> BOOL;
    }

    fn MAKEINTRESOURCEW(id: int) -> LPCWSTR {
        unsafe{ transmute(id) }
    }

    pub fn embed_manifest(filename: &Path,
                          manifest: &str) -> Result<(),~str> {
        unsafe {
            let hUpdate = as_utf16_p(filename.as_str().unwrap(), |path| {
                BeginUpdateResourceW(path, FALSE)
            });
            if hUpdate.is_null() {
                return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error()));
            }

            let ok = manifest.as_imm_buf(|p, len| {
                UpdateResourceW(hUpdate,
                                MAKEINTRESOURCEW(24), // RT_MANIFEST
                                MAKEINTRESOURCEW(1),  // CREATEPROCESS_MANIFEST_RESOURCE_ID
{'fraction_non_alphanumeric': 0.13263997720473003, 'fraction_numerical': 0.013107280239350335, 'mean_word_length': 2.1807884005437246, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 2, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6175529', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2087, 'n_words': 489}