Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Ifo.cpp « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 217170c4e391e01b28a974d58736522aee83a486 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * (C) 2008-2014 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC 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 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "Ifo.h"


#ifdef WORDS_BIGENDIAN
#define bswap_16(x) (x)
#define bswap_32(x) (x)
#define bswap_64(x) (x)
#else

// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
#define bswap_16(x) \
    ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))

// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
#define bswap_32(x)                                            \
    ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
    (((x)  & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))

#define bswap_64(x)                                       \
    (__extension__                                        \
    ({ union { __extension__ unsigned long long int __ll; \
                unsigned long int __l[2]; } __w, __r;     \
        __w.__ll = (x);                                   \
        __r.__l[0] = bswap_32 (__w.__l[1]);               \
        __r.__l[1] = bswap_32 (__w.__l[0]);               \
        __r.__ll; }))
#endif

#ifdef WORDS_BIGENDIAN
#define be2me_16(x) (x)
#define be2me_32(x) (x)
#define be2me_64(x) (x)
#define le2me_16(x) bswap_16(x)
#define le2me_32(x) bswap_32(x)
#define le2me_64(x) bswap_64(x)
#else
#define be2me_16(x) bswap_16(x)
#define be2me_32(x) bswap_32(x)
#define be2me_64(x) bswap_64(x)
#define le2me_16(x) (x)
#define le2me_32(x) (x)
#define le2me_64(x) (x)
#endif

#define DVD_VIDEO_LB_LEN    2048
#define IFO_HDR_LEN            8
#define LU_SUB_LEN             8

extern HANDLE(__stdcall* Real_CreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);


uint32_t get4bytes(const BYTE* buf)
{
    return be2me_32(*((uint32_t*)buf));
}


// VMG files
#define OFF_VMGM_PGCI_UT(buf)   get4bytes (buf + 0xC8)

// VTS files
#define OFF_VTSM_PGCI_UT(buf)   get4bytes (buf + 0xD0)
#define OFF_VTS_PGCIT(buf)      get4bytes (buf + 0xCC)


CIfo::CIfo()
    : m_pBuffer(nullptr)
    , m_dwSize(0)
    , m_pPGCI(nullptr)
    , m_pPGCIT(nullptr)
{
}

CIfo::~CIfo()
{
    delete [] m_pBuffer;
}

int CIfo::GetMiscPGCI(CIfo::ifo_hdr_t* hdr, int title, uint8_t** ptr)
{
    pgci_sub_t* pgci_sub;

    *ptr = (uint8_t*) hdr;
    *ptr += IFO_HDR_LEN;
    pgci_sub = (pgci_sub_t*) *ptr + title;

    *ptr = (uint8_t*) hdr + be2me_32(pgci_sub->start);

    return 0;
}

void CIfo::RemovePgciUOPs(uint8_t* ptr)
{
    ifo_hdr_t* hdr = (ifo_hdr_t*) ptr;
    uint16_t num;
    int i;

    ptr += IFO_HDR_LEN;
    num  = be2me_16(hdr->num);

    for (i = 1; i <= num; i++) {
        lu_sub_t* lu_sub = (lu_sub_t*) ptr;
        UNREFERENCED_PARAMETER(lu_sub);

        ptr += LU_SUB_LEN;
    }

    for (i = 0; i < be2me_16(hdr->num); i++) {
        uint8_t* ptr2;

        if (GetMiscPGCI(hdr, i, &ptr2) >= 0) {
            pgc_t* pgc = (pgc_t*) ptr2;
            pgc->prohibited_ops = 0;
        }
    }
}

CIfo::pgc_t* CIfo::GetFirstPGC()
{
    if (m_pBuffer) {
        return (pgc_t*)(m_pBuffer + 0x0400);
    } else {
        return nullptr;
    }
}

CIfo::pgc_t* CIfo::GetPGCI(const int title, const ifo_hdr_t* hdr)
{
    CIfo::pgci_sub_t* pgci_sub;
    uint8_t* ptr;

    ptr = (uint8_t*) hdr;
    ptr += IFO_HDR_LEN;

    pgci_sub = (pgci_sub_t*) ptr + title;

    ptr = (uint8_t*) hdr + be2me_32(pgci_sub->start);

    /* jdw */
    if (ptr >= ((uint8_t*) hdr + be2me_32(hdr->len))) {
        return nullptr;
    }
    /* /jdw */

    return (pgc_t*) ptr;
}

bool CIfo::IsVTS()
{
    if (m_dwSize < 12 || (strncmp((char*)m_pBuffer, "DVDVIDEO-VTS", 12) != 0)) {
        return false;
    }

    return true;
}

bool CIfo::IsVMG()
{
    if (m_dwSize < 12 || (strncmp((char*)m_pBuffer, "DVDVIDEO-VMG", 12) != 0)) {
        return false;
    }

    return true;
}

bool CIfo::OpenFile(LPCTSTR strFile)
{
    bool bRet = false;

    HANDLE hFile = Real_CreateFileW(strFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
    if (hFile != INVALID_HANDLE_VALUE) {
        LARGE_INTEGER size;
        // min size of the ifo file comes from dvd sector size,
        // max size we allow is 8 MB (taken with reserve),
        // also the file size must a multiple of dvd sector size
        if (GetFileSizeEx(hFile, &size) && (size.QuadPart >= DVD_VIDEO_LB_LEN) &&
                (size.QuadPart <= 0x800000) && !(size.QuadPart % DVD_VIDEO_LB_LEN)) {
            ASSERT(!m_pBuffer);
            m_pBuffer = DEBUG_NEW BYTE [(size_t)size.QuadPart];
            if (ReadFile(hFile, m_pBuffer, (DWORD)size.QuadPart, &m_dwSize, nullptr)) {
                ASSERT(!m_pPGCI);
                ASSERT(!m_pPGCIT);
                uint32_t sector, sectorCount = (uint32_t)size.QuadPart / DVD_VIDEO_LB_LEN;
                if (IsVTS()) {
                    sector = OFF_VTSM_PGCI_UT(m_pBuffer);
                    if (sector && (sector < sectorCount)) {
                        m_pPGCI = (ifo_hdr_t*)(m_pBuffer + sector * DVD_VIDEO_LB_LEN);
                    } else {
                        TRACE(_T("IFO: Missing or invalid VTSM_PGCI_UT sector"));
                    }
                    sector = OFF_VTS_PGCIT(m_pBuffer);
                    if (sector && (sector < sectorCount)) {
                        m_pPGCIT = (ifo_hdr_t*)(m_pBuffer + sector * DVD_VIDEO_LB_LEN);
                    } else {
                        TRACE(_T("IFO: Missing or invalid VTS_PGCI sector"));
                    }
                } else if (IsVMG()) {
                    sector = OFF_VMGM_PGCI_UT(m_pBuffer);
                    if (sector && (sector < sectorCount)) {
                        m_pPGCI = (ifo_hdr_t*)(m_pBuffer + sector * DVD_VIDEO_LB_LEN);
                    } else {
                        TRACE(_T("IFO: Missing or invalid VTSM_PGCI_UT sector"));
                    }
                }
                bRet = (m_pPGCI != nullptr);
            }
        }
        CloseHandle(hFile);
    } else {
        ASSERT(FALSE);
    }

    return bRet;
}

bool CIfo::RemoveUOPs()
{
    pgc_t* pgc;

    if (m_pPGCI) {
        pgc = GetFirstPGC();
        pgc->prohibited_ops = 0;

        for (int i = 0; i < be2me_16(m_pPGCI->num); i++) {
            pgc = GetPGCI(i, m_pPGCI);
            if (pgc) {
                RemovePgciUOPs((uint8_t*)pgc);
            }
        }
    }
    if (m_pPGCIT) {
        for (int i = 0; i < be2me_16(m_pPGCIT->num); i++) {
            pgc = GetPGCI(i, m_pPGCIT);
            if (pgc) {
                pgc->prohibited_ops = 0;
            }
        }
    }
    return true;
}

bool CIfo::SaveFile(LPCTSTR strFile)
{
    bool bRet = false;

    if (m_pBuffer) {
        HANDLE hFile = Real_CreateFileW(strFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                                        nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
        if (hFile != INVALID_HANDLE_VALUE) {
            DWORD written;
            if (WriteFile(hFile, m_pBuffer, m_dwSize, &written, nullptr)) {
                bRet = true;
            }
            CloseHandle(hFile);
        }
    }

    ASSERT(bRet);

    return bRet;
}