Encode QUIC ACK receive timestamp delta count as N - 1 per draft-ietf-quic-receive-ts-04

`AckFrameEmptyFirstReceiveTimestampRange` is removed as it tests an edge case that is no longer possible.

Also do minor cleanup to the parsing code while we're at it.

PiperOrigin-RevId: 1024174973
diff --git a/quiche/quic/core/quic_ack_timestamp_fuzz_test.cc b/quiche/quic/core/quic_ack_timestamp_fuzz_test.cc
index b3bfa05..e4dfa6f 100644
--- a/quiche/quic/core/quic_ack_timestamp_fuzz_test.cc
+++ b/quiche/quic/core/quic_ack_timestamp_fuzz_test.cc
@@ -244,9 +244,9 @@
   constexpr char kPayload[] =
       "\x52\x34\x00\x00\x00"                  // ACK header
       "\x03"                                  // range count
-      "\x02\x03\xa9\xff\xff\xff\x51\x11\x01"  // range 1
-      "\x0b\x01\x50\x00"                      // range 2
-      "\x15\x02\x10\x41\x00";                 // range 3
+      "\x02\x02\xa9\xff\xff\xff\x51\x11\x01"  // range 1
+      "\x0b\x00\x50\x00"                      // range 2
+      "\x15\x01\x10\x41\x00";                 // range 3
   return std::string(kPayload, std::size(kPayload) - 1);
 }
 
