String overflowing when concatenated

I am trying to make a software packer, but I always fail because when I concat three lines (one contains the source prefix, one contains the executable content, the other contains the source suffix) the content overflows into the suffix. Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.IO.Compression;

namespace ProgramPacker
{
    public partial class Form1 : Form
    {
        static string prefix = @"using System;
using System.Collections.Generic;
using System.Text; 
using System.IO; 

namespace ProgramPacker 
{ 
    class Program 
    { 
        static string inside = @" + "\"";
        static string suffix = "\";\n" + @"static void Main(string[] args) 
        { 
            string temp = Path.GetRandomFileName() +" + "\"" + @".exe" + "\"" + @";
            BinaryWriter sw = new BinaryWriter(new FileStream(temp, FileMode.Create));
            sw.Write(inside);
            sw.Flush();
            sw.Close();
            System.Diagnostics.Process.Start(temp); 
        } 
    } 
}";
    public string code = "";
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        BinaryReader br = new BinaryReader(new FileStream(openFileDialog1.FileName, FileMode.Open));
        byte[] data = new byte[br.BaseStream.Length];
        br.Read(data, 0, (int)br.BaseStream.Length);
        br.Close();
        string inside = Encoding.UTF7.GetString(data);
        code = string.Concat(prefix, string.Concat(inside, suffix));
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Console.Write(code);
        CSharpCodeProvider cs = new CSharpCodeProvider();
        ICodeCompiler compile = cs.CreateCompiler();
        CompilerParameters param = new CompilerParameters();
        param.GenerateInMemory = false;
        param.ReferencedAssemblies.Add("mscorlib.dll");
        param.ReferencedAssemblies.Add("System.dll");
        param.ReferencedAssemblies.Add("System.Core.dll");
        param.GenerateExecutable = true;
        param.OutputAssembly = Environment.CurrentDirectory + "/a.exe";
        param.WarningLevel = 4;
        CompilerResults comp = compile.CompileAssemblyFromSource(param, code);
        foreach (CompilerError error in comp.Errors)
        {
            MessageBox.Show(error.Line + " " + error.Column + " "  + error.ErrorText);
        }
        MessageBox.Show(comp.PathToAssembly);
        MessageBox.Show("Finish!");
    }
}

      

}

It outputs:

using System;
using System.Collections.Generic;
using System.Text; 
using System.IO; 

namespace ProgramPacker
{ 
    class Program 
    { 
        static string inside = @"?ãÎoüoy±ôãøÿ?ÿQÔh¦Rt!?^ÿyóë=G:Fÿ»??¿Ç/}òKÿ?úõßû3õMù?c·?Äûòëoª?À_Û>LÖïá^öÿ·õ©üüòcÊ?ª??÷Ô?î:^ȯÏäG¶?ù ?ñË?oñëy:ôã7??ò@ÚyN?w¿=?ëÆ÷ëÛßTëºÓ»yßüø]áå{ö/àïªêL©Oÿ;ú5rù% ä¸?1)Ï?¥?y®ÿ]0QL8û×ÖGòów±?øÿèü[?ª~éá¿ÿó£üüJ~ܹ
Êw¡h??/?úçeñÈ_?¿?ü½L^ywü7?,ùÅû¿ß£ó÷w?É~Ç????ê?ÿQÌyç¿??¹Cö×vZ__>?? ûïx?_ü"õ!ì; ;ùõåÇ?ú3*­¿   @ÿV?èÿ¿Ë??/½P¶y?â¡??ùñÿÑæä/A¶_ò?v??ÿ?ÿQtÄx÷w?O'N?u$ÿk÷U«yÆò?£.Y?ùw(?ßÔ6ÿ]2ÿeUÆò¿?ضö·ßO~ü#*y£Jæ·Ä w yóYëï´
õ}y¡ä¨ù÷YPdú?Z©Óé¼R?æg?ÀSyà쬽Éÿ­ûÁ?ym>ut?ÿA??>?¿[ôçF ê´)9ß9xÿ£¿?
åùïyí ÁTlù
-ùñ?Òÿ·!·
E?£üO5÷]çºy?7Áè¿?Zô?(å§
#ü¥úõ®?o«âû]0Èßä7¿à??¿õ?d?ѵ;Jü_K?úAMìT>Àü1mR0sâ¿ê¨õ{äö×ó??¿.úÿõ½Eä?ÔïÿÎÿ±³v'ÿûìw ?»oÃø1±WtB
É¡wì&øuå£Îÿ?Éw?|úWÿ=ö÷_ÿ·?y®YWöwû¿ßß?Öe'û?%?yß/×Àî-yÿ.?8åóù?ûÇÚ6ÿÂßøï?Áàï?{8Zy?¿Å5yø    ÇpÏ9=ó»üÑ>Õ?Èÿ?Y¿,?ëÿ?Êçÿì_}E?÷Ûú|î$ò÷ø}Jÿ^Êtò¿?@á|ò{Ø`<ªÿQ?QLæ½õo)¿ü&¿áÂÿ¼ó¿_÷7yMå8)¿Ùo'¿õ±üü=~#LÚ¿ó§¢Ë÷üO?Æ?å÷WJÍ$¾¸øW?A?Òÿ(09Áÿà³_ëÿù3l?SWYµßô÷¤~?ÀG¿ñ
??Às%ÍyÑ;Ò¸!ÿ?_Iÿ¼¼í/Ôrö
?T´ôÑùyǹßE~üV?ñ¿C~VßùuÿG   X8|._¼Å_:Æü-?îü:?øì?|W  ¤ÃÓW01?$å×?Û#ùIÿû·1:4û½ü._y^òÙNó?º1?ßIÍ?ÿ¿ß_~|ÖùX?@?në_B?¡µ~_×ãÿhãèïØÏßì¡yïôk_y;HÿÛyÿàâ/sÀ??? $ìw0Jüßü«@?ûo?¿îËX
´úÏ?Æ?Y>øçÿFpyÅÿèËöyK¡²Ì`}ù½Ò¿íÿÀñn"?Í?ëOëi»ò¹Î~çÿ?_òüîÿh£ôáêDøÏÿ¶?ç·fy ÄúçÿÖÿ?yÿ?¿?ã0ÿ¿Ë÷åå?ÿQ|ü/ü9¿?÷×Hè×ÉÏ#õ??æ?ÿâ?x?ß?5µ}~Øy¾ß^øè{¿
y{íü;Òÿ¿óú7¤¡î?»    yû?ÀÓøiP?ÿÀ_ôK?Gëªíá@??ú%íwS×ñ¿¨#Û¦ÿÿÛªy\]¼÷Z~yR~ü¶ÿ£hû???ÇãÿQVèÿ©?÷{O?Gë¬ ??¡ø\«L` æcëí¿ ½?»?ü?oë¼Ätÿ>ò÷£­_¯ÖiÄ?¯ÿÓ¿÷Ï?(c¦äàéûèó?ü£´çÕè4üy_
r?qÆ¿ø'?KÿÀ_ó×xùkÔ¿FõkÌ~õ¯1y5Ú_ã'??n~âר~å¯ñkü»¿Æø×رÿÿ5~?_ã7ø5~Í_ã?Z4ÔrñkL~ò׸?5Òßûÿ??ñïñnQ¦?yYÕò³vÇ;¥ùrZÍ?åÅg}õæÙöÁGiÓfËYVVËü³®óæ£ßãè7NgM?/&åuJrustInfo>
</assembly>
; 
        } 
    } 
}

      

