aboutsummaryrefslogtreecommitdiff
path: root/cmd/extfs-pdf/main.go
blob: 74f8433968b5130fd7ea19542232e2b7ec738a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

// extfs-pdf is an external VFS plugin for Midnight Commander.
// More serious image extractors should rewrite this to use pdfimages(1).
package main

import (
	"flag"
	"fmt"
	"os"

	"janouch.name/pdf-simple-sign/pdf"
)

func die(status int, format string, args ...interface{}) {
	os.Stderr.WriteString(fmt.Sprintf(format+"\n", args...))
	os.Exit(status)
}

func usage() {
	die(1, "Usage: %s [-h] COMMAND DOCUMENT [ARG...]", os.Args[0])
}

func streamSuffix(o *pdf.Object) string {
	if filter, _ := o.Dict["Filter"]; filter.Kind == pdf.Name {
		switch filter.String {
		case "JBIG2Decode":
			// This is the file extension used by pdfimages(1).
			// This is not a complete JBIG2 standalone file.
			return "jb2e"
		case "JPXDecode":
			return "jp2"
		case "DCTDecode":
			return "jpg"
		default:
			return filter.String
		}
	}
	return "stream"
}

func list(updater *pdf.Updater) {
	for _, o := range updater.ListIndirect() {
		object, err := updater.Get(o.N, o.Generation)
		size := 0
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", err)
		} else {
			// Accidental transformation, retrieving original data is more work.
			size = len(object.Serialize())
		}
		fmt.Printf("-r--r--r-- 1 0 0 %d 01-01-1970 00:00 %d-%d\n",
			size, o.N, o.Generation)
		if object.Kind == pdf.Stream {
			fmt.Printf("-r--r--r-- 1 0 0 %d 01-01-1970 00:00 %d-%d.%s\n",
				len(object.Stream), o.N, o.Generation, streamSuffix(&object))
		}
	}
}

func copyout(updater *pdf.Updater, storedFilename, extractTo string) {
	var (
		n, generation uint
		suffix        string
	)
	m, err := fmt.Sscanf(storedFilename, "%d-%d%s", &n, &generation, &suffix)
	if m < 2 {
		die(3, "%s: %s", storedFilename, err)
	}

	object, err := updater.Get(n, generation)
	if err != nil {
		die(3, "%s: %s", storedFilename, err)
	}

	content := []byte(object.Serialize())
	if suffix != "" {
		content = object.Stream
	}
	if err = os.WriteFile(extractTo, content, 0666); err != nil {
		die(3, "%s", err)
	}
}

func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 2 {
		usage()
	}

	command, documentPath := flag.Arg(0), flag.Arg(1)
	doc, err := os.ReadFile(documentPath)
	if err != nil {
		die(1, "%s", err)
	}

	updater, err := pdf.NewUpdater(doc)
	if err != nil {
		die(2, "%s", err)
	}

	switch command {
	default:
		die(1, "unsupported command: %s", command)
	case "list":
		if flag.NArg() != 2 {
			usage()
		} else {
			list(updater)
		}
	case "copyout":
		if flag.NArg() != 4 {
			usage()
		} else {
			copyout(updater, flag.Arg(2), flag.Arg(3))
		}
	}
}