Skip to content

Implement round-trip encoding for leading and trailing zero bytes without breaking API#15

Draft
Copilot wants to merge 4 commits into
masterfrom
copilot/finish-pending-pr-13
Draft

Implement round-trip encoding for leading and trailing zero bytes without breaking API#15
Copilot wants to merge 4 commits into
masterfrom
copilot/finish-pending-pr-13

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 9, 2026

Addresses review feedback on PR #13: preserve leading and trailing zero bytes (EMV padding) during TLV parse/encode cycles while avoiding public API changes.

Changes

  • Refactored read_tag return type: Returns (Tag, usize) tuple instead of modifying TlvError::InvalidTagNumber enum variant. The second element tracks leading zero count without breaking downstream pattern matching.

  • Added leading_zeroes field to Tlv struct: Tracks padding bytes before TLV objects per EMV 4.3 Book 3 Annex B1. The to_vec() method now prepends these bytes before the tag.

  • Added trailing_zeroes field to Tlv struct: Tracks padding bytes after TLV objects per EMV 4.3 Book 3 Annex B1. The to_vec() method now appends these bytes after the value.

  • Fixed Value::len() for constructed TLVs: Now includes leading_zeroes of child TLVs when calculating total length. This ensures parent length fields correctly reflect the actual byte count in the value field.

Example

// Leading zeroes are now preserved during round-trip
let input = vec![0x00, 0x00, 0x01, 0x02, 0x00, 0x00];
assert_eq!(Tlv::from_vec(&input).unwrap().to_vec(), input);

// Constructed TLV length correctly accounts for padding between children
let input = vec![0xE1, 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0x02, 0x01, 0x02];
//                tag   len=8  child1------  padding  child2------
assert_eq!(Tlv::from_vec(&input).unwrap().to_vec(), input);

// Trailing zeroes are preserved
let input = vec![0x01, 0x02, 0x00, 0x00, 0x00, 0x00];
assert_eq!(Tlv::from_vec(&input).unwrap().to_vec(), input);

// Both leading and trailing zeroes are preserved
let input = vec![0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00];
assert_eq!(Tlv::from_vec(&input).unwrap().to_vec(), input);

EMV Compliance

The implementation now fully supports all three types of padding mentioned in EMV 4.3 Book 3 Annex B1:

  • Before: Leading zeroes (padding before TLV objects)
  • Between: Padding between child TLVs (captured as leading_zeroes of next TLV)
  • After: Trailing zeroes (padding after TLV objects)
Original prompt

there is pending PR: #13
I need help to finish it. Check the notes I left in the #13 (comment)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: lexxvir <331063+lexxvir@users.noreply.github.com>
Copilot AI changed the title [WIP] Complete pending work on PR #13 Implement round-trip encoding for leading zero bytes without breaking API Feb 9, 2026
Copilot AI requested a review from lexxvir February 9, 2026 15:48
@lexxvir
Copy link
Copy Markdown
Owner

lexxvir commented Feb 10, 2026

@copilot add support for trailing zeroes too.

Copilot AI and others added 2 commits February 10, 2026 06:45
Co-authored-by: lexxvir <331063+lexxvir@users.noreply.github.com>
Co-authored-by: lexxvir <331063+lexxvir@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 10, 2026

@copilot add support for trailing zeroes too.

Added trailing zeroes support in commit e97ed18. The implementation now handles all three types of EMV padding (leading, between, and trailing zero bytes) with complete round-trip encoding.

Copilot AI changed the title Implement round-trip encoding for leading zero bytes without breaking API Implement round-trip encoding for leading and trailing zero bytes without breaking API Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants