public void Copy(uint SourceAddress, uint DestinationAddress, int Size) { var Source = PspAddressToPointerSafe(SourceAddress, Size); var Destination = PspAddressToPointerSafe(DestinationAddress, Size); PointerUtils.Memcpy((byte *)Destination, (byte *)Source, Size); }
protected void ExtraV2Mangle(byte *buffer1, byte codeExtra) { var gDataTmp = new byte[20 + 0xA0]; fixed(byte *buffer2 = gDataTmp) // aligned { PointerUtils.Memcpy(buffer2 + 20, buffer1, 0xA0); var pl2 = (uint *)buffer2; pl2[0] = 5; pl2[1] = pl2[2] = 0; pl2[3] = codeExtra; pl2[4] = 0xA0; var ret = _kirk.HleUtilsBufferCopyWithRange( buffer2, 20 + 0xA0, buffer2, 20 + 0xA0, Kirk.CommandEnum.PspKirkCmdDecrypt ); if (ret != 0) { throw new Exception($"extra de-mangle returns {ret}, "); } // copy result back PointerUtils.Memcpy(buffer1, buffer2, 0xA0); } }
/// <summary> /// /// </summary> /// <param name="Event"></param> /// <param name="Mac"></param> /// <param name="Data"></param> public void NotifyEvent(Event Event, MacAddress Mac, byte[] Data) { var MacPartition = HleMemoryManager.GetPartition(MemoryPartitions.User).Allocate(8); PointerUtils.Memcpy((byte *)MacPartition.LowPointer, new ArraySegment <byte>(Mac.GetAddressBytes())); { var DataPartition = HleMemoryManager.GetPartition(MemoryPartitions.User).Allocate(Data.Length); PointerUtils.Memcpy((byte *)DataPartition.LowPointer, new ArraySegment <byte>(Data)); Console.WriteLine( "Executing callback. Matching.NotifyEvent: 0x{0:X8}, {1}, {2}, 0x{3:X8}, {4}, 0x{5:X8}", this.Callback, this.GetUidIndex(InjectContext), Event, MacPartition.Low, DataPartition.Size, DataPartition.Low); HleInterop.ExecuteFunctionLater( this.Callback, this.GetUidIndex(InjectContext), (int)Event, MacPartition.Low, DataPartition.Size, (DataPartition.Size != 0) ? DataPartition.Low : 0 ); DataPartition.DeallocateFromParent(); } MacPartition.DeallocateFromParent(); }
public void TestMemcpy4() { int TotalSize = 12; var Source = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var Dest = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; foreach (var Set64 in new[] { false, true }) { //PointerUtils.Is64 = Set64; fixed(byte *sourcePtr = Source) fixed(byte *destPtr = Dest) { for (var count = 0; count < TotalSize; count++) { for (var m = 0; m < TotalSize; m++) { Dest[m] = 0; } PointerUtils.Memcpy(destPtr, sourcePtr, count); for (var m = 0; m < TotalSize; m++) { Assert.Equal(m < count ? m : 0, Dest[m]); } } } } }
public int sceNetAdhocMatchingGetHelloOpt(Matching Matching, ref int DataLength, byte *DataPointer) { var Data = Matching.GetHelloOpt(); DataLength = Math.Min(Data.Length, DataLength); PointerUtils.Memcpy(DataPointer, Data, DataLength); return(0); }
public int sceNetInetRecv(int SocketId, void *BufferPointer, int BufferLength, SocketFlags Flags) { var Socket = Sockets.Get(SocketId); var RecvBuffer = new byte[BufferLength]; int Received = Socket.Receive(RecvBuffer, Flags); PointerUtils.Memcpy((byte *)BufferPointer, RecvBuffer, Received); return(Received); }
public int sceKernelUtilsSha1Digest(byte *Data, uint Size, byte *Digest) { PointerUtils.Memcpy( Digest, SHA1.Create().ComputeHash(PointerUtils.PointerToByteArray(Data, (int)Size)), 20 ); return(0); }
public unsafe int IoRead(HleIoDrvFileArg HleIoDrvFileArg, byte *OutputPointer, int OutputLength) { var IsoFileArgument = ((IsoFileArgument)HleIoDrvFileArg.FileArgument); var OutputData = new byte[OutputLength]; int Readed = IsoFileArgument.Stream.Read(OutputData, 0, OutputLength); PointerUtils.Memcpy(OutputPointer, OutputData, OutputLength); return(Readed); //throw new NotImplementedException(); }
private static void UnswizzleInline(void *data, int rowWidth, int textureHeight) { var temp = new byte[rowWidth * textureHeight]; fixed(void *tempPointer = temp) { Unswizzle(data, tempPointer, rowWidth, textureHeight); } PointerUtils.Memcpy((byte *)data, temp, rowWidth * textureHeight); }
public void TestMemcpyOverlapping() { var _Data = new byte[] { 1, 0, 0, 0, 0, 0 }; var Expected = new byte[] { 1, 1, 1, 1, 1, 1 }; fixed(byte *Data = _Data) { PointerUtils.Memcpy(&Data[1], &Data[0], 5); } Assert.Equal(Expected, _Data); }
public uint sceKernelMemcpy(uint destinationPointer, uint sourcePointer, int size) { try { var destination = (byte *)Memory.PspAddressToPointerSafe(destinationPointer, size); var source = (byte *)Memory.PspAddressToPointerSafe(sourcePointer, size); PointerUtils.Memcpy(destination, source, size); } catch { } return(destinationPointer); }
public uint sceKernelMemcpy(uint DestinationPointer, uint SourcePointer, int Size) { try { var Destination = (byte *)Memory.PspAddressToPointerSafe(DestinationPointer, Size); var Source = (byte *)Memory.PspAddressToPointerSafe(SourcePointer, Size); PointerUtils.Memcpy(Destination, Source, Size); } catch { } return(DestinationPointer); }
//[HlePspNotImplemented] public int sceKernelReferEventFlagStatus(HleEventFlag EventFlag, ref EventFlagInfo Info) { #if true if (Info.Size != 0) { Info = EventFlag.Info; } #else fixed(void *OutPtr = &Info) fixed(void *InPtr = &EventFlag.Info) { PointerUtils.Memcpy((byte *)OutPtr, (byte *)InPtr, Info.Size); } #endif //Console.WriteLine(Info); return(0); }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <typeparam name="TFrom"></typeparam> /// <typeparam name="TTo"></typeparam> /// <returns></returns> public static TTo[] CastToStructArray <TFrom, TTo>(this TFrom[] input) { var totalBytes = Marshal.SizeOf(typeof(TFrom)) * input.Length; var output = new TTo[totalBytes / Marshal.SizeOf(typeof(TTo))]; var inputHandle = GCHandle.Alloc(input, GCHandleType.Pinned); var outputHandle = GCHandle.Alloc(output, GCHandleType.Pinned); try { PointerUtils.Memcpy( (byte *)outputHandle.AddrOfPinnedObject().ToPointer(), (byte *)inputHandle.AddrOfPinnedObject().ToPointer(), totalBytes ); } finally { inputHandle.Free(); outputHandle.Free(); } return(output); }
static public TTo[] CastToStructArray <TFrom, TTo>(this TFrom[] Input) { int TotalBytes = (Marshal.SizeOf(typeof(TFrom)) * Input.Length); var Output = new TTo[TotalBytes / Marshal.SizeOf(typeof(TTo))]; var InputHandle = GCHandle.Alloc(Input, GCHandleType.Pinned); var OutputHandle = GCHandle.Alloc(Output, GCHandleType.Pinned); try { PointerUtils.Memcpy( (byte *)OutputHandle.AddrOfPinnedObject().ToPointer(), (byte *)InputHandle.AddrOfPinnedObject().ToPointer(), TotalBytes ); } finally { InputHandle.Free(); OutputHandle.Free(); } return(Output); }
public void TestMemcpy() { int SizeStart = 17; int SizeMiddle = 77; int SizeEnd = 17; var Dst = new byte[SizeStart + SizeMiddle + SizeEnd]; fixed(byte *DstPtr = &Dst[SizeStart]) { PointerUtils.Memcpy(DstPtr, ((byte)0x1D).Repeat(SizeMiddle).ToArray(), SizeMiddle); } var Expected = ((byte)0x00).Repeat(SizeStart).Concat(((byte)0x1D).Repeat(SizeMiddle)).Concat(((byte)0x00).Repeat(SizeEnd)).ToArray(); //Console.WriteLine(BitConverter.ToString(Dst)); //Console.WriteLine(BitConverter.ToString(Expected)); CollectionAssert.AreEqual( Expected, Dst ); }
/// <summary> /// Creates a SHA1 Hash /// /// Command: 11, 0xB /// </summary> /// <param name="outputBuffer"></param> /// <param name="inputBuffer"></param> /// <param name="inputSize"></param> /// <returns></returns> public void KirkSha1(byte *outputBuffer, byte *inputBuffer, int inputSize) { //CheckInitialized(); var header = (KirkSha1Header *)inputBuffer; if (inputSize == 0 || header->DataSize == 0) { throw(new KirkException(ResultEnum.PspKirkDataSizeIsZero)); } //Size <<= 4; //Size >>= 4; inputSize &= 0x0FFFFFFF; inputSize = (inputSize < header->DataSize) ? inputSize : header->DataSize; var sha1Hash = Sha1( PointerUtils.PointerToByteArray(inputBuffer + 4, inputSize) ); PointerUtils.Memcpy(outputBuffer, sha1Hash, sha1Hash.Length); }
private void PrepareWrite(GpuStateStruct *GpuState) { //Console.WriteLine("PrepareWrite"); try { var GlPixelFormat = GlPixelFormatList[(int)GpuState->DrawBufferState.Format]; int Width = (int)GpuState->DrawBufferState.Width; int Height = 272; int ScanWidth = PixelFormatDecoder.GetPixelsSize(GlPixelFormat.GuPixelFormat, Width); int PixelSize = PixelFormatDecoder.GetPixelsSize(GlPixelFormat.GuPixelFormat, 1); //GpuState->DrawBufferState.Format var Address = (void *)Memory.PspAddressToPointerSafe(GpuState->DrawBufferState.Address); //Console.WriteLine("{0}", GlPixelFormat.GuPixelFormat); //Console.WriteLine("{0:X}", GpuState->DrawBufferState.Address); GL.PixelStore(PixelStoreParameter.PackAlignment, PixelSize); GL.ReadPixels(0, 0, Width, Height, PixelFormat.Rgba, GlPixelFormat.OpenglPixelType, TempBuffer); fixed(void *_TempBufferPtr = &TempBuffer[0]) { var Input = (byte *)_TempBufferPtr; var Output = (byte *)Address; for (int Row = 0; Row < Height; Row++) { var ScanIn = (byte *)&Input[ScanWidth * Row]; var ScanOut = (byte *)&Output[ScanWidth * (Height - Row - 1)]; //Console.WriteLine("{0}:{1},{2},{3}", Row, PixelSize, Width, ScanWidth); PointerUtils.Memcpy(ScanOut, ScanIn, ScanWidth); } } } catch (Exception Exception) { Console.WriteLine(Exception); } }
public void TestMemcpy() { var sizeStart = 17; var sizeMiddle = 77; var sizeEnd = 17; var dst = new byte[sizeStart + sizeMiddle + sizeEnd]; fixed(byte *DstPtr = &dst[sizeStart]) { PointerUtils.Memcpy(DstPtr, ((byte)0x1D).Repeat(sizeMiddle).ToArray(), sizeMiddle); } var Expected = ((byte)0x00).Repeat(sizeStart).Concat(((byte)0x1D).Repeat(sizeMiddle)) .Concat(((byte)0x00).Repeat(sizeEnd)).ToArray(); //Console.WriteLine(BitConverter.ToString(Dst)); //Console.WriteLine(BitConverter.ToString(Expected)); Assert.Equal( Expected, dst ); }
/// <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))); } }
/// <summary> /// /// </summary> /// <param name="fuse"></param> /// <returns></returns> public int SceUtilsSetFuseId(void *fuse) { //PointerUtils.Memcpy(new ArraySegment<byte>(fuseID, 0, 16), (byte*)fuse); PointerUtils.Memcpy(fuseID, (byte *)fuse, 16); return(0); }
public void ReadBytes(uint Address, byte *DataOutPointer, int DataOutLength) => PointerUtils.Memcpy(DataOutPointer, (byte *)PspAddressToPointerSafe(Address, DataOutLength), DataOutLength);
static public int LZXdecompress(LZXstate pState, byte *inpos, byte *outpos, int inlen, int outlen) { fixed(byte *window = pState.window) { byte *endinp = inpos + inlen; byte *runsrc; byte *rundest; ushort[] hufftbl; // used in READ_HUFFSYM macro as chosen decoding table uint window_posn = pState.window_posn; uint window_size = pState.window_size; uint R0 = pState.R0; uint R1 = pState.R1; uint R2 = pState.R2; uint bitbuf; int bitsleft; uint match_offset, i = 0, j = 0, k = 0; /* ijk used in READ_HUFFSYM macro */ var lb = default(lzx_bits); /* used in READ_LENGTHS macro */ int togo = outlen, this_run, main_element, aligned_bits; int match_length, length_footer, extra, verbatim_bits = 0; int copy_length; //INIT_BITSTREAM(); bitsleft = 0; bitbuf = 0; // read header if necessary if (pState.header_read == 0) { i = j = 0; READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref k, 1); if (k != 0) { READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref i, 16); READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref j, 16); } pState.intel_filesize = (int)((i << 16) | j); // or 0 if not encoded pState.header_read = 1; } // main decoding loop while (togo > 0) { #if DEBUG_LZX_COMPRESSION Console.WriteLine("------------------------------------------------------------"); Console.WriteLine("MainLoop togo: {0}, pState.block_remaining: {1}", togo, pState.block_remaining); #endif // last block finished, new block expected if (pState.block_remaining == 0) { READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref pState.block_type, 3); READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref i, 16); READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref j, 8); pState.block_remaining = pState.block_length = (i << 8) | j; #if DEBUG_LZX_COMPRESSION Console.WriteLine("BlockSize: {0}", pState.block_length); #endif switch (pState.block_type) { case LZX_BLOCKTYPE_ALIGNED: #if DEBUG_LZX_COMPRESSION Console.WriteLine("[L]LZX_BLOCKTYPE_ALIGNED"); #endif for (i = 0; i < 8; i++) { READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref j, 3); LENTABLE(pState.ALIGNED)[i] = (byte)j; } BUILD_TABLE(pState.ALIGNED); /* rest of aligned header is same as verbatim */ goto case LZX_BLOCKTYPE_VERBATIM; case LZX_BLOCKTYPE_VERBATIM: #if DEBUG_LZX_COMPRESSION Console.WriteLine("[L]LZX_BLOCKTYPE_VERBATIM"); #endif READ_LENGTHS(pState, ref bitbuf, ref bitsleft, ref inpos, ref lb, pState.MAINTREE, 0, 256); READ_LENGTHS(pState, ref bitbuf, ref bitsleft, ref inpos, ref lb, pState.MAINTREE, 256, pState.main_elements); BUILD_TABLE(pState.MAINTREE); if (LENTABLE(pState.MAINTREE)[0xE8] != 0) { pState.intel_started = true; } READ_LENGTHS(pState, ref bitbuf, ref bitsleft, ref inpos, ref lb, pState.LENGTH, 0, LZX_NUM_SECONDARY_LENGTHS); BUILD_TABLE(pState.LENGTH); break; case LZX_BLOCKTYPE_UNCOMPRESSED: #if DEBUG_LZX_COMPRESSION Console.WriteLine("[L]LZX_BLOCKTYPE_UNCOMPRESSED"); #endif pState.intel_started = true; /* because we can't assume otherwise */ ENSURE_BITS(ref bitsleft, ref inpos, ref bitbuf, 16); // get up to 16 pad bits into the buffer if (bitsleft > 16) { inpos -= 2; /* and align the bitstream! */ } R0 = (uint)(inpos[0] | (inpos[1] << 8) | (inpos[2] << 16) | (inpos[3] << 24)); inpos += 4; R1 = (uint)(inpos[0] | (inpos[1] << 8) | (inpos[2] << 16) | (inpos[3] << 24)); inpos += 4; R2 = (uint)(inpos[0] | (inpos[1] << 8) | (inpos[2] << 16) | (inpos[3] << 24)); inpos += 4; break; default: Console.Error.WriteLine("Invalid Block Type: {0}", pState.block_type); return(DECR_ILLEGALDATA); } } if (inpos > endinp) { if (inpos > (endinp + 2) || bitsleft < 16) { Console.Error.WriteLine("Error (inpos > endinp) && (inpos > (endinp + 2) || bitsleft < 16)"); return(DECR_ILLEGALDATA); } } while ((this_run = (int)pState.block_remaining) > 0 && togo > 0) { if (this_run > togo) { this_run = togo; } togo -= this_run; pState.block_remaining -= (uint)this_run; /* apply 2^x-1 mask */ window_posn &= window_size - 1; /* runs can't straddle the window wraparound */ if ((window_posn + this_run) > window_size) { return(DECR_DATAFORMAT); } switch (pState.block_type) { case LZX_BLOCKTYPE_VERBATIM: #if DEBUG_LZX_COMPRESSION Console.WriteLine("[B]LZX_BLOCKTYPE_VERBATIM"); #endif while (this_run > 0) { READ_HUFFSYM(out hufftbl, ref bitsleft, ref inpos, ref bitbuf, ref i, ref j, pState.MAINTREE, out main_element); if (main_element < LZX_NUM_CHARS) { /* literal: 0 to LZX_NUM_CHARS-1 */ window[window_posn++] = (byte)main_element; this_run--; } else { /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */ main_element -= LZX_NUM_CHARS; match_length = main_element & LZX_NUM_PRIMARY_LENGTHS; if (match_length == LZX_NUM_PRIMARY_LENGTHS) { READ_HUFFSYM(out hufftbl, ref bitsleft, ref inpos, ref bitbuf, ref i, ref j, pState.LENGTH, out length_footer); match_length += length_footer; } match_length += LZX_MIN_MATCH; match_offset = (uint)(main_element >> 3); if (match_offset > 2) { /* not repeated offset */ if (match_offset != 3) { extra = extra_bits[match_offset]; READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref verbatim_bits, extra); match_offset = (uint)(position_base[match_offset] - 2 + verbatim_bits); } else { match_offset = 1; } /* update repeated offset LRU queue */ R2 = R1; R1 = R0; R0 = match_offset; } else if (match_offset == 0) { match_offset = R0; } else if (match_offset == 1) { match_offset = R1; R1 = R0; R0 = match_offset; } else /* match_offset == 2 */ { match_offset = R2; R2 = R0; R0 = match_offset; } rundest = window + window_posn; this_run -= match_length; /* copy any wrapped around source data */ if (window_posn >= match_offset) { /* no wrap */ runsrc = rundest - match_offset; } else { runsrc = rundest + (window_size - match_offset); copy_length = (int)(match_offset - window_posn); if (copy_length < match_length) { match_length -= copy_length; window_posn += (uint)copy_length; while (copy_length-- > 0) { *rundest++ = *runsrc++; } runsrc = window; } } window_posn += (uint)match_length; /* copy match data - no worries about destination wraps */ while (match_length-- > 0) { *rundest++ = *runsrc++; } } } break; case LZX_BLOCKTYPE_ALIGNED: #if DEBUG_LZX_COMPRESSION Console.WriteLine("[B]LZX_BLOCKTYPE_ALIGNED"); #endif while (this_run > 0) { READ_HUFFSYM(out hufftbl, ref bitsleft, ref inpos, ref bitbuf, ref i, ref j, pState.MAINTREE, out main_element); if (main_element < LZX_NUM_CHARS) { /* literal: 0 to LZX_NUM_CHARS-1 */ window[window_posn++] = (byte)main_element; this_run--; } else { /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */ main_element -= LZX_NUM_CHARS; match_length = main_element & LZX_NUM_PRIMARY_LENGTHS; if (match_length == LZX_NUM_PRIMARY_LENGTHS) { READ_HUFFSYM(out hufftbl, ref bitsleft, ref inpos, ref bitbuf, ref i, ref j, pState.LENGTH, out length_footer); match_length += length_footer; } match_length += LZX_MIN_MATCH; match_offset = (uint)(main_element >> 3); if (match_offset > 2) { /* not repeated offset */ extra = extra_bits[match_offset]; match_offset = position_base[match_offset] - 2; if (extra > 3) { /* verbatim and aligned bits */ extra -= 3; READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref verbatim_bits, extra); match_offset += (uint)(verbatim_bits << 3); READ_HUFFSYM(out hufftbl, ref bitsleft, ref inpos, ref bitbuf, ref i, ref j, pState.ALIGNED, out aligned_bits); match_offset += (uint)aligned_bits; } else if (extra == 3) { /* aligned bits only */ READ_HUFFSYM(out hufftbl, ref bitsleft, ref inpos, ref bitbuf, ref i, ref j, pState.ALIGNED, out aligned_bits); match_offset += (uint)aligned_bits; } else if (extra > 0) { /* extra==1, extra==2 */ /* verbatim bits only */ READ_BITS(ref bitsleft, ref inpos, ref bitbuf, ref verbatim_bits, extra); match_offset += (uint)verbatim_bits; } else /* extra == 0 */ { /* ??? */ match_offset = 1; } /* update repeated offset LRU queue */ R2 = R1; R1 = R0; R0 = match_offset; } else if (match_offset == 0) { match_offset = R0; } else if (match_offset == 1) { match_offset = R1; R1 = R0; R0 = match_offset; } else /* match_offset == 2 */ { match_offset = R2; R2 = R0; R0 = match_offset; } rundest = window + window_posn; this_run -= match_length; /* copy any wrapped around source data */ if (window_posn >= match_offset) { /* no wrap */ runsrc = rundest - match_offset; } else { runsrc = rundest + (window_size - match_offset); copy_length = (int)(match_offset - window_posn); if (copy_length < match_length) { match_length -= copy_length; window_posn += (uint)copy_length; while (copy_length-- > 0) { *rundest++ = *runsrc++; } runsrc = window; } } window_posn += (uint)match_length; /* copy match data - no worries about destination wraps */ while (match_length-- > 0) { *rundest++ = *runsrc++; } } } break; case LZX_BLOCKTYPE_UNCOMPRESSED: #if DEBUG_LZX_COMPRESSION Console.WriteLine("[B]LZX_BLOCKTYPE_UNCOMPRESSED"); #endif if ((inpos + this_run) > endinp) { Console.Error.WriteLine("Error: ((inpos + this_run) > endinp)"); return(DECR_ILLEGALDATA); } PointerUtils.Memcpy(window + window_posn, inpos, (int)this_run); inpos += this_run; window_posn += (uint)this_run; break; default: return(DECR_ILLEGALDATA); /* might as well */ } } } if (togo != 0) { return(DECR_ILLEGALDATA); } PointerUtils.Memcpy(outpos, window + ((window_posn == 0) ? window_size : window_posn) - outlen, (int)outlen); pState.window_posn = window_posn; pState.R0 = R0; pState.R1 = R1; pState.R2 = R2; /* intel E8 decoding */ if ((pState.frames_read++ < 32768) && pState.intel_filesize != 0) { if (outlen <= 6 || !pState.intel_started) { pState.intel_curpos += outlen; } else { byte *data = outpos; byte *dataend = data + outlen - 10; int curpos = pState.intel_curpos; int filesize = pState.intel_filesize; int abs_off, rel_off; pState.intel_curpos = curpos + outlen; while (data < dataend) { if (*data++ != 0xE8) { curpos++; continue; } abs_off = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); if ((abs_off >= -curpos) && (abs_off < filesize)) { rel_off = (abs_off >= 0) ? abs_off - curpos : abs_off + filesize; data[0] = (byte)rel_off; data[1] = (byte)(rel_off >> 8); data[2] = (byte)(rel_off >> 16); data[3] = (byte)(rel_off >> 24); } data += 4; curpos += 5; } } } return(DECR_OK); } }
//memcpy(block_buff, src, 16); static public void memcpy(byte *dst, byte *src, int count) { PointerUtils.Memcpy(dst, src, count); }
private void TransferGeneric(GpuStateStruct *gpuState) { Console.WriteLine("TransferGeneric Not Implemented"); var textureTransferState = gpuState->TextureTransferState; var sourceX = textureTransferState.SourceX; var sourceY = textureTransferState.SourceY; var destinationX = textureTransferState.DestinationX; var destinationY = textureTransferState.DestinationY; var bytesPerPixel = textureTransferState.BytesPerPixel; var sourceTotalBytes = textureTransferState.SourceLineWidth * textureTransferState.Height * bytesPerPixel; var destinationTotalBytes = textureTransferState.DestinationLineWidth * textureTransferState.Height * bytesPerPixel; var sourcePointer = (byte *)Memory.PspAddressToPointerSafe(textureTransferState.SourceAddress.Address, sourceTotalBytes); var destinationPointer = (byte *)Memory.PspAddressToPointerSafe(textureTransferState.DestinationAddress.Address, destinationTotalBytes); for (uint y = 0; y < textureTransferState.Height; y++) { var rowSourceOffset = (uint)( (textureTransferState.SourceLineWidth * (y + sourceY)) + sourceX ); var rowDestinationOffset = (uint)( (textureTransferState.DestinationLineWidth * (y + destinationY)) + destinationX ); PointerUtils.Memcpy( destinationPointer + rowDestinationOffset * bytesPerPixel, sourcePointer + rowSourceOffset * bytesPerPixel, textureTransferState.Width * bytesPerPixel ); } /* * // Generic implementation. * with (gpu.state.textureTransfer) { * auto srcAddressHost = cast(ubyte*)gpu.memory.getPointer(srcAddress); * auto dstAddressHost = cast(ubyte*)gpu.memory.getPointer(dstAddress); * * if (gpu.state.drawBuffer.isAnyAddressInBuffer([srcAddress, dstAddress])) { * gpu.performBufferOp(BufferOperation.STORE, BufferType.COLOR); * } * * for (int n = 0; n < height; n++) { * int srcOffset = ((n + srcY) * srcLineWidth + srcX) * bpp; * int dstOffset = ((n + dstY) * dstLineWidth + dstX) * bpp; * (dstAddressHost + dstOffset)[0.. width * bpp] = (srcAddressHost + srcOffset)[0.. width * bpp]; * //writefln("%08X <- %08X :: [%d]", dstOffset, srcOffset, width * bpp); * } * //std.file.write("buffer", dstAddressHost[0..512 * 272 * 4]); * * if (gpu.state.drawBuffer.isAnyAddressInBuffer([dstAddress])) { * //gpu.impl.test(); * //gpu.impl.test("trxkick"); * gpu.markBufferOp(BufferOperation.LOAD, BufferType.COLOR); * } * //gpu.impl.test(); * } */ }
public int sceDmacMemcpy(byte *destination, byte *source, int size) { PointerUtils.Memcpy(destination, source, size); return(0); }
public unsafe int IoIoctl(HleIoDrvFileArg HleIoDrvFileArg, uint Command, byte *InputPointer, int InputLength, byte *OutputPointer, int OutputLength) { var IsoFileArgument = ((IsoFileArgument)HleIoDrvFileArg.FileArgument); ActionIntPVoid ExpectedOutputSize = (int MinimumSize) => { if (OutputLength < MinimumSize || OutputPointer == null) { throw (new SceKernelException(SceKernelErrors.ERROR_INVALID_ARGUMENT)); } return(OutputPointer); }; ActionIntPVoid ExpectedInputSize = (int MinimumSize) => { if (InputLength < MinimumSize || InputPointer == null) { throw (new SceKernelException(SceKernelErrors.ERROR_INVALID_ARGUMENT)); } return(InputPointer); }; switch ((UmdCommandEnum)Command) { #if false case UmdCommandEnum.DefineDecryptionKey: { return(0); } #endif case UmdCommandEnum.FileSeekSet: { var In = (uint *)ExpectedInputSize(sizeof(uint)); IsoFileArgument.Stream.Position = *In; return(0); } case UmdCommandEnum.ReadSectors: { var In = (uint *)ExpectedInputSize(sizeof(uint)); var NumberOfSectors = *In; var CopySize = (int)(IsoFile.SectorSize * NumberOfSectors); var Out = (byte *)ExpectedOutputSize(CopySize); var BytesReaded = IsoFileArgument.Stream.ReadBytes(CopySize); PointerUtils.Memcpy(Out, BytesReaded, BytesReaded.Length); return(0); } case UmdCommandEnum.FileSeek: { var In = (FileSeekIn *)ExpectedInputSize(sizeof(FileSeekIn)); IsoFileArgument.Stream.Seek(In->Offset, (SeekOrigin)In->Whence); return(0); } case UmdCommandEnum.GetFilePointer: { var Out = (uint *)ExpectedOutputSize(sizeof(uint)); * Out = (uint)IsoFileArgument.Stream.Position; return(0); } case UmdCommandEnum.GetStartSector: { var Out = (uint *)ExpectedOutputSize(sizeof(uint)); * Out = (uint)IsoFileArgument.StartSector; return(0); } case UmdCommandEnum.GetSectorSize: { var Out = (uint *)ExpectedOutputSize(sizeof(uint)); * Out = IsoFile.SectorSize; return(0); } case UmdCommandEnum.GetLengthInBytes: { var Out = (ulong *)ExpectedOutputSize(sizeof(ulong)); * Out = (uint)IsoFileArgument.Size; return(0); } case UmdCommandEnum.GetPrimaryVolumeDescriptor: { var Out = (PrimaryVolumeDescriptor *)ExpectedOutputSize(sizeof(PrimaryVolumeDescriptor)); * Out = Iso.PrimaryVolumeDescriptor; return(0); } default: throw new NotImplementedException( $"Not implemented command 0x{Command:X} : {(UmdCommandEnum) Command}"); } }
//public delegate void WriteBytesDelegate(uint Address, byte* DataInPointer, int DataInLength); //public event WriteBytesDelegate WriteBytesHook; public void WriteBytes(uint Address, byte *DataInPointer, int DataInLength) { //if (WriteBytesHook != null) WriteBytesHook(Address, DataInPointer, DataInLength); PointerUtils.Memcpy((byte *)PspAddressToPointerSafe(Address, DataInLength), DataInPointer, DataInLength); }
public TTexture Get(GpuStateStruct GpuState) { var TextureMappingState = GpuState.TextureMappingState; var ClutState = TextureMappingState.ClutState; var TextureState = TextureMappingState.TextureState; TTexture Texture; //GC.Collect(); bool Swizzled = TextureState.Swizzled; uint TextureAddress = TextureState.Mipmap0.Address; uint ClutAddress = ClutState.Address; var ClutFormat = ClutState.PixelFormat; var ClutStart = ClutState.Start; var ClutDataStart = PixelFormatDecoder.GetPixelsSize(ClutFormat, ClutStart); ulong Hash1 = TextureAddress | (ulong)((ClutAddress + ClutDataStart) << 32); bool Recheck = false; if (Cache.TryGetValue(Hash1, out Texture)) { if (Texture.RecheckTimestamp != RecheckTimestamp) { Recheck = true; } } else { Recheck = true; } if (Recheck) { //Console.Write("."); //Console.WriteLine("{0:X}", ClutAddress); var TextureFormat = TextureState.PixelFormat; //var Width = TextureState->Mipmap0.TextureWidth; int BufferWidth = TextureState.Mipmap0.BufferWidth; // FAKE! //BufferWidth = TextureState->Mipmap0.TextureWidth; var Height = TextureState.Mipmap0.TextureHeight; var TextureDataSize = PixelFormatDecoder.GetPixelsSize(TextureFormat, BufferWidth * Height); if (ClutState.NumberOfColors > 256) { ClutState.NumberOfColors = 256; } var ClutDataSize = PixelFormatDecoder.GetPixelsSize(ClutFormat, ClutState.NumberOfColors); var ClutCount = ClutState.NumberOfColors; var ClutShift = ClutState.Shift; var ClutMask = ClutState.Mask; //Console.WriteLine(TextureFormat); // INVALID TEXTURE if (!PspMemory.IsRangeValid(TextureAddress, TextureDataSize) || TextureDataSize > 2048 * 2048 * 4) { Console.Error.WriteLineColored(ConsoleColor.DarkRed, "UPDATE_TEXTURE(TEX={0},CLUT={1}:{2}:{3}:{4}:0x{5:X},SIZE={6}x{7},{8},Swizzled={9})", TextureFormat, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask, BufferWidth, Height, BufferWidth, Swizzled); Console.Error.WriteLineColored(ConsoleColor.DarkRed, "Invalid TEXTURE! TextureAddress=0x{0:X}, TextureDataSize={1}", TextureAddress, TextureDataSize); if (InvalidTexture == null) { InvalidTexture = new TTexture(); InvalidTexture.Init(GpuImpl); int InvalidTextureWidth = 2, InvalidTextureHeight = 2; int InvalidTextureSize = InvalidTextureWidth * InvalidTextureHeight; var Data = new OutputPixel[InvalidTextureSize]; fixed(OutputPixel *DataPtr = Data) { var Color1 = OutputPixel.FromRgba(0xFF, 0x00, 0x00, 0xFF); var Color2 = OutputPixel.FromRgba(0x00, 0x00, 0xFF, 0xFF); for (int n = 0; n < InvalidTextureSize; n++) { DataPtr[n] = (n & 1) != 0 ? Color1 : Color2; } InvalidTexture.SetData(Data, InvalidTextureWidth, InvalidTextureHeight); } } return(InvalidTexture); } //Console.WriteLine("TextureAddress=0x{0:X}, TextureDataSize=0x{1:X}", TextureAddress, TextureDataSize); byte *TexturePointer = null; byte *ClutPointer = null; try { TexturePointer = (byte *)PspMemory.PspAddressToPointerSafe(TextureAddress); ClutPointer = (byte *)PspMemory.PspAddressToPointerSafe(ClutAddress); } catch (PspMemory.InvalidAddressException InvalidAddressException) { throw InvalidAddressException; } TextureCacheKey TextureCacheKey = new TextureCacheKey() { TextureAddress = TextureAddress, TextureFormat = TextureFormat, TextureHash = FastHash(TexturePointer, TextureDataSize), ClutHash = FastHash(&ClutPointer[ClutDataStart], ClutDataSize), ClutAddress = ClutAddress, ClutFormat = ClutFormat, ClutStart = ClutStart, ClutShift = ClutShift, ClutMask = ClutMask, Swizzled = Swizzled, ColorTestEnabled = GpuState.ColorTestState.Enabled, ColorTestRef = GpuState.ColorTestState.Ref, ColorTestMask = GpuState.ColorTestState.Mask, ColorTestFunction = GpuState.ColorTestState.Function, }; if (Texture == null || !Texture.TextureCacheKey.Equals(TextureCacheKey)) { string TextureName = "texture_" + TextureCacheKey.TextureHash + "_" + TextureCacheKey.ClutHash + "_" + TextureFormat + "_" + ClutFormat + "_" + BufferWidth + "x" + Height + "_" + Swizzled; #if DEBUG_TEXTURE_CACHE Console.Error.WriteLine("UPDATE_TEXTURE(TEX={0},CLUT={1}:{2}:{3}:{4}:0x{5:X},SIZE={6}x{7},{8},Swizzled={9})", TextureFormat, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask, BufferWidth, Height, BufferWidth, Swizzled); #endif Texture = new TTexture(); Texture.Init(GpuImpl); Texture.TextureCacheKey = TextureCacheKey; //Texture.Hash = Hash1; { //int TextureWidth = Math.Max(BufferWidth, Height); //int TextureHeight = Math.Max(BufferWidth, Height); int TextureWidth = BufferWidth; int TextureHeight = Height; int TextureWidthHeight = TextureWidth * TextureHeight; fixed(OutputPixel *TexturePixelsPointer = DecodedTextureBuffer) { if (Swizzled) { fixed(byte *SwizzlingBufferPointer = SwizzlingBuffer) { PointerUtils.Memcpy(SwizzlingBuffer, TexturePointer, TextureDataSize); PixelFormatDecoder.UnswizzleInline(TextureFormat, (void *)SwizzlingBufferPointer, BufferWidth, Height); PixelFormatDecoder.Decode( TextureFormat, (void *)SwizzlingBufferPointer, TexturePixelsPointer, BufferWidth, Height, ClutPointer, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask, strideWidth: PixelFormatDecoder.GetPixelsSize(TextureFormat, TextureWidth) ); } } else { PixelFormatDecoder.Decode( TextureFormat, (void *)TexturePointer, TexturePixelsPointer, BufferWidth, Height, ClutPointer, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask, strideWidth: PixelFormatDecoder.GetPixelsSize(TextureFormat, TextureWidth) ); } if (TextureCacheKey.ColorTestEnabled) { byte EqualValue, NotEqualValue; switch (TextureCacheKey.ColorTestFunction) { case ColorTestFunctionEnum.GuAlways: EqualValue = 0xFF; NotEqualValue = 0xFF; break; case ColorTestFunctionEnum.GuNever: EqualValue = 0x00; NotEqualValue = 0x00; break; case ColorTestFunctionEnum.GuEqual: EqualValue = 0xFF; NotEqualValue = 0x00; break; case ColorTestFunctionEnum.GuNotequal: EqualValue = 0x00; NotEqualValue = 0xFF; break; default: throw new NotImplementedException(); } ConsoleUtils.SaveRestoreConsoleState(() => { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Yellow; Console.Error.WriteLine("{0} : {1}, {2} : ref:{3} : mask:{4}", TextureCacheKey.ColorTestFunction, EqualValue, NotEqualValue, TextureCacheKey.ColorTestRef, TextureCacheKey.ColorTestMask); }); for (int n = 0; n < TextureWidthHeight; n++) { if ((TexturePixelsPointer[n] & TextureCacheKey.ColorTestMask).Equals( TextureCacheKey.ColorTestRef & TextureCacheKey.ColorTestMask)) { TexturePixelsPointer[n].A = EqualValue; } else { TexturePixelsPointer[n].A = NotEqualValue; } if (TexturePixelsPointer[n].A == 0) { //Console.Write("yup!"); } } } var TextureInfo = new TextureHookInfo() { TextureCacheKey = TextureCacheKey, Data = DecodedTextureBuffer, Width = TextureWidth, Height = TextureHeight }; MessageBus.Dispatch(TextureInfo); var Result = Texture.SetData(TextureInfo.Data, TextureInfo.Width, TextureInfo.Height); } } if (Cache.ContainsKey(Hash1)) { Cache[Hash1].Dispose(); } Cache[Hash1] = Texture; } } Texture.RecheckTimestamp = RecheckTimestamp; return(Texture); }
public void WriteBytes(uint Address, byte *DataInPointer, int DataInLength) { PointerUtils.Memcpy((byte *)PspAddressToPointer(Address), DataInPointer, DataInLength); }