Răsfoiți Sursa

* Fixed a bug with code generation that caused invalid files to be output. Made similar changes to the decompilation phase.

* Fixed a potential segfault in stripExtension
Matthew Iselin 14 ani în urmă
părinte
comite
730d83c850
3 a modificat fișierele cu 17 adăugiri și 4 ștergeri
  1. 2 0
      src/main.cpp
  2. 11 4
      src/tibasic.cpp
  3. 4 0
      src/tibasic.h

+ 2 - 0
src/main.cpp

@@ -52,6 +52,8 @@ void log(LogSeverity severity, const char *out)
 
 void stripExtension(const char *in, char *out, size_t len)
 {
+    if(strrchr(in, '.') == NULL)
+        return;
     strncpy(out, in, len);
     *strrchr(out, '.') = 0;
 }

+ 11 - 4
src/tibasic.cpp

@@ -130,12 +130,17 @@ bool Compiler::compile(string inFile, string outFile)
     if(n == inFile.npos) n = outFile.find_last_of('\\');
     if(n == inFile.npos) n = 0; else n++;
     for(; (i < 8) && (n < inFile.length() - 4); n++)
+    {
+        if(outFile[n] == '.')
+            break;
         ventry.name[i++] = toupper(outFile[n]);
+    }
 
     // Begin writing to file.
     FILE *out = fopen(outFile.c_str(), "wb");
     fwrite(&phdr, sizeof(phdr), 1, out);
     fwrite(&ventry, sizeof(ventry), 1, out);
+    fwrite(&outputSize, 2, 1, out);
 
     // Sum of all bytes for checksum purposes.
     size_t sum = 0;
@@ -149,10 +154,9 @@ bool Compiler::compile(string inFile, string outFile)
     }
 
     // Perform a checksum and write to file.
-    sum += sumBytes(reinterpret_cast<const char*>(&phdr), sizeof(phdr));
     sum += sumBytes(reinterpret_cast<const char*>(&ventry), sizeof(ventry));
-    unsigned char checksum = doChecksum(sum);
-    fwrite(&checksum, 1, 1, out);
+    unsigned short checksum = doChecksum(sum);
+    fwrite(&checksum, 2, 1, out);
 
     fclose(out);
 
@@ -179,6 +183,9 @@ bool Compiler::decompile(string inFile, string outFile)
     struct VariableEntry ventry;
     fread(&ventry, sizeof(ventry), 1, fp);
 
+    size_t tokenLength = 0;
+    fread(&tokenLength, 2, 1, fp);
+
     size_t nBytesRead = 0;
     unsigned short temp;
     
@@ -186,7 +193,7 @@ bool Compiler::decompile(string inFile, string outFile)
 
     bool bAsmProgram = false;
 
-    while((!feof(fp)) && (nBytesRead < ventry.length2))
+    while((!feof(fp)) && (nBytesRead < tokenLength))
     {
         fread(&temp, 1, 2, fp);
 

+ 4 - 0
src/tibasic.h

@@ -88,6 +88,8 @@ class Compiler
         /// Perform a checksum over a region of data.
         size_t sumBytes(const char *data, size_t len);
         unsigned char doChecksum(size_t sum);
+
+#pragma pack(push, 1)
         
         /// 8xp file header
         struct ProgramHeader
@@ -109,6 +111,8 @@ class Compiler
             char flags;
             unsigned short length2;
         };
+
+#pragma pack(pop)
 };
 
 #endif