Move key size fixups from rpmdbAdd & Remove to td2key()
[platform/upstream/rpm.git] / lib / header_internal.c
1 /** \ingroup header
2  * \file lib/header_internal.c
3  */
4
5 #include "system.h"
6
7 #include <rpm/rpmtypes.h>
8 #include "lib/header_internal.h"
9
10 #include "debug.h"
11
12 char ** headerGetLangs(Header h)
13 {
14     char **s, *e, **table;
15     rpmTagType type;
16     rpm_count_t i, count;
17
18     if (!headerGetRawEntry(h, HEADER_I18NTABLE, &type, (rpm_data_t)&s, &count))
19         return NULL;
20
21     /* XXX xcalloc never returns NULL. */
22     if ((table = (char **)xcalloc((count+1), sizeof(char *))) == NULL)
23         return NULL;
24
25     for (i = 0, e = *s; i < count; i++, e += strlen(e)+1)
26         table[i] = e;
27     table[count] = NULL;
28
29     return table;       /* LCL: double indirection? */
30 }
31
32 void headerDump(Header h, FILE *f, int flags)
33 {
34     int i;
35     indexEntry p;
36     const char * tag;
37     const char * type;
38
39     /* First write out the length of the index (count of index entries) */
40     fprintf(f, "Entry count: %d\n", h->indexUsed);
41
42     /* Now write the index */
43     p = h->index;
44     fprintf(f, "\n             CT  TAG                  TYPE         "
45                 "      OFSET      COUNT\n");
46     for (i = 0; i < h->indexUsed; i++) {
47         switch (p->info.type) {
48         case RPM_NULL_TYPE:
49             type = "NULL";
50             break;
51         case RPM_CHAR_TYPE:
52             type = "CHAR";
53             break;
54         case RPM_BIN_TYPE:
55             type = "BIN";
56             break;
57         case RPM_INT8_TYPE:
58             type = "INT8";
59             break;
60         case RPM_INT16_TYPE:
61             type = "INT16";
62             break;
63         case RPM_INT32_TYPE:
64             type = "INT32";
65             break;
66         /*case RPM_INT64_TYPE:          type = "INT64";         break;*/
67         case RPM_STRING_TYPE:
68             type = "STRING";
69             break;
70         case RPM_STRING_ARRAY_TYPE:
71             type = "STRING_ARRAY";
72             break;
73         case RPM_I18NSTRING_TYPE:
74             type = "I18N_STRING";
75             break;
76         default:
77             type = "(unknown)";
78             break;
79         }
80
81         tag = rpmTagGetName(p->info.tag);
82
83         fprintf(f, "Entry      : %3.3d (%d)%-14s %-18s 0x%.8x %.8d\n", i,
84                 p->info.tag, tag, type, (unsigned) p->info.offset,
85                 (int) p->info.count);
86
87         if (flags & HEADER_DUMP_INLINE) {
88             char *dp = p->data;
89             int c = p->info.count;
90             int ct = 0;
91
92             /* Print the data inline */
93             switch (p->info.type) {
94             case RPM_INT32_TYPE:
95                 while (c--) {
96                     fprintf(f, "       Data: %.3d 0x%08x (%d)\n", ct++,
97                             (unsigned) *((int32_t *) dp),
98                             (int) *((int32_t *) dp));
99                     dp += sizeof(int32_t);
100                 }
101                 break;
102
103             case RPM_INT16_TYPE:
104                 while (c--) {
105                     fprintf(f, "       Data: %.3d 0x%04x (%d)\n", ct++,
106                             (unsigned) (*((int16_t *) dp) & 0xffff),
107                             (int) *((int16_t *) dp));
108                     dp += sizeof(int16_t);
109                 }
110                 break;
111             case RPM_INT8_TYPE:
112                 while (c--) {
113                     fprintf(f, "       Data: %.3d 0x%02x (%d)\n", ct++,
114                             (unsigned) (*((int8_t *) dp) & 0xff),
115                             (int) *((int8_t *) dp));
116                     dp += sizeof(int8_t);
117                 }
118                 break;
119             case RPM_BIN_TYPE:
120                 while (c > 0) {
121                     fprintf(f, "       Data: %.3d ", ct);
122                     while (c--) {
123                         fprintf(f, "%02x ", (unsigned) (*(int8_t *)dp & 0xff));
124                         ct++;
125                         dp += sizeof(int8_t);
126                         if (! (ct % 8)) {
127                             break;
128                         }
129                     }
130                     fprintf(f, "\n");
131                 }
132                 break;
133             case RPM_CHAR_TYPE:
134                 while (c--) {
135                     char ch = (char) *((char *) dp);
136                     fprintf(f, "       Data: %.3d 0x%2x %c (%d)\n", ct++,
137                             (unsigned)(ch & 0xff),
138                             (isprint(ch) ? ch : ' '),
139                             (int) *((char *) dp));
140                     dp += sizeof(char);
141                 }
142                 break;
143             case RPM_STRING_TYPE:
144             case RPM_STRING_ARRAY_TYPE:
145             case RPM_I18NSTRING_TYPE:
146                 while (c--) {
147                     fprintf(f, "       Data: %.3d %s\n", ct++, (char *) dp);
148                     dp = strchr(dp, 0);
149                     dp++;
150                 }
151                 break;
152             default:
153                 fprintf(stderr, _("Data type %d not supported\n"), 
154                         (int) p->info.type);
155                 break;
156             }
157         }
158         p++;
159     }
160 }
161