* chunkcopy() can read or write more than the safe length if the length is not multiple of chunk size.
/* Behave like chunkcopy, but avoid writing beyond of legal output. */
Z_INTERNAL uint8_t* CHUNKCOPY_SAFE(uint8_t *out, uint8_t const *from, unsigned len, uint8_t *safe) {
len = MIN(len, safe - out + 1);
- if (len < sizeof(chunk_t)) {
-#if CHUNK_SIZE > 16
- if (len & 16) {
- memcpy(out, from, 16);
- out += 16;
- from += 16;
- }
+#if CHUNK_SIZE >= 32
+ while (len >= 32) {
+ memcpy(out, from, 32);
+ out += 32;
+ from += 32;
+ len -= 32;
+ }
#endif
-#if CHUNK_SIZE > 8
- if (len & 8) {
- memcpy(out, from, 8);
- out += 8;
- from += 8;
- }
+#if CHUNK_SIZE >= 16
+ while (len >= 16) {
+ memcpy(out, from, 16);
+ out += 16;
+ from += 16;
+ len -= 16;
+ }
#endif
- if (len & 4) {
- memcpy(out, from, 4);
- out += 4;
- from += 4;
- }
- if (len & 2) {
- memcpy(out, from, 2);
- out += 2;
- from += 2;
- }
- if (len & 1) {
- *out++ = *from++;
- }
- return out;
+#if CHUNK_SIZE >= 8
+ while (len >= 8) {
+ memcpy(out, from, 8);
+ out += 8;
+ from += 8;
+ len -= 8;
+ }
+#endif
+ if (len >= 4) {
+ memcpy(out, from, 4);
+ out += 4;
+ from += 4;
+ len -= 4;
}
- return CHUNKCOPY(out, from, len);
+ if (len >= 2) {
+ memcpy(out, from, 2);
+ out += 2;
+ from += 2;
+ len -= 2;
+ }
+ if (len == 1) {
+ *out++ = *from++;
+ }
+ return out;
}
/* Perform short copies until distance can be rewritten as being at least