/// <summary> /// </summary> /// <param name="stream"></param> /// <param name="Byte"></param> /// <param name="totalCount"></param> /// <param name="progress"></param> /// <returns></returns> public static Stream WriteByteRepeated(this Stream stream, byte Byte, long totalCount, Action <long, long> progress = null) { if (totalCount > 0) { long maxBuffer = 2 * 1024 * 1024; var bytes = new byte[Math.Min(maxBuffer, totalCount)]; PointerUtils.Memset(bytes, Byte, bytes.Length); var left = totalCount; long current = 0; if (progress == null) { progress = (cur, max) => { } } ; progress(0, totalCount); while (left > 0) { var toWrite = Math.Min(bytes.Length, left); stream.Write(bytes, 0, (int)toWrite); left -= toWrite; current += toWrite; progress(current, totalCount); } } return(stream); }
public void TestMemset4() { int TotalSize = 65; var Dest = new byte[TotalSize]; foreach (var Set64 in new[] { false, true }) { //PointerUtils.Is64 = Set64; fixed(byte *DestPtr = Dest) { for (int count = 0; count < TotalSize; count++) { for (int m = 0; m < TotalSize; m++) { Dest[m] = 0; } PointerUtils.Memset(DestPtr, 1, count); for (int m = 0; m < TotalSize; m++) { Assert.Equal(m < count ? 1 : 0, Dest[m]); } } } } }
public void TestMemset() { var Data = new byte[131]; PointerUtils.Memset(Data, 0x3E, Data.Length); Assert.Equal( ((byte)0x3E).Repeat(Data.Length), Data ); }
public uint sceKernelMemset(uint PspPointer, int Data, int Size) { try { PointerUtils.Memset((byte *)Memory.PspAddressToPointerSafe(PspPointer, Size), (byte)Data, Size); } catch { } return(PspPointer); }
unsafe public static Stream WriteByteRepeated(this Stream Stream, byte Byte, int Count = 1) { if (Count > 0) { var Bytes = new byte[Count]; fixed(byte *BytesPtr = &Bytes[0]) { PointerUtils.Memset(BytesPtr, Byte, Count); } Stream.WriteBytes(Bytes); } return(Stream); }
private void BufferFillEventHandler(IntPtr data, int size) { if (Queue.Count > 0) { short[] Result; while (!Queue.TryDequeue(out Result)) { if (_mPlayer.Disposing) { return; } } Marshal.Copy(Result, 0, data, size / 2); } else { PointerUtils.Memset((byte *)data.ToPointer(), 0, size); } }
/// <summary> /// Block dithering function. Simply dithers a block to 565 RGB. /// (Floyd-Steinberg) /// </summary> /// <param name="dest"></param> /// <param name="block"></param> static void stb__DitherBlock(ARGB_Rev *dest, ARGB_Rev *block) { var err = stackalloc int[8]; int *ep1 = err; int *ep2 = err + 4; int *et; int ch, y; fixed(byte *_stb__QuantGTab = stb__QuantGTab) fixed(byte *_stb__QuantRBTab = stb__QuantRBTab) { var bpList = new byte *[] {&block->R, &block->G, &block->B }; var dpList = new byte *[] {&dest->R, &dest->G, &dest->B }; // process channels seperately for (ch = 0; ch < 3; ++ch) { byte *bp = bpList[ch]; byte *dp = dpList[ch]; byte *quant = (ch == 1) ? _stb__QuantGTab + 8 : _stb__QuantRBTab + 8; PointerUtils.Memset((byte *)err, 0, sizeof(int) * 8); for (y = 0; y < 4; ++y) { dp[0] = quant[bp[0] + ((3 * ep2[1] + 5 * ep2[0]) >> 4)]; ep1[0] = bp[0] - dp[0]; dp[4] = quant[bp[4] + ((7 * ep1[0] + 3 * ep2[2] + 5 * ep2[1] + ep2[0]) >> 4)]; ep1[1] = bp[4] - dp[4]; dp[8] = quant[bp[8] + ((7 * ep1[1] + 3 * ep2[3] + 5 * ep2[2] + ep2[1]) >> 4)]; ep1[2] = bp[8] - dp[8]; dp[12] = quant[bp[12] + ((7 * ep1[2] + 5 * ep2[3] + ep2[2]) >> 4)]; ep1[3] = bp[12] - dp[12]; bp += 16; dp += 16; et = ep1; ep1 = ep2; ep2 = et; // swap } } } }
/// <summary> /// /// </summary> /// <param name="Stream"></param> /// <param name="Byte"></param> /// <param name="TotalCount"></param> /// <returns></returns> unsafe public static Stream WriteByteRepeated(this Stream Stream, byte Byte, long TotalCount, Action <long, long> Progress = null) { if (TotalCount > 0) { long MaxBuffer = 2 * 1024 * 1024; var Bytes = new byte[Math.Min(MaxBuffer, TotalCount)]; PointerUtils.Memset(Bytes, Byte, Bytes.Length); long Left = TotalCount; long Current = 0; if (Progress == null) { Progress = (_Current, _Max) => { } } ; Progress(0, TotalCount); while (Left > 0) { var ToWrite = Math.Min(Bytes.Length, Left); Stream.Write(Bytes, 0, (int)ToWrite); Left -= ToWrite; Current += ToWrite; Progress(Current, TotalCount); } } return(Stream); } /// <summary> /// /// </summary> /// <param name="Stream"></param> /// <param name="Byte"></param> /// <param name="PositionStop"></param> /// <returns></returns> unsafe public static Stream WriteByteRepeatedTo(this Stream Stream, byte Byte, long PositionStop, Action <long, long> Progress = null)
public void ZeroFillSegment(Segment Segment) { //Range<byte>(Segment.Low, (int)Segment.Size).Fill(0); PointerUtils.Memset((byte *)PspAddressToPointerSafe(Segment.Low), 0, (int)Segment.Size); }
public void ZeroFillSegment(Segment Segment) { PointerUtils.Memset((byte *)PspAddressToPointerSafe(Segment.Low), 0, (int)Segment.Size); }
public void WriteRepeated1(byte Value, uint Address, int Count) { PointerUtils.Memset((byte *)PspAddressToPointerSafe(Address, Count), Value, Count); }
/// <summary> /// /// </summary> /// <param name="Input"></param> /// <param name="OutputMaxSize"></param> /// <param name="Version"></param> /// <returns></returns> public static byte[] Decode13(byte[] Input, int OutputMaxSize, int Version) { if ((Version != 1) && (Version != 3)) { throw(new InvalidOperationException("Can't handle version '" + Version + "'")); } var Output = new byte[OutputMaxSize + 0x1000]; bool HasRle = (Version == 3); const int WindowSize = 0x1000; var MinLength = 3; int MaxLength = HasRle ? 0x11 : 0x12; var OffsetStart = WindowSize - MaxLength; WritePrependData(Output, OffsetStart); #if DEBUG_COMPRESSION Console.WriteLine("Decoding at 0x{0:X8}", OffsetStart); #endif //File.WriteAllBytes("c:/temp/lol.bin", Input); #if false Console.WriteLine(""); Console.WriteLine("START: 0x{0:X3}", OffsetStart); #endif fixed(byte *InStart = &Input[0]) fixed(byte *OutStart = &Output[OffsetStart]) { byte *InCurrent = InStart, InEnd = InStart + Input.Length; byte *OutCurrent = OutStart, OutEnd = OutStart + OutputMaxSize; uint Data = 0x001; while (InCurrent < InEnd) { if (Data == 0x001) { Data = (uint)(*InCurrent++ | 0x100); #if DEBUG_COMPRESSION Console.WriteLine("ControlByte: 0x{0:X2}", (byte)Data); #endif } bool Uncompressed = ((Data & 1) != 0); Data >>= 1; // UNCOMPRESSED if (Uncompressed) { #if DEBUG_COMPRESSION Console.WriteLine("{0:X8}: BYTE(0x{1:X2})", (OutCurrent - OutStart) + OffsetStart, *InCurrent); #endif *OutCurrent++ = *InCurrent++; } // COMPRESSED else { int Byte1 = *InCurrent++; int Byte2 = *InCurrent++; var WindowOffset = Byte1 | ((Byte2 & 0xF0) << 4); var Length = (Byte2 & 0x0F) + MinLength; // RLE if (HasRle && Length > MaxLength) { int RleLength; byte RleByte; /* * if ((WindowOffset >> 8) == 0) * { * RleByte = (byte)(*InCurrent++); * RleLength = (WindowOffset & 0xFF) + MaxLength + 1 + 1; * } * else * { * RleByte = (byte)(WindowOffset & 0xFF); * RleLength = (WindowOffset >> 8) + MinLength; * } */ //int Type; if (WindowOffset < 0x100) { //Type = 1; RleByte = *InCurrent++; RleLength = WindowOffset + MaxLength + 1 + 1; } else { //Type = 0; RleByte = (byte)WindowOffset; RleLength = (WindowOffset >> 8) + MinLength; } #if DEBUG_COMPRESSION Console.WriteLine("{0:X8}: RLE(Byte: 0x{1:X2}, Length: {2}, Type: {3})", (OutCurrent - OutStart) + OffsetStart, RleByte, RleLength, Type); #endif PointerUtils.Memset(OutCurrent, RleByte, RleLength); OutCurrent += RleLength; } // LZ else { //int CurrentWindowPos = (OffsetStart + (int)(OutCurrent - OutStart)) % WindowSize; //int MinusDisp = (CurrentWindowPos - WindowOffset + WindowSize) % WindowSize; int MinusDisp = ((OffsetStart + (int)(OutCurrent - OutStart)) - WindowOffset + WindowSize) % WindowSize; if (MinusDisp == 0) { MinusDisp = WindowSize; } #if false Console.WriteLine( "LZ(0x{0:X3}, {1}) : CUR:0x{2:X3} : MIN:{3}", WindowOffset, Length, CurrentWindowPos, -MinusDisp ); #endif //SourcePointer = (OutCurrent - WindowSize) + (WindowOffset + Offset) % WindowSize; PointerUtils.Memcpy(OutCurrent, (OutCurrent - MinusDisp), Length); #if DEBUG_COMPRESSION Console.WriteLine("{0:X8}: LZ(Offset: {1}, Length: {2})", (OutCurrent - OutStart) + OffsetStart, -MinusDisp, Length); for (int n = 0; n < Length; n++) { Console.WriteLine(" {0:X8}: 0x{1:X2}", (OutCurrent - OutStart) + OffsetStart - MinusDisp + n, OutCurrent[-MinusDisp + n]); } #endif OutCurrent += Length; } } } return(PointerUtils.PointerToByteArray(OutStart, (int)(OutCurrent - OutStart))); } }
private static void *_AllocRange(void *Address, uint Size, bool Guard) { switch (OS) { case OS.Windows: { byte *Pointer; if (Guard) { Pointer = InternalWindows.VirtualAlloc(Address, Size, MEM_RESERVE, PAGE_GUARD); } else { Pointer = InternalWindows.VirtualAlloc(Address, Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); } if (Pointer != null) { if ((void *)Pointer != (void *)Address) { throw (new Exception( $"Not allocated the desired address! Expected {new IntPtr(Address):X}, Allocated: {new IntPtr(Pointer):X}")); } PointerUtils.Memset(Pointer, 0, (int)Size); } return(Pointer); } default: { int errno; InternalUnix.reset_errno(); void *AllocatedAddress = InternalUnix.mmap( Address, Size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0 ); errno = InternalUnix.errno(); if (errno != 0) { Console.Error.WriteLine("mmap errno: {0} : {1}", errno, InternalUnix.strerror(errno)); } if (AllocatedAddress == (void *)-1) { AllocatedAddress = null; } if (AllocatedAddress != Address) { Console.WriteLine("Alloc pointer mismatch! {0}, {1}", new IntPtr(AllocatedAddress), new IntPtr(Address)); //Console.ReadKey(); } #if false InternalUnix.reset_errno(); int result3 = InternalUnix.mprotect(Address, Size, PROT_READ | PROT_WRITE); errno = InternalUnix.errno(); if (errno != 0) { Console.Error.WriteLine("mprotect errno: {0} : {1}", errno, InternalUnix.strerror(errno)); } if (result3 != 0) { Console.Error.WriteLine("mprotect result3: {0}", result3); } #endif return(AllocatedAddress); } } throw (new NotImplementedException()); }
public int sceNetEtherStrton(string String, byte *MacAddress) { PointerUtils.Memset(MacAddress, 0, 6); return(0); //throw (new NotImplementedException()); }
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()); } }
protected byte[] DecryptPrx2(byte[] pbIn, bool showInfo = false) { var size = pbIn.Length; var pbOut = new byte[size]; pbIn.CopyTo(pbOut, 0); var tmp1Bytes = new byte[0x150]; var tmp2Bytes = new byte[0x90 + 0x14]; var tmp3Bytes = new byte[0x60 + 0x14]; fixed(byte *inbuf = pbIn) fixed(byte *outbuf = pbOut) fixed(byte *tmp1 = tmp1Bytes) fixed(byte *tmp2 = tmp2Bytes) fixed(byte *tmp3 = tmp3Bytes) { //var headerPointer = (HeaderStruct*) inbuf; Header = *(HeaderStruct *)inbuf; var pti = GetTagInfo2(Header.Tag); Console.WriteLine("{0}", pti); var retsize = *(int *)&inbuf[0xB0]; PointerUtils.Memset(tmp1Bytes, 0, 0x150); PointerUtils.Memset(tmp2Bytes, 0, 0x90 + 0x14); PointerUtils.Memset(tmp3Bytes, 0, 0x60 + 0x14); PointerUtils.Memcpy(outbuf, inbuf, size); if (size < 0x160) { throw new InvalidDataException("buffer not big enough, "); } if (size - 0x150 < retsize) { throw new InvalidDataException("not enough data, "); } PointerUtils.Memcpy(tmp1, outbuf, 0x150); int i, j; //byte *p = tmp2+0x14; for (i = 0; i < 9; i++) { for (j = 0; j < 0x10; j++) { tmp2Bytes[0x14 + (i << 4) + j] = pti.Key[j]; } tmp2Bytes[0x14 + (i << 4)] = (byte)i; } if (Scramble((uint *)tmp2, 0x90, pti.Code) < 0) { throw new InvalidDataException("error in Scramble#1, "); } PointerUtils.Memcpy(outbuf, tmp1 + 0xD0, 0x5C); PointerUtils.Memcpy(outbuf + 0x5C, tmp1 + 0x140, 0x10); PointerUtils.Memcpy(outbuf + 0x6C, tmp1 + 0x12C, 0x14); PointerUtils.Memcpy(outbuf + 0x80, tmp1 + 0x080, 0x30); PointerUtils.Memcpy(outbuf + 0xB0, tmp1 + 0x0C0, 0x10); PointerUtils.Memcpy(outbuf + 0xC0, tmp1 + 0x0B0, 0x10); PointerUtils.Memcpy(outbuf + 0xD0, tmp1 + 0x000, 0x80); PointerUtils.Memcpy(tmp3 + 0x14, outbuf + 0x5C, 0x60); if (Scramble((uint *)tmp3, 0x60, pti.Code) < 0) { throw new InvalidDataException("error in Scramble#2, "); } PointerUtils.Memcpy(outbuf + 0x5C, tmp3, 0x60); PointerUtils.Memcpy(tmp3, outbuf + 0x6C, 0x14); PointerUtils.Memcpy(outbuf + 0x70, outbuf + 0x5C, 0x10); PointerUtils.Memset(outbuf + 0x18, 0, 0x58); PointerUtils.Memcpy(outbuf + 0x04, outbuf, 0x04); *((uint *)outbuf) = 0x014C; PointerUtils.Memcpy(outbuf + 0x08, tmp2, 0x10); /* sha-1 */ if (_kirk.HleUtilsBufferCopyWithRange(outbuf, 3000000, outbuf, 3000000, Kirk.CommandEnum.PspKirkCmdSha1Hash) != Kirk.ResultEnum.Ok) { throw new InvalidDataException("error in sceUtilsBufferCopyWithRange 0xB, "); } if (PointerUtils.Memcmp(outbuf, tmp3, 0x14) != 0) { throw new InvalidDataException("WARNING (SHA-1 incorrect), "); } int iXor; for (iXor = 0; iXor < 0x40; iXor++) { tmp3[iXor + 0x14] = (byte)(outbuf[iXor + 0x80] ^ tmp2Bytes[iXor + 0x10]); } if (Scramble((uint *)tmp3, 0x40, pti.Code) != 0) { throw new InvalidDataException("error in Scramble#3, "); } for (iXor = 0x3F; iXor >= 0; iXor--) { outbuf[iXor + 0x40] = (byte)(tmp3Bytes[iXor] ^ tmp2Bytes[iXor + 0x50]); // uns 8 } PointerUtils.Memset(outbuf + 0x80, 0, 0x30); *(uint *)&outbuf[0xA0] = 1; PointerUtils.Memcpy(outbuf + 0xB0, outbuf + 0xC0, 0x10); PointerUtils.Memset(outbuf + 0xC0, 0, 0x10); // the real decryption var ret = _kirk.HleUtilsBufferCopyWithRange(outbuf, size, outbuf + 0x40, size - 0x40, Kirk.CommandEnum.PspKirkCmdDecryptPrivate); if (ret != 0) { throw new InvalidDataException( $"error in sceUtilsBufferCopyWithRange 0x1 (0x{ret:X}), "); } if (retsize < 0x150) { // Fill with 0 PointerUtils.Memset(outbuf + retsize, 0, 0x150 - retsize); } return(pbOut.Slice(0, retsize).ToArray()); } }