diff --git a/quiche/quic/core/quic_ack_timestamp_list.cc b/quiche/quic/core/quic_ack_timestamp_list.cc
index deb6932..955b37e 100644
--- a/quiche/quic/core/quic_ack_timestamp_list.cc
+++ b/quiche/quic/core/quic_ack_timestamp_list.cc
@@ -137,8 +137,9 @@
   QuicByteCount total_size = 0;
   total_size += VarintSize(ranges_.size());
   for (const TimestampRange& range : ranges_) {
+    QUICHE_DCHECK_GE(range.timestamp_count, 1u);
     total_size += VarintSize(range.delta_from_largest_acked);
-    total_size += VarintSize(range.timestamp_count);
+    total_size += VarintSize(range.timestamp_count - 1);
   }
   // Every element in `timestamps_` belongs to exactly one TimestampRange.
   for (const uint64_t timestamp : timestamps_) {
@@ -211,13 +212,13 @@
     // Atomically write the range header and the first timestamp, since an empty
     // range is generally unhelpful.
     pending_size += VarintSize(range.delta_from_largest_acked) +
-                    VarintSize(range.timestamp_count) +
+                    VarintSize(range.timestamp_count - 1) +
                     VarintSize(timestamps_in_range[0]);
     if (pending_size > max_size) {
       return true;
     }
     encoded.push_back(range.delta_from_largest_acked);
-    encoded.push_back(1);
+    encoded.push_back(0);
     size_t timestamp_count_offset = encoded.size() - 1;
     encoded.push_back(timestamps_in_range[0]);
     ++encoded[range_count_offset];
diff --git a/quiche/quic/core/quic_ack_timestamp_list.h b/quiche/quic/core/quic_ack_timestamp_list.h
index 51133e9..963fa88 100644
--- a/quiche/quic/core/quic_ack_timestamp_list.h
+++ b/quiche/quic/core/quic_ack_timestamp_list.h
@@ -19,7 +19,7 @@
 namespace quic {
 
 // QuicAckTimestampList stores the block of ACK timestamps
-// (draft-ietf-quic-receive-ts-02) in a form suitable for serialization.
+// (draft-ietf-quic-receive-ts-04) in a form suitable for serialization.
 class QUICHE_EXPORT QuicAckTimestampList {
  public:
   // Constructs the ACK timestamp list from the provided `ack` frame.  If an
@@ -44,7 +44,7 @@
   const std::string& error() const { return error_; }
 
  private:
-  // `Timestamp Range` as defined in draft-ietf-quic-receive-ts-02.
+  // `Timestamp Range` as defined in draft-ietf-quic-receive-ts-04.
   struct QUICHE_EXPORT TimestampRange {
     QuicPacketCount delta_from_largest_acked;
     // Offset into `timestamps_` list.
diff --git a/quiche/quic/core/quic_ack_timestamp_list_test.cc b/quiche/quic/core/quic_ack_timestamp_list_test.cc
index e40052d..7026a65 100644
--- a/quiche/quic/core/quic_ack_timestamp_list_test.cc
+++ b/quiche/quic/core/quic_ack_timestamp_list_test.cc
@@ -59,7 +59,7 @@
                        "00");
 }
 
-// Based on Section 8, Table 1 of draft-ietf-quic-receive-ts-02.
+// Based on Section 8, Table 1 of draft-ietf-quic-receive-ts-04.
 TEST_F(QuicAckTimestampListTest, DraftExampleTable1) {
   ack_.largest_acked = QuicPacketNumber(100);
   ack_.received_packet_times = {
@@ -81,16 +81,16 @@
                        "02"            // Range count = 2
                                        // ------------ Range 1 ------------
                        "00"            // Delta largest acked = 0
-                       "05"            // Count = 5
+                       "04"            // Additional count = 4 (5 timestamps)
                        "417c0a0a0505"  // Deltas = 380, 10, 10, 5, 5
                                        // ------------ Range 2 ------------
                        "09"            // Delta largest acked = 9
-                       "05"            // Count = 5
+                       "04"            // Additional count = 4 (5 timestamps)
                        "140a0a0505"    // Deltas = 20, 10, 10, 5, 5
   );
 }
 
-// Based on Section 8, Table 2 of draft-ietf-quic-receive-ts-02.
+// Based on Section 8, Table 2 of draft-ietf-quic-receive-ts-04.
 TEST_F(QuicAckTimestampListTest, DraftExampleTable2OutOfOrder) {
   ack_.largest_acked = QuicPacketNumber(100);
   ack_.received_packet_times = {
@@ -116,15 +116,15 @@
                        "03"          // Range count = 3
                                      // ------------ Range 1 ------------
                        "05"          // Delta largest acked = 5
-                       "04"          // Count = 4
+                       "03"          // Additional count = 3 (4 timestamps)
                        "418b010202"  // Deltas = 395, 1, 2, 2
                                      // ------------ Range 2 ------------
                        "00"          // Delta largest acked = 0
-                       "05"          // Count = 5
+                       "04"          // Additional count = 4 (5 timestamps)
                        "0a0a0a0505"  // Deltas = 10, 10, 10, 5, 5
                                      // ------------ Range 3 ------------
                        "09"          // Delta largest acked = 9
-                       "05"          // Count = 5
+                       "04"          // Additional count = 4 (5 timestamps)
                        "140a0a0505"  // Deltas = 20, 10, 10, 5, 5
   );
 }
@@ -152,15 +152,15 @@
                        "03"        // Range count = 3
                                    // ------------ Range 1 ------------
                        "02"        // Delta largest acked = 2
-                       "03"        // Count = 3
+                       "02"        // Additional count = 2 (3 timestamps)
                        "41903232"  // Deltas = 400, 50, 50
                                    // ------------ Range 2 ------------
                        "0b"        // Delta largest acked = 11
-                       "01"        // Count = 1
+                       "00"        // Additional count = 0 (1 timestamp)
                        "4064"      // Deltas = 100
                                    // ------------ Range 3 ------------
                        "15"        // Delta largest acked = 21
-                       "02"        // Count = 2
+                       "01"        // Additional count = 1 (2 timestamps)
                        "405a0a"    // Deltas = 90, 10
   );
 }
@@ -184,7 +184,7 @@
                        "01"        // Range count = 1
                                    // Range 1
                        "00"        // Delta largest acked = 0
-                       "03"        // Count = 3
+                       "02"        // Additional count = 2 (3 timestamps)
                        "40640a0a"  // Deltas = 100, 10, 10
   );
 }
@@ -213,7 +213,7 @@
                        "01"      // Range count = 1
                                  // Range 1
                        "00"      // Delta largest acked = 0
-                       "03"      // Count = 3
+                       "02"      // Additional count = 2 (3 timestamps)
                        "060201"  // Deltas = 6, 2, 1
   );
 }
@@ -232,7 +232,7 @@
                        "01"      // Range count = 1
                                  // Range 1
                        "00"      // Delta largest acked = 0
-                       "03"      // Count = 3
+                       "02"      // Additional count = 2 (3 timestamps)
                        "000000"  // Deltas = 0, 0, 0
   );
 }
@@ -310,7 +310,7 @@
   QuicDataWriter writer(buffer.size(), buffer.data());
   EXPECT_TRUE(list.Write(writer));
   EXPECT_EQ(writer.remaining(), 0u);
-  EXPECT_EQ(absl::BytesToHexString(buffer), "010002140a");
+  EXPECT_EQ(absl::BytesToHexString(buffer), "010001140a");
 }
 
 TEST_F(QuicAckTimestampListTest, BufferTruncationMultipleRanges) {
@@ -336,7 +336,7 @@
     EXPECT_TRUE(list.Write(writer)) << "buf_len: " << buf_len;
     EXPECT_EQ(writer.length(), 5u) << "buf_len: " << buf_len;
     EXPECT_EQ(absl::BytesToHexString(absl::string_view(buffer.data(), 5)),
-              "010002140a")
+              "010001140a")
         << "buf_len: " << buf_len;
   }
 
@@ -347,7 +347,7 @@
     QuicDataWriter writer(buffer.size(), buffer.data());
     EXPECT_TRUE(list.Write(writer));
     EXPECT_EQ(writer.length(), 4u);
-    EXPECT_EQ(absl::BytesToHexString(buffer), "01000114");
+    EXPECT_EQ(absl::BytesToHexString(buffer), "01000014");
   }
 
   // Buffer sizes 1, 2, and 3 are not large enough to encode any range (as even
@@ -377,7 +377,7 @@
       {
         QuicAckTimestampList list(ack_, /*max_ack_count=*/10, /*exponent=*/0);
         EXPECT_EQ(list.error(), "");
-        ExpectBinaryEncoding(list, "020002140a0a0105");
+        ExpectBinaryEncoding(list, "020001140a0a0005");
       },
       "Sending receive timestamp for unacknowledged packet 90");
 }
diff --git a/quiche/quic/core/quic_framer.cc b/quiche/quic/core/quic_framer.cc
index a6c4883..30059ef 100644
--- a/quiche/quic/core/quic_framer.cc
+++ b/quiche/quic/core/quic_framer.cc
@@ -3886,7 +3886,7 @@
   //
   // Timestamp Range {
   //    Delta Largest Acknowledged (i),
-  //    Timestamp Delta Count (i),
+  //    Additional Timestamp Delta Count (i),
   //    Timestamp Delta (i) ...,
   //  }
   for (uint64_t i = 0; i < timestamp_range_count; i++) {
@@ -3900,16 +3900,17 @@
       set_detailed_error("Receive delta largest acked too high.");
       return false;
     }
-    QuicPacketNumber packet_number = largest_acked - delta;
-    uint64_t timestamp_count;
-    if (!reader->ReadVarInt62(&timestamp_count)) {
+    const QuicPacketNumber first_packet_number = largest_acked - delta;
+    uint64_t additional_timestamp_count;
+    if (!reader->ReadVarInt62(&additional_timestamp_count)) {
       set_detailed_error("Unable to read receive timestamp count.");
       return false;
     }
-    if (packet_number.ToUint64() < timestamp_count) {
+    if (first_packet_number.ToUint64() < additional_timestamp_count) {
       set_detailed_error("Receive timestamp count too high.");
       return false;
     }
+    const uint64_t timestamp_count = additional_timestamp_count + 1;
     total_timestamp_count += timestamp_count;
     if (total_timestamp_count > local_max_receive_timestamps_per_ack_) {
       set_detailed_error("Too many receive timestamps in ACK frame.");
@@ -3946,12 +3947,11 @@
         set_detailed_error("Receive timestamp delta too high.");
         return false;
       }
-      if (!visitor_->OnAckTimestamp(packet_number, *receive_time)) {
+      if (!visitor_->OnAckTimestamp(first_packet_number - j, *receive_time)) {
         set_detailed_error(
             "Visitor suppresses further processing of ACK frame.");
         return false;
       }
-      packet_number--;
     }
   }
   return true;
diff --git a/quiche/quic/core/quic_framer_test.cc b/quiche/quic/core/quic_framer_test.cc
index 87607d8..3b00af2 100644
--- a/quiche/quic/core/quic_framer_test.cc
+++ b/quiche/quic/core/quic_framer_test.cc
@@ -3671,7 +3671,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x01 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x02 }},
+         { kVarInt62OneByte + 0x01 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62FourBytes + 0x36, 0x54, 0x32, 0x10 }},
        { "Unable to read receive timestamp delta.",
@@ -3749,7 +3749,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x02 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x03 }},
+         { kVarInt62OneByte + 0x02 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62FourBytes + 0x29, 0xff, 0xff, 0xff}},
        { "Unable to read receive timestamp delta.",
@@ -3761,7 +3761,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x0b }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x01 }},
+         { kVarInt62OneByte + 0x00 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62TwoBytes + 0x10, 0x00 }},
 
