public void TestSprintf()
        {
            var expected = "Hello 0x00000001, 'World     '!";
            var actual   = CStringFormater.Sprintf("Hello 0x%08X, '%-10s'!", 1, "World");

            Assert.Equal(expected, actual);
        }
Exemple #2
0
        //[HlePspNotImplemented]
        public void Kprintf(string Format, CpuThreadState CpuThreadState)
        {
            var Arguments = new ArgumentReader(CpuThreadState);

            Arguments.LoadInteger(); // Skips format

            ConsoleUtils.SaveRestoreConsoleColor(ConsoleColor.Blue,
                                                 () => { Console.Error.Write("{0}", CStringFormater.Sprintf(Format, Arguments)); });
        }
        public void ImportModules()
        {
            foreach (var ModuleImports in ModulesImports)
            {
                HleModuleHost HleModuleHost = null;
                try
                {
                    HleModuleHost = ModuleManager.GetModuleByName(ModuleImports.Name);
                }
                catch (Exception Exception)
                {
                    //Logger.Error(Exception);
                    Console.Error.WriteLine(Exception);
                }

                // Can't use a host module. Try to use a Guest module.
                if (HleModuleHost == null)
                {
                    var HleModuleGuest =
                        ModuleManager.LoadedGuestModules.FirstOrDefault(ModuleExports =>
                                                                        ModuleExports.Name == ModuleImports.Name);
                    if (HleModuleGuest != null)
                    {
                        HleModuleGuest.ExportModules(this);
                        continue;
                    }
                }

                Logger.Info("'{0}' - {1}", ModuleImports.Name,
                            HleModuleHost != null ? HleModuleHost.ModuleLocation : "?");
                foreach (var Function in ModuleImports.Functions)
                {
                    var NID         = Function.Key;
                    var CallAddress = Function.Value.Address;

                    var DefaultEntry = new HleFunctionEntry()
                    {
                        NID         = 0x00000000,
                        Name        = CStringFormater.Sprintf("__<unknown:0x%08X>", (int)NID),
                        Description = "Unknown",
                        Module      = null,
                        ModuleName  = ModuleImports.Name,
                    };
                    var FunctionEntry = HleModuleHost?.EntriesByNID.GetOrDefault(NID, DefaultEntry) ?? DefaultEntry;
                    FunctionEntry.NID = NID;
                    //var Delegate = Module.DelegatesByNID.GetOrDefault(NID, null);

                    LinkFunction(CallAddress, FunctionEntry);
                }
            }
        }
Exemple #4
0
 public override string ToString()
 {
     return(CStringFormater.Sprintf("TAG_INFO2(tag=0x%08X, key=%s, code=%02X)", Tag,
                                    (Key != null) ? BitConverter.ToString(Key) : "null", Code));
 }
Exemple #5
0
        protected byte[] DecryptPrx1(byte[] pbInBytes, bool showInfo = false)
        {
            var cbTotal    = pbInBytes.Length;
            var pbOutBytes = new byte[cbTotal];

            pbInBytes.CopyTo(pbOutBytes, 0);

            fixed(byte *pbIn = pbInBytes)
            fixed(byte *pbOut = pbOutBytes)
            {
                //var headerPointer = (HeaderStruct*) pbIn;
                Header = *(HeaderStruct *)pbIn;
                var pti = GetTagInfo(Header.Tag);

                if (showInfo)
                {
                    Console.WriteLine("TAG_INFO: {0}", pti);
                }

                // build conversion into pbOut
                PointerUtils.Memcpy(pbOut, pbIn, pbInBytes.Length);
                PointerUtils.Memset(pbOut, 0, 0x150);
                PointerUtils.Memset(pbOut, 0x55, 0x40);

                // step3 demangle in place
                var h7Header = (Kirk.KirkAes128CbcHeader *) & pbOut[0x2C];

                h7Header->Mode     = Kirk.KirkMode.DecryptCbc;
                h7Header->Unknown4 = 0;
                h7Header->Unknown8 = 0;
                h7Header->KeySeed  = pti.Code; // initial seed for PRX
                h7Header->Datasize = 0x70;     // size

                // redo part of the SIG check (step2)
                var buffer1Bytes = new byte[0x150];

                fixed(byte *buffer1 = buffer1Bytes)
                {
                    PointerUtils.Memcpy(buffer1 + 0x00, pbIn + 0xD0, 0x80);
                    PointerUtils.Memcpy(buffer1 + 0x80, pbIn + 0x80, 0x50);
                    PointerUtils.Memcpy(buffer1 + 0xD0, pbIn + 0x00, 0x80);

                    if (pti.CodeExtra != 0)
                    {
                        ExtraV2Mangle(buffer1 + 0x10, pti.CodeExtra);
                    }

                    PointerUtils.Memcpy(pbOut + 0x40 /* 0x2C+20 */, buffer1 + 0x40, 0x40);
                }

                for (var iXor = 0; iXor < 0x70; iXor++)
                {
                    pbOut[0x40 + iXor] = (byte)(pbOut[0x40 + iXor] ^ pti.Key[0x14 + iXor]);
                }

                var ret = _kirk.HleUtilsBufferCopyWithRange(
                    pbOut + 0x2C,
                    20 + 0x70,
                    pbOut + 0x2C,
                    20 + 0x70,
                    Kirk.CommandEnum.PspKirkCmdDecrypt
                    );

                if (ret != 0)
                {
                    throw new Exception(CStringFormater.Sprintf("mangle#7 returned 0x%08X, ", ret));
                }

                for (var iXor = 0x6F; iXor >= 0; iXor--)
                {
                    pbOut[0x40 + iXor] = (byte)(pbOut[0x2C + iXor] ^ pti.Key[0x20 + iXor]);
                }

                PointerUtils.Memset(pbOut + 0x80, 0, 0x30); // $40 bytes kept, clean up

                pbOut[0xA0] = 1;
                // copy unscrambled parts from header
                PointerUtils.Memcpy(pbOut + 0xB0, pbIn + 0xB0, 0x20); // file size + lots of zeros
                PointerUtils.Memcpy(pbOut + 0xD0, pbIn + 0x00, 0x80); // ~PSP header

                // step4: do the actual decryption of code block
                //  point 0x40 bytes into the buffer to key info
                ret = _kirk.HleUtilsBufferCopyWithRange(
                    pbOut,
                    cbTotal,
                    pbOut + 0x40,
                    cbTotal - 0x40,
                    Kirk.CommandEnum.PspKirkCmdDecryptPrivate
                    );

                if (ret != 0)
                {
                    throw new Exception(CStringFormater.Sprintf("mangle#1 returned 0x%08X", ret));
                }

                //File.WriteAllBytes("../../../TestInput/temp.bin", _pbOut);

                var outputSize = *(int *)&pbIn[0xB0];

                return(pbOutBytes.Slice(0, outputSize).ToArray());
            }
        }