summaryrefslogtreecommitdiffstats
path: root/src/file_tiff.h
blob: 5ef232515ee737f6c11463811ce178a9cccda60b (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
133
134
135
136
137
/*

    File: file_tiff.h

    Copyright (C) 2009 Christophe GRENIER <grenier@cgsecurity.org>
  
    This software is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 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 General Public License for more details.
  
    You should have received a copy of the GNU General Public License along
    with this program; if not, write the Free Software Foundation, Inc., 51
    Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */
#ifdef __cplusplus
extern "C" {
#endif
#define TIFF_ERROR 0xffffffffffffffffull

#define TIFF_BIGENDIAN          	0x4d4d
#define TIFF_LITTLEENDIAN       	0x4949
#define TIFFTAG_IMAGEDESCRIPTION        270     /* info about image */
#define TIFFTAG_MAKE                    271     /* scanner manufacturer name */
#define TIFFTAG_MODEL                   272     /* scanner model name/number */
#define	TIFFTAG_STRIPOFFSETS		273	/* offsets to data strips */
#define	TIFFTAG_STRIPBYTECOUNTS		279	/* bytes counts for strips */
#define TIFFTAG_TILEOFFSETS		324
#define TIFFTAG_TILEBYTECOUNTS		325
#define TIFFTAG_SUBIFD                  330
#define	TIFFTAG_JPEGIFOFFSET		513	/* !pointer to SOI marker */
#define	TIFFTAG_JPEGIFBYTECOUNT		514	/* !JFIF stream length */
#define TIFFTAG_KODAKIFD 		33424
#define TIFFTAG_EXIFIFD                 34665
#define EXIFTAG_MAKERNOTE		37500	/* Manufacturer notes */
#define TIFFTAG_SONY_FILEFORMAT		0xb000
#define TIFFTAG_IMAGEOFFSET		0xbcc0
#define TIFFTAG_IMAGEBYTECOUNT		0xbcc1
#define TIFFTAG_ALPHAOFFSET		0xbcc2
#define TIFFTAG_ALPHABYTECOUNT		0xbcc3
#define TIFFTAG_PRINTIM			50341
#define TIFFTAG_DNGVERSION		50706
#define TIFFTAG_DNGPRIVATEDATA		50740	/* &manufacturer's private data */

typedef struct {
        uint16_t  tiff_magic;     /* magic number (defines byte order) */
        uint16_t  tiff_version;   /* TIFF version number */
        uint32_t  tiff_diroff;    /* byte offset to first directory */
} TIFFHeader;

typedef struct {
        uint16_t          tdir_tag;       /* see below */
        uint16_t          tdir_type;      /* data type; see below */
        uint32_t          tdir_count;     /* number of items; length in spec */
        uint32_t          tdir_offset;    /* byte offset to field data */
} TIFFDirEntry;

/* Work around a gcc bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52991 */
struct ifd_header {
  uint16_t nbr_fields;
  TIFFDirEntry ifd;
} __attribute__ ((gcc_struct, __packed__));

/*@
  @ requires tiff_size >= sizeof(TIFFHeader);
  @ requires \valid_read((const unsigned char *)tiff+(0..tiff_size-1));
  @*/
time_t get_date_from_tiff_header(const TIFFHeader *tiff, const unsigned int tiff_size);

/*@
  @ requires \valid_read((const unsigned char *)tiff+(0..tiff_size-1));
  @*/
const char *find_tag_from_tiff_header(const TIFFHeader *tiff, const unsigned int tiff_size, const unsigned int tag, const char **potential_error);

/*@
  @ requires \valid(file_recovery);
  @ requires \valid(file_recovery->handle);
  @*/
void file_check_tiff(file_recovery_t *file_recovery);

/*@
  @ requires tiff_size >= sizeof(TIFFHeader);
  @ requires \valid_read((const unsigned char *)tiff+(0..tiff_size-1));
  @ requires \valid(potential_error);
  @*/
const char *find_tag_from_tiff_header_be(const TIFFHeader *tiff, const unsigned int tiff_size, const unsigned int tag, const char**potential_error);
/*@
  @ requires tiff_size >= sizeof(TIFFHeader);
  @ requires \valid_read((const unsigned char *)tiff+(0..tiff_size-1));
  @ requires \valid(potential_error);
  @*/
const char *find_tag_from_tiff_header_le(const TIFFHeader *tiff, const unsigned int tiff_size, const unsigned int tag, const char**potential_error);

/*@
  @ requires \valid(fr);
  @ requires \valid(fr->handle);
  @ requires \valid_read(&fr->extension);
  @ requires \initialized(&fr->extension);
  @
 */
uint64_t file_check_tiff_be(file_recovery_t *fr, const uint32_t tiff_diroff, const unsigned int depth, const unsigned int count);

/*@
  @ requires \valid(fr);
  @ requires \valid(fr->handle);
  @ requires \valid_read(&fr->extension);
  @ requires \initialized(&fr->extension);
  @
 */
uint64_t file_check_tiff_le(file_recovery_t *fr, const uint32_t tiff_diroff, const unsigned int depth, const unsigned int count);

/*@
  @ requires \valid_read(buffer+(0..buffer_size-1));
  @
 */
int header_check_tiff_be(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new);

/*@
  @ requires \valid_read(buffer+(0..buffer_size-1));
  @
 */
int header_check_tiff_le(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new);

unsigned int tiff_type2size(const unsigned int type);
#ifdef DEBUG_TIFF
const char *tag_name(unsigned int tag);
#endif

#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif