Пример #1
0
        static byte[] get_TTF_digest(OTFile f)
        {
            OTFont         fn     = f.GetFont(0);
            Table_DSIG     tDSIG  = (Table_DSIG)fn.GetTable("DSIG");
            DirectoryEntry deDSIG = null;
            // sort table by offset
            Dictionary <uint, int> offsetlookup = new Dictionary <uint, int>();
            var list = new List <uint>();

            for (ushort i = 0; i < fn.GetNumTables(); i++)
            {
                DirectoryEntry de = fn.GetDirectoryEntry(i);
                offsetlookup.Add(de.offset, i);
                list.Add(de.offset);
                if ((string)de.tag == "DSIG")
                {
                    deDSIG = de;
                }
            }
            list.Sort();

            // New offset table
            var         old_ot = fn.GetOffsetTable();
            OffsetTable ot     = new OffsetTable(old_ot.sfntVersion, (ushort)(old_ot.numTables - 1));

            for (ushort i = 0; i < fn.GetNumTables(); i++)
            {
                DirectoryEntry oldde = fn.GetDirectoryEntry(i);
                if ((string)oldde.tag != "DSIG")
                {
                    DirectoryEntry de = new DirectoryEntry(oldde);
                    de.offset -= 16; // one less entry
                    if (de.offset > deDSIG.offset)
                    {
                        de.offset -= tDSIG.GetBuffer().GetPaddedLength();
                    }
                    ot.DirectoryEntries.Add(de);
                }
            }
            hash.TransformBlock(ot.m_buf.GetBuffer(), 0, (int)ot.m_buf.GetLength(), ot.m_buf.GetBuffer(), 0);

            for (int i = 0; i < ot.DirectoryEntries.Count; i++)
            {
                DirectoryEntry de = (DirectoryEntry)ot.DirectoryEntries[i];
                hash.TransformBlock(de.m_buf.GetBuffer(), 0, (int)de.m_buf.GetLength(), de.m_buf.GetBuffer(), 0);
            }

            Table_head headTable = (Table_head)fn.GetTable("head");

            // calculate the checksum
            uint sum = 0;

            sum += ot.CalcOffsetTableChecksum();
            sum += ot.CalcDirectoryEntriesChecksum();
            foreach (var key in list)
            {
                OTTable table = fn.GetTable((ushort)offsetlookup[key]);

                if ((string)table.GetTag() != "DSIG")
                {
                    sum += table.CalcChecksum();
                }
            }

            Table_head.head_cache headCache = (Table_head.head_cache)headTable.GetCache();

            // set the checkSumAdujustment field
            headCache.checkSumAdjustment = 0xb1b0afba - sum;
            Table_head newHead = (Table_head)headCache.GenerateTable();

            foreach (var key in list)
            {
                OTTable table = fn.GetTable((ushort)offsetlookup[key]);

                if ((string)table.GetTag() == "head")
                {
                    table = newHead;
                }

                if ((string)table.GetTag() != "DSIG")
                {
                    hash.TransformBlock(table.m_bufTable.GetBuffer(), 0, (int)table.GetBuffer().GetPaddedLength(),
                                        table.m_bufTable.GetBuffer(), 0);
                }
            }

            byte[] usFlag = { 0, 1 };
            hash.TransformFinalBlock(usFlag, 0, 2);
            return(hash.Hash);
        }