Any help?

EDIT: Why is everyone voting? I just asked a question.

+3


source to share


4 answers


You are having a problem with a view that is hardly possible to fix with your code as is.

However, you can take a similar approach with minor changes (which don't actually compress your program at all).

  • Remove the @

    lines from the definition inside

    . This is one of the reasons for your problems.
  • You can't just put a BELL or NUL character on a string in your string, write them out in Unicode instead:

    string inside = String.Concat(
        data.Select(b => String.Format(@"\u{0:X4}", b)));
    
          

  • Now, in your code suffix

    , reinterpret the string inside

    as the characters you passed in bytes:

    sw.Write(inside.Select(c => (byte)c).ToArray()); // hardly efficient
    
          



I was able to use these changes and successfully "package" and execute the following:

C:\temp>type hello.cs 
using System;
class M {
static void Main(string[] args) {
System.IO.File.Create("hello.world");
}
}

C:\temp>csc hello.cs 
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.


C:\temp>pack.exe hello.exe 
3584 bytes

C:\temp>a.exe

C:\temp>dir *.world 
 Volume in drive C is OSDisk
 Volume Serial Number is AABD-D663

 Directory of C:\temp

 03/22/2012  16:36                 0 hello.world
               1 File(s)              0 bytes
               0 Dir(s)  279,351,762,944 bytes free

      

+1


source


Don't use bare UTF7 or UTF8 strings, use Base64 encoding instead.



// given: byte[] data = new byte[...]
string inside = System.Convert.ToBase64String(data);
code = string.Concat(prefix, string.Concat(inside, suffix));    

// in your target code
sw.Write(System.Convert.FromBase64String(inside));

      

+4


source


Better to use CodeCompileUnit to generate C # code from C # program: http://msdn.microsoft.com/en-us/library/system.codedom.codecompileunit.aspx

You can also use this to compile the generated ATS into an assembly.

Or parse template parts from files. This will make the code more readable.

+1


source


You cannot simply express the executable binary as a C # string without dumping it. At the very least, you need to replace any occurrences of the double quote character ( '"'

) with a sequence of two double quote characters. I would be very surprised if the only problem you are facing.

Note that a string may contain control characters that cause the screen to display the string in a garbled way, but this does not necessarily lead to incorrect compilation of the code containing the string. For example, if you have a shorthand string containing backspace ( "stac{backspace}koverflow"

say), the character after backspace will overwrite the character before the backspace, so looking at the string on the screen will give an inaccurate representation of its contents ( "stakoverflow"

). The compiler will presumably see the full 14-character string, including backspace.

+1


source







All Articles