@@ -42,21 +42,20 @@ func TranslateAccountName(username string, from, to uint32, initSize int) (strin
42
42
if e != nil {
43
43
return "" , e
44
44
}
45
- b := make ([]uint16 , 50 )
46
- n := uint32 (len (b ))
47
- e = TranslateName (u , from , to , & b [0 ], & n )
48
- if e != nil {
45
+ n := uint32 (50 )
46
+ for {
47
+ b := make ([]uint16 , n )
48
+ e = TranslateName (u , from , to , & b [0 ], & n )
49
+ if e == nil {
50
+ return UTF16ToString (b [:n ]), nil
51
+ }
49
52
if e != ERROR_INSUFFICIENT_BUFFER {
50
53
return "" , e
51
54
}
52
- // make receive buffers of requested size and try again
53
- b = make ([]uint16 , n )
54
- e = TranslateName (u , from , to , & b [0 ], & n )
55
- if e != nil {
55
+ if n <= uint32 (len (b )) {
56
56
return "" , e
57
57
}
58
58
}
59
- return UTF16ToString (b ), nil
60
59
}
61
60
62
61
const (
@@ -137,26 +136,23 @@ func LookupSID(system, account string) (sid *SID, domain string, accType uint32,
137
136
return nil , "" , 0 , e
138
137
}
139
138
}
140
- db := make ([]uint16 , 50 )
141
- dn := uint32 (len (db ))
142
- b := make ([]byte , 50 )
143
- n := uint32 (len (b ))
144
- sid = (* SID )(unsafe .Pointer (& b [0 ]))
145
- e = LookupAccountName (sys , acc , sid , & n , & db [0 ], & dn , & accType )
146
- if e != nil {
139
+ n := uint32 (50 )
140
+ dn := uint32 (50 )
141
+ for {
142
+ b := make ([]byte , n )
143
+ db := make ([]uint16 , dn )
144
+ sid = (* SID )(unsafe .Pointer (& b [0 ]))
145
+ e = LookupAccountName (sys , acc , sid , & n , & db [0 ], & dn , & accType )
146
+ if e == nil {
147
+ return sid , UTF16ToString (db ), accType , nil
148
+ }
147
149
if e != ERROR_INSUFFICIENT_BUFFER {
148
150
return nil , "" , 0 , e
149
151
}
150
- // make receive buffers of requested size and try again
151
- b = make ([]byte , n )
152
- sid = (* SID )(unsafe .Pointer (& b [0 ]))
153
- db = make ([]uint16 , dn )
154
- e = LookupAccountName (sys , acc , sid , & n , & db [0 ], & dn , & accType )
155
- if e != nil {
152
+ if n <= uint32 (len (b )) {
156
153
return nil , "" , 0 , e
157
154
}
158
155
}
159
- return sid , UTF16ToString (db ), accType , nil
160
156
}
161
157
162
158
// String converts sid to a string format
@@ -198,24 +194,22 @@ func (sid *SID) LookupAccount(system string) (account, domain string, accType ui
198
194
return "" , "" , 0 , err
199
195
}
200
196
}
201
- b := make ([]uint16 , 50 )
202
- n := uint32 (len (b ))
203
- db := make ([]uint16 , 50 )
204
- dn := uint32 (len (db ))
205
- e := LookupAccountSid (sys , sid , & b [0 ], & n , & db [0 ], & dn , & accType )
206
- if e != nil {
197
+ n := uint32 (50 )
198
+ dn := uint32 (50 )
199
+ for {
200
+ b := make ([]uint16 , n )
201
+ db := make ([]uint16 , dn )
202
+ e := LookupAccountSid (sys , sid , & b [0 ], & n , & db [0 ], & dn , & accType )
203
+ if e == nil {
204
+ return UTF16ToString (b ), UTF16ToString (db ), accType , nil
205
+ }
207
206
if e != ERROR_INSUFFICIENT_BUFFER {
208
207
return "" , "" , 0 , e
209
208
}
210
- // make receive buffers of requested size and try again
211
- b = make ([]uint16 , n )
212
- db = make ([]uint16 , dn )
213
- e = LookupAccountSid (nil , sid , & b [0 ], & n , & db [0 ], & dn , & accType )
214
- if e != nil {
209
+ if n <= uint32 (len (b )) {
215
210
return "" , "" , 0 , e
216
211
}
217
212
}
218
- return UTF16ToString (b ), UTF16ToString (db ), accType , nil
219
213
}
220
214
221
215
const (
@@ -327,21 +321,20 @@ func (t Token) Close() error {
327
321
328
322
// getInfo retrieves a specified type of information about an access token.
329
323
func (t Token ) getInfo (class uint32 , initSize int ) (unsafe.Pointer , error ) {
330
- b := make ([]byte , initSize )
331
- var n uint32
332
- e := GetTokenInformation (t , class , & b [0 ], uint32 (len (b )), & n )
333
- if e != nil {
324
+ n := uint32 (initSize )
325
+ for {
326
+ b := make ([]byte , n )
327
+ e := GetTokenInformation (t , class , & b [0 ], uint32 (len (b )), & n )
328
+ if e == nil {
329
+ return unsafe .Pointer (& b [0 ]), nil
330
+ }
334
331
if e != ERROR_INSUFFICIENT_BUFFER {
335
332
return nil , e
336
333
}
337
- // make receive buffers of requested size and try again
338
- b = make ([]byte , n )
339
- e = GetTokenInformation (t , class , & b [0 ], uint32 (len (b )), & n )
340
- if e != nil {
334
+ if n <= uint32 (len (b )) {
341
335
return nil , e
342
336
}
343
337
}
344
- return unsafe .Pointer (& b [0 ]), nil
345
338
}
346
339
347
340
// GetTokenUser retrieves access token t user account information.
@@ -367,19 +360,18 @@ func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
367
360
// GetUserProfileDirectory retrieves path to the
368
361
// root directory of the access token t user's profile.
369
362
func (t Token ) GetUserProfileDirectory () (string , error ) {
370
- b := make ([]uint16 , 100 )
371
- n := uint32 (len (b ))
372
- e := GetUserProfileDirectory (t , & b [0 ], & n )
373
- if e != nil {
363
+ n := uint32 (100 )
364
+ for {
365
+ b := make ([]uint16 , n )
366
+ e := GetUserProfileDirectory (t , & b [0 ], & n )
367
+ if e == nil {
368
+ return UTF16ToString (b ), nil
369
+ }
374
370
if e != ERROR_INSUFFICIENT_BUFFER {
375
371
return "" , e
376
372
}
377
- // make receive buffers of requested size and try again
378
- b = make ([]uint16 , n )
379
- e = GetUserProfileDirectory (t , & b [0 ], & n )
380
- if e != nil {
373
+ if n <= uint32 (len (b )) {
381
374
return "" , e
382
375
}
383
376
}
384
- return UTF16ToString (b ), nil
385
377
}
0 commit comments