Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / filesystem / udf25.h
1 #ifndef UDF25_H
2 #define UDF25_H
3 /*
4  *      Copyright (C) 2010 Team Boxee
5  *      http://www.boxee.tv
6  *
7  *      Copyright (C) 2010-2013 Team XBMC
8  *      http://xbmc.org
9  *
10  *  This Program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  This Program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with XBMC; see the file COPYING.  If not, see
22  *  <http://www.gnu.org/licenses/>.
23  *
24  *  Note: parts of this code comes from libdvdread.
25  *  Jorgen Lundman and team boxee did the necessary modifications to support udf 2.5
26  *
27  */
28 #include "File.h"
29
30 /**
31  * The length of one Logical Block of a DVD.
32  */
33 #define DVD_VIDEO_LB_LEN 2048
34
35 /**
36  * Maximum length of filenames allowed in UDF.
37  */
38 #define MAX_UDF_FILE_NAME_LEN 2048
39
40 struct Partition {
41   int valid;
42   char VolumeDesc[128];
43   uint16_t Flags;
44   uint16_t Number;
45   char Contents[32];
46   uint32_t AccessType;
47   uint32_t Start;
48   uint32_t Length;
49   uint32_t Start_Correction;
50 };
51
52 struct AD {
53   uint32_t Location;
54   uint32_t Length;
55   uint8_t  Flags;
56   uint16_t Partition;
57 };
58
59 /* Previously dvdread would assume files only had one AD chain, and since they
60  * are 1GB or less, this is most problably true. However, now we handle chains
61  * for large files. ECMA_167 does not specify the maximum number of chains, is
62  * it as many as can fit in a 2048 block (minus ID 266 size), or some other
63  * limit. For now, I have assumed that;
64  * a 4.4GB file uses 5 AD chains. A BluRay disk can store 50GB of data, so the
65  * largest file should be 50 GB. So the maximum number of chains should be
66  * around 62.
67  *
68  * However, with AD chain extensions there has been examples of chains up to
69  * around 1600 entries.
70  */
71
72 #define UDF_MAX_AD_CHAINS 2000
73
74 struct FileAD {
75     uint64_t Length;
76     uint32_t num_AD;
77     uint16_t Partition;
78     uint32_t Partition_Start;
79     uint32_t Partition_Start_Correction;
80     uint8_t  Type;
81     uint16_t Flags;
82     struct AD AD_chain[UDF_MAX_AD_CHAINS];
83 };
84
85 struct extent_ad {
86   uint32_t location;
87   uint32_t length;
88 };
89
90 struct avdp_t {
91   struct extent_ad mvds;
92   struct extent_ad rvds;
93 };
94
95 struct pvd_t {
96   uint8_t VolumeIdentifier[32];
97   uint8_t VolumeSetIdentifier[128];
98 };
99
100 struct lbudf {
101   uint32_t lb;
102   uint8_t *data;
103   /* needed for proper freeing */
104   uint8_t *data_base;
105 };
106
107 struct icbmap {
108   uint32_t lbn;
109   struct FileAD  file;
110 };
111
112 struct udf_cache {
113   int avdp_valid;
114   struct avdp_t avdp;
115   int pvd_valid;
116   struct pvd_t pvd;
117   int partition_valid;
118   struct Partition partition;
119   int rooticb_valid;
120   struct AD rooticb;
121   int lb_num;
122   struct lbudf *lbs;
123   int map_num;
124   struct icbmap *maps;
125 };
126
127 typedef enum {
128   PartitionCache, RootICBCache, LBUDFCache, MapCache, AVDPCache, PVDCache
129 } UDFCacheType;
130
131 /*
132  * DVDReaddir entry types.
133  */
134 typedef enum {
135   DVD_DT_UNKNOWN = 0,
136   DVD_DT_FIFO,
137   DVD_DT_CHR,
138   DVD_DT_DIR,
139   DVD_DT_BLK,
140   DVD_DT_REG,
141   DVD_DT_LNK,
142   DVD_DT_SOCK,
143   DVD_DT_WHT
144 } udf_dir_type_t;
145
146 /*
147  * DVDReaddir structure.
148  * Extended a little from POSIX to also return filesize.
149  */
150 typedef struct {
151   unsigned char  d_name[MAX_UDF_FILE_NAME_LEN];
152   // "Shall not exceed 1023; Ecma-167 page 123"
153   udf_dir_type_t d_type;       // DT_REG, DT_DIR
154   unsigned int   d_namlen;
155   uint64_t       d_filesize;
156 } udf_dirent_t;
157
158
159 /*
160  * DVDOpendir DIR* structure
161  */
162 typedef struct {
163   uint32_t dir_location;
164   uint32_t dir_length;
165   uint32_t dir_current;   // Separate to _location should we one day want to
166                           // implement dir_rewind()
167   unsigned int current_p; // Internal implementation specific. UDFScanDirX
168   udf_dirent_t entry;
169 } udf_dir_t;
170
171
172
173 typedef struct FileAD *UDF_FILE;
174
175 typedef struct _BD_FILE
176 {
177   UDF_FILE file;
178   uint64_t seek_pos;  // in bytes
179   uint64_t filesize;  // in bytes
180
181 } *BD_FILE;
182
183
184 class udf25
185 {
186
187 public:
188   udf25( );
189   virtual ~udf25( );
190
191   DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod );
192   int64_t GetFileSize(HANDLE hFile);
193   int64_t GetFilePosition(HANDLE hFile);
194   int64_t Seek(HANDLE hFile, int64_t lOffset, int whence);
195   bool   Open(const char *isofile);
196   HANDLE OpenFile( const char* filename );
197   long ReadFile(HANDLE fd, unsigned char *pBuffer, long lSize);
198   void CloseFile(HANDLE hFile);
199
200   udf_dir_t *OpenDir( const char *subdir );
201   udf_dirent_t *ReadDir( udf_dir_t *dirp );
202   int CloseDir( udf_dir_t *dirp );
203
204   void Reset();
205   void Scan();
206   bool IsScanned();
207
208 private:
209   UDF_FILE UDFFindFile( const char* filename, uint64_t *filesize );
210   int UDFScanDirX( udf_dir_t *dirp );
211   int DVDUDFCacheLevel(int level);
212   void* GetUDFCacheHandle();
213   void SetUDFCacheHandle(void *cache);
214   int GetUDFCache(UDFCacheType type,uint32_t nr, void *data);
215   int UDFFindPartition( int partnum, struct Partition *part );
216   int UDFGetAVDP( struct avdp_t *avdp);
217   int DVDReadLBUDF( uint32_t lb_number, size_t block_count, unsigned char *data, int encrypted );
218   int ReadAt( int64_t pos, size_t len, unsigned char *data );
219   int UDFMapICB( struct AD ICB, struct Partition *partition, struct FileAD *File );
220   int UDFScanDir( struct FileAD Dir, char *FileName, struct Partition *partition, struct AD *FileICB, int cache_file_info);
221   int SetUDFCache(UDFCacheType type, uint32_t nr, void *data);
222 protected:
223     /* Filesystem cache */
224   int m_udfcache_level; /* 0 - turned off, 1 - on */
225   void *m_udfcache;
226   XFILE::CFile* m_fp;
227 };
228
229 #endif