@@ -3769,7 +3769,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x15 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x02 }},
+         { kVarInt62OneByte + 0x01 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62OneByte + 0x10 }},
        { "Unable to read receive timestamp delta.",
@@ -3847,7 +3847,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x02 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x03 }},
+         { kVarInt62OneByte + 0x02 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62FourBytes + 0x29, 0xff, 0xff, 0xff}},
        { "Unable to read receive timestamp delta.",
@@ -3918,7 +3918,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x00 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x03 }},
+         { kVarInt62OneByte + 0x02 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62TwoBytes + 0x29, 0xff}},
        { "Unable to read receive timestamp delta.",
@@ -3986,7 +3986,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x79 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x01 }},
+         { kVarInt62OneByte + 0x00 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62TwoBytes + 0x29, 0xff}},
   };
@@ -4040,7 +4040,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x02 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x02 }},
+         { kVarInt62OneByte + 0x01 }},
        { "Unable to read receive timestamp delta.",
          { kVarInt62OneByte + 0x0a}},
        { "Unable to read receive timestamp delta.",
@@ -4150,7 +4150,7 @@
        { "Unable to read receive timestamp delta largest acked.",
          { kVarInt62OneByte + 0x00 }},
        { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x01 }},
+         { kVarInt62OneByte + 0x00 }},
        // Max 62-bit varint (0x3fffffffffffffff), which overflows when shifted
        // by exponent >= 2.
        { "Unable to read receive timestamp delta.",
@@ -4168,75 +4168,6 @@
                                "Receive timestamp delta too high."));
 }
 
