2022-01-23 13:41:31 +00:00
|
|
|
/*
|
|
|
|
exif-terminator
|
|
|
|
Copyright (C) 2022 SuperSeriousBusiness admin@gotosocial.org
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package terminator
|
|
|
|
|
|
|
|
import (
|
2022-06-23 13:38:19 +00:00
|
|
|
"bytes"
|
2022-01-23 13:41:31 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-06-23 13:38:19 +00:00
|
|
|
exif "github.com/dsoprea/go-exif/v3"
|
2022-03-21 18:46:51 +00:00
|
|
|
jpegstructure "github.com/superseriousbusiness/go-jpeg-image-structure/v2"
|
2022-01-23 13:41:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var markerLen = map[byte]int{
|
|
|
|
0x00: 0,
|
|
|
|
0x01: 0,
|
|
|
|
0xd0: 0,
|
|
|
|
0xd1: 0,
|
|
|
|
0xd2: 0,
|
|
|
|
0xd3: 0,
|
|
|
|
0xd4: 0,
|
|
|
|
0xd5: 0,
|
|
|
|
0xd6: 0,
|
|
|
|
0xd7: 0,
|
|
|
|
0xd8: 0,
|
|
|
|
0xd9: 0,
|
|
|
|
0xda: 0,
|
|
|
|
|
|
|
|
// J2C
|
|
|
|
0x30: 0,
|
|
|
|
0x31: 0,
|
|
|
|
0x32: 0,
|
|
|
|
0x33: 0,
|
|
|
|
0x34: 0,
|
|
|
|
0x35: 0,
|
|
|
|
0x36: 0,
|
|
|
|
0x37: 0,
|
|
|
|
0x38: 0,
|
|
|
|
0x39: 0,
|
|
|
|
0x3a: 0,
|
|
|
|
0x3b: 0,
|
|
|
|
0x3c: 0,
|
|
|
|
0x3d: 0,
|
|
|
|
0x3e: 0,
|
|
|
|
0x3f: 0,
|
|
|
|
0x4f: 0,
|
|
|
|
0x92: 0,
|
|
|
|
0x93: 0,
|
|
|
|
|
|
|
|
// J2C extensions
|
|
|
|
0x74: 4,
|
|
|
|
0x75: 4,
|
|
|
|
0x77: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
type jpegVisitor struct {
|
2022-08-10 12:10:46 +00:00
|
|
|
js *jpegstructure.JpegSplitter
|
|
|
|
writer io.Writer
|
|
|
|
expectedFileSize int
|
|
|
|
writtenTotalBytes int
|
2022-01-23 13:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleSegment satisfies the visitor interface{} of the jpegstructure library.
|
|
|
|
//
|
2022-08-10 12:10:46 +00:00
|
|
|
// We don't really care about many of the parameters, since all we're interested
|
2022-01-23 13:41:31 +00:00
|
|
|
// in here is the very last segment that was scanned.
|
2022-08-10 12:10:46 +00:00
|
|
|
func (v *jpegVisitor) HandleSegment(segmentMarker byte, _ string, _ int, _ bool) error {
|
|
|
|
// get the most recent segment scanned (ie., last in the segments list)
|
2022-01-23 13:41:31 +00:00
|
|
|
segmentList := v.js.Segments()
|
|
|
|
segments := segmentList.Segments()
|
2022-08-10 12:10:46 +00:00
|
|
|
mostRecentSegment := segments[len(segments)-1]
|
|
|
|
|
|
|
|
// check if we've written the expected number of bytes by EOI
|
|
|
|
if segmentMarker == jpegstructure.MARKER_EOI {
|
|
|
|
// take account of the last 2 bytes taken up by the EOI
|
|
|
|
eoiLength := 2
|
2022-12-06 13:15:25 +00:00
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
// this is the total file size we will
|
|
|
|
// have written including the EOI
|
|
|
|
willHaveWritten := v.writtenTotalBytes + eoiLength
|
|
|
|
|
|
|
|
if willHaveWritten < v.expectedFileSize {
|
|
|
|
// if we won't have written enough,
|
|
|
|
// pad the final segment before EOI
|
|
|
|
// so that we meet expected file size
|
|
|
|
missingBytes := make([]byte, v.expectedFileSize-willHaveWritten)
|
|
|
|
if _, err := v.writer.Write(missingBytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process the segment
|
|
|
|
return v.writeSegment(mostRecentSegment)
|
2022-01-23 13:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *jpegVisitor) writeSegment(s *jpegstructure.Segment) error {
|
2022-08-10 12:10:46 +00:00
|
|
|
var writtenSegmentData int
|
2022-01-23 13:41:31 +00:00
|
|
|
w := v.writer
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// whatever happens, when we finished then evict data from the segment;
|
|
|
|
// once we've written it we don't want it in memory anymore
|
|
|
|
s.Data = s.Data[:0]
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The scan-data will have a marker-ID of (0) because it doesn't have a marker-ID or length.
|
|
|
|
if s.MarkerId != 0 {
|
2022-08-10 12:10:46 +00:00
|
|
|
markerIDWritten, err := w.Write([]byte{0xff, s.MarkerId})
|
|
|
|
if err != nil {
|
2022-01-23 13:41:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenSegmentData += markerIDWritten
|
2022-01-23 13:41:31 +00:00
|
|
|
|
|
|
|
sizeLen, found := markerLen[s.MarkerId]
|
|
|
|
if !found || sizeLen == 2 {
|
|
|
|
sizeLen = 2
|
|
|
|
l := uint16(len(s.Data) + sizeLen)
|
|
|
|
|
|
|
|
if err := binary.Write(w, binary.BigEndian, &l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenSegmentData += 2
|
2022-01-23 13:41:31 +00:00
|
|
|
} else if sizeLen == 4 {
|
|
|
|
l := uint32(len(s.Data) + sizeLen)
|
|
|
|
|
|
|
|
if err := binary.Write(w, binary.BigEndian, &l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenSegmentData += 4
|
2022-01-23 13:41:31 +00:00
|
|
|
} else if sizeLen != 0 {
|
|
|
|
return fmt.Errorf("not a supported marker-size: MARKER-ID=(0x%02x) MARKER-SIZE-LEN=(%d)", s.MarkerId, sizeLen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:38:19 +00:00
|
|
|
if !s.IsExif() {
|
|
|
|
// if this isn't exif data just copy it over and bail
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenNormalData, err := w.Write(s.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
writtenSegmentData += writtenNormalData
|
|
|
|
v.writtenTotalBytes += writtenSegmentData
|
|
|
|
return nil
|
2022-06-23 13:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ifd, _, err := s.Exif()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
// amount of bytes we've writtenExifData into the exif body, we'll update this as we go
|
|
|
|
var writtenExifData int
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
if orientationEntries, err := ifd.FindTagWithName("Orientation"); err == nil && len(orientationEntries) == 1 {
|
|
|
|
// If we have an orientation entry, we don't want to completely obliterate the exif data.
|
|
|
|
// Instead, we want to surgically obliterate everything *except* the orientation tag, so
|
|
|
|
// that the image will still be rotated correctly when shown in client applications etc.
|
|
|
|
//
|
|
|
|
// To accomplish this, we're going to extract just the bytes that we need and write them
|
|
|
|
// in according to the exif specification, then fill in the rest of the space with empty
|
|
|
|
// bytes.
|
|
|
|
//
|
|
|
|
// First we need to write the exif prefix for this segment.
|
|
|
|
//
|
|
|
|
// Then we write the exif header which contains the byte order and offset of the first ifd.
|
|
|
|
//
|
|
|
|
// Then we write the ifd0 entry which contains the orientation data.
|
|
|
|
//
|
2022-08-10 12:10:46 +00:00
|
|
|
// After that we just fill.
|
2022-06-23 13:38:19 +00:00
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
newExifData := &bytes.Buffer{}
|
|
|
|
byteOrder := ifd.ByteOrder()
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
// 1. Write exif prefix.
|
|
|
|
// https://www.ozhiker.com/electronics/pjmt/jpeg_info/app_segments.html
|
|
|
|
prefix := []byte{'E', 'x', 'i', 'f', 0, 0}
|
2022-08-10 12:10:46 +00:00
|
|
|
if err := binary.Write(newExifData, byteOrder, &prefix); err != nil {
|
2022-06-23 13:38:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += len(prefix)
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
// 2. Write exif header, taking the existing byte order.
|
2022-08-10 12:10:46 +00:00
|
|
|
exifHeader, err := exif.BuildExifHeader(byteOrder, exif.ExifDefaultFirstIfdOffset)
|
2022-06-23 13:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
hWritten, err := newExifData.Write(exifHeader)
|
2022-06-23 13:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += hWritten
|
2022-06-23 13:38:19 +00:00
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
// 3. Write in the new ifd
|
2022-06-23 13:38:19 +00:00
|
|
|
//
|
|
|
|
// An ifd with one orientation entry is structured like this:
|
|
|
|
// 2 bytes: the number of entries in the ifd uint16(1)
|
|
|
|
// 2 bytes: the tag id uint16(274)
|
|
|
|
// 2 bytes: the tag type uint16(3)
|
|
|
|
// 4 bytes: the tag count uint32(1)
|
|
|
|
// 4 bytes: the tag value offset: uint32(one of the below with padding on the end)
|
|
|
|
// 1 = Horizontal (normal)
|
|
|
|
// 2 = Mirror horizontal
|
|
|
|
// 3 = Rotate 180
|
|
|
|
// 4 = Mirror vertical
|
|
|
|
// 5 = Mirror horizontal and rotate 270 CW
|
|
|
|
// 6 = Rotate 90 CW
|
|
|
|
// 7 = Mirror horizontal and rotate 90 CW
|
|
|
|
// 8 = Rotate 270 CW
|
2022-08-10 12:10:46 +00:00
|
|
|
//
|
|
|
|
// see https://web.archive.org/web/20190624045241if_/http://www.cipa.jp:80/std/documents/e/DC-008-Translation-2019-E.pdf - p24-25
|
2022-06-23 13:38:19 +00:00
|
|
|
orientationEntry := orientationEntries[0]
|
|
|
|
|
|
|
|
ifdCount := uint16(1) // we're only adding one entry into the ifd
|
2022-08-10 12:10:46 +00:00
|
|
|
if err := binary.Write(newExifData, byteOrder, &ifdCount); err != nil {
|
2022-06-23 13:38:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += 2
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
tagID := orientationEntry.TagId()
|
2022-08-10 12:10:46 +00:00
|
|
|
if err := binary.Write(newExifData, byteOrder, &tagID); err != nil {
|
2022-06-23 13:38:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += 2
|
2022-06-23 13:38:19 +00:00
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
tagType := uint16(orientationEntry.TagType())
|
|
|
|
if err := binary.Write(newExifData, byteOrder, &tagType); err != nil {
|
2022-06-23 13:38:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += 2
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
tagCount := orientationEntry.UnitCount()
|
2022-08-10 12:10:46 +00:00
|
|
|
if err := binary.Write(newExifData, byteOrder, &tagCount); err != nil {
|
2022-06-23 13:38:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += 4
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
valueOffset, err := orientationEntry.GetRawBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
vWritten, err := newExifData.Write(valueOffset)
|
2022-06-23 13:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += vWritten
|
2022-06-23 13:38:19 +00:00
|
|
|
|
|
|
|
valuePad := make([]byte, 4-vWritten)
|
2022-08-10 12:10:46 +00:00
|
|
|
pWritten, err := newExifData.Write(valuePad)
|
2022-06-23 13:38:19 +00:00
|
|
|
if err != nil {
|
2022-01-23 13:41:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenExifData += pWritten
|
2022-06-23 13:38:19 +00:00
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
// write all the new data into the writer from the segment
|
|
|
|
writtenNewExifData, err := io.Copy(w, newExifData)
|
|
|
|
if err != nil {
|
2022-01-23 13:41:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:10:46 +00:00
|
|
|
|
|
|
|
writtenSegmentData += int(writtenNewExifData)
|
2022-01-23 13:41:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
// fill in any remaining exif body with blank bytes
|
|
|
|
blank := make([]byte, len(s.Data)-writtenExifData)
|
|
|
|
writtenPadding, err := w.Write(blank)
|
|
|
|
if err != nil {
|
2022-06-23 13:38:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-10 12:10:46 +00:00
|
|
|
writtenSegmentData += writtenPadding
|
|
|
|
v.writtenTotalBytes += writtenSegmentData
|
2022-01-23 13:41:31 +00:00
|
|
|
return nil
|
|
|
|
}
|