summaryrefslogtreecommitdiff
path: root/src/repopack.h
blob: 81b7b88a9c89de40f399b1b0a651818eef957da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Copyright (c) 2007, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/* pack/unpack functions for key data */

#ifndef LIBSOLV_REPOPACK_H
#define LIBSOLV_REPOPACK_H

static inline unsigned char *
data_read_id(unsigned char *dp, Id *idp)
{
  Id x;
  unsigned char c;
  if (!(dp[0] & 0x80))
    {
      *idp = dp[0];
      return dp + 1;
    }
  if (!(dp[1] & 0x80))
    {
      *idp = dp[0] << 7 ^ dp[1] ^ 0x4000;
      return dp + 2;
    }
  if (!(dp[2] & 0x80))
    {
      *idp = dp[0] << 14 ^ dp[1] << 7 ^ dp[2] ^ 0x204000;
      return dp + 3;
    }
  if (!(dp[3] & 0x80))
    {
      *idp = dp[0] << 21 ^ dp[1] << 14 ^ dp[2] << 7 ^ dp[3] ^ 0x10204000;
      return dp + 4;
    }
  x = dp[0] << 28 ^ dp[1] << 21 ^ dp[2] << 14 ^ dp[3] << 7 ^ dp[4] ^ 0x10204000;
  if (!(dp[4] & 0x80))
    {
      *idp = x;
      return dp + 5;
    }
  x ^= 80;
  dp += 5;
  for (;;)
    {
      c = *dp++;
      if (!(c & 0x80))
        {
          *idp = (x << 7) ^ c;
          return dp;
        }
      x = (x << 7) ^ (c ^ 128);
    }
}

static inline unsigned char *
data_read_num64(unsigned char *dp, unsigned int *low, unsigned int *high)
{
  unsigned long long int x;
  unsigned char c;

  *high = 0;
  if (!(dp[0] & 0x80))
    {
      *low = dp[0];
      return dp + 1;
    }
  if (!(dp[1] & 0x80))
    {
      *low = dp[0] << 7 ^ dp[1] ^ 0x4000;
      return dp + 2;
    }
  if (!(dp[2] & 0x80))
    {
      *low = dp[0] << 14 ^ dp[1] << 7 ^ dp[2] ^ 0x204000;
      return dp + 3;
    }
  if (!(dp[3] & 0x80))
    {
      *low = dp[0] << 21 ^ dp[1] << 14 ^ dp[2] << 7 ^ dp[3] ^ 0x10204000;
      return dp + 4;
    }
  if (!(dp[4] & 0x80))
    {
      *low = dp[0] << 28 ^ dp[1] << 21 ^ dp[2] << 14 ^ dp[3] << 7 ^ dp[4] ^ 0x10204000;
      *high = (dp[0] ^ 0x80) >> 4;
      return dp + 5;
    }
  x = (unsigned long long)(dp[0] ^ 0x80) << 28 ^ (unsigned int)(dp[1] << 21 ^ dp[2] << 14 ^ dp[3] << 7 ^ dp[4] ^ 0x10204080);
  dp += 5;
  for (;;)
    {
      c = *dp++;
      if (!(c & 0x80))
	{
	  x = (x << 7) ^ c;
	  *low = x;
	  *high = x >> 32;
	  return dp;
	}
      x = (x << 7) ^ (c ^ 128);
    }
}

static inline unsigned char *
data_read_ideof(unsigned char *dp, Id *idp, int *eof)
{
  Id x = 0;
  unsigned char c;
  for (;;)
    {
      c = *dp++;
      if (!(c & 0x80))
        {
          if (c & 0x40)
            {
              c ^= 0x40;
              *eof = 0;
            }
          else
            *eof = 1;
          *idp = (x << 6) ^ c;
          return dp;
        }
      x = (x << 7) ^ c ^ 128;
    }
}

static inline unsigned char *
data_read_u32(unsigned char *dp, unsigned int *nump)
{
  *nump = (dp[0] << 24) | (dp[1] << 16) | (dp[2] << 8) | dp[3];
  return dp + 4;
}

static inline unsigned char *
data_fetch(unsigned char *dp, KeyValue *kv, Repokey *key)
{
  kv->eof = 1;
  if (!dp)
    return 0;
  switch (key->type)
    {
    case REPOKEY_TYPE_VOID:
      return dp;
    case REPOKEY_TYPE_CONSTANT:
      kv->num2 = 0;
      kv->num = key->size;
      return dp;
    case REPOKEY_TYPE_CONSTANTID:
      kv->id = key->size;
      return dp;
    case REPOKEY_TYPE_STR:
      kv->str = (const char *)dp;
      return dp + strlen(kv->str) + 1;
    case REPOKEY_TYPE_ID:
    case REPOKEY_TYPE_DIR:
      return data_read_id(dp, &kv->id);
    case REPOKEY_TYPE_NUM:
      return data_read_num64(dp, &kv->num, &kv->num2);
    case REPOKEY_TYPE_U32:
      kv->num2 = 0;
      return data_read_u32(dp, &kv->num);
    case REPOKEY_TYPE_MD5:
      kv->str = (const char *)dp;
      return dp + SIZEOF_MD5;
    case REPOKEY_TYPE_SHA1:
      kv->str = (const char *)dp;
      return dp + SIZEOF_SHA1;
    case REPOKEY_TYPE_SHA256:
      kv->str = (const char *)dp;
      return dp + SIZEOF_SHA256;
    case REPOKEY_TYPE_BINARY:
      dp = data_read_id(dp, (Id *)&kv->num);
      kv->str = (const char *)dp;
      return dp + kv->num;
    case REPOKEY_TYPE_IDARRAY:
      return data_read_ideof(dp, &kv->id, &kv->eof);
    case REPOKEY_TYPE_DIRSTRARRAY:
      dp = data_read_ideof(dp, &kv->id, &kv->eof);
      kv->str = (const char *)dp;
      return dp + strlen(kv->str) + 1;
    case REPOKEY_TYPE_DIRNUMNUMARRAY:
      dp = data_read_id(dp, &kv->id);
      dp = data_read_id(dp, (Id *)&kv->num);
      return data_read_ideof(dp, (Id *)&kv->num2, &kv->eof);
    case REPOKEY_TYPE_FIXARRAY:
      dp = data_read_id(dp, (Id *)&kv->num);
      return data_read_id(dp, &kv->id);
    case REPOKEY_TYPE_FLEXARRAY:
      return data_read_id(dp, (Id *)&kv->num);
    default:
      return 0;
    }
}

