示例#1
0
        private Object Read(BinaryParser parser, FieldInfo field, Type type)
        {
            PscxArgumentException.ThrowIf((type == field.DeclaringType), "Parsing field {0} would loop forever.", field);

            if (IsRecord(type))
            {
                return(new RecordParser(type).Parse(parser));
            }

            if (type == typeof(String))
            {
                return(ReadString(parser, field));
            }

            if (type == typeof(DateTime))
            {
                return(ReadDateTime(parser, field));
            }

            if (type == typeof(Version))
            {
                return(ReadVersion(parser, field));
            }

            return(ReadIntegral(parser, type));
        }
示例#2
0
        private static DsNameResultItem[] CrackNames(
            SafeDsHandle hDS,
            DsNameFlags flags,
            DsNameFormat formatOffered,
            DsNameFormat formatDesired,
            string[] names)
        {
            PscxArgumentException.ThrowIfIsNullOrEmpty(names);

            IntPtr ptr = IntPtr.Zero;

            try
            {
                NativeMethods.EnforceSuccess(NativeMethods.DsCrackNames(
                                                 hDS,
                                                 flags,
                                                 formatOffered,
                                                 formatDesired,
                                                 names.Length,
                                                 names,
                                                 out ptr));

                DsNameResult result = Utils.PtrToStructure <DsNameResult>(ptr);

                IEnumerable <DsNameResultItem> items =
                    Utils.ReadNativeArray <DsNameResultItem>(result.rItems, result.cItems);

                return(new List <DsNameResultItem>(items).ToArray());
            }
            finally
            {
                NativeMethods.DsFreeNameResult(ptr);
            }
        }
示例#3
0
        public static NonSIUnit Parse(String str)
        {
            NonSIUnit result;
            Boolean   success = TryParse(str, out result);

            PscxArgumentException.ThrowIf(!success, Resources.Errors.UnknownNonSIUnit, str);

            return(result);
        }
示例#4
0
        protected BinaryParser OpenSection(PESection section)
        {
            PscxArgumentException.ThrowIfIsNull(section);

            uint index = (section.PointerToRawData);
            uint count = (section.SizeOfRawData);

            return(new BinaryParser(OpenImage(index)));
        }
示例#5
0
        public void Remove(string[] values)
        {
            PscxArgumentException.ThrowIfIsNull(values);
            EnsureValuesLoaded();

            for (int i = 0; i < values.Length; i++)
            {
                Remove(values[i]);
            }
        }
示例#6
0
        public void Prepend(string[] values)
        {
            PscxArgumentException.ThrowIfIsNull(values);
            EnsureValuesLoaded();

            for (int i = values.Length - 1; i >= 0; i--)
            {
                Prepend(values[i]);
            }
        }
示例#7
0
        public RecordParser(Type recordType)
        {
            var ctor = recordType.GetConstructor(new Type[0]);
            var attr = BinaryParser.GetAttribute <BinaryRecordAttribute>(recordType);

            PscxArgumentException.ThrowIfIsNull(ctor, "type {0} does not have default constructor", recordType);
            PscxArgumentException.ThrowIfIsNull(attr, "type {0} does not have BinaryRecordAttribute", recordType);

            _type = recordType;
            _pack = attr.Pack;
        }
示例#8
0
        public void Prepend(string value)
        {
            PscxArgumentException.ThrowIfIsNullOrEmpty(value);
            EnsureValuesLoaded();

            value = value.Trim();

            if (value.Length > 0 && !Contains(value))
            {
                _values.Insert(0, value);
            }
        }
示例#9
0
        private static SafeDsCredentialsHandle MakeCredentials(string user, string domain, string password)
        {
            PscxArgumentException.ThrowIfIsNullOrEmpty(user);
            PscxArgumentException.ThrowIfIsNullOrEmpty(domain);
            PscxArgumentException.ThrowIfIsNullOrEmpty(password);

            SafeDsCredentialsHandle cred;

            NativeMethods.EnforceSuccess(NativeMethods.DsMakePasswordCredentials(user, domain, password, out cred));

            return(cred);
        }
示例#10
0
        public void Remove(string value)
        {
            PscxArgumentException.ThrowIfIsNullOrEmpty(value);
            EnsureValuesLoaded();

            value = value.Trim();
            int index = IndexOf(value);

            if (index >= 0)
            {
                _values.RemoveAt(index);
            }
        }
示例#11
0
        private static DateTime ReadDateTime(BinaryParser parser, FieldInfo field)
        {
            var attr = BinaryParser.GetAttribute <BinaryFieldAttribute>(field);

            if (attr != null)
            {
                if (attr.FieldType == BinaryFieldType.UnixTime)
                {
                    return(UnixTime.ToDateTime(parser.ReadUInt32()));
                }
            }

            PscxArgumentException.Throw("Don't know how to parse DateTime field " + field.ToString());
            return(DateTime.MinValue);
        }
