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