Example #1
0
        public override void ParseFromRaw(ChunkRaw chunk)
        {
            byte[] data       = chunk.Data;
            int    length     = data.Length;
            int    nullsFound = 0;

            int[] nullsIdx = new int[3];
            for (int k = 0; k < length; k++)
            {
                if (data[k] != 0)
                {
                    continue;
                }
                nullsIdx[nullsFound] = k;
                nullsFound++;
                if (nullsFound == 1)
                {
                    k += 2;
                }
                if (nullsFound == 3)
                {
                    break;
                }
            }
            if (nullsFound != 3)
            {
                throw new System.Exception("Bad formed PngChunkITXT chunk");
            }
            key = ChunkHelper.ToString(data, 0, nullsIdx[0]);
            int i = nullsIdx[0] + 1;

            compressed = data[i] == 0 ? false : true;
            i++;
            if (compressed && data[i] != 0)
            {
                throw new System.Exception("Bad formed PngChunkITXT chunk - bad compression method ");
            }
            langTag       = ChunkHelper.ToString(data, i, nullsIdx[1] - i);
            translatedTag = ChunkHelper.ToStringUTF8(data, nullsIdx[1] + 1, nullsIdx[2] - nullsIdx[1] - 1);
            i             = nullsIdx[2] + 1;
            if (compressed)
            {
                byte[] bytes = ChunkHelper.CompressBytes(data, i, length - i, false);
                val = ChunkHelper.ToStringUTF8(bytes);
            }
            else
            {
                val = ChunkHelper.ToStringUTF8(data, i, length - i);
            }
        }