代码之家  ›  专栏  ›  技术社区  ›  montooner

randomaccessfile.write不写我告诉它的内容

  •  0
  • montooner  · 技术社区  · 15 年前

    嘿,大家好。我正在从一个SQL格式文件读取到另一个,中间的两个字节正在被破坏,我认为这是我没有做的一些准备或保护。

    损坏数据示例:

    //From the file that is read from. added ** to emphasize the corrupted byte
    insert into viruses (virusSig,virusHash) values (
    X'579fdc569b170419e15750f0feb360aa9c58d8**90**eede50def97ee7cb03b9e905',
    X'ee002fe5');
    
    //From the file that is written to. added ** to emphasize the corrupted byte
    insert into changes (filepath,loc,dat,vir,hash) values (
    'E:\MyDocs\intel\antivirus\RandomFiles\0\2\5\11\24\49\EG1AxxeJSr.data',
    243540,
    X'9f4246ff8c73c5a5b470cab8c38416929c4eacc1e0021d5ac1fdbb88145d3e6f',
    X'579fdc569b170419e15750f0feb360aa9c58d8**3f**eede50def97ee7cb03b9e905',
    X'6546dd27');
    

    读取/写入的代码:

    public static void insertViruses(FileLocation[] locations, byte[][] viruses, String logpath)
    {
        int numViruses = viruses.length;
        int virusLength = GenerateRandomCorpus.virusSignatureLengthInBytes;
    
        try{
    
    
            for (int i = 0; i < numViruses; i++)
            {   
                FileOutputStream logwriter = new FileOutputStream(logpath, true);
    
                // Prep to copy section
                int locationOfChange = locations[i].index;
                String filepathToChange = locations[i].filepath;
                File checkIfBackupExists = new File(filepathToChange + ".bak");
                if (!checkIfBackupExists.exists())
                    copyFile(filepathToChange, filepathToChange + ".bak");
                copyFile(filepathToChange, filepathToChange + ".tmp");
    
                RandomAccessFile x = new RandomAccessFile(filepathToChange, "rw");
                x.seek(locationOfChange);
    
                // Copy section into byte array to write in log
                byte[] removedSection = new byte[virusLength];
                x.read(removedSection, 0, virusLength);
                if (GenerateRandomCorpus.dbg)
                    System.out.println(filepathToChange + ":" + locationOfChange);
                x.close();
    
                // Write changes to log
                byte[] removedSectionConvertedToHexString = StringUtils.getHexString(removedSection).getBytes();
                byte[] virusConvertedToHexString = StringUtils.getHexString(viruses[i]).getBytes();
                byte[] hashConvertedToHexString = StringUtils.getHexString(GenerateRandomViruses.intToByteArray(new String(viruses[i]).hashCode())).getBytes();
                System.out.println(StringUtils.getHexString(removedSection));
                System.out.println(StringUtils.getHexString(viruses[i]));
                logwriter.write(String.format("insert into changes (filepath,loc,dat,vir,hash) values " +
                        "('%s',%d,X'", filepathToChange, locationOfChange).getBytes());
                logwriter.write(removedSectionConvertedToHexString);
                logwriter.write("',X'".getBytes());
                logwriter.write(virusConvertedToHexString);
                logwriter.write("',X'".getBytes());
                logwriter.write(hashConvertedToHexString);
                logwriter.write("');\n".getBytes());
    
                // Insert virus into file
                File original = new File(filepathToChange);
                original.delete();
                RandomAccessFile fileToInsertIn = new RandomAccessFile(filepathToChange + ".tmp", "rw");
                fileToInsertIn.seek(locationOfChange);
                fileToInsertIn.write(viruses[i]);
                fileToInsertIn.close();
    
                File a = new File(filepathToChange + ".tmp");
                original = new File(filepathToChange);
                a.renameTo(original);
                a.delete();
    
                logwriter.close();
            }
    
    
        } catch (Exception e)
        {   
            System.err.println(e.toString());
            System.err.println("Error: InsertVirusesIntoCorpus, line 100");
        }
    }
    

    有什么想法吗?

    1 回复  |  直到 15 年前
        1
  •  0
  •   Stu Thompson Helter Scelter    15 年前

    你的代码让我有点困惑,为什么要进行这么多的转换,但我要说……

    我的直觉告诉我,字符集转换是无意的,或者是由于原始字节、Java字节基元和Java int基元之间的移动。记住Java byte 值范围在-127和128之间,该字符串的.getbytes()支持字符编码方案。

    具体来说,我觉得这很奇怪:

    byte[] virusConvertedToHexString = StringUtils.getHexString(viruses[i]).getBytes();
    

    这就是发生的事情:

    1. viruses[i] 给你一个 字节 数组
    2. StringUtils.getHexString() 接受该字节数组,并给出它的十六进制表示形式 字节 数组作为 String (假设:这是什么? StringUtils ?好像不是从 [org.apache.commons.lang][1] )
    3. 最后,您要存储 字节 阵列进入 virusConvertedToHexString

    第二步是我怀疑有麻烦。

    此外,上面的代码块不包括生成的代码:

    //From the file that is read from. added ** to emphasize the corrupted byte
    insert into viruses (virusSig,virusHash) values (
    X'579fdc569b170419e15750f0feb360aa9c58d8**90**eede50def97ee7cb03b9e905',
    X'ee002fe5');
    

    这会有帮助的。

    推荐文章