News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Easy program to copy only part of a file  (Read 5917 times)

Guadozoku

  • Sr. Member
  • ****
  • Posts: 257
    • View Profile
Easy program to copy only part of a file
« on: March 21, 2013, 11:45:47 pm »
I'm looking for an easy way to copy only part of a file (c004 to 2c004) to another file without doing it manually in a hex editor. Anyone know a program to do that? I tried Google, but couldn't find one.

BRPXQZME

  • Hero Member
  • *****
  • Posts: 4572
  • じー
    • View Profile
    • The BRPXQZME Network
Re: Easy program to copy only part of a file
« Reply #1 on: March 22, 2013, 03:59:05 am »
dd if=in.bin of=out.bin bs=1 count=$((0x20000)) skip=$((0xc004))

... or at least I think that’s right. You do use a Unix, right? Or is that just my memory playing tricks again?
we are in a horrible and deadly danger

FAST6191

  • Hero Member
  • *****
  • Posts: 2622
    • View Profile
Re: Easy program to copy only part of a file
« Reply #2 on: March 22, 2013, 07:17:34 am »
Does it have to be an arbitrary part of another file or basically slice this section out and stick in a new file?
BRPXQZME took the unix a like method and I will assume no powershell (not that I can think how to do it there at this point, primarily as I do not know it that well) or cygwin in the case of windows. If you want windows I would suggest using a file trimmer instead. If you do need an arbitrary insertion you will probably have to do something like trim the first part of the file, the file after what you are replacing and do something like "copy /b firstpart.file+newfragment.file+restoffile.file newshinyfile.file".
My command line file trimmer of choice these days is filecutter
http://crackerscrap.com/projects.php has it. Syntax is filecutter file.in length file.out <-s start>
0x is the indicator for hex and it otherwise accepts decimal.

Bisqwit

  • Sr. Member
  • ****
  • Posts: 369
  • Polite, but politically incorrect.
    • View Profile
    • http://iki.fi/bisqwit/
Re: Easy program to copy only part of a file
« Reply #3 on: March 24, 2013, 04:44:08 am »
Or you might use Python or PHP or some other scripting language to do it.
For example, a complete PHP script to do it:

<?php
$fp = fopen('inputfile.bin', 'r');
fseek($fp, 16); // Source file position
file_put_contents('outputfile.bin', // Output file name
  fread($fp, 0x20000)); // Number of bytes to copy

BlackDog61

  • Hero Member
  • *****
  • Posts: 784
    • View Profile
    • Super Robot Wars A Portable translation thread
Re: Easy program to copy only part of a file
« Reply #4 on: January 12, 2014, 08:38:20 am »
(For the record)
Or you could use Swiss Free Knife
http://sourceforge.net/projects/swissfileknife/

With a command line like this (on windows, just remove .exe in *nux):
sfk169.exe partcopy myrom.gba 1E83E3 1000 extractedfile.bin

where 1E83E3 = starting offset and 1000=size in decimal

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Easy program to copy only part of a file
« Reply #5 on: January 17, 2014, 05:28:04 pm »
you do you mean when you say doing it manually?
go r2, use debug. .... White hand was fainted

Zoinkity

  • Hero Member
  • *****
  • Posts: 562
    • View Profile
Re: Easy program to copy only part of a file
« Reply #6 on: January 18, 2014, 02:27:47 pm »
Python would be along the lines of:
Code: [Select]
with open("filename.ext", 'rb') as f:
f.seek(offset)
data = f.read(size)
with open("output.ext", 'wb') as f:
f.write(data)
Still think it's faster to just open a hex editor, but if you need to do it programmicaly then it's all cool.

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Easy program to copy only part of a file
« Reply #7 on: January 22, 2014, 05:47:54 pm »
what's wrong wtih using hex-editors for this?
go r2, use debug. .... White hand was fainted

BRPXQZME

  • Hero Member
  • *****
  • Posts: 4572
  • じー
    • View Profile
    • The BRPXQZME Network
Re: Easy program to copy only part of a file
« Reply #8 on: January 22, 2014, 05:54:24 pm »
I’d say it depends on how many times and exactly the way you have to do it, really. If it’s going into a makefile, for instance, having the command would be better. Not that you can’t Sikuli up a hex editor, but surely that’s not the way to go!
we are in a horrible and deadly danger

FAST6191

  • Hero Member
  • *****
  • Posts: 2622
    • View Profile
Re: Easy program to copy only part of a file
« Reply #9 on: January 22, 2014, 06:47:00 pm »
I use it when I am making extraction files that slice up archive files for various people and myself for that matter. If you are staring at an archive containing several hundred files it considerably less tedious and error prone than referencing a file location/size list, possibly doing some maths on it and then selecting it or telling my hex editor to select a block before doing the rest, even if not it is still easier than holding someone's hand through using a hex editor (for some reason a lot of people treat them as magic tools with the power to end the universe or something).

Granted for not a lot more effort I could probably have a python script or something do the same, indeed I probably should get around to making some program fragments aimed at such a thing, and probably even get myself halfway to a repacking/rebuilding tool but eh.

In short, we see an awful lot of requests of dubious merit around here but this is not one of them.

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Easy program to copy only part of a file
« Reply #10 on: January 22, 2014, 07:25:01 pm »
you might have noticed, that I really like r2. Here's how I would do it:

open src-file with r2
s [src-offset]
wt foo [number of bytes to copy]
quit r2
open r2 with -w
s [dst-offset]
wf foo
quit r2

done
go r2, use debug. .... White hand was fainted

FAST6191

  • Hero Member
  • *****
  • Posts: 2622
    • View Profile
Re: Easy program to copy only part of a file
« Reply #11 on: January 22, 2014, 08:12:58 pm »
r2= https://github.com/radare/radare2 ?

I had never heard of it, I will look further though as it could be useful to have such a thing to point people at and it has a nicer license than ROMulan which was my previous "not quite a full language" tool of choice.

cret

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Easy program to copy only part of a file
« Reply #12 on: January 23, 2014, 08:57:58 am »
yup, if you install radare2, r2 is set as an alias for radare2
go r2, use debug. .... White hand was fainted

assassin

  • Full Member
  • ***
  • Posts: 142
    • View Profile
    • My Barren Webpage
Re: Easy program to copy only part of a file
« Reply #13 on: January 24, 2014, 03:50:40 am »
dd for DOS:
http://www.ibiblio.org/pub/micro/pc-stuff/freedos/gnuish/gnufut21.zip

John Fine's partcopy:
http://geezer.osdevbrasil.net/johnfine/
NOTE: this program can also write to raw sectors on the drive and such, so using the wrong parameters could be lethal!  (under DOS anyway, Windows NT+ will probably prevent the raw writes.)

both of these are dated, pure DOS programs, so they won't understand long filenames.