Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelsc committed Jun 10, 2020
1 parent a2b94dc commit 4ad16e9
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
out/
HDDFsDumper.iml
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Manuel Caspari
Copyright (c) 2020-2020 Manuel S. Caspari

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# HDDFs 00.07 MPG Dumper

[![HDDFs MPG Dumper](assets/screenshot_01.png)

## What is HDDFs
HDDFs is a file system that shipped with some VCR receivers. There's no real documentation
on how it operates which is why I created this little tool. It allows you
to dump your VHS converted videos from the HDD.

## Usage
First create an image from your HDD with a tool like dd.

Then extract your MPGs by running
```
java -jar hddfsdump.jar --input ./hddfs.bin --output ./mpg-out/
```

## Dependencies:
* [Clikt](https://github.com/ajalt/clikt) Copyright 2018-2020 AJ Alt, Apache License Version 2.0
* [Progressbar](https://github.com/ctongfei/progressbar) Copyright 2015-2020 Tongfei Chen, The MIT License

# Thanks to
Thanks to the guys over at avsforum who dealt with the same issue around 10 years ago.
Especially to PeterTheGeek for documenting his findings on HDDFs
https://www.avsforum.com/forum/106-dvd-recorders-standard-def/1277209-hard-disk-file-system-investigation-magnavox-537-535-533-515-513-2160a-2160-2080-philips-3576-3575-a.html

## Donations / Support
Ethereum: 0xa9981a33f6b1A18da5Db58148B2357f22B44e1e0
Bitcoin: 1Ju9G5U4iwJekRvUnG1JDme11vjxJ889Ux

## Licence
The MIT License (MIT)

Copyright 2020-2020 Manuel S. Caspari

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added assets/screenshot_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: MainKt

85 changes: 85 additions & 0 deletions src/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
import fs.HDDFsImage
import logger.ConsoleLogger
import utils.blocksToMpgSize
import utils.format
import java.io.File
import kotlin.system.exitProcess

const val VERSION = 0.1

@Suppress("SpellCheckingInspection")
class Main : CliktCommand(printHelpOnEmptyArgs = true) {

private val input: File by option("-i", "--input", envvar = "HDDFS_DUMP_IN", help = "HDDFs Image").file(
mustExist = true,
canBeFile = true,
canBeDir = false
).required()

private val output: File by option("-o", "--output", envvar = "HDDFS_DUMP_OUT", help = "MPG output directory").file(
mustExist = false,
canBeFile = false,
canBeDir = true
).required()

override fun run() {
intro()

val pathToFile = input.toString()
val outPath = output.toString()

val hddfs = loadHDDFSImage(pathToFile)
checkValidHDDFs(hddfs)

val mpgPosition = hddfs.findMpgSectionPosition().also {
println("MPG sector found at: ${it.first}, length: ${it.second} ")
}

val mpgBlocks = getMPGBlockCount(hddfs, mpgPosition)

println("Extracting to \"$outPath\"...")
hddfs.extractAllMpgs(outPath, mpgPosition, ConsoleLogger(mpgBlocks))
}

private fun intro() {
println("\nHDDFs MPG Dumper v$VERSION by Manuel S. Caspari, June 2020")
println("Bitcoin: 1Ju9G5U4iwJekRvUnG1JDme11vjxJ889Ux")
println("Ethereum: 0xa9981a33f6b1A18da5Db58148B2357f22B44e1e0")
println("Tip me a beer if you like this tool :)\n")
println("Licences: ")
println("- Clikt, Copyright 2018-2020 AJ Alt, Apache License Version 2.0 (https://github.com/ajalt/clikt)")
println("- Progressbar, Copyright 2015-2020 Tongfei Chen, The MIT License (https://github.com/ctongfei/progressbar)\n")
}

private fun getMPGBlockCount(hddfs: HDDFsImage, mpgPosition: Pair<UInt, UInt>): Long {
print("Analysing MPG sector (this might take a while)... ")
val mpgBlocks = hddfs.getMpgBlockCount(mpgPosition)
val mpgSize = mpgBlocks.blocksToMpgSize
println("OK")
println("Found ${mpgSize.toULong().format()} bytes of MPG data (Blocks: $mpgBlocks)\n")
return mpgBlocks
}

private fun loadHDDFSImage(pathToFile: String): HDDFsImage {
print("Reading \"$pathToFile\"... ")
return HDDFsImage(pathToFile).also {
println("OK")
}
}

private fun checkValidHDDFs(hddfs: HDDFsImage) {
print("Check for HDDFs file system... ")
if (!hddfs.isValidHDDFs()) {
println("NOK")
println("No valid HDDFs Image found")
exitProcess(-1)
}
println("OK")
}
}

fun main(args: Array<String>) = Main().main(args)
15 changes: 15 additions & 0 deletions src/fs/FileSeeker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fs

import java.io.RandomAccessFile

open class FileSeeker(pathToFile: String){

private val raf = RandomAccessFile(pathToFile, "r")

fun readBytes(start: Long, length: Int): ByteArray {
raf.seek(start)
val content = ByteArray(length)
raf.readFully(content)
return content
}
}
48 changes: 48 additions & 0 deletions src/fs/HDDFs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fs

import utils.sectorAsPosition

object HDDFs {

/** MPG Allocation Block Size (MPGs are stored in ~8MB Blocks) **/
const val MPG_ALLOC_BLOCK_SECTORS = 16384L

/** Same as MPG_ALLOC_BLOCK_SECTORS only in bytes instead of sectors. ~8MB **/
val MPG_ALLOC_BLOCK_SIZE = MPG_ALLOC_BLOCK_SECTORS.sectorAsPosition

/** Contains the HDDFs signature that is found in SYSCTR **/
val HDDFS_SIGNATURE = byteArrayOf(0x48, 0x44, 0x44, 0x46, 0x73, 0x20, 0x30, 0x30, 0x2E, 0x30, 0x37)

/** Offset from SYSCTR beginning until signature begins **/
const val HDDFS_SIGNATURE_OFFSET = 0x0F

/** HDD Sector size in bytes **/
const val HDD_SECTOR_SIZE = 512

/** This signature starts after the MPG Signature (00 00 01 BA) and signals a new file start **/
val MPG_NEW_SIGNATURE = byteArrayOf(0x44, 0x00, 0x04, 0x00, 0x04, 0x01)

/** Empty array, used to check whether we reached end of MPG data **/
val EMPTY = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

/** AV001_0AV001_00.MPG, search for this file in the FB000 Section **/
val MPG_FILE_NAME = byteArrayOf(
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x41, 0x56, 0x30,
0x30, 0x31, 0x5F, 0x30, 0x41, 0x56, 0x30, 0x30, 0x31, 0x5F, 0x30,
0x30, 0x2E, 0x4D, 0x50, 0x47
)

/** AV001_0AV001_00.MPG, offset from the MPG location to its name (name comes after location) **/
const val MPG_LOCATION_OFFSET_TO_NAME = 0x0F

/** Specifies the SYSCTR Sector
* Start sector: 2, length: 2
* This sector contains the HDDFs Signature **/
val SYSCTR = 2L.sectorAsPosition to 2L.sectorAsPosition // Pair(start, length)

/** Specifies the File Map Sector
* Start sector: 4, length: 3
* This sectors holds the file map **/
val FB000 = 4L.sectorAsPosition to 3L.sectorAsPosition // Pair(start, length)

}
92 changes: 92 additions & 0 deletions src/fs/HDDFsImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package fs

import logger.ProgressLogger
import utils.sectorAsPosition
import utils.toUInt32

@Suppress("MemberVisibilityCanBePrivate")
class HDDFsImage(pathToFile: String) : FileSeeker(pathToFile) {

fun getSignatureSector() = readBytes(HDDFs.SYSCTR.first, HDDFs.SYSCTR.second.toInt())

fun getFileMap() = readBytes(HDDFs.FB000.first, HDDFs.FB000.second.toInt())

/**
* Checks the SYSCTR Sector of the HDDFs image and validates it's signature
* @return true if we found a valid HDDFs file system, otherwise false
*/
fun isValidHDDFs(): Boolean {
val signatureSector = getSignatureSector()
for (i in 0 until HDDFs.HDDFS_SIGNATURE.size) {
if (signatureSector[HDDFs.HDDFS_SIGNATURE_OFFSET + 1 + i] != HDDFs.HDDFS_SIGNATURE[i]) return false
}
return true
}

/**
* Tries to locate the MPG file name in the FB000 Sector and returns the position
* of the MPG Sector.
* @return Pair containing the starting position (first) and length (second) of the MPG Sector
*/
fun findMpgSectionPosition(): Pair<UInt, UInt> {
val fileMap = getFileMap()
val rawRelativePosition =
findAndReturnPosition(fileMap, HDDFs.MPG_FILE_NAME)
check(rawRelativePosition != -1) { "Could not locate MPG position" }

val absolutePosition = rawRelativePosition - HDDFs.MPG_LOCATION_OFFSET_TO_NAME + HDDFs.FB000.first - 1
val mpgStart = readBytes(absolutePosition, 4).toUInt32()
val mpgLength = readBytes(absolutePosition + 4, 4).toUInt32()

return Pair(mpgStart, mpgLength)
}

/**
* Skipping through MPG_ALLOC_BLOCKS and see if we read empty (zero) bytes
* @param position sector position + length of MPG sector (can be retrieved by calling [findMpgSectionPosition])
* @return last block number with non empty data
*/
fun getMpgBlockCount(position: Pair<UInt, UInt>): Long {
for (i in 0 until position.second.toInt() / HDDFs.MPG_ALLOC_BLOCK_SECTORS) {
val absolutePosition = (position.first.toLong() + (i * HDDFs.MPG_ALLOC_BLOCK_SECTORS)).sectorAsPosition
val chunk = readBytes(absolutePosition, HDDFs.EMPTY.size)
if (HDDFs.EMPTY.findIn(chunk, 0)) return (i - 1L)
}
return position.second.toLong()
}

/**
* Extracts all mpgs found in the MPG Sector
* @param outPath Directory path on where to store output
* @param position sector position + length of MPG sector (can be retrieved with [findMpgSectionPosition])
* @param logger used only for progress bar, can be retrieved by calling [getMpgBlockCount]
*/
fun extractAllMpgs(outPath: String, position: Pair<UInt, UInt>, logger: ProgressLogger) {
MpgScratcher(this, position, logger).extractTo(outPath)
}

companion object {
fun findAndReturnPosition(byteArray: ByteArray, lookFor: ByteArray): Int {
for (i in 0 until byteArray.size) {
if (lookFor.findIn(byteArray, i)) {
return i
}
}
return -1
}

/**
* Pass byte array you are looking for as this and use
* @param bytes to pass source array and
* @param position at its current position
* @return true if this was found in bytes at position, false if not
*/
fun ByteArray.findIn(bytes: ByteArray, position: Int): Boolean {
this.forEachIndexed { index, value ->
if (index + position >= bytes.size || value != bytes[position + index]) return false
}
return true
}
}

}
71 changes: 71 additions & 0 deletions src/fs/MpgScratcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fs

import fs.HDDFsImage.Companion.findIn
import logger.ProgressLogger
import utils.sectorAsPosition
import java.io.File
import java.io.FileOutputStream

@Suppress("ConstantConditionIf", "SpellCheckingInspection")
class MpgScratcher(
private val source: HDDFsImage,
private val position: Pair<UInt, UInt>,
private val logger: ProgressLogger
) {

fun extractTo(outPath: String) {
val outputPath = File(outPath)
if (!outputPath.exists()) outputPath.mkdirs()

exportAll(outPath, 0, 1)
}

private fun exportAll(outPath: String, offset: Int, fileOffset: Int) {
val nextOffset = write("$outPath/$fileOffset.mpg", offset)
if (nextOffset == COMPLETE) {
logger.complete()
return
}
exportAll(outPath, nextOffset, fileOffset + 1)
}

private fun write(outFile: String, offset: Int): Int {
val file = File(outFile)
var fos: FileOutputStream? = null

try {
fos = FileOutputStream(file)

var i = offset
while (i in offset until position.second.toInt() / HDDFs.MPG_ALLOC_BLOCK_SECTORS) {
logger.progress(i.toLong(), file.name)

val absolutePosition = (position.first.toLong() + (i * HDDFs.MPG_ALLOC_BLOCK_SECTORS)).sectorAsPosition
val chunk = source.readBytes(absolutePosition, HDDFs.MPG_ALLOC_BLOCK_SIZE.toInt())

// Reached end of mpg
if (HDDFs.EMPTY.findIn(chunk, 0)) {
i = COMPLETE
break
}

// Reached end of file, write next one
if (i != offset && HDDFs.MPG_NEW_SIGNATURE.findIn(chunk, 4)) {
break
}

fos.write(chunk)
i++
}

fos.flush()
return i
} finally {
fos?.close()
}
}

companion object {
const val COMPLETE = -1
}
}
Loading

0 comments on commit 4ad16e9

Please sign in to comment.