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