public unsafe void AtualizarCRC() { Byte[] dados = toByteArray(); fixed(byte *pSrc = dados) { FormatoMotoristaPaths *regiao = (FormatoMotoristaPaths *)pSrc; this.crc = CalcularCRC(dados); } }
public bool VerificarCRC(byte[] dados) { unsafe { fixed(byte *pSrc = dados) { FormatoMotoristaPaths *parametros = (FormatoMotoristaPaths *)pSrc; return(parametros->crc == CalcularCRC(dados)); } } }
private unsafe UInt16 CalcularCRC(Byte[] dados) { Byte[] dadosCRC = new byte[dados.Length - sizeof(UInt16)]; fixed(byte *pSrc = dados) { FormatoMotoristaPaths *motorista = (FormatoMotoristaPaths *)pSrc; Array.Copy(dados, 0, dadosCRC, 0, (int)&motorista->crc - (int)pSrc); Array.Copy(dados, ((int)&motorista->crc - (int)pSrc + sizeof(UInt16)), dadosCRC, (int)&motorista->crc - (int)pSrc, dados.Length - ((int)&motorista->crc - (int)pSrc + sizeof(UInt16))); return(CRC16CCITT.Calcular(dadosCRC)); } }
private byte[] toByteArray() { unsafe { Byte[] resultado = new Byte[sizeof(FormatoMotoristaPaths)]; fixed(byte *pSrc = resultado) { FormatoMotoristaPaths *motoristaPaths = (FormatoMotoristaPaths *)pSrc; motoristaPaths->versao = this.versao; motoristaPaths->idMotorista = this.idMotorista; motoristaPaths->crc = this.crc; ArrayLDX2.StringToByteArray(motoristaPaths->pathExibicaoId, this.pathExibicaoID, 64); ArrayLDX2.StringToByteArray(motoristaPaths->pathExibicaoNome, this.pathExibicaoNome, 64); return(resultado); } } }
public void GerarFormatoNovo(string arquivoNome, string diretorioRaiz) { UInt32 offsetId = 0; UInt32 offsetNome = 0; FileStream fs = File.OpenRead(arquivoNome + Util.Util.sequencial_arquivo_motoristas.ToString("X8") + Util.Util.ARQUIVO_EXT_DPT); byte[] dados = new byte[(int)fs.Length]; fs.Read(dados, 0, dados.Length); fs.Close(); unsafe { fixed(byte *pSrc = dados) { FormatoMotoristaPaths *motoristaPaths = (FormatoMotoristaPaths *)pSrc; this.versao = motoristaPaths->versao; this.idMotorista = motoristaPaths->idMotorista; this.crc = motoristaPaths->crc; this.pathExibicaoID = Util.Util.TrataDiretorio(ArrayLDX2.ByteArrayToString(motoristaPaths->pathExibicaoId, 64)); this.pathExibicaoNome = Util.Util.TrataDiretorio(ArrayLDX2.ByteArrayToString(motoristaPaths->pathExibicaoNome, 64)); } if (this.pathExibicaoID.Contains(":")) { fs = File.OpenRead(this.pathExibicaoID); } else { fs = File.OpenRead(diretorioRaiz + this.pathExibicaoID); } byte[] dadosVideoId = new byte[(int)fs.Length]; fs.Read(dadosVideoId, 0, dadosVideoId.Length); fs.Close(); offsetId = 64; Array.Resize(ref dados, (dados.Length - (2 * 64)) + dadosVideoId.Length); Array.Copy(dadosVideoId, 0, dados, offsetId, dadosVideoId.Length); if (this.pathExibicaoNome.Contains(":")) { fs = File.OpenRead(this.pathExibicaoNome); } else { fs = File.OpenRead(diretorioRaiz + this.pathExibicaoNome); } byte[] dadosVideoNome = new byte[(int)fs.Length]; fs.Read(dadosVideoNome, 0, dadosVideoNome.Length); fs.Close(); offsetNome = (UInt32)(offsetId + dadosVideoId.Length); Array.Resize(ref dados, dados.Length + dadosVideoNome.Length); Array.Copy(dadosVideoNome, 0, dados, offsetNome, dadosVideoNome.Length); BitConverter.GetBytes(offsetId).CopyTo(dados, 4); BitConverter.GetBytes(offsetNome).CopyTo(dados, 8); fixed(byte *pSrc = dados) { FormatoMotoristaPaths *motoristaPaths = (FormatoMotoristaPaths *)pSrc; motoristaPaths->crc = CalcularCRC(dados); BitConverter.GetBytes(motoristaPaths->crc).CopyTo(dados, Util.Util.CRCPosition1); } fs = File.Create(arquivoNome + Util.Util.sequencial_arquivo_motoristas.ToString("X8") + Util.Util.ARQUIVO_EXT_DPT); fs.Write(dados, 0, dados.Length); fs.Close(); } }