Пример #1
0
        private void roundUpFixedOutput(OutBuffer @out)
        {
            int i;

            for (i = @out.kn - 1; i >= @out.k0; i--)
            {
                if (@out.buf[i] == '9')
                {
                    @out.buf[i] = '0';

                    // carry by continuing
                }
                else if (@out.buf[i] != '.')
                {
                    @out.buf[i]++; // += 1;

                    break;
                }
            }

            if (i < @out.k0)
            {
                @out.buf[[email protected]] = '1';
            }
        }
Пример #2
0
        public static Result GetAndClearMmcErrorInfo(this StorageService service, out StorageErrorInfo errorInfo,
                                                     out long logSize, Span <byte> logBuffer)
        {
            UnsafeHelpers.SkipParamInit(out errorInfo, out logSize);

            ReferenceCountedDisposable <IStorageDeviceOperator> mmcOperator = null;

            try
            {
                Result rc = service.GetMmcManagerOperator(out mmcOperator);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                int       operationId        = MakeOperationId(MmcManagerOperationIdValue.GetAndClearErrorInfo);
                var       logOutBuffer       = new OutBuffer(logBuffer);
                OutBuffer errorInfoOutBuffer = OutBuffer.FromStruct(ref errorInfo);

                return(mmcOperator.Target.OperateOut2(out _, errorInfoOutBuffer, out logSize, logOutBuffer,
                                                      operationId));
            }
            finally
            {
                mmcOperator?.Dispose();
            }
        }
Пример #3
0
        public static Result GetAndClearPatrolReadAllocateBufferCount(this StorageService service,
                                                                      out long successCount, out long failureCount)
        {
            UnsafeHelpers.SkipParamInit(out successCount, out failureCount);

            ReferenceCountedDisposable <IStorageDeviceOperator> mmcOperator = null;

            try
            {
                Result rc = service.GetMmcManagerOperator(out mmcOperator);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                int       operationId        = MakeOperationId(MmcManagerOperationIdValue.GetAndClearPatrolReadAllocateBufferCount);
                OutBuffer successCountBuffer = OutBuffer.FromStruct(ref successCount);
                OutBuffer failureCountBuffer = OutBuffer.FromStruct(ref failureCount);

                return(mmcOperator.Target.OperateOut2(out _, successCountBuffer, out _, failureCountBuffer,
                                                      operationId));
            }
            finally
            {
                mmcOperator?.Dispose();
            }
        }
        public Result Read(out long entriesRead, OutBuffer entryBuffer)
        {
            const int maxTryCount = 2;

            UnsafeHelpers.SkipParamInit(out entriesRead);

            Span <DirectoryEntry> entries = MemoryMarshal.Cast <byte, DirectoryEntry>(entryBuffer.Buffer);

            Result rc             = Result.Success;
            long   tmpEntriesRead = 0;

            for (int tryNum = 0; tryNum < maxTryCount; tryNum++)
            {
                rc = BaseDirectory.Read(out tmpEntriesRead, entries);

                // Retry on ResultDataCorrupted
                if (!ResultFs.DataCorrupted.Includes(rc))
                {
                    break;
                }
            }

            if (rc.IsFailure())
            {
                return(rc);
            }

            entriesRead = tmpEntriesRead;
            return(Result.Success);
        }
Пример #5
0
        public Result Read(long offset, OutBuffer destination, long size)
        {
            const int maxTryCount = 2;

            if (offset < 0)
            {
                return(ResultFs.InvalidOffset.Log());
            }

            if (destination.Size < 0)
            {
                return(ResultFs.InvalidSize.Log());
            }

            Result rc = Result.Success;

            for (int tryNum = 0; tryNum < maxTryCount; tryNum++)
            {
                rc = BaseStorage.Target.Read(offset, destination.Buffer.Slice(0, (int)size));

                // Retry on ResultDataCorrupted
                if (!ResultFs.DataCorrupted.Includes(rc))
                {
                    break;
                }
            }

            return(rc);
        }
Пример #6
0
        protected void Save()
        {
            string extension;

            extension = MediaType.MimeToExt(MusicMime);
            if (extension == null)
            {
                throw new FileLoadException($"\"{InPath}\" is invalid.");
            }

            string path;

            if (OutName == null)
            {
                OutName = Path.GetFileNameWithoutExtension(InPath);
            }
            path = ((OutputDir == null) ? Path.Combine(Path.GetDirectoryName(InPath), OutName) : Path.Combine(OutputDir, OutName)) + $".{extension}";

            if (File.Exists(path) && SkipDuplicate)
            {
                Logger.Info("Skipping {Path}", path);
                return;
            }

            using FileStream file = new FileStream(path, FileMode.Create);
            OutBuffer.WriteTo(file);
            SuccessCount++;
            Logger.Info("File was decrypted successfully at {Path}", path);
        }
Пример #7
0
 public Repo()
 {
     numProg         = 1;
     ob              = new OutBuffer();
     hp              = new Heap();
     pslist[numProg] = new ProgState(new ExeStack(), ob, new VarTable(), cid, hp);
     ++cid;
 }
