public override int ReadByte() { var b = SourceStream.ReadByte(); if (b > -1) { outStream.WriteByte((byte)b); } return(b); }
//public static int? ReadLength(byte[] data, int offset) //{ // var lenByte = (int)data[offset]; // if (lenByte == -1) // return null; // if ((lenByte & 0x80) == 0) // return lenByte; // int length = 0; // int lengthBytes = lenByte & 0x7F; // if (lengthBytes == 0) // return null; // if (lengthBytes > 4) // return null; // for (int i = 0; i < lengthBytes; i++, offset++) // { // lenByte = (int)data[offset]; // if (lenByte == -1) // throw new IOException("Unexpected end of stream"); // length <<= 8; // length |= (int)lenByte; // } // return length; //} private int?ReadLength() { var lenByte = SourceStream.ReadByte(); if (lenByte == -1) { throw new IOException("Unexpected end of stream"); } if ((lenByte & 0x80) == 0) { return(lenByte); } int length = 0; int lengthBytes = lenByte & 0x7F; if (lengthBytes == 0) { return(null); } if (lengthBytes > 4) { throw new IOException("Invalid length value"); } for (int i = 0; i < lengthBytes; i++) { lenByte = SourceStream.ReadByte(); if (lenByte == -1) { throw new IOException("Unexpected end of stream"); } length <<= 8; length |= (int)lenByte; } return(length); }
private uint?ReadNextTag() { uint tagValue = 0; int byteCount = 0; while (true) { if (byteCount >= 4) { throw new Exception(string.Format("Tag is more than 4 bytes long. Partial tag value: 0x{0:x8}", tagValue)); } var thisByte = SourceStream.ReadByte(); if (thisByte == -1) { if (byteCount > 0) { throw new Exception(string.Format("Unexpected end of tag data. Partial tag value: 0x{0:x8}", tagValue)); } return(null); // end of stream, no tag found } if (RemoveEMVPadding && byteCount == 0 && (thisByte == 0x00 || thisByte == 0xff)) // todo: check 0xff usage!!! { continue; // skip if not already reading tag (EMV allows 0x00 or 0xff padding between TLV entries) } byteCount++; tagValue <<= 8; tagValue |= (uint)thisByte; if (byteCount == 1 && (thisByte & 0x1f) != 0x1f) { return(tagValue); // no more data (tag number is 0 to 30 inclusive, so only one octet used) } if (byteCount != 1 && (thisByte & 0x80) == 0) { return(tagValue); // no more data (bit 8 is not set for last octet) } } }