shithub: pokecrystal

Download patch

ref: 267352c4d8d4d390fc41e9db7e35f501d059eed3
parent: 73d20afd15d625889d4cb6617833bafd0316f12b
author: aaaaaa123456789 <aaaaaa123456789@acidch.at>
date: Sun Jun 28 01:20:05 EDT 2020

Update lzcomp

--- a/tools/lz/mpcomp.c
+++ b/tools/lz/mpcomp.c
@@ -101,7 +101,7 @@
       current = buffer + refpos - (length - 3);
     else
       current = reference + refpos;
-    if (memcmp(data + position, current, 4)) continue;
+    if (memcmp(data + position, current, ((position + 4) > length) ? length - position : 4)) continue;
     for (count = 4; (count < (length - position)) && (count < (length - refpos)); count ++) if (data[position + count] != current[count]) break;
     if (count > (length - refpos)) count = length - refpos;
     if (count > (length - position)) count = length - position;
--- a/tools/lz/output.c
+++ b/tools/lz/output.c
@@ -28,8 +28,16 @@
   if (fputs("\tlzend\n", fp) < 0) error_exit(1, "could not write terminator to compressed output");
   if (padding_size) {
     input_stream += padding_offset;
-    int rv = fprintf(fp, "\tdb $%02hhx", *(input_stream ++));
-    while ((rv >= 0) && (-- padding_size)) rv = fprintf(fp, ", $%02hhx", *(input_stream ++));
+    int rv = 0;
+    unsigned pos;
+    const char * prefix = "\tdb";
+    for (pos = 0; (rv >= 0) && (pos < padding_size); pos ++) {
+      if (input_stream[pos])
+        rv = fprintf(fp, "%s $%02hhx", prefix, input_stream[pos]);
+      else
+        rv = fprintf(fp, "%s 0", prefix);
+      prefix = ",";
+    }
     if (rv >= 0) rv = -(putc('\n', fp) == EOF);
     if (rv < 0) error_exit(1, "could not write padding to compressed output");
   }
--- a/tools/lz/uncomp.c
+++ b/tools/lz/uncomp.c
@@ -49,7 +49,7 @@
   }
   if (slack) *slack = *size - (rp - data);
   *size = current - result;
-  return realloc(result, *size * sizeof(struct command));
+  return realloc(result, (*size ? *size : 1) * sizeof(struct command));
   error:
   free(result);
   return NULL;
@@ -88,5 +88,5 @@
     }
   }
   *size = current - result;
-  return result;
+  return realloc(result, *size ? *size : 1);
 }