Ejemplo n.º 1
0
Archivo: C4Log.cpp Proyecto: ev1313/yaC
bool GetLogSection(size_t iStart, size_t iLength, StdStrBuf &rsOut) {
  if (!iLength) {
    rsOut.Clear();
    return true;
  }
  // read section from log file
  CStdFile LogFileRead;
  char *szBuf, *szBufOrig;
  size_t iSize;  // size exclusing terminator
  if (!LogFileRead.Load(sLogFileName.getData(), (BYTE **)&szBuf, (int *)&iSize,
                        1))
    return false;
  szBufOrig = szBuf;
  // reduce to desired buffer section
  if (iStart > iSize) iStart = iSize;
  if (iStart + iLength > iSize) iLength = iSize - iStart;
  szBuf += iStart;
  szBuf[iLength] = '\0';
  // strip timestamps; convert linebreaks to Clonk-linebreaks '|'
  char *szPosWrite = szBuf;
  const char *szPosRead = szBuf;
  while (*szPosRead) {
    // skip timestamp
    if (*szPosRead == '[')
      while (*szPosRead && *szPosRead != ']') {
        --iSize;
        ++szPosRead;
      }
    // skip whitespace behind timestamp
    if (!*szPosRead) break;
    szPosRead++;
    // copy data until linebreak
    size_t iLen = 0;
    while (*szPosRead && *szPosRead != 0x0d && *szPosRead != 0x0a) {
      ++szPosRead;
      ++iLen;
    }
    if (iLen && szPosRead - iLen != szPosWrite)
      memmove(szPosWrite, szPosRead - iLen, iLen);
    szPosWrite += iLen;
    // skip additional linebreaks
    while (*szPosRead == 0x0d || *szPosRead == 0x0a) ++szPosRead;
    // write a Clonk-linebreak
    if (*szPosRead) *szPosWrite++ = '|';
  }
  // done; create string buffer from data
  rsOut.Copy(szBuf, szPosWrite - szBuf);
  // old buf no longer used
  delete[] szBufOrig;
  // done, success
  return true;
}
Ejemplo n.º 2
0
BOOL C4UpdatePackage::MakeUpdate(const char *strFile1, const char *strFile2,
                                 const char *strUpdateFile,
                                 const char *strName) {
#ifdef UPDATE_DEBUG
    char *pData;
    int iSize;
    CStdFile MyFile;
    MyFile.Load(strFile2, (BYTE **)&pData, &iSize, 0, TRUE);
    MyFile.Create("SoIstRichtig.txt", FALSE);
    MyFile.Write(pData, iSize, FALSE);
    MyFile.Close();
    MemScramble((BYTE *)pData, iSize);
    MyFile.Create("UndSoAuch.txt", FALSE);
    MyFile.Write(pData, iSize, FALSE);
    MyFile.Close();
#endif

    // open Log
    if (!Log.Create("Update.log")) return FALSE;

    // begin message
    WriteLog("Source: %s\nTarget: %s\nOutput: %s\n\n", strFile1, strFile2,
             strUpdateFile);

    // open both groups
    C4Group Group1, Group2;
    if (!Group1.Open(strFile1)) {
        WriteLog("Error: could not open %s!\n", strFile1);
        return FALSE;
    }
    if (!Group2.Open(strFile2)) {
        WriteLog("Error: could not open %s!\n", strFile2);
        return FALSE;
    }

    // All groups to be compared need to be packed
    if (!Group1.IsPacked()) {
        WriteLog("Error: source group %s not packed!\n", strFile1);
        return FALSE;
    }
    if (!Group2.IsPacked()) {
        WriteLog("Error: target group %s not packed!\n", strFile2);
        return FALSE;
    }
    if (Group1.HasPackedMother()) {
        WriteLog("Error: source group %s must not have a packed mother group!\n",
                 strFile1);
        return FALSE;
    }
    if (Group2.HasPackedMother()) {
        WriteLog("Error: target group %s must not have a packed mother group!\n",
                 strFile2);
        return FALSE;
    }

    // create/open update-group
    C4GroupEx UpGroup;
    if (!UpGroup.Open(strUpdateFile, TRUE)) {
        WriteLog("Error: could not open %s!\n", strUpdateFile);
        return FALSE;
    }

    // may be continued update-file -> try to load core
    UpGrpCnt = 0;
    BOOL fContinued = C4UpdatePackageCore::Load(UpGroup);

    // save crc2 for later check
    unsigned int iOldChks2 = GrpChks2;

    // create core info
    if (strName)
        SCopy(strName, Name, C4MaxName);
    else
        sprintf(Name, "%s Update", GetFilename(strFile1));
    SCopy(strFile1, DestPath, _MAX_PATH);
    GrpUpdate = TRUE;
    if (!C4Group_GetFileCRC(strFile1, &GrpChks1[UpGrpCnt])) {
        WriteLog("Error: could not calc checksum for %s!\n", strFile1);
        return FALSE;
    }
    if (!C4Group_GetFileCRC(strFile2, &GrpChks2)) {
        WriteLog("Error: could not calc checksum for %s!\n", strFile2);
        return FALSE;
    }
    if (fContinued) {
        // continuation check: GrpChks2 matches?
        if (GrpChks2 != iOldChks2)
            // that would mess up the update result...
        {
            WriteLog(
                "Error: could not add to update package - target groups don't match "
                "(checksum error)\n");
            return FALSE;
        }
        // already supported by this update?
        int i = 0;
        for (; i < UpGrpCnt; i++)
            if (GrpChks1[UpGrpCnt] == GrpChks1[i]) break;
        if (i < UpGrpCnt) {
            WriteLog(
                "This update already supports the version of the source file.\n");
            return FALSE;
        }
    }

    UpGrpCnt++;

    // save core
    if (!C4UpdatePackageCore::Save(UpGroup)) {
        WriteLog("Could not save update package core!\n");
        return FALSE;
    }

    // compare groups, create update
    BOOL fModified = FALSE;
    BOOL fSuccess = MkUp(&Group1, &Group2, &UpGroup, &fModified);
    // close (save) it
    UpGroup.Close(FALSE);
    // error?
    if (!fSuccess) {
        WriteLog("Update package not created.\n");
        remove(strUpdateFile);
        return FALSE;
    }

    WriteLog("Update package created.\n");
    return TRUE;
}
Ejemplo n.º 3
0
BOOL C4UpdatePackage::Execute(C4Group *pGroup) {
    // search target
    C4GroupEx TargetGrp;
    char strTarget[_MAX_PATH];
    SCopy(DestPath, strTarget, _MAX_PATH);
    char *p = strTarget, *lp = strTarget;
    while (p = strchr(p + 1, '\\')) {
        *p = 0;
        if (!*(p + 1)) break;
        if (!SEqual(lp, ".."))
            if (TargetGrp.Open(strTarget)) {
                // packed?
                bool fPacked = TargetGrp.IsPacked();
                // maker check (someone might try to unpack directories w/o asking user)
                if (fPacked)
                    if (!SEqual(TargetGrp.GetMaker(), pGroup->GetMaker())) return FALSE;
                // Close Group
                TargetGrp.Close(TRUE);
                if (fPacked)
                    // Unpack
                    C4Group_UnpackDirectory(strTarget);
            } else {
                // GrpUpdate -> file must exist
                if (GrpUpdate) return FALSE;
                // create dir
                CreateDirectory(strTarget, NULL);
            }
        *p = '\\';
        lp = p + 1;
    }

    // try to open it
    if (!TargetGrp.Open(strTarget, !GrpUpdate)) return FALSE;

    // check if the update is allowed
    if (GrpUpdate) {
        // maker must match
        /*if(!SEqual(TargetGrp.GetMaker(), pGroup->GetMaker())) - now allowing
           updates from different makers
                return FALSE;*/
        // check checksum
        uint32_t iCRC32;
        if (!C4Group_GetFileCRC(TargetGrp.GetFullName().getData(), &iCRC32))
            return FALSE;
        int i = 0;
        for (; i < UpGrpCnt; i++)
            if (iCRC32 == GrpChks1[i]) break;
        if (i >= UpGrpCnt) return FALSE;
    } else {
        // only allow Extra.c4g-Updates
        if (!SEqual2(DestPath, "Extra.c4g")) return FALSE;
    }

    // update children
    char ItemFileName[_MAX_PATH];
    pGroup->ResetSearch();
    while (pGroup->FindNextEntry("*", ItemFileName))
        if (!SEqual(ItemFileName, C4CFN_UpdateCore) &&
                !SEqual(ItemFileName, C4CFN_UpdateEntries))
            DoUpdate(pGroup, &TargetGrp, ItemFileName);

    // do GrpUpdate
    if (GrpUpdate) DoGrpUpdate(pGroup, &TargetGrp);

    // close the group
    TargetGrp.Close(FALSE);

    if (GrpUpdate) {
        // check the result
        uint32_t iResChks;
        if (!C4Group_GetFileCRC(strTarget, &iResChks)) return FALSE;
        if (iResChks != GrpChks2) {
#ifdef UPDATE_DEBUG
            char *pData;
            int iSize;
            CStdFile MyFile;
            MyFile.Load(strTarget, (BYTE **)&pData, &iSize, 0, TRUE);
            MyFile.Create("DiesesDingIstMist.txt", FALSE);
            MyFile.Write(pData, iSize, FALSE);
            MyFile.Close();
#endif
            return FALSE;
        }
    }

    return TRUE;
}