windows: make it easier to iterate over groups and privileges
Rather than having to write grotesque things like:
groups, _ := processToken.GetTokenGroups()
for _, g := range (*[(1 << 28) - 1]windows.SIDAndAttributes)(unsafe.Pointer(&groups.Groups[0]))[:groups.GroupCount] {
// ...
}
Users can now write clean things like this:
groups, _ := processToken.GetTokenGroups()
for _, g := range groups.AllGroups() {
// ...
}
Change-Id: Ief06de6899c497175628ff51b9d6ae55a90d14f1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178857
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
diff --git a/windows/security_windows.go b/windows/security_windows.go
index b84840f..61b4964 100644
--- a/windows/security_windows.go
+++ b/windows/security_windows.go
@@ -603,12 +603,22 @@
type Tokengroups struct {
GroupCount uint32
- Groups [1]SIDAndAttributes
+ Groups [1]SIDAndAttributes // Use AllGroups() for iterating.
+}
+
+// AllGroups returns a slice that can be used to iterate over the groups in g.
+func (g *Tokengroups) AllGroups() []SIDAndAttributes {
+ return (*[(1 << 28) - 1]SIDAndAttributes)(unsafe.Pointer(&g.Groups[0]))[:g.GroupCount:g.GroupCount]
}
type Tokenprivileges struct {
PrivilegeCount uint32
- Privileges [1]LUIDAndAttributes
+ Privileges [1]LUIDAndAttributes // Use AllPrivileges() for iterating.
+}
+
+// AllPrivileges returns a slice that can be used to iterate over the privileges in p.
+func (p *Tokenprivileges) AllPrivileges() []LUIDAndAttributes {
+ return (*[(1 << 27) - 1]LUIDAndAttributes)(unsafe.Pointer(&p.Privileges[0]))[:p.PrivilegeCount:p.PrivilegeCount]
}
type Tokenmandatorylabel struct {