static inline unsigned char *
data_skip(unsigned char *dp, int type)
{
  unsigned char x;
  switch (type)
    {
    case REPOKEY_TYPE_VOID:
    case REPOKEY_TYPE_CONSTANT:
    case REPOKEY_TYPE_CONSTANTID:
    case REPOKEY_TYPE_DELETED:
      return dp;
    case REPOKEY_TYPE_ID:
    case REPOKEY_TYPE_NUM:
    case REPOKEY_TYPE_DIR:
      while ((*dp & 0x80) != 0)
        dp++;
      return dp + 1;
    case REPOKEY_TYPE_U32:
      return dp + 4;
    case REPOKEY_TYPE_MD5:
      return dp + SIZEOF_MD5;
    case REPOKEY_TYPE_SHA1:
      return dp + SIZEOF_SHA1;
    case REPOKEY_TYPE_SHA256:
      return dp + SIZEOF_SHA256;
    case REPOKEY_TYPE_IDARRAY:
    case REPOKEY_TYPE_REL_IDARRAY:
      while ((*dp & 0xc0) != 0)
        dp++;
      return dp + 1;
    case REPOKEY_TYPE_STR:
      while ((*dp) != 0)
        dp++;
      return dp + 1;
    case REPOKEY_TYPE_BINARY:
      {
	unsigned int len;
	dp = data_read_id(dp, (Id *)&len);
	return dp + len;
      }
    case REPOKEY_TYPE_DIRSTRARRAY:
      for (;;)
        {
          while ((*dp & 0x80) != 0)
            dp++;
          x = *dp++;
          while ((*dp) != 0)
            dp++;
          dp++;
          if (!(x & 0x40))
            return dp;
        }
    case REPOKEY_TYPE_DIRNUMNUMARRAY:
      for (;;)
        {
          while ((*dp & 0x80) != 0)
            dp++;
          dp++;
          while ((*dp & 0x80) != 0)
            dp++;
          dp++;
          while ((*dp & 0x80) != 0)
            dp++;
          if (!(*dp & 0x40))
            return dp + 1;
          dp++;
        }
    default:
      return 0;
    }
}

static inline unsigned char *
data_skip_verify(unsigned char *dp, int type, int maxid, int maxdir)
{
  Id id;
  int eof;

  switch (type)
    {
    case REPOKEY_TYPE_VOID:
    case REPOKEY_TYPE_CONSTANT:
    case REPOKEY_TYPE_CONSTANTID:
    case REPOKEY_TYPE_DELETED:
      return dp;
    case REPOKEY_TYPE_NUM:
      while ((*dp & 0x80) != 0)
        dp++;
      return dp + 1;
    case REPOKEY_TYPE_U32:
      return dp + 4;
    case REPOKEY_TYPE_MD5:
      return dp + SIZEOF_MD5;
    case REPOKEY_TYPE_SHA1:
      return dp + SIZEOF_SHA1;
    case REPOKEY_TYPE_SHA256:
      return dp + SIZEOF_SHA256;
    case REPOKEY_TYPE_ID:
      dp = data_read_id(dp, &id);
      if (id >= maxid)
	return 0;
      return dp;
    case REPOKEY_TYPE_DIR:
      dp = data_read_id(dp, &id);
      if (id >= maxdir)
	return 0;
      return dp;
    case REPOKEY_TYPE_IDARRAY:
      for (;;)
	{
	  dp = data_read_ideof(dp, &id, &eof);
	  if (id >= maxid)
	    return 0;
	  if (eof)
	    return dp;
	}
    case REPOKEY_TYPE_STR:
      while ((*dp) != 0)
        dp++;
      return dp + 1;
    case REPOKEY_TYPE_BINARY:
      {
	unsigned int len;
	dp = data_read_id(dp, (Id *)&len);
	return dp + len;
      }
    case REPOKEY_TYPE_DIRSTRARRAY:
      for (;;)
        {
	  dp = data_read_ideof(dp, &id, &eof);
	  if (id >= maxdir)
	    return 0;
          while ((*dp) != 0)
            dp++;
          dp++;
          if (eof)
            return dp;
        }
    case REPOKEY_TYPE_DIRNUMNUMARRAY:
      for (;;)
        {
	  dp = data_read_id(dp, &id);
	  if (id >= maxdir)
	    return 0;
          while ((*dp & 0x80) != 0)
            dp++;
          dp++;
          while ((*dp & 0x80) != 0)
            dp++;
          if (!(*dp & 0x40))
            return dp + 1;
          dp++;
        }
    default:
      return 0;
    }
}

#endif	/* LIBSOLV_REPOPACK */