public byte[] Serialize()
        {
            int bufferBase;

            if (this.___entitystate == EntitySerialization03.EntityState.UNINITIALIZED)
            {
                Initialize();
            }

            EntitySerialization03.EntityState oldState = OnChangeStateOnly(EntitySerialization03.EntityState.SERIALIZED);

            byte[][] fieldBuffers = new byte[this.___nfields][];

            // Entity System fields
            bufferBase = 0;
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this.__entitytypehash); bufferBase++;
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this.__created); bufferBase++;
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this.__modified); bufferBase++;
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this.__changeindex); bufferBase++;

            // Entity Data fields
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this._imageVersion); bufferBase++;
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this._imageName); bufferBase++;
            fieldBuffers[bufferBase] = FieldSerialization03.Serialize(this._imageContent); bufferBase++;

            byte[] buffer = EntitySerialization03.SerializeFieldBuffers(fieldBuffers,
                                                                        this.___entityserializationsignature, this.___entityserializationversion, this.___fieldserializationversion,
                                                                        (byte)this.___entitystate, this.___nfields);
            this.___entitystate = oldState;

            return(buffer);
        }
示例#2
0
        public byte[] Serialize()
        {
            if (___entitystate == EntitySerialization03.EntityState.UNINITIALIZED)
            {
                Initialize();
            }

            EntitySerialization03.EntityState oldState = OnChangeState(EntitySerialization03.EntityState.SERIALIZED);

            byte[][] fieldBuffer = new byte[this.___nfields][];

            // Entity System fields
            fieldBuffer[0] = FieldSerialization03.Serialize(this.__entitytypehash);
            fieldBuffer[1] = FieldSerialization03.Serialize(this.__created);
            fieldBuffer[2] = FieldSerialization03.Serialize(this.__modified);
            fieldBuffer[3] = FieldSerialization03.Serialize(this.__changeindex);

            // Entity Data fields
            fieldBuffer[3] = FieldSerialization03.Serialize(this._id);
            fieldBuffer[4] = FieldSerialization03.Serialize(this._username);
            fieldBuffer[5] = FieldSerialization03.Serialize(this._passphrase);

            int lBuffer = EntitySerialization03.NHEADERFIELDS * sizeof(byte);

            for (int fi = 0; fi < ___nfields; fi++)
            {
                lBuffer += fieldBuffer[fi].Length;                                     // Entity System fields plus Entity data fields
            }
            byte[] buffer     = new byte[lBuffer];
            int    bufferBase = 0;

            // Entity Header fields
            buffer[bufferBase++] = ___entityserializationsignature;
            buffer[bufferBase++] = ___entityserializationversion;
            buffer[bufferBase++] = ___fieldserializationversion;
            buffer[bufferBase++] = (byte)___entitystate;
            buffer[bufferBase++] = ___nfields;

            // Entity System fields plus Entity data fields
            for (int fi = 0; fi < ___nfields; fi++)
            {
                if (bufferBase + fieldBuffer[fi].Length > lBuffer)
                {
                    OnChangeState(EntitySerialization03.EntityState.EXCEPTION_SERIALIZATION);
                    return(Array.Empty <byte>());
                }
                fieldBuffer[fi].CopyTo(buffer, bufferBase);
                bufferBase += fieldBuffer[fi].Length;
            }

            ___entitystate = oldState;

            return(buffer);
        }
示例#3
0
        public byte[] Serialize()
        {
            int fieldBufferBase;

            if (this.___entitystate == EntitySerialization03.EntityState.UNINITIALIZED)
            {
                Initialize();
            }

            EntitySerialization03.EntityState oldState = OnChangeState(EntitySerialization03.EntityState.SERIALIZED);

            byte[][] fieldBuffer = new byte[this.___nfields][];
            fieldBufferBase = 0;

            // Entity System fields
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__entitytypehash); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__created); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__modified); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__changeindex); fieldBufferBase++;

            // Entity Data fields
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this._imageVersion); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this._imageName); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this._imageContent); fieldBufferBase++;

            // Blockchain data fields
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__previousentityBlockindex); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__previousentityTxhash, false); fieldBufferBase++; // FieldEncoding.ASCIIString
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__previousentityContentHash); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__thisentityRecentBlockIndex); fieldBufferBase++;
            fieldBuffer[fieldBufferBase] = FieldSerialization03.Serialize(this.__thisentityContentHash); fieldBufferBase++;

            int lBuffer = EntitySerialization03.NHEADERFIELDS * sizeof(byte);

            for (int fi = 0; fi < this.___nfields; fi++)
            {
                lBuffer += fieldBuffer[fi].Length;                                          // System + Entity + Blockchain data fields
            }
            byte[] buffer = new byte[lBuffer];
            fieldBufferBase = 0;

            // Entity Header fields
            buffer[fieldBufferBase++] = this.___entityserializationsignature;
            buffer[fieldBufferBase++] = this.___entityserializationversion;
            buffer[fieldBufferBase++] = this.___fieldserializationversion;
            buffer[fieldBufferBase++] = (byte)this.___entitystate;
            buffer[fieldBufferBase++] = this.___nfields;

            // System + Entity + Blockchain data fields - copy all fields except __thisentityContentHash
            for (int fi = 0; fi < this.___nfields - 1; fi++)
            {
                if (fieldBufferBase + fieldBuffer[fi].Length > lBuffer)
                {
                    OnChangeState(EntitySerialization03.EntityState.EXCEPTION_SERIALIZATION);
                    return(Array.Empty <byte>());
                }

                fieldBuffer[fi].CopyTo(buffer, fieldBufferBase);
                fieldBufferBase += fieldBuffer[fi].Length;
            }

            // Set __thisentityContentHash and encode it into the buffer
            using (SHA256 SHA256Factory = SHA256.Create())
            {
                __thisentityContentHash = SHA256Factory.ComputeHash(buffer, 0, fieldBufferBase);
                fieldBuffer[this.___nfields - 1].CopyTo(buffer, fieldBufferBase);
                fieldBufferBase += fieldBuffer[this.___nfields - 1].Length;
            }

            this.___entitystate = oldState;

            return(buffer);
        }