-TEST_P(QuicFramerTest, AckFrameEmptyFirstReceiveTimestampRange) {
-  if (!VersionIsIetfQuic(framer_.transport_version())) {
-    return;
-  }
-  SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
-  // clang-format off
-  PacketFragments packet_ietf = {
-      // type (short header, 4 byte packet number)
-      {"",
-       { 0x43 }},
-      // connection_id
-      {"",
-       { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
-      // packet number
-      {"",
-       { 0x12, 0x34, 0x56, 0x78 }},
-
-      // frame type (IETF_ACK_RECEIVE_TIMESTAMPS frame)
-      {"",
-       { 0x83, 0x17, 0x83, 0x07 }},
-       // largest acked
-       {"Unable to read largest acked.",
-        { kVarInt62TwoBytes + 0x12, 0x34 }},   // = 4660
-       // Zero delta time.
-       {"Unable to read ack delay time.",
-        { kVarInt62OneByte + 0x00 }},
-       // number of additional ack blocks
-       {"Unable to read ack block count.",
-        { kVarInt62OneByte + 0x00 }},
-       // first ack block length.
-       {"Unable to read first ack block length.",
-        { kVarInt62OneByte + 0x00 }},  // 1st block length = 1
-
-       // Receive Timestamps (2 ranges; first range has 0 timestamps).
-       { "Unable to read receive timestamp range count.",
-         { kVarInt62OneByte + 0x02 }},
-       // Timestamp range 1 (empty).
-       { "Unable to read receive timestamp delta largest acked.",
-         { kVarInt62OneByte + 0x00 }},
-       { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x00 }},
-       // Timestamp range 2 (two packets).
-       { "Unable to read receive timestamp delta largest acked.",
-         { kVarInt62OneByte + 0x02 }},
-       { "Unable to read receive timestamp count.",
-         { kVarInt62OneByte + 0x02 }},
-       { "Unable to read receive timestamp delta.",
-         { kVarInt62OneByte + 0x20 }},
-       { "Unable to read receive timestamp delta.",
-         { kVarInt62OneByte + 0x05 }},
-  };
-  // clang-format on
-
-  std::unique_ptr<QuicEncryptedPacket> encrypted(
-      AssemblePacketFromFragments(packet_ietf));
-
-  framer_.set_local_max_receive_timestamps_per_ack(1000);
-  EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
-  EXPECT_THAT(framer_.error(), IsQuicNoError());
-  ASSERT_TRUE(visitor_.header_.get());
-  const QuicAckFrame& frame = *visitor_.ack_frames_[0];
-
-  EXPECT_THAT(frame.received_packet_times,
-              ContainerEq(PacketTimeVector{
-                  {LargestAcked(frame) - 2, CreationTimePlus(0x20)},
-                  {LargestAcked(frame) - 3, CreationTimePlus(0x1b)},
-              }));
-}
-
 TEST_P(QuicFramerTest, AckFrameTimeStampDeltaTooHigh) {
   SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
   // clang-format off
@@ -6953,8 +6884,8 @@
       // Timestamp range 1 (three packets).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x02,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x03,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x02,
       // Timestamp Delta
       kVarInt62FourBytes + 0x29,
       0xff,
@@ -6969,8 +6900,8 @@
       // Timestamp range 2 (one packet).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x0b,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x01,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x00,
       // Timestamp Delta
       kVarInt62TwoBytes + 0x10,
       0x00,
@@ -6978,8 +6909,8 @@
       // Timestamp range 3 (two packets).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x15,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x02,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x01,
       // Timestamp Delta
       kVarInt62OneByte + 0x10,
       // Timestamp Delta
@@ -7060,8 +6991,8 @@
       // Timestamp range 1 (two packets).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x02,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x02,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x01,
       // Timestamp Delta
       kVarInt62FourBytes + 0x29,
       0xff,
@@ -7073,8 +7004,8 @@
       // Timestamp range 2 (one packet).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x01,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x01,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x00,
       // Timestamp Delta
       kVarInt62TwoBytes + 0x10,
       0x00,
@@ -7082,8 +7013,8 @@
       // Timestamp range 3 (one packet).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x04,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x01,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x00,
       // Timestamp Delta
       kVarInt62OneByte + 0x10,
   };
@@ -7167,8 +7098,8 @@
       // Timestamp range 1 (three packets).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x02,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x03,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x02,
       // Timestamp Delta
       kVarInt62FourBytes + 0x29,
       0xff,
@@ -7259,8 +7190,8 @@
       // Timestamp range 1 (three packets).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x00,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x03,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x02,
       // Timestamp Delta
       kVarInt62FourBytes + 0x29,
       0xff,
@@ -7275,8 +7206,8 @@
       // Timestamp range 2 (one packet).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x09,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x01,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x00,
       // Timestamp Delta
       kVarInt62TwoBytes + 0x10,
       0x00,
@@ -7360,8 +7291,8 @@
       // Timestamp range 1 (three packets).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x02,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x04,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x03,
       // Timestamp Delta
       kVarInt62FourBytes + 0x00,
       0x02,
@@ -7377,8 +7308,8 @@
       // Timestamp range 2 (one packet).
       // Delta Largest Acknowledged
       kVarInt62OneByte + 0x0b,
-      // Timestamp Range Count
-      kVarInt62OneByte + 0x02,
+      // Additional Timestamp Delta Count
+      kVarInt62OneByte + 0x01,
       // Timestamp Delta
       kVarInt62TwoBytes + 0x11,
       0x00,