示例#12
0
        public static DosHeader Parse(BinaryReader br)
        {
            PscxArgumentException.ThrowIfIsNull(br);

            ushort signature = br.ReadUInt16();

            if (DosSignature != signature)
            {
                InvalidPEFileException.ThrowInvalidDosHeader();
            }

            DosHeader hdr = new DosHeader();

            hdr.BytesOnLastPage       = br.ReadUInt16();
            hdr.PageCount             = br.ReadUInt16();
            hdr.RelocationCount       = br.ReadUInt16();
            hdr.HeaderSize            = br.ReadUInt16();
            hdr.MinExtraParagraphs    = br.ReadUInt16();
            hdr.MaxExtraParagraphs    = br.ReadUInt16();
            hdr.InitialSS             = br.ReadUInt16();
            hdr.InitialSP             = br.ReadUInt16();
            hdr.Checksum              = br.ReadUInt16();
            hdr.InitialIP             = br.ReadUInt16();
            hdr.InitialCS             = br.ReadUInt16();
            hdr.RelocationTableOffset = br.ReadUInt16();
            hdr.OverlayNumber         = br.ReadUInt16();

            // reserved words
            for (int i = 0; i < 4; i++)
            {
                br.ReadUInt16();
            }

            hdr.OemID   = br.ReadUInt16();
            hdr.OemInfo = br.ReadUInt16();

            // reserved words
            for (int i = 0; i < 10; i++)
            {
                br.ReadUInt16();
            }

            hdr.CoffHeaderOffset = br.ReadUInt32();

            return(hdr);
        }
示例#13
0
        private Delegate CreateDelegate(Type type)
        {
            if (type.IsAbstract)
            {
                PscxArgumentOutOfRangeException.Throw("Type {0} is abstract.", type.Name);
            }
            // need a target for delegate
            PscxArgumentException.ThrowIf(this._arguments.Length == 0,
                                          "Please supply a ScriptBlock as an argument.");

            // must be a scriptblock
            ScriptBlock target = this._arguments[0] as ScriptBlock;

            PscxArgumentException.ThrowIfIsNull(target, "The only argument in ArgumentList must be a ScriptBlock.");

            Delegate invoker = GetScriptBlockDelegate(target, type);

            return(invoker);
        }
示例#14
0
        private static Object ReadVersion(BinaryParser parser, FieldInfo field)
        {
            var attr = BinaryParser.GetAttribute <VersionFieldAttribute>(field);

            if (attr != null)
            {
                var args = new Object[attr.ComponentCount];

                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = ReadIntegral(parser, attr.ComponentType);
                }

                return(Activator.CreateInstance(typeof(Version), args));
            }

            PscxArgumentException.Throw(field.Name, "Don't know how to parse field " + field.ToString());
            return(null);
        }
示例#15
0
        private static String ReadString(BinaryParser parser, FieldInfo field)
        {
            var attr = BinaryParser.GetAttribute <BinaryFieldAttribute>(field);

            if (attr != null)
            {
                if (attr.FieldType == BinaryFieldType.StringAsciiZ)
                {
                    if (attr.FixedLength > 0)
                    {
                        return(parser.ReadStringAsciiZ());
                    }

                    return(parser.ReadStringAsciiZ());
                }
            }

            PscxArgumentException.Throw(field.Name, "Don't know how to parse field " + field.ToString());
            return(null);
        }
示例#16
0
        public static string GetCanonicalName(DirectoryEntry entry)
        {
            string canonicalName = GetPropertyValueAsString(entry, "canonicalName");

            if (string.IsNullOrEmpty(canonicalName))
            {
                using (DirectoryEntry parent = entry.Parent)
                {
                    canonicalName = GetPropertyValueAsString(parent, "canonicalName");
                }

                PscxArgumentException.ThrowIfIsNullOrEmpty(canonicalName,
                                                           Resources.Errors.CannotGetDirectoryEntryCanonicalName,
                                                           entry.Path);

                canonicalName = canonicalName.TrimEnd('/') + '/' +
                                GetPropertyValueAsString(entry, "cn");
            }

            return(canonicalName);
        }
示例#17
0
        private string GetGenericTypeScriptDefinition(string typeName, int typeArgsCount, ref Type closedType)
        {
            // TODO: account for generic type arrays, e.g. collection<string>[]
            string openTypeName = String.Format("{0}`{1}", typeName, typeArgsCount);

            Type genericType = null;

            if (!LanguagePrimitives.TryConvertTo <Type>(openTypeName, out genericType))
            {
                PscxArgumentException.Throw("Generic Type {0} not found.", openTypeName);
            }

            string closedTypeName = CreateClosedTypeName(openTypeName);

            if (!LanguagePrimitives.TryConvertTo <Type>(closedTypeName, out closedType))
            {
                PscxArgumentException.Throw("Could not construct Closed Type using the provided arguments.");
            }

            WriteDebug("AssemblyQualifiedName: " + closedType.AssemblyQualifiedName);
            WriteVerbose("TypeName: " + closedTypeName);

            return(closedTypeName);
        }