Пример #8
0
 public void removePs(int i)
 {
     ob = (OutBuffer)pslist[1].ob;
     hp = (Heap)pslist [1].hp;
     for (; i < numProg; ++i)
     {
         pslist[i] = pslist[i + 1];
     }
     --numProg;
 }
Пример #9
0
        public static Result GetMmcSpeedMode(this StorageService service, out MmcSpeedMode speedMode)
        {
            UnsafeHelpers.SkipParamInit(out speedMode);

            ReferenceCountedDisposable <IStorageDeviceOperator> mmcOperator = null;

            try
            {
                Result rc = service.GetMmcOperator(out mmcOperator, MmcPartition.UserData);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                Unsafe.SkipInit(out SpeedMode sdmmcSpeedMode);
                OutBuffer outBuffer   = OutBuffer.FromStruct(ref sdmmcSpeedMode);
                int       operationId = MakeOperationId(MmcOperationIdValue.GetSpeedMode);

                rc = mmcOperator.Target.OperateOut(out _, outBuffer, operationId);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                speedMode = sdmmcSpeedMode switch
                {
                    SpeedMode.MmcIdentification => MmcSpeedMode.Identification,
                    SpeedMode.MmcLegacySpeed => MmcSpeedMode.LegacySpeed,
                    SpeedMode.MmcHighSpeed => MmcSpeedMode.HighSpeed,
                    SpeedMode.MmcHs200 => MmcSpeedMode.Hs200,
                    SpeedMode.MmcHs400 => MmcSpeedMode.Hs400,
                    _ => MmcSpeedMode.Unknown
                };

                return(Result.Success);
            }
            finally
            {
                mmcOperator?.Dispose();
            }
        }
Пример #10
0
        protected override void Decrypt()
        {
            for (int chunkSize = 0x8000; ;)
            {
                byte[] chunk = ReadFixedChunk(ref chunkSize);

                for (int i = 0; i < chunkSize; i++)
                {
                    chunk[i] ^= NextMask();
                }

                if (chunkSize < 0x8000)
                {
                    OutBuffer.Write(chunk.Take(chunkSize).ToArray());
                    break;
                }
                else
                {
                    OutBuffer.Write(chunk);
                }
            }
        }
Пример #11
0
        public static Result GetMmcExtendedCsd(this StorageService service, Span <byte> buffer)
        {
            ReferenceCountedDisposable <IStorageDeviceOperator> mmcOperator = null;

            try
            {
                Result rc = service.GetMmcOperator(out mmcOperator, MmcPartition.UserData);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                int operationId = MakeOperationId(MmcOperationIdValue.GetExtendedCsd);
                var outBuffer   = new OutBuffer(buffer);

                return(mmcOperator.Target.OperateOut(out _, outBuffer, operationId));
            }
            finally
            {
                mmcOperator?.Dispose();
            }
        }
Пример #12
0
        public Result Read(out long bytesRead, long offset, OutBuffer destination, long size, ReadOption option)
        {
            const int maxTryCount = 2;

            UnsafeHelpers.SkipParamInit(out bytesRead);

            if (offset < 0)
            {
                return(ResultFs.InvalidOffset.Log());
            }

            if (destination.Size < 0)
            {
                return(ResultFs.InvalidSize.Log());
            }

            Result rc           = Result.Success;
            long   tmpBytesRead = 0;

            for (int tryNum = 0; tryNum < maxTryCount; tryNum++)
            {
                rc = BaseFile.Read(out tmpBytesRead, offset, destination.Buffer.Slice(0, (int)size), option);

                // Retry on ResultDataCorrupted
                if (!ResultFs.DataCorrupted.Includes(rc))
                {
                    break;
                }
            }

            if (rc.IsFailure())
            {
                return(rc);
            }

            bytesRead = tmpBytesRead;
            return(Result.Success);
        }
Пример #13
0
        public static Result GetMmcPatrolCount(this StorageService service, out uint count)
        {
            UnsafeHelpers.SkipParamInit(out count);

            ReferenceCountedDisposable <IStorageDeviceOperator> mmcOperator = null;

            try
            {
                Result rc = service.GetMmcManagerOperator(out mmcOperator);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                int       operationId = MakeOperationId(MmcManagerOperationIdValue.GetPatrolCount);
                OutBuffer outBuffer   = OutBuffer.FromStruct(ref count);

                return(mmcOperator.Target.OperateOut(out _, outBuffer, operationId));
            }
            finally
            {
                mmcOperator?.Dispose();
            }
        }
