@@ -127,18 +127,17 @@ lz4_decompress_block_internal:
127127 ld c, a
128128 ld b, 0
129129 ; Check for extended length
130- cp a, $0F + 4
130+ add a, -( $0F + 4)
131131 jr nz, .L.copy_backref
132132 ; Save the backref pointer and restore the input pointer
133133 ex (sp), hl
134134.L.backref_loop:
135- ; Read the next length byte
136- ld a, (hl)
135+ ; Read the next length byte and add 1
136+ adc a, (hl)
137137 inc hl
138138 ; Decrement the length by 1
139139 dec bc
140- ; Increment the length byte, and check if it was 255
141- inc a
140+ ; Check if the length byte was 255
142141 jr nz, .L.copy_backref_long
143142 ; Increment the length by 256
144143 inc b
@@ -153,6 +152,8 @@ lz4_decompress_block_internal:
153152 inc bc
154153 ; Restore C
155154 ld c, a
155+ ; Set A = 0, preserving carry as 1
156+ ld a, b
156157 jr .L.backref_loop
157158
158159.L.literal_long:
@@ -161,14 +162,16 @@ lz4_decompress_block_internal:
161162 ; Start with literal length of 15
162163 ld b, 0
163164 dec bc
165+ .L.literal_loop_outer:
166+ ; Set A = 0, preserving carry as 1
167+ ld a, b
164168.L.literal_loop:
165- ; Read the next length byte
166- ld a, (hl)
169+ ; Read the next length byte and add 1
170+ adc a, (hl)
167171 inc hl
168172 ; Decrement the length by 1
169173 dec bc
170- ; Increment the length byte, and check if it was 255
171- inc a
174+ ; Check if the length byte was 255
172175 jr nz, .L.copy_literal_long
173176 ; Increment the length by 256
174177 inc b
@@ -183,7 +186,7 @@ lz4_decompress_block_internal:
183186 inc bc
184187 ; Restore C
185188 ld c, a
186- jr .L.literal_loop
189+ jr .L.literal_loop_outer
187190
188191.L.copy_literal_long:
189192 ; Copy the amount prior to the last length byte, minus 1
0 commit comments