r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
[metze/samba/wip.git] / source3 / registry / reg_perfcount.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit    2005,
6  *  Copyright (C) Gerald (Jerry) Carter      2005.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28 #define PERFCOUNT_MAX_LEN 256
29
30 #define PERFCOUNTDIR    "perfmon"
31 #define NAMES_DB        "names.tdb"
32 #define DATA_DB         "data.tdb"
33
34 /*********************************************************************
35 *********************************************************************/
36
37 static char* counters_directory( const char *dbname )
38 {
39         static pstring fname;
40         fstring path;
41         
42         if ( !dbname )
43                 return NULL;
44         
45         fstr_sprintf( path, "%s/%s", PERFCOUNTDIR, dbname );
46         
47         pstrcpy( fname, lock_path( path ) );
48         
49         return fname;
50 }
51
52 /*********************************************************************
53 *********************************************************************/
54
55 void perfcount_init_keys( void )
56 {
57         char *p = lock_path(PERFCOUNTDIR);
58
59         /* no registry keys; just create the perfmon directory */
60         
61         if ( !directory_exist( p, NULL ) )
62                 mkdir( p, 0755 );
63         
64         return;
65 }
66
67 /*********************************************************************
68 *********************************************************************/
69
70 uint32 reg_perfcount_get_base_index(void)
71 {
72         const char *fname = counters_directory( NAMES_DB );
73         TDB_CONTEXT *names;
74         TDB_DATA kbuf, dbuf;
75         char key[] = "1";
76         uint32 retval = 0;
77         char buf[PERFCOUNT_MAX_LEN];
78
79         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
80
81         if ( !names ) {
82                 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
83                 return 0;
84         }    
85         /* needs to read the value of key "1" from the counter_names.tdb file, as that is
86            where the total number of counters is stored. We're assuming no holes in the
87            enumeration.
88            The format for the counter_names.tdb file is:
89            key        value
90            1          num_counters
91            2          perf_counter1
92            3          perf_counter1_help
93            4          perf_counter2
94            5          perf_counter2_help
95            even_num   perf_counter<even_num>
96            even_num+1 perf_counter<even_num>_help
97            and so on.
98            So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
99         kbuf.dptr = key;
100         kbuf.dsize = strlen(key);
101         dbuf = tdb_fetch(names, kbuf);
102         if(dbuf.dptr == NULL)
103         {
104                 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
105                 tdb_close(names);
106                 return 0;
107         }
108         else
109         {
110                 tdb_close(names);
111                 memset(buf, 0, PERFCOUNT_MAX_LEN);
112                 memcpy(buf, dbuf.dptr, dbuf.dsize);
113                 retval = (uint32)atoi(buf);
114                 SAFE_FREE(dbuf.dptr);
115                 return retval;
116         }
117         return 0;
118 }
119
120 /*********************************************************************
121 *********************************************************************/
122
123 uint32 reg_perfcount_get_last_counter(uint32 base_index)
124 {
125         uint32 retval;
126
127         if(base_index == 0)
128                 retval = 0;
129         else
130                 retval = base_index * 2;
131
132         return retval;
133 }
134
135 /*********************************************************************
136 *********************************************************************/
137
138 uint32 reg_perfcount_get_last_help(uint32 last_counter)
139 {
140         uint32 retval;
141
142         if(last_counter == 0)
143                 retval = 0;
144         else
145                 retval = last_counter + 1;
146
147         return retval;
148 }
149
150
151 /*********************************************************************
152 *********************************************************************/
153
154 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
155                                                int keyval,
156                                                char **retbuf,
157                                                uint32 buffer_size)
158 {
159         TDB_DATA kbuf, dbuf;
160         char temp[256];
161         char *buf1 = *retbuf;
162         uint32 working_size = 0;
163         UNISTR2 name_index, name;
164
165         memset(temp, 0, sizeof(temp));
166         snprintf(temp, sizeof(temp), "%d", keyval);
167         kbuf.dptr = temp;
168         kbuf.dsize = strlen(temp);
169         dbuf = tdb_fetch(tdb, kbuf);
170         if(dbuf.dptr == NULL)
171         {
172                 /* If a key isn't there, just bypass it -- this really shouldn't 
173                    happen unless someone's mucking around with the tdb */
174                 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
175                           temp, tdb->name));
176                 return buffer_size;
177         }
178         /* First encode the name_index */
179         working_size = (kbuf.dsize + 1)*sizeof(uint16);
180         buf1 = SMB_REALLOC(buf1, buffer_size + working_size);
181         if(!buf1) {
182                 buffer_size = 0;
183                 return buffer_size;
184         }
185         init_unistr2(&name_index, kbuf.dptr, UNI_STR_TERMINATE);
186         memcpy(buf1+buffer_size, (char *)name_index.buffer, working_size);
187         buffer_size += working_size;
188         /* Now encode the actual name */
189         working_size = (dbuf.dsize + 1)*sizeof(uint16);
190         buf1 = SMB_REALLOC(buf1, buffer_size + working_size);
191         if(!buf1) {
192                 buffer_size = 0;
193                 return buffer_size;
194         }
195         memset(temp, 0, sizeof(temp));
196         memcpy(temp, dbuf.dptr, dbuf.dsize);
197         SAFE_FREE(dbuf.dptr);
198         init_unistr2(&name, temp, UNI_STR_TERMINATE);
199         memcpy(buf1+buffer_size, (char *)name.buffer, working_size);
200         buffer_size += working_size;
201
202         *retbuf = buf1;
203
204         return buffer_size;
205 }
206
207 /*********************************************************************
208 *********************************************************************/
209
210 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
211 {
212         char *buf1 = NULL;
213         uint32 buffer_size = 0;
214         TDB_CONTEXT *names;
215         const char *fname = counters_directory( NAMES_DB );
216         int i;
217
218         if(base_index == 0)
219                 return 0;
220
221         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
222
223         if(names == NULL)
224         {
225                 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
226                 return 0;
227         }    
228
229         for(i = 1; i <= base_index; i++)
230         {
231                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
232         }
233         tdb_close(names);
234
235         /* Now terminate the MULTI_SZ with a double unicode NULL */
236         buf1 = *retbuf;
237         buf1 = SMB_REALLOC(buf1, buffer_size + 2);
238         if(!buf1) {
239                 buffer_size = 0;
240         } else {
241                 buf1[buffer_size++] = '\0';
242                 buf1[buffer_size++] = '\0';
243         }
244
245         *retbuf = buf1;
246
247         return buffer_size;
248 }
249
250 /*********************************************************************
251 *********************************************************************/
252
253 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
254 {
255         char *buf1 = NULL;
256         uint32 buffer_size = 0;
257         TDB_CONTEXT *names;
258         const char *fname = counters_directory( NAMES_DB );
259         int i;
260
261         if(base_index == 0)
262                 return 0;
263
264         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
265
266         if(names == NULL)
267         {
268                 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
269                 return 0;
270         }    
271
272         buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
273
274         for(i = 1; i <= base_index; i++)
275         {
276                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
277         }
278         tdb_close(names);
279
280         /* Now terminate the MULTI_SZ with a double unicode NULL */
281         buf1 = *retbuf;
282         buf1 = SMB_REALLOC(buf1, buffer_size + 2);
283         if(!buf1) {
284                 buffer_size = 0;
285         } else {
286                 buf1[buffer_size++] = '\0';
287                 buf1[buffer_size++] = '\0';
288         }
289
290         *retbuf=buf1;
291
292         return buffer_size;
293 }
294
295 /*********************************************************************
296 *********************************************************************/
297
298 static void _reg_perfcount_make_key(TDB_DATA *key,
299                                     char *buf,
300                                     int buflen,
301                                     int key_part1,
302                                     const char *key_part2)
303 {
304         memset(buf, 0, buflen);
305         if(key_part2 != NULL)
306                 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
307         else 
308                 snprintf(buf, buflen, "%d", key_part1);
309
310         key->dptr = buf;
311         key->dsize = strlen(buf);
312
313         return;
314 }
315
316 /*********************************************************************
317 *********************************************************************/
318
319 static BOOL _reg_perfcount_isparent(TDB_DATA data)
320 {
321         if(data.dsize > 0)
322         {
323                 if(data.dptr[0] == 'p')
324                         return True;
325                 else
326                         return False;
327         }
328         return False;
329 }
330
331 /*********************************************************************
332 *********************************************************************/
333
334 static BOOL _reg_perfcount_ischild(TDB_DATA data)
335 {
336         if(data.dsize > 0)
337         {
338                 if(data.dptr[0] == 'c')
339                         return True;
340                 else
341                         return False;
342         }
343         return False;
344 }
345
346 /*********************************************************************
347 *********************************************************************/
348
349 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
350 {
351         TDB_DATA key, data;
352         char buf[PERFCOUNT_MAX_LEN];
353
354         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
355         data = tdb_fetch(names, key);
356
357         if(data.dptr == NULL)
358                 return (uint32)PERF_NO_INSTANCES;
359     
360         memset(buf, 0, PERFCOUNT_MAX_LEN);
361         memcpy(buf, data.dptr, data.dsize);
362         return (uint32)atoi(buf);
363 }
364
365 /*********************************************************************
366 *********************************************************************/
367
368 static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
369                                       prs_struct *ps,
370                                       int num,
371                                       TDB_DATA data,
372                                       TDB_CONTEXT *names)
373 {
374         int i;
375         BOOL success = False;
376         PERF_OBJECT_TYPE *obj;
377
378         block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
379                                                                   block->objects,
380                                                                   PERF_OBJECT_TYPE,
381                                                                   block->NumObjectTypes+1);
382         if(block->objects == NULL)
383                 return False;
384         obj = &(block->objects[block->NumObjectTypes]);
385         memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(PERF_OBJECT_TYPE));
386         block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
387         block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
388         block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
389         block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
390         block->objects[block->NumObjectTypes].NumCounters = 0;
391         block->objects[block->NumObjectTypes].DefaultCounter = 0;
392         block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
393         block->objects[block->NumObjectTypes].counters = NULL;
394         block->objects[block->NumObjectTypes].instances = NULL;
395         block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
396         block->objects[block->NumObjectTypes].counter_data.data = NULL;
397         block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
398         block->NumObjectTypes+=1;
399
400         for(i = 0; i < (int)obj->NumInstances; i++)
401         {
402                 success = _reg_perfcount_add_instance(obj, ps, i, names);
403         }
404
405         return True;
406 }
407
408 /*********************************************************************
409 *********************************************************************/
410
411 BOOL _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
412 {
413         TDB_CONTEXT *counters;
414         const char *fname = counters_directory( DATA_DB );
415     
416         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
417
418         if(counters == NULL)
419         {
420                 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
421                 return False;
422         }    
423
424         *data = tdb_fetch(counters, key);
425     
426         tdb_close(counters);
427
428         return True;
429 }
430
431 /*********************************************************************
432 *********************************************************************/
433
434 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
435 {
436         uint32 retval;
437
438         retval = CounterType;
439
440         /* First mask out reserved lower 8 bits */
441         retval = retval & 0xFFFFFF00;
442         retval = retval << 22;
443         retval = retval >> 22;
444
445         return retval;
446 }
447
448 /*********************************************************************
449 *********************************************************************/
450
451 static uint32 _reg_perfcount_compute_scale(SMB_BIG_INT data)
452 {
453         int scale = 0;
454         if(data == 0)
455                 return scale;
456         while(data > 100)
457         {
458                 data /= 10;
459                 scale--;
460         }
461         while(data < 10)
462         {
463                 data *= 10;
464                 scale++;
465         }
466
467         return (uint32)scale;
468 }
469
470 /*********************************************************************
471 *********************************************************************/
472
473 static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
474                                             prs_struct *ps,
475                                             int CounterIndex,
476                                             PERF_OBJECT_TYPE *obj,
477                                             TDB_CONTEXT *names)
478 {
479         TDB_DATA key, data;
480         char buf[PERFCOUNT_MAX_LEN];
481         size_t dsize, padding;
482         long int data32, dbuf[2];
483         SMB_BIG_INT data64;
484         uint32 counter_size;
485
486         obj->counters[obj->NumCounters].DefaultScale = 0;
487         dbuf[0] = dbuf[1] = 0;
488         padding = 0;
489
490         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
491         data = tdb_fetch(names, key);
492         if(data.dptr == NULL)
493         {
494                 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
495                 return False;
496         }
497         memset(buf, 0, PERFCOUNT_MAX_LEN);
498         memcpy(buf, data.dptr, data.dsize);
499         obj->counters[obj->NumCounters].CounterType = atoi(buf);
500         DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
501                    obj->counters[obj->NumCounters].CounterType, CounterIndex));
502         free(data.dptr);
503
504         /* Fetch the actual data */
505         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
506         _reg_perfcount_get_counter_data(key, &data);
507         if(data.dptr == NULL)
508         {
509                 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
510                 return False;
511         }
512     
513         counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
514
515         if(counter_size == PERF_SIZE_DWORD)
516         {
517                 dsize = sizeof(data32);
518                 memset(buf, 0, PERFCOUNT_MAX_LEN);
519                 memcpy(buf, data.dptr, data.dsize);
520                 data32 = strtol(buf, NULL, 0);
521                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
522                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((SMB_BIG_INT)data32);
523                 else
524                         obj->counters[obj->NumCounters].DefaultScale = 0;
525                 dbuf[0] = data32;
526                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
527         }
528         else if(counter_size == PERF_SIZE_LARGE)
529         {
530                 dsize = sizeof(data64);
531                 memset(buf, 0, PERFCOUNT_MAX_LEN);
532                 memcpy(buf, data.dptr, data.dsize);
533                 data64 = atof(buf);
534                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
535                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
536                 else
537                         obj->counters[obj->NumCounters].DefaultScale = 0;
538                 memcpy((void *)dbuf, (const void *)&data64, dsize);
539                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
540         }
541         else /* PERF_SIZE_VARIABLE_LEN */
542         {
543                 dsize = data.dsize;
544                 memset(buf, 0, PERFCOUNT_MAX_LEN);
545                 memcpy(buf, data.dptr, data.dsize);
546         }
547         free(data.dptr);
548
549         obj->counter_data.ByteLength += dsize + padding;
550         obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
551                                                       obj->counter_data.data,
552                                                       uint8,
553                                                       obj->counter_data.ByteLength - sizeof(uint32));
554         if(obj->counter_data.data == NULL)
555                 return False;
556         if(dbuf[0] != 0 || dbuf[1] != 0)
557         {
558                 memcpy((void *)(obj->counter_data.data + 
559                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))), 
560                        (const void *)dbuf, dsize);
561         }
562         else
563         {
564                 /* Handling PERF_SIZE_VARIABLE_LEN */
565                 memcpy((void *)(obj->counter_data.data +
566                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
567                        (const void *)buf, dsize);
568         }
569         obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
570         if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
571         {
572                 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
573         }
574         obj->counters[obj->NumCounters].CounterSize = dsize;
575
576         return True;
577 }
578
579 /*********************************************************************
580 *********************************************************************/
581
582 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
583 {
584         int i;
585
586         PERF_OBJECT_TYPE *obj = NULL;
587
588         for(i = 0; i < block->NumObjectTypes; i++)
589         {
590                 if(block->objects[i].ObjectNameTitleIndex == objind)
591                 {
592                         obj = &(block->objects[i]);
593                 }
594         }
595
596         return obj;
597 }
598
599 /*********************************************************************
600 *********************************************************************/
601
602 static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
603                                        prs_struct *ps,
604                                        int num,
605                                        TDB_DATA data,
606                                        TDB_CONTEXT *names)
607 {
608         char *begin, *end, *start, *stop;
609         int parent;
610         PERF_OBJECT_TYPE *obj;
611         BOOL success = False;
612         char buf[PERFCOUNT_MAX_LEN];
613     
614         obj = NULL;
615         memset(buf, 0, PERFCOUNT_MAX_LEN);
616         memcpy(buf, data.dptr, data.dsize);
617         begin = index(buf, '[');
618         end = index(buf, ']');
619         if(begin == NULL || end == NULL)
620                 return False;
621         start = begin+1;
622
623         while(start < end)
624         {
625                 stop = index(start, ',');
626                 if(stop == NULL)
627                         stop = end;
628                 *stop = '\0';
629                 parent = atoi(start);
630
631                 obj = _reg_perfcount_find_obj(block, parent);
632                 if(obj == NULL)
633                 {
634                         /* At this point we require that the parent object exist.
635                            This can probably be handled better at some later time */
636                         DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
637                                   parent, num));
638                         return False;
639                 }
640                 obj->counters = (PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
641                                                                                 obj->counters,
642                                                                                 PERF_COUNTER_DEFINITION,
643                                                                                 obj->NumCounters+1);
644                 if(obj->counters == NULL)
645                         return False;
646                 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(PERF_COUNTER_DEFINITION));
647                 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
648                 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
649                 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
650                 obj->counters[obj->NumCounters].ByteLength = sizeof(PERF_COUNTER_DEFINITION);
651                 success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
652                 obj->NumCounters += 1;
653                 start = stop + 1;
654         }
655         
656         /* Handle case of Objects/Counters without any counter data, which would suggest
657            that the required instances are not there yet, so change NumInstances from
658            PERF_NO_INSTANCES to 0 */
659
660         return True;
661 }
662
663 /*********************************************************************
664 *********************************************************************/
665
666 BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
667                                       prs_struct *ps,
668                                       int instId,
669                                       PERF_OBJECT_TYPE *obj,
670                                       TDB_CONTEXT *names)
671 {
672         TDB_DATA key, data;
673         char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
674         wpstring name;
675         int pad;
676
677         /* First grab the instance data from the data file */
678         memset(temp, 0, PERFCOUNT_MAX_LEN);
679         snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
680         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
681         _reg_perfcount_get_counter_data(key, &data);
682         if(data.dptr == NULL)
683         {
684                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
685                           buf));
686                 return False;
687         }
688         inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
689         inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
690                                                        inst->counter_data.data,
691                                                        uint8,
692                                                        data.dsize);
693         if(inst->counter_data.data == NULL)
694                 return False;
695         memset(inst->counter_data.data, 0, data.dsize);
696         memcpy(inst->counter_data.data, data.dptr, data.dsize);
697         free(data.dptr);
698
699         /* Fetch instance name */
700         memset(temp, 0, PERFCOUNT_MAX_LEN);
701         snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
702         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
703         data = tdb_fetch(names, key);
704         if(data.dptr == NULL)
705         {
706                 /* Not actually an error, but possibly unintended? -- just logging FYI */
707                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
708                           buf));
709                 inst->NameLength = 0;
710         }
711         else
712         {
713                 memset(buf, 0, PERFCOUNT_MAX_LEN);
714                 memcpy(buf, data.dptr, data.dsize);
715                 rpcstr_push((void *)name, buf, sizeof(name), STR_TERMINATE);
716                 inst->NameLength = (strlen_w(name) * 2) + 2;
717                 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
718                                                   inst->data,
719                                                   uint8,
720                                                   inst->NameLength);
721                 memcpy(inst->data, name, inst->NameLength);
722                 free(data.dptr);
723         }
724
725         inst->ParentObjectTitleIndex = 0;
726         inst->ParentObjectTitlePointer = 0;
727         inst->UniqueID = PERF_NO_UNIQUE_ID;
728         inst->NameOffset = 6 * sizeof(uint32);
729     
730         inst->ByteLength = inst->NameOffset + inst->NameLength;
731         /* Need to be aligned on a 64-bit boundary here for counter_data */
732         if((pad = (inst->ByteLength % 8)))
733         {
734                 pad = 8 - pad;
735                 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
736                                                   inst->data,
737                                                   uint8,
738                                                   inst->NameLength + pad);
739                 memset(inst->data + inst->NameLength, 0, pad);
740                 inst->ByteLength += pad;
741         }
742
743         return True;
744 }
745
746 /*********************************************************************
747 *********************************************************************/
748
749 BOOL _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
750                                  prs_struct *ps,
751                                  int instInd,
752                                  TDB_CONTEXT *names)
753 {
754         BOOL success;
755         PERF_INSTANCE_DEFINITION *inst;
756
757         success = False;
758
759         if(obj->instances == NULL)
760         {
761                 obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
762                                                       obj->instances,
763                                                       PERF_INSTANCE_DEFINITION,
764                                                       obj->NumInstances);
765         }
766         if(obj->instances == NULL)
767                 return False;
768     
769         memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
770         inst = &(obj->instances[instInd]);
771         success = _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
772     
773         return True;
774 }
775
776 /*********************************************************************
777 *********************************************************************/
778
779 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
780                                           prs_struct *ps,
781                                           int base_index,
782                                           TDB_CONTEXT *names)
783 {
784         BOOL success;
785         int i, j, retval = 0;
786         char keybuf[PERFCOUNT_MAX_LEN];
787         TDB_DATA key, data;
788
789         for(i = 1; i <= base_index; i++)
790         {
791                 j = i*2;
792                 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
793                 data = tdb_fetch(names, key);
794                 if(data.dptr != NULL)
795                 {
796                         if(_reg_perfcount_isparent(data))
797                                 success = _reg_perfcount_add_object(block, ps, j, data, names);
798                         else if(_reg_perfcount_ischild(data))
799                                 success = _reg_perfcount_add_counter(block, ps, j, data, names);
800                         else
801                         {
802                                 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
803                                 success = False;
804                         }
805                         if(success == False)
806                         {
807                                 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
808                                 retval = -1;
809                         }
810                         free(data.dptr);
811                 }
812                 else
813                         DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
814         }       
815         return retval;
816 }
817
818 /*********************************************************************
819 *********************************************************************/
820
821 static BOOL _reg_perfcount_get_64(SMB_BIG_UINT *retval,
822                                   TDB_CONTEXT *tdb,
823                                   int key_part1,
824                                   const char *key_part2)
825 {
826         TDB_DATA key, data;
827         char buf[PERFCOUNT_MAX_LEN];
828
829         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
830
831         data = tdb_fetch(tdb, key);
832         if(data.dptr == NULL)
833         {
834                 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
835                 return False;
836         }
837
838         memset(buf, 0, PERFCOUNT_MAX_LEN);
839         memcpy(buf, data.dptr, data.dsize);
840         free(data.dptr);
841
842         *retval = atof(buf);
843
844         return True;
845 }
846
847 /*********************************************************************
848 *********************************************************************/
849
850 static BOOL _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
851                                                 TDB_CONTEXT *names)
852 {
853         SMB_BIG_UINT PerfFreq, PerfTime, PerfTime100nSec;
854         TDB_CONTEXT *counters;
855         BOOL status = False;
856         const char *fname = counters_directory( DATA_DB );
857     
858         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
859     
860         if(counters == NULL)
861         {
862                 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
863                 return False;
864         }    
865     
866         status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
867         if(status == False)
868         {
869                 tdb_close(counters);
870                 return status;
871         }
872         memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
873
874         status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
875         if(status == False)
876         {
877                 tdb_close(counters);
878                 return status;
879         }
880         memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
881
882         status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
883         if(status == False)
884         {
885                 tdb_close(counters);
886                 return status;
887         }
888         memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
889
890         tdb_close(counters);
891         return True;
892 }
893
894 /*********************************************************************
895 *********************************************************************/
896
897 static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block, prs_struct *ps, TDB_CONTEXT *names)
898 {
899         wpstring temp;
900         time_t tm;
901  
902         memset(temp, 0, sizeof(temp));
903         rpcstr_push((void *)temp, "PERF", sizeof(temp), STR_TERMINATE);
904         memcpy(block->Signature, temp, strlen_w(temp) *2);
905
906         if(ps->bigendian_data == RPC_BIG_ENDIAN)
907                 block->LittleEndian = 0;
908         else
909                 block->LittleEndian = 1;
910         block->Version = 1;
911         block->Revision = 1;
912         block->TotalByteLength = 0;
913         block->NumObjectTypes = 0;
914         block->DefaultObject = -1;
915         block->objects = NULL;
916         tm = time(NULL);
917         make_systemtime(&(block->SystemTime), gmtime(&tm));
918         _reg_perfcount_init_data_block_perf(block, names);
919         memset(temp, 0, sizeof(temp));
920         rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
921         block->SystemNameLength = (strlen_w(temp) * 2) + 2;
922         block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
923         memcpy(block->data, temp, block->SystemNameLength);
924         block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data); 
925         block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
926         /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
927            so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
928         block->HeaderLength += 8 - (block->HeaderLength % 8);
929
930         return;
931 }
932
933 /*********************************************************************
934 *********************************************************************/
935
936 static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
937 {
938         int obj, cnt, inst, pad, i;
939         PERF_OBJECT_TYPE *object;
940         PERF_INSTANCE_DEFINITION *instance;
941         PERF_COUNTER_DEFINITION *counter;
942         PERF_COUNTER_BLOCK *counter_data;
943         char *temp = NULL, *src_addr, *dst_addr;
944
945         block->TotalByteLength = 0;
946         object = block->objects;
947         for(obj = 0; obj < block->NumObjectTypes; obj++)
948         {
949                 object[obj].TotalByteLength = 0;
950                 object[obj].DefinitionLength = 0;
951                 instance = object[obj].instances;
952                 counter = object[obj].counters;
953                 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
954                 {
955                         object[obj].TotalByteLength += counter[cnt].ByteLength;
956                         object[obj].DefinitionLength += counter[cnt].ByteLength;
957                 }
958                 if(object[obj].NumInstances != PERF_NO_INSTANCES)
959                 {
960                         for(inst = 0; inst < object[obj].NumInstances; inst++)
961                         {
962                                 instance = &(object[obj].instances[inst]);
963                                 object[obj].TotalByteLength += instance->ByteLength;
964                                 counter_data = &(instance->counter_data);
965                                 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
966                                 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
967                                 temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
968                                                             temp, 
969                                                             char, 
970                                                             counter_data->ByteLength- sizeof(counter_data->ByteLength));
971                                 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
972                                 src_addr = (char *)counter_data->data;
973                                 for(i = 0; i < object[obj].NumCounters; i++)
974                                 {
975                                         counter = &(object[obj].counters[i]);
976                                         dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
977                                         memcpy(dst_addr, src_addr, counter->CounterSize);
978                                         src_addr += counter->CounterSize;
979                                 }
980                                 /* Make sure to be 64-bit aligned */
981                                 if((pad = (counter_data->ByteLength % 8)))
982                                 {
983                                         pad = 8 - pad;
984                                 }
985                                 counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
986                                                                          counter_data->data,
987                                                                          uint8,
988                                                                          counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
989                                 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
990                                 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
991                                 counter_data->ByteLength += pad;
992                                 object[obj].TotalByteLength += counter_data->ByteLength;
993                         }
994                 }
995                 else
996                 {
997                         /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
998                            so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
999                         if((pad = (object[obj].counter_data.ByteLength % 8)))
1000                         {
1001                                 pad = 8 - pad;
1002                                 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
1003                                                                                      object[obj].counter_data.data,
1004                                                                                      uint8, 
1005                                                                                      object[obj].counter_data.ByteLength + pad);
1006                                 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1007                                 object[obj].counter_data.ByteLength += pad;
1008                         }
1009                         object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1010                 }
1011                 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK));
1012                 object[obj].TotalByteLength += object[obj].HeaderLength;
1013                 object[obj].DefinitionLength += object[obj].HeaderLength;
1014                 
1015                 block->TotalByteLength += object[obj].TotalByteLength;
1016         }
1017
1018         return block->TotalByteLength;
1019 }
1020
1021 /*********************************************************************
1022 *********************************************************************/
1023
1024 uint32 reg_perfcount_get_perf_data_block(uint32 base_index, 
1025                                          prs_struct *ps, 
1026                                          PERF_DATA_BLOCK *block,
1027                                          char *object_ids)
1028 {
1029         uint32 buffer_size = 0, last_counter;
1030         const char *fname = counters_directory( NAMES_DB );
1031         TDB_CONTEXT *names;
1032         int retval;
1033         
1034         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1035
1036         if(names == NULL)
1037         {
1038                 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1039                 return 0;
1040         }
1041
1042         _reg_perfcount_init_data_block(block, ps, names);
1043
1044         last_counter = reg_perfcount_get_last_counter(base_index);
1045     
1046         if(object_ids == NULL)
1047         {
1048                 /* we're getting a request for "Global" here */
1049                 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1050         }
1051         else
1052         {
1053                 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1054                 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1055         }
1056         buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);
1057
1058         tdb_close(names);
1059
1060         return buffer_size + block->HeaderLength;
1061 }
1062
1063 /*********************************************************************
1064 *********************************************************************/
1065
1066 static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1067 {
1068         int i;
1069         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1070         depth++;
1071
1072         if(!prs_align(ps))
1073                 return False;
1074         for(i = 0; i < 4; i++)
1075         {
1076                 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1077                         return False;
1078         }
1079         if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1080                 return False;
1081         if(!prs_uint32("Version", ps, depth, &block.Version))
1082                 return False;
1083         if(!prs_uint32("Revision", ps, depth, &block.Revision))
1084                 return False;
1085         if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1086                 return False;
1087         if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1088                 return False;
1089         if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1090                 return False;
1091         if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1092                 return False;
1093         if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1094                 return False;
1095         if(!prs_uint32("Padding", ps, depth, &block.Padding))
1096                 return False;
1097         if(!prs_align_uint64(ps))
1098                 return False;
1099         if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1100                 return False;
1101         if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1102                 return False;
1103         if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1104                 return False;
1105         if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1106                 return False;
1107         if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1108                 return False;
1109         /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1110         if(!prs_uint8s(False, "SystemName", ps, depth, block.data, 
1111                        block.HeaderLength - block.SystemNameOffset)) 
1112                 return False;
1113
1114         return True;
1115 }
1116
1117 /*********************************************************************
1118 *********************************************************************/
1119
1120 static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1121                                                   PERF_OBJECT_TYPE object,
1122                                                   int depth)
1123 {
1124         int cnt;
1125         PERF_COUNTER_DEFINITION counter;
1126
1127         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1128         depth++;
1129     
1130         for(cnt = 0; cnt < object.NumCounters; cnt++)
1131         {
1132                 counter = object.counters[cnt];
1133
1134                 if(!prs_align(ps))
1135                         return False;
1136                 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1137                         return False;
1138                 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1139                         return False;
1140                 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1141                         return False;
1142                 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1143                         return False;
1144                 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1145                         return False;
1146                 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1147                         return False;
1148                 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1149                         return False;
1150                 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1151                         return False;
1152                 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1153                         return False;
1154                 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1155                         return False;
1156         }
1157
1158         return True;
1159 }
1160
1161 /*********************************************************************
1162 *********************************************************************/
1163
1164 static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
1165                                                       PERF_COUNTER_BLOCK counter_data, 
1166                                                       int depth)
1167 {
1168         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1169         depth++;
1170     
1171         if(!prs_align_uint64(ps))
1172                 return False;
1173     
1174         if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1175                 return False;
1176         if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1177                 return False;
1178         if(!prs_align_uint64(ps))
1179                 return False;
1180
1181         return True;
1182 }
1183
1184 /*********************************************************************
1185 *********************************************************************/
1186
1187 static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1188                                                    PERF_OBJECT_TYPE object, 
1189                                                    int depth)
1190 {
1191         PERF_INSTANCE_DEFINITION instance;
1192         int inst;
1193
1194         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1195         depth++;
1196
1197         for(inst = 0; inst < object.NumInstances; inst++)
1198         {
1199                 instance = object.instances[inst];
1200
1201                 if(!prs_align(ps))
1202                         return False;
1203                 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1204                         return False;
1205                 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1206                         return False;
1207                 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1208                         return False;
1209                 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1210                         return False;
1211                 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1212                         return False;
1213                 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1214                         return False;
1215                 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1216                                instance.ByteLength - instance.NameOffset))
1217                         return False;
1218                 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1219                         return False;
1220         }
1221         
1222         return True;
1223 }
1224
1225 /*********************************************************************
1226 *********************************************************************/
1227
1228 static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1229 {
1230         int obj;
1231
1232         PERF_OBJECT_TYPE object;
1233     
1234         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1235         depth++;
1236
1237         for(obj = 0; obj < block.NumObjectTypes; obj++)
1238         {
1239                 object = block.objects[obj];
1240
1241                 if(!prs_align(ps))
1242                         return False;
1243
1244                 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1245                         return False;
1246                 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1247                         return False;
1248                 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1249                         return False;
1250                 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1251                         return False;
1252                 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1253                         return False;
1254                 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1255                         return False;
1256                 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1257                         return False;
1258                 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1259                         return False;
1260                 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1261                         return False;
1262                 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1263                         return False;
1264                 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1265                         return False;
1266                 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1267                         return False;
1268                 if(!prs_align_uint64(ps))
1269                         return False;
1270                 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1271                         return False;
1272                 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1273                         return False;
1274
1275                 /* Now do the counters */
1276                 /* If no instances, encode counter_data */
1277                 /* If instances, encode instace plus counter data for each instance */
1278                 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1279                         return False;
1280                 if(object.NumInstances == PERF_NO_INSTANCES)
1281                 {
1282                         if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1283                                 return False;
1284                 }
1285                 else
1286                 {
1287                         if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1288                                 return False;
1289                 }
1290         }
1291
1292         return True;
1293 }
1294
1295 /*********************************************************************
1296 *********************************************************************/
1297
1298 static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
1299 {
1300         int depth = 0;
1301         if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
1302         {
1303                 if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
1304                         return True;
1305         }
1306         return False;
1307 }
1308
1309 /*********************************************************************
1310 *********************************************************************/
1311
1312 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, char *object_ids)
1313 {
1314         /*
1315          * For a detailed description of the layout of this structure,
1316          * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1317          */
1318         PERF_DATA_BLOCK block;
1319         uint32 buffer_size, base_index; 
1320     
1321         buffer_size = 0;
1322         base_index = reg_perfcount_get_base_index();
1323         ZERO_STRUCT(block);
1324
1325         buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);
1326
1327         if(buffer_size < max_buf_size)
1328         {
1329                 *outbuf_len = buffer_size;
1330                 if(_reg_perfcount_marshall_hkpd(ps, block) == True)
1331                         return WERR_OK;
1332                 else
1333                         return WERR_NOMEM;
1334         }
1335         else
1336         {
1337                 *outbuf_len = max_buf_size;
1338                 _reg_perfcount_marshall_perf_data_block(ps, block, 0);
1339                 return WERR_INSUFFICIENT_BUFFER;
1340         }
1341 }