Пример #14
0
        protected override void Decrypt()
        {
            try
            {
                // Read key
                byte[] keyChunk = ReadIndexedChunk(0x64);
                byte[] key      = AesEcbDecrypt(keyChunk, rootKey);

                // Build main key
                for (uint i = 0; i < MainKey.Length; i++)
                {
                    MainKey[i] = Convert.ToByte(i);
                }
                for (uint trgIndex = 0, swpIndex, lastIndex = 0, srcOffset = 17, srcIndex = srcOffset; trgIndex < MainKey.Length; trgIndex++)
                {
                    byte swap = MainKey[trgIndex];
                    swpIndex = (swap + lastIndex + key[srcIndex++]) & 0xff;
                    if (srcIndex >= key.Length)
                    {
                        srcIndex = srcOffset;
                    }
                    MainKey[trgIndex] = MainKey[swpIndex];
                    MainKey[swpIndex] = swap;
                    lastIndex         = swpIndex;
                }
            }
            catch (NullFileChunkException e)
            {
                throw new FileLoadException(e.Message);
            }

            try
            {
                // Read metadata
                byte[] metaChunk = ReadIndexedChunk(0x63);
                int    skipCount = 22;
                for (int i = 0; i < metaChunk.LongLength; i++)
                {
                    if (metaChunk[i] == 58)
                    {
                        skipCount = i + 1;
                        break;
                    }
                }

                // Resolve metadata
                PropMetadata = JsonConvert.DeserializeObject <NetEaseMetadata>(
                    Encoding.UTF8.GetString(
                        AesEcbDecrypt(
                            Convert.FromBase64String(
                                Encoding.UTF8.GetString(
                                    metaChunk.Skip(skipCount).ToArray())
                                ),
                            jsonKey)
                        .Skip(6).ToArray())
                    );
                StdMetadata = new Metadata(PropMetadata);
            }
            catch (NullFileChunkException)
            {
                Logger.Warn("Missing metadata in {Path}", InPath);
            }

            // Skip ahead
            _ = InBuffer.Seek(9, SeekOrigin.Current);

            // Get cover data
            try
            {
                // Plan A: Read cover from file
                CoverBuffer.Write(ReadIndexedChunk(null));
            }
            catch (NullFileChunkException)
            {
                Logger.Warn("Failed to load cover from {Path}, trying to get from server...", InPath);

                // Plan B: get image from server
                try
                {
                    string coverUri = PropMetadata.AlbumPic;
                    if (!Uri.IsWellFormedUriString(coverUri, UriKind.Absolute))
                    {
                        Logger.Error("No cover URI was found in {Path}", InPath);
                        throw;
                    }
                    using WebClient webClient = new WebClient();
                    CoverBuffer.Write(webClient.DownloadData(coverUri));
                }
                catch (Exception)
                {
                    Logger.Error("Failed to download cover image for {Path}", InPath);
                }
            }
            CoverMime = MediaType.GetStreamMime(CoverBuffer);
            if (CoverMime.Substring(0, 5) != "image")
            {
                CoverBuffer.Dispose();
                CoverMime = null;
            }

            // Read music
            for (int chunkSize = 0x8000; ;)
            {
                byte[] mainChunk = ReadFixedChunk(ref chunkSize);

                for (int i = 0; i < chunkSize; i++)
                {
                    int j = (i + 1) & 0xff;
                    mainChunk[i] ^= MainKey[(MainKey[j] + MainKey[(MainKey[j] + j) & 0xff]) & 0xff];
                }

                if (chunkSize < 0x8000)
                {
                    OutBuffer.Write(mainChunk.Take(chunkSize).ToArray());
                    break;
                }
                else
                {
                    OutBuffer.Write(mainChunk);
                }
            }
            MusicMime = MediaType.GetStreamMime(OutBuffer);
        }
Пример #15
0
 public bool Equals(BaseParseFrame other)
 {
     return(true && Buffer.Equals(other.Buffer) && OutBuffer.Equals(other.OutBuffer) && Flags.Equals(other.Flags) && Offset.Equals(other.Offset) && Overhead.Equals(other.Overhead) && size.Equals(other.size) && _private_flags.Equals(other._private_flags));
 }
Пример #16
0
 public override int GetHashCode()
 {
     return(this.GetType().FullName.GetHashCode() ^ Buffer.GetHashCode() ^ OutBuffer.GetHashCode() ^ Flags.GetHashCode() ^ Offset.GetHashCode() ^ Overhead.GetHashCode() ^ size.GetHashCode() ^ _private_flags.GetHashCode());
 }
Пример #17
0
        private Result ReadSaveDataInfoImpl(out long readCount, Span <SaveDataInfo> buffer)
        {
            var outBuffer = new OutBuffer(MemoryMarshal.Cast <SaveDataInfo, byte>(buffer));

            return(Reader.Target.Read(out readCount, outBuffer));
        }
Пример #18
0
 protected override void Decrypt()
 {
     OutBuffer.Write(header);
     OutBuffer.Write(InBuffer.GetBuffer().Skip(8).ToArray());
 }
Пример #19
0
        // ~ Constructors

        ///
        ///	 <summary> * Creates a new instance of PrintfFormat from the supplied format string. The structure of the format string is
        ///	 * described in the documentation for the <tt>set</tt> method.
        ///	 *  </summary>
        ///	 * <param name="fmt"> Format string </param>
        ///	 * <exception cref="IllegalArgumentException"> Malformed format string </exception>
        ///	 * <seealso cref= PrintfFormat#set </seealso>
        ///
        public PrintfFormat(string fmt)
        {
            output = new OutBuffer(1024);
            @set(fmt);
        }
Пример #20
0
 public virtual void Dispose()
 {
     InBuffer.Dispose();
     OutBuffer.Dispose();
     CoverBuffer.Dispose();
 }