Пример #1
1
        /// <summary>
        /// Loads a <see cref="PropertyStore"/> for the specified file or extension, containing only the specified properties. Note that if creating a PropertyStore based on extension only, this store will be read only. The <see cref="GetFlags.BestEffort"/>
        /// should be used for retrieving that store, as the file does not exist to enable the reading of properties that would be stored in it, and any other flags are likely to produce an exception.
        /// </summary>
        /// <param name="path">The path or extension of the file the store is required for</param>
        /// <param name="keys">The required <see cref="PropertyKey"/>s</param>
        /// <param name="flags">The <see cref="GetFlags"/> indicating the type of store to load</param>
        public PropertyStore(string path, IEnumerable <PropertyKey> keys, GetFlags flags)
        {
            int    nKeys    = keys.Count();
            IntPtr keyArray = Marshal.AllocCoTaskMem(nKeys * Marshal.SizeOf(typeof(PROPERTYKEY)));

            try {
                IntPtr keyPtr = keyArray;
                foreach (PropertyKey key in keys)
                {
                    Marshal.StructureToPtr <PROPERTYKEY>(key.PROPERTKEY, keyPtr, false);
                    keyPtr += Marshal.SizeOf(typeof(PROPERTYKEY));
                }
                if (File.Exists(path))
                {
                    IntPtr pidl = ILCreateFromPath(Path.GetFullPath(path));
                    try {
                        IntPtr shUnk = IntPtr.Zero;
                        SHCreateItemFromIDList(pidl, IID.IShellItem2, out shUnk);
                        _pSource = (IShellItem2)Marshal.GetUniqueObjectForIUnknown(shUnk);
                        try {
                            IntPtr pUnk = IntPtr.Zero;
                            _pSource.GetPropertyStoreForKeys(keyArray, (uint)nKeys, (GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
                            try {
                                _pStore = (IPropertyStore)Marshal.GetUniqueObjectForIUnknown(pUnk);
                            }
                            finally { Marshal.Release(pUnk); }
                        }
                        finally {
                            if (shUnk != IntPtr.Zero)
                            {
                                Marshal.Release(shUnk);
                            }
                        }
                    }
                    finally {
                        if (pidl != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(pidl);
                        }
                    }
                }
                else
                {
                    string file;
                    path = path.Substring(path.LastIndexOf('\\') + 1);
                    if (path[0] == '.' || !path.Contains('.'))
                    {
                        file = Path.ChangeExtension("_Fake", path);
                    }
                    else
                    {
                        file = Path.GetFileName(path);
                    }
                    WIN32_FIND_DATA fd = new WIN32_FIND_DATA()
                    {
                        nFileSizeLow = 42
                    };
                    using (FakeFile f = new FakeFile(ref fd, file))
                    {
                        _pSource = (IShellItem2)f.GetShellItem();
                        IntPtr pUnk = IntPtr.Zero;
                        try
                        {
                            _pSource.GetPropertyStoreForKeys(keyArray, (uint)nKeys, (GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
                            _pStore = (IPropertyStore)Marshal.GetUniqueObjectForIUnknown(pUnk);
                        }
                        finally
                        {
                            if (pUnk != IntPtr.Zero)
                            {
                                Marshal.Release(pUnk);
                            }
                        }
                    }
                }
            }
            finally {
                if (keyArray != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(keyArray);
                }
            }
        }
Пример #2
0
        //Directily searches a name in an export list.
        private ScopeVar GetExternVariable(string name, GetFlags getFlags, int line, int col)
        {
            var vk = default(ExportVarData);

            //Looks for variable in export list
            if (exports.FindVariable(name, out vk))
            {
                var flags = vk.Flags;
                var data  = vk.Data;

                //All externals are added to the list of 'latebounds'. These names are then verified by a linker.
                //This verification is used only when a module is deserialized from an object file.
                frame.LateBounds.Add(new LateBoundSymbol(name, vk.ModuleHandle | vk.Address << 8, data, (Int32)flags, line, col));
                return(new ScopeVar(flags | ElaVariableFlags.External, vk.ModuleHandle | vk.Address << 8, data));
            }
            else
            {
                if (!options.IgnoreUndefined && (getFlags & GetFlags.NoError) != GetFlags.NoError)
                {
                    AddError(ElaCompilerError.UndefinedName, line, col, name);
                }

                return(ScopeVar.Empty);
            }
        }
Пример #3
0
        /// <summary>
        /// Loads a <see cref="PropertyStore"/> for the specified file or extension. Note that if creating a PropertyStore based on extension only, this store will be read only. The <see cref="GetFlags.BestEffort"/>
        /// should be used for retrieving that store, as the file does not exist to enable the reading of properties that would be stored in it, and any other flags are likely to produce an exception.
        /// </summary>
        /// <param name="path">The path or extension of the file the store is required for</param>
        /// <param name="flags">The <see cref="GetFlags"/> indicating the type of store to load</param>
        public PropertyStore(string path, GetFlags flags)
        {
            //if (File.Exists(path)) {
            IntPtr pidl = ILCreateFromPath(Path.GetFullPath(path));

            try {
                IntPtr  shUnk = IntPtr.Zero;
                HRESULT hr    = SHCreateItemFromIDList(pidl, IID.IShellItem2, out shUnk);
                _pSource = (IShellItem2)Marshal.GetUniqueObjectForIUnknown(shUnk);
                try {
                    IntPtr pUnk = IntPtr.Zero;
                    _pSource.GetPropertyStore((GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
                    try {
                        _pStore = (IPropertyStore)Marshal.GetUniqueObjectForIUnknown(pUnk);
                    }
                    finally { Marshal.Release(pUnk); }
                }
                finally {
                    if (shUnk != IntPtr.Zero)
                    {
                        Marshal.Release(shUnk);
                    }
                }
            }
            finally {
                if (pidl != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pidl);
                }
            }

            /*}
             * else {
             *      string file;
             *      path = path.Substring(path.LastIndexOf('\\') + 1);
             *      if (path[0] == '.' || !path.Contains('.'))
             *              file = Path.ChangeExtension("_Fake", path);
             *      else
             *              file = Path.GetFileName(path);
             *      WIN32_FIND_DATA fd = new WIN32_FIND_DATA() { nFileSizeLow = 42 };
             *      FakeFile f = new FakeFile(ref fd, file);
             *      _pSource = (IShellItem2)f.GetShellItem();
             *      IntPtr pUnk = IntPtr.Zero;
             *      try {
             *              _pSource.GetPropertyStore((GETPROPERTYSTOREFLAGS)flags, IID.IPropertyStore, out pUnk);
             *              _pStore = (IPropertyStore)Marshal.GetUniqueObjectForIUnknown(pUnk);
             *      }
             *      finally {
             *              if (pUnk != IntPtr.Zero)
             *                      Marshal.Release(pUnk);
             *      }
             * }*/
        }
Пример #4
0
        //This method allows to specify a scope from which to start search.
        private ScopeVar GetVariable(string name, Scope startScope, GetFlags getFlags, int line, int col)
        {
            var cur   = startScope;
            var shift = 0;
            var var   = ScopeVar.Empty;

            //Walks the scopes recursively to look for a variable
            do
            {
                if (cur.Locals.TryGetValue(name, out var))
                {
                    var.Address = shift | var.Address << 8;

                    if ((var.Flags & ElaVariableFlags.NoInit) == ElaVariableFlags.NoInit &&
                        (var.Flags & ElaVariableFlags.Function) != ElaVariableFlags.Function &&
                        (var.Flags & ElaVariableFlags.ClassFun) != ElaVariableFlags.ClassFun &&
                        (var.Flags & ElaVariableFlags.TypeFun) != ElaVariableFlags.TypeFun)
                    {
                        cleans.Replace(false);
                    }

                    return(var);
                }

                if (cur.Function)
                {
                    shift++;
                }

                var = ScopeVar.Empty;
                cur = cur.Parent;
            }while (cur != null);

            //If this flag is set we don't need to go further
            if ((getFlags & GetFlags.Local) == GetFlags.Local)
            {
                if (!options.IgnoreUndefined && (getFlags & GetFlags.NoError) != GetFlags.NoError)
                {
                    AddError(ElaCompilerError.UndefinedName, line, col, name);
                }

                return(ScopeVar.Empty);
            }

            return(GetExternVariable(name, getFlags, line, col));
        }
Пример #5
0
        //Main method to query a variable that starts to search a variable in
        //the current scope.
        private ScopeVar GetGlobalVariable(string name, GetFlags flags, int line, int col)
        {
            var cur   = CurrentScope;
            var shift = 0;
            var var   = ScopeVar.Empty;

            //Rolls the scopes to find global
            while (cur.Parent != null)
            {
                if (cur.Function)
                {
                    shift++;
                }

                cur = cur.Parent;
            }

            if (globalScope.Locals.TryGetValue(name, out var))
            {
                var.Address = shift | var.Address << 8;
                return(var);
            }

            //If this flag is set we don't need to go further
            if ((flags & GetFlags.Local) == GetFlags.Local)
            {
                if (!options.IgnoreUndefined && (flags & GetFlags.NoError) != GetFlags.NoError)
                {
                    AddError(ElaCompilerError.UndefinedName, line, col, name);
                }

                return(ScopeVar.Empty);
            }

            return(GetExternVariable(name, flags, line, col));
        }
Пример #6
0
        //This method allows to specify a scope from which to start search.
        private ScopeVar GetVariable(string name, Scope startScope, GetFlags getFlags, int line, int col)
        {
            var cur = startScope;
            var shift = 0;
            var var = ScopeVar.Empty;

            //Walks the scopes recursively to look for a variable
            do
            {
                if (cur.Locals.TryGetValue(name, out var))
                {
                    var.Address = shift | var.Address << 8;

                    if ((var.Flags & ElaVariableFlags.NoInit) == ElaVariableFlags.NoInit &&
                        (var.Flags & ElaVariableFlags.Function) != ElaVariableFlags.Function &&
                        (var.Flags & ElaVariableFlags.ClassFun) != ElaVariableFlags.ClassFun &&
                        (var.Flags & ElaVariableFlags.TypeFun) != ElaVariableFlags.TypeFun)
                        cleans.Replace(false);

                    return var;
                }

                if (cur.Function)
                    shift++;

                var = ScopeVar.Empty;
                cur = cur.Parent;
            }
            while (cur != null);

            //If this flag is set we don't need to go further
            if ((getFlags & GetFlags.Local) == GetFlags.Local)
            {
                if (!options.IgnoreUndefined && (getFlags & GetFlags.NoError) != GetFlags.NoError)
                    AddError(ElaCompilerError.UndefinedName, line, col, name);

                return ScopeVar.Empty;
            }

            return GetExternVariable(name, getFlags, line, col);
        }
Пример #7
0
        //Main method to query a variable that starts to search a variable in
        //the current scope.
        private ScopeVar GetGlobalVariable(string name, GetFlags flags, int line, int col)
        {
            var cur = CurrentScope;
            var shift = 0;
            var var = ScopeVar.Empty;

            //Rolls the scopes to find global
            while (cur.Parent != null)
            {
                if (cur.Function)
                    shift++;

                cur = cur.Parent;
            }

            if (globalScope.Locals.TryGetValue(name, out var))
            {
                var.Address = shift | var.Address << 8;
                return var;
            }

            //If this flag is set we don't need to go further
            if ((flags & GetFlags.Local) == GetFlags.Local)
            {
                if (!options.IgnoreUndefined && (flags & GetFlags.NoError) != GetFlags.NoError)
                    AddError(ElaCompilerError.UndefinedName, line, col, name);

                return ScopeVar.Empty;
            }

            return GetExternVariable(name, flags, line, col);
        }
Пример #8
0
        //Directily searches a name in an export list.
        private ScopeVar GetExternVariable(string name, GetFlags getFlags, int line, int col)
        {
            var vk = default(ExportVarData);

            //Looks for variable in export list
            if (exports.FindVariable(name, out vk))
            {
                var flags = vk.Flags;
                var data = vk.Data;

                //All externals are added to the list of 'latebounds'. These names are then verified by a linker.
                //This verification is used only when a module is deserialized from an object file.
                frame.LateBounds.Add(new LateBoundSymbol(name, vk.ModuleHandle | vk.Address << 8, data, (Int32)flags, line, col));
                return new ScopeVar(flags | ElaVariableFlags.External, vk.ModuleHandle | vk.Address << 8, data);
            }
            else
            {
                if (!options.IgnoreUndefined && (getFlags & GetFlags.NoError) != GetFlags.NoError)
                    AddError(ElaCompilerError.UndefinedName, line, col, name);

                return ScopeVar.Empty;
            }
        }
Пример #9
0
        private void buttonParse_Click(object sender, EventArgs e)
        {
            Tools.HandleApiExceptions(delegate()
            {
                initSelection();
                entries.Clear();
                using (Desko.Mrz.MrzDescriptor desc = new Desko.Mrz.MrzDescriptor())
                {
                    GetFlags getFlags = GetFlags.None;
                    foreach (GetFlags f in listBoxGetFlags.SelectedItems)
                    {
                        getFlags = (getFlags | f);
                    }

                    desc.ParseMrz(textBoxMrz.Text);

                    Desko.Mrz.DocField subfield = (Desko.Mrz.DocField)comboBoxSubstitutionField.SelectedItem;
                    foreach (Desko.Mrz.DocField v in Enum.GetValues(typeof(Desko.Mrz.DocField)))
                    {
                        if (v == subfield && checkBoxSubstitute.Checked)
                        {
                            using (SubstitutionRules rules = new SubstitutionRules())
                            {
                                rules.InitFromString(textBoxSubstitutions.Text);

                                entries.Add(new MrzEntry {
                                    Field = v.ToString(), Text = rules.Substitute(desc.GetField(v, getFlags))
                                });
                            }
                        }
                        else
                        {
                            entries.Add(new MrzEntry {
                                Field = v.ToString(), Text = desc.GetField(v, getFlags)
                            });
                        }
                    }

                    try
                    {
                        stateLabelIcao.State = desc.ValidateIcao() ? LabelState.Good : LabelState.Bad;
                    }
                    catch (MrzException ex)
                    {
                        stateLabelIcao.State = LabelState.Attention;
                    }
                    try
                    {
                        stateLabelChecksumValid.State = desc.ValidateAllChecksums() ? LabelState.Good : LabelState.Bad;
                    } catch (MrzException ex) {
                        stateLabelChecksumValid.State = LabelState.Attention;
                    }
                    try
                    {
                        stateLabelCharsValid.State = desc.ValidateCharacters() ? LabelState.Good : LabelState.Bad;
                    }
                    catch (MrzException ex)
                    {
                        stateLabelCharsValid.State = LabelState.Attention;
                    }
                    try
                    {
                        stateLabelDaysTillExpiry.State = desc.ValidateExpiryDate((int)numericUpDownMinDaysTillExpiry.Value) ? LabelState.Good : LabelState.Bad;
                    }
                    catch (MrzException ex)
                    {
                        stateLabelDaysTillExpiry.State = LabelState.Attention;
                    }
                    try
                    {
                        stateLabelMinAge.State = desc.ValidateMinimumAge((int)numericUpDownMinAge.Value) ? LabelState.Good : LabelState.Bad;
                    }
                    catch (MrzException ex)
                    {
                        stateLabelMinAge.State = LabelState.Attention;
                    }
                    stateLabelDocType.Text = desc.DocType.ToString();

                    dataGridViewDocFields.DataSource = entries;
                    dataGridViewDocFields.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
                    dataGridViewDocFields.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
                }
            });
        }