/// <summary> /// PolEntryインスタンスからGroupPolicyObjectインスタンスへの変換 /// </summary> /// <param name="polEntry"></param> /// <returns></returns> public static GroupPolicyObject ConvertFromPolEntry(PolEntry polEntry) { string valueKindString = RegistryControl.REG_SZ; switch (polEntry.Type) { case PolEntryType.REG_SZ: valueKindString = RegistryControl.REG_SZ; break; case PolEntryType.REG_BINARY: valueKindString = RegistryControl.REG_BINARY; break; case PolEntryType.REG_DWORD: valueKindString = RegistryControl.REG_DWORD; break; case PolEntryType.REG_QWORD: valueKindString = RegistryControl.REG_QWORD; break; case PolEntryType.REG_MULTI_SZ: valueKindString = RegistryControl.REG_MULTI_SZ; break; case PolEntryType.REG_EXPAND_SZ: valueKindString = RegistryControl.REG_EXPAND_SZ; break; case PolEntryType.REG_NONE: valueKindString = RegistryControl.REG_NONE; break; } return(new GroupPolicyObject() { Path = polEntry.Path, Name = polEntry.Name, Type = valueKindString, Value = RegistryControl.RegistryValueToString(polEntry.Value, valueKindString), }); }
/// <summary> /// Path,Name,Valut,Typeから値をセット /// </summary> /// <param name="path"></param> /// <param name="name"></param> /// <param name="val"></param> /// <param name="type"></param> public void AddValue(string path, string name, object val, PolEntryType type) { PolEntry pe = new PolEntry() { Path = path, Name = name, Type = type, Value = val }; SetValue(pe); }
/// <summary> /// PolEntryを直接セット /// </summary> /// <param name="pe"></param> public void SetValue(PolEntry pe) { pe.SetIndex(_entryIndex++); this.Entries[pe.Path + "\\" + pe.Name] = pe; }
/// <summary> /// Polファイルをロード /// </summary> /// <param name="fileName"></param> public void Load(string fileName) { //if (!string.IsNullOrEmpty(file)) { this.FileName = file; } if (!File.Exists(fileName)) { return; } byte[] bytes; int nBytes = 0; this.Entries.Clear(); this._entryIndex = 0; //using (FileStream fs = new FileStream(this.FileName, FileMode.Open, FileAccess.Read)) using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { bytes = new byte[fs.Length]; int nBytesToRead = (int)fs.Length; while (nBytesToRead > 0) { int n = fs.Read(bytes, nBytes, nBytesToRead); if (n == 0) { break; } nBytes += n; nBytesToRead -= n; } } if (nBytes < 8) { throw new FileFormatException(); } int header = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; int version = (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7]; if (header != PolFile.PolHeader || version != PolFile.PolVersion) { throw new FileFormatException(); } var parseState = PolEntryParseState.Start; int i = 8; var keyName = new StringBuilder(50); var valueName = new StringBuilder(50); uint type = 0; int size = 0; while (i < (nBytes - 1)) { char[] curChar = UnicodeEncoding.Unicode.GetChars(bytes, i, 2); switch (parseState) { case PolEntryParseState.Start: if (curChar[0] != '[') { throw new FileFormatException(); } i += 2; parseState = PolEntryParseState.Key; continue; case PolEntryParseState.Key: if (curChar[0] == '\0') { if (i > (nBytes - 4)) { throw new FileFormatException(); } curChar = UnicodeEncoding.Unicode.GetChars(bytes, i + 2, 2); if (curChar[0] != ';') { throw new FileFormatException(); } i += 4; parseState = PolEntryParseState.ValueName; } else { keyName.Append(curChar[0]); i += 2; } continue; case PolEntryParseState.ValueName: if (curChar[0] == '\0') { if (i > (nBytes - 16)) { throw new FileFormatException(); } curChar = UnicodeEncoding.Unicode.GetChars(bytes, i + 2, 2); if (curChar[0] != ';') { throw new FileFormatException(); } type = (uint)(bytes[i + 7] << 24 | bytes[i + 6] << 16 | bytes[i + 5] << 8 | bytes[i + 4]); if (Enum.IsDefined(typeof(PolEntryType), type) == false) { throw new FileFormatException(); } curChar = UnicodeEncoding.Unicode.GetChars(bytes, i + 8, 2); if (curChar[0] != ';') { throw new FileFormatException(); } size = bytes[i + 13] << 24 | bytes[i + 12] << 16 | bytes[i + 11] << 8 | bytes[i + 10]; if ((size > 0xFFFF) || (size < 0)) { throw new FileFormatException(); } curChar = UnicodeEncoding.Unicode.GetChars(bytes, i + 14, 2); if (curChar[0] != ';') { throw new FileFormatException(); } i += 16; if (i > (nBytes - (size + 2))) { throw new FileFormatException(); } curChar = UnicodeEncoding.Unicode.GetChars(bytes, i + size, 2); if (curChar[0] != ']') { throw new FileFormatException(); } PolEntry pe = new PolEntry(); pe.Path = keyName.ToString(); pe.Name = valueName.ToString(); pe.Type = (PolEntryType)type; for (int j = 0; j < size; j++) { pe.DataBytes.Add(bytes[i + j]); } this.SetValue(pe); i += size + 2; keyName.Length = 0; valueName.Length = 0; parseState = PolEntryParseState.Start; } else { valueName.Append(curChar[0]); i += 2; } continue; default: throw new Exception("Unreachable code"); } } }