Example #1
0
        /// <summary>
        /// Convert a subset of an array of text bytes to a string.
        /// </summary>
        /// <param name="byteData">The array of bytes.</param>
        /// <param name="offset">The index of the first byte to be converted.</param>
        /// <param name="length">The number of bytes to be converted.</param>
        /// <param name="replace">True to replace non-Ascii bytes with a space; false to ignore them.</param>
        /// <returns>The converted string.</returns>
        public static string GetString(byte[] byteData, int offset, int length, bool replace)
        {
            if (length == 0)
            {
                return(string.Empty);
            }

            string isoTable  = null;
            int    startByte = 0;

            if (RunParameters.Instance.CharacterSet != null)
            {
                isoTable = RunParameters.Instance.CharacterSet;
                if (byteData[offset] < 0x20)
                {
                    startByte = 1;
                }
            }
            else
            {
                if (byteData[offset] >= 0x20)
                {
                    isoTable = "iso-8859-1";
                }
                else
                {
                    switch (byteData[offset])
                    {
                    case 0x01:
                    case 0x02:
                    case 0x03:
                    case 0x04:
                    case 0x05:
                    case 0x06:
                    case 0x07:
                    case 0x08:
                    case 0x09:
                    case 0x0a:
                    case 0x0b:
                        isoTable  = "iso-8859-" + (byteData[offset] + 4).ToString();
                        startByte = 1;
                        break;

                    case 0x10:
                        if (byteData[offset + 1] == 0x00)
                        {
                            if (byteData[offset + 2] != 0x00 && byteData[offset + 2] != 0x0c)
                            {
                                isoTable  = "iso-8859-" + ((int)byteData[offset + 2]).ToString();
                                startByte = 3;
                                break;
                            }
                            else
                            {
                                return("Invalid DVB text string: byte 3 is not a valid value");
                            }
                        }
                        else
                        {
                            return("Invalid DVB text string: byte 2 is not a valid value");
                        }

                    case 0x11:
                    case 0x15:
                        isoTable  = "utf-8";
                        startByte = 1;
                        break;

                    case 0x1f:
                        if (byteData[offset + 1] == 0x01 || byteData[offset + 1] == 0x02)
                        {
                            if (MultiTreeDictionaryEntry.Loaded)
                            {
                                return(MultiTreeDictionaryEntry.DecodeData(Utils.GetBytes(byteData, offset, length + 1)));
                            }
                            else
                            {
                                return("Huffman text: " + Utils.ConvertToHex(byteData, offset, length));
                            }
                        }
                        else
                        {
                            return("Invalid DVB text string: Custom text specifier is not recognized");
                        }

                    default:
                        return("Invalid DVB text string: byte 1 is not a valid value");
                    }
                }
            }

            byte[] editedBytes  = new byte[length];
            int    editedLength = 0;

            for (int index = startByte; index < length; index++)
            {
                if (byteData[offset + index] > 0x1f)
                {
                    if (byteData[offset + index] < 0x80 || byteData[offset + index] > 0x9f)
                    {
                        editedBytes[editedLength] = byteData[offset + index];
                        editedLength++;
                    }
                }
                else
                {
                    if (replace)
                    {
                        editedBytes[editedLength] = 0x20;
                        editedLength++;
                    }
                }
            }

            if (editedLength == 0)
            {
                return(string.Empty);
            }

            try
            {
                Encoding sourceEncoding = Encoding.GetEncoding(isoTable);
                if (sourceEncoding == null)
                {
                    sourceEncoding = Encoding.GetEncoding("iso-8859-1");
                }

                return(sourceEncoding.GetString(editedBytes, 0, editedLength));
            }
            catch (ArgumentException e)
            {
                Logger.Instance.Write("<E> A text string could not be decoded");
                Logger.Instance.Write("<E> String: " + Utils.ConvertToHex(byteData, offset, length));
                Logger.Instance.Write("<E> Error: " + e.Message);
                return("** ERROR DECODING STRING - SEE COLLECTION LOG **");
            }
        }
Example #2
0
 internal string processHuffmanData(int compressionType)
 {
     return(MultiTreeDictionaryEntry.DecodeData(compressionType, compressedString));
 }