public static string ReadStringUTF8(this BinaryReader reader) { int byteCount = reader.Read7BitEncodedInt(); byte[] bytes = reader.ReadBytesRequired(byteCount); return(Encoding.UTF8.GetString(bytes)); }
public static CompressionHandler Load(Stream stream) { using (var br = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true)) { if (br.Read7BitEncodedInt() != 1337) throw new InvalidDataException("Not a saved compression handler"); if (br.Read7BitEncodedInt() != 1) throw new InvalidDataException("Not a known version"); var dicLen = br.Read7BitEncodedInt(); var readBytes = br.ReadBytes(dicLen); var packer = HuffmanPacker.Load(br); return new CompressionHandler(readBytes, packer); } }
public static HuffmanPacker Load(BinaryReader reader) { var symbols = HuffmanTable.Load(reader); var len = reader.Read7BitEncodedInt(); var offsets = new HuffmanTable[len]; for (int i = 0; i < len; i++) { offsets[i] = HuffmanTable.Load(reader); } return new HuffmanPacker(symbols, offsets); }
public void LoadUncompressed(int initialVersion, Vector3D position, BinaryReader reader, string defaultMaterial, bool loadMaterial) { string voxelType = "Cell"; int materialBaseCount = 0; switch (initialVersion) { case 1: // cell tag header voxelType = reader.ReadString(); FileVersion = reader.Read7BitEncodedInt(); break; default: FileVersion = reader.ReadInt32(); break; } if (voxelType == "Cell") { var sizeX = reader.ReadInt32(); var sizeY = reader.ReadInt32(); var sizeZ = reader.ReadInt32(); var cellSizeX = reader.ReadInt32(); var cellSizeY = reader.ReadInt32(); var cellSizeZ = reader.ReadInt32(); _cellSize = new Vector3I(cellSizeX, cellSizeY, cellSizeZ); InitVoxelMap(position, new Vector3I(sizeX, sizeY, sizeZ), defaultMaterial, true); if (FileVersion == 2) materialBaseCount = reader.ReadByte(); } else { //voxelType == "Octree" // Don't know how to read this format. var a1 = reader.ReadByte(); // no idea var a2 = reader.ReadByte(); var a3 = reader.ReadByte(); var a4 = reader.ReadByte(); var a5 = reader.ReadByte(); var a6 = reader.ReadByte(); var a7 = reader.ReadByte(); var sizeX = reader.ReadInt32(); var sizeY = reader.ReadInt32(); var sizeZ = reader.ReadInt32(); var c1 = reader.ReadByte(); // no idea var c2 = reader.ReadByte(); var c3 = reader.ReadByte(); var c4 = reader.ReadByte(); var c5 = reader.ReadByte(); _cellSize = new Vector3I(8, 8, 8); InitVoxelMap(position, new Vector3I(sizeX, sizeY, sizeZ), defaultMaterial, false); materialBaseCount = reader.ReadInt32(); //FileVersion = 2; IsValid = false; return; } switch (FileVersion) { case 0: case 1: LoadUncompressedV1(reader, loadMaterial); break; case 2: LoadUncompressedV2(reader, loadMaterial, materialBaseCount); break; default: throw new Exception("Voxel format not implmented"); } }
/// <summary> /// Initializes the license. /// </summary> public static void InitLicense() { _isActivated = false; _licenseHash = null; if (IsActivated != _isActivated) { Process.GetCurrentProcess().Kill(); } if (IsOriginalAssembly.HasValue && !IsOriginalAssembly.Value) { _licenseStatus = LicenseStatus.Aborted; Log.Warn("License initialization aborted due to assembly tampering."); return; } var licfile = Path.Combine(AppDataPath, ".license"); if (!File.Exists(licfile)) { _licenseStatus = LicenseStatus.NotAvailable; Log.Debug("Licensing data not found."); return; } Log.Debug("Loading licensing information..."); try { byte[] xkey, lic; using (var fs = File.OpenRead(licfile)) using (var br = new BinaryReader(fs)) { var status = br.ReadByte(); if (status > 200) { _licenseStatus = LicenseStatus.KeyStatusError; Log.Error("The license is cryptographically valid, but has been denied by the activation server. Your key was most likely revoked. Please contact [email protected] for more information."); return; } _user = br.ReadString(); xkey = br.ReadBytes(br.Read7BitEncodedInt()); lic = br.ReadBytes(br.Read7BitEncodedInt()); } try { xkey = ProtectedData.Unprotect(xkey, null, DataProtectionScope.LocalMachine); lic = ProtectedData.Unprotect(lic, null, DataProtectionScope.LocalMachine); _key = Encoding.UTF8.GetString(xkey); } catch { _licenseStatus = LicenseStatus.LicenseDecryptError; Log.Error("The license is not for this computer or Windows machine keys were reset."); return; } if (!VerifyKey(_user, _key)) { _licenseStatus = LicenseStatus.KeyCryptoError; Log.Error("The key within the license is cryptographically invalid. If you have recently updated the software, the keying scheme might have been changed. If you did not receive an email containing a new key, please contact [email protected] for more information."); return; } var mbdsn = string.Empty; try { var mos = new ManagementObjectSearcher("select SerialNumber from Win32_BaseBoard").Get(); foreach (var mo in mos) { mbdsn = mo["SerialNumber"].ToString(); break; } } catch { } var cpusn = string.Empty; try { var mos = new ManagementObjectSearcher("select ProcessorId from Win32_Processor").Get(); foreach (var mo in mos) { cpusn = mo["ProcessorId"].ToString(); break; } } catch { } var hddsn = string.Empty; try { var mos = new ManagementObjectSearcher("select SerialNumber from Win32_DiskDrive where MediaType = \"Fixed hard disk media\"").Get(); foreach (var mo in mos) { hddsn = mo["SerialNumber"].ToString(); break; } } catch { } var prtsn = string.Empty; try { var mos = new ManagementObjectSearcher("select VolumeSerialNumber from Win32_LogicalDisk where DriveType = 3").Get(); foreach (var mo in mos) { prtsn = mo["VolumeSerialNumber"].ToString(); break; } } catch { } var verify = Encoding.UTF8.GetBytes(_user + '\0' + _key + '\0' + mbdsn + '\0' + cpusn + '\0' + hddsn + '\0' + prtsn); using (var rsa = new RSACryptoServiceProvider(0)) { var dt = DateTime.Now; rsa.ImportParameters(PublicKey); _isActivated = rsa.VerifyData(verify, SHA512.Create(), lic); Log.Trace("Signature verified in " + (DateTime.Now - dt).TotalSeconds + "s."); if (_isActivated) { _licenseHash = BitConverter.ToString(new HMACSHA384(SHA384.Create().ComputeHash(Encoding.UTF8.GetBytes(_user.ToLower().Trim())).Truncate(16)).ComputeHash(Encoding.UTF8.GetBytes(_key.Trim()))).ToLower().Replace("-", string.Empty); _licenseStatus = LicenseStatus.Valid; Log.Info("License validated for " + _user + ". Thank you for supporting the software!"); } else { _licenseStatus = LicenseStatus.LicenseInvalid; Log.Error("The license is not for this computer or hardware S/Ns have changed recently."); } } } catch (Exception ex) { _licenseStatus = LicenseStatus.LicenseException; Log.Error("Exception occurred during license validation.", ex); } }
void ReadMetadata() { byte[] mdBlock = null; int numBlocks = -1; try { if (_cache != null) { mdBlock = _cache.GetBlock (_baseFileName, _level, _version, int.MaxValue); if (mdBlock == null) { numBlocks = (int)internalFileStream.Length / Config.SortedBlockSize; mdBlock = ReadBlock (LocalThreadAllocatedBlock (), numBlocks - 1); byte[] blockCopy = (byte[])mdBlock.Clone (); _cache.SetBlock (_baseFileName, _level, _version, int.MaxValue, blockCopy); } } else { numBlocks = (int)internalFileStream.Length / Config.SortedBlockSize; mdBlock = ReadBlock (LocalThreadAllocatedBlock (), numBlocks - 1); } MemoryStream ms = new MemoryStream (mdBlock); BinaryReader reader = new BinaryReader (ms); string checkString = Encoding.ASCII.GetString (reader.ReadBytes (8)); if (checkString != "@RAZORDB") throw new InvalidDataException ("This does not appear to be a valid table file."); _totalBlocks = reader.Read7BitEncodedInt (); _dataBlocks = reader.Read7BitEncodedInt (); _indexBlocks = reader.Read7BitEncodedInt (); if (_totalBlocks != numBlocks && numBlocks != -1) throw new InvalidDataException ("The file size does not match the metadata size."); if (_totalBlocks != (_dataBlocks + _indexBlocks + 1)) throw new InvalidDataException ("Corrupted metadata."); } catch (Exception ex) { if (Config.ExceptionHandling == ExceptionHandling.ThrowAll) throw; HandleEmptySortedBlockTable (ex); } }