libcros 0.5.2

A Rust library that provides easy-to-use functions for interacting with a Chrome device
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * Copyright 2014 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/lib_vpd.h"


#ifndef MIN
#define MIN(a, b) ((a < b) ? a : b)
#endif


/***********************************************************************
 * Container helpers
 ***********************************************************************/
void initContainer(struct PairContainer *container) {
  container->first = NULL;
}


/*
 * Returns the pointer to the 'key' entry.
 * Returns NULL if the key is not found in 'container'.
 *
 * If 'prev_next' is not NULL, findString() stores the address of the "next"
 * member of the StringPair prior to the returned StringPair (the special case
 * is &container->first if first StringPair matches 'key'). This is for linked
 * list manipulation. If 'prev_next' is NULL, findString() would just ignore
 * it.
 */
struct StringPair *findString(struct PairContainer *container,
                              const uint8_t *key,
                              struct StringPair ***prev_next) {
  struct StringPair *current;

  if (prev_next) {
    *prev_next = &container->first;
  }

  for (current = container->first; current; current = current->next) {
    if (!strcmp((char*)key, (char*)current->key)) {
      return current;
    }
    if (prev_next) {
      *prev_next = &current->next;
    }
  }
  return NULL;
}

/* Just a helper function for setString() */
static void fillStringPair(struct StringPair *pair,
                           const uint8_t *key,
                           const uint8_t *value,
                           const int pad_len) {
  pair->key = malloc(strlen((char*)key) + 1);
  assert(pair->key);
  strcpy((char*)pair->key, (char*)key);
  pair->value = malloc(strlen((char*)value) + 1);
  strcpy((char*)pair->value, (char*)value);
  pair->pad_len = pad_len;
}

/* If key is already existed in container, its value will be replaced.
 * If not existed, creates new entry in container.
 */
void setString(struct PairContainer *container,
               const uint8_t *key,
               const uint8_t *value,
               const int pad_len) {
  struct StringPair *found;

  found = findString(container, key, NULL);
  if (found) {
    free(found->key);
    free(found->value);
    fillStringPair(found, key, value, pad_len);
  } else {
    struct StringPair *new_pair = malloc(sizeof(struct StringPair));
    assert(new_pair);
    memset(new_pair, 0, sizeof(struct StringPair));

    fillStringPair(new_pair, key, value, pad_len);

    /* append this pair to the end of list. to keep the order */
    if ((found = container->first)) {
      while (found->next) found = found->next;
      found->next = new_pair;
    } else {
      container->first = new_pair;
    }
    new_pair->next = NULL;
  }
}


/*
 * Remove a key.
 * Returns VPD_OK if deleted successfully. Otherwise, VPD_FAIL.
 */
vpd_err_t deleteKey(struct PairContainer *container,
                    const uint8_t *key) {
  struct StringPair *found, **prev_next;

  found = findString(container, key, &prev_next);
  if (found) {
    free(found->key);
    free(found->value);

    /* remove the 'found' from the linked list. */
    assert(prev_next);
    *prev_next = found->next;
    free(found);

    return VPD_OK;
  } else {
    return VPD_FAIL;
  }
}


/*
 * Returns number of pairs in container.
 */
int lenOfContainer(const struct PairContainer *container) {
  int count;
  struct StringPair *current;

  for (count = 0, current = container->first;
       current;
       count++, current = current->next);

  return count;
}


/* Iterate src container and setString() in dst.
 * so that if key is duplicate, the one in dst is overwritten.
 */
void mergeContainer(struct PairContainer *dst,
                    const struct PairContainer *src) {
  struct StringPair *current;

  for (current = src->first; current; current = current->next) {
    setString(dst, current->key, current->value, current->pad_len);
  }
}


int subtractContainer(struct PairContainer *dst,
                       const struct PairContainer *src) {
  struct StringPair *current;
  int count = 0;

  for (current = src->first; current; current = current->next) {
    if (VPD_OK == deleteKey(dst, current->key))
      count++;
  }

  return count;
}


vpd_err_t encodeContainer(const struct PairContainer *container,
                          const int max_buf_len,
                          uint8_t *buf,
                          int *generated) {
  struct StringPair *current;

  for (current = container->first; current; current = current->next) {
    if (VPD_OK != encodeVpdString(current->key,
                                  current->value,
                                  current->pad_len,
                                  max_buf_len,
                                  buf,
                                  generated)) {
      return VPD_FAIL;
    }
  }
  return VPD_OK;
}

static int callbackDecodeToContainer(const uint8_t *key,
                                           uint32_t key_len,
                                           const uint8_t *value,
                                           uint32_t value_len,
                                           void *arg) {
  struct PairContainer *container = (struct PairContainer*)arg;
  uint8_t *key_string = (uint8_t*)malloc(key_len + 1),
          *value_string = (uint8_t*)malloc(value_len + 1);
  assert(key_string && value_string);
  memcpy(key_string, key, key_len);
  memcpy(value_string, value, value_len);
  key_string[key_len] = '\0';
  value_string[value_len] = '\0';
  setString(container, key_string, value_string, value_len);
  /* setString() makes its own copies. */
  free(key_string);
  free(value_string);
  return VPD_DECODE_OK;
}

vpd_err_t decodeToContainer(struct PairContainer *container,
                            const uint32_t max_len,
                            const uint8_t *input_buf,
                            uint32_t *consumed) {
  return decodeVpdString(max_len, input_buf, consumed,
                         callbackDecodeToContainer, (void*)container);
}

vpd_err_t setContainerFilter(struct PairContainer *container,
                             const uint8_t *filter) {
  struct StringPair *str;

  for (str = container->first; str; str = str->next) {
    if (filter) {
      /*
       * TODO(yjlou):
       * Now, we treat the inputing filter string as plain string.
       * Will support regular expression syntax in future if needed.
       */
      if (strcmp((char*)str->key, (char*)filter)) {
        str->filter_out = 1;
      }
    } else {
      str->filter_out = 0;
    }
  }
  return VPD_OK;
}


/*
 * A helper function to append a sequence of bytes to the given buffer.  If
 * the buffer size is not enough, this function will return VPD_ERR_OVERFLOW;
 * otherwise it will return VPD_OK.
 */
static vpd_err_t _appendToBuf(const void *buf_to_append,
                              int len,
                              const int max_buf_len,
                              uint8_t *buf,
                              int *generated) {
  if (*generated + len > max_buf_len) return VPD_ERR_OVERFLOW;
  memcpy(&buf[*generated], buf_to_append, len);
  *generated += len;
  return VPD_OK;
}


/*
 * A helper function to resolve the number of bytes to be exported for the
 * value field of an instance of StringPair.
 */
static int _getStringPairValueLen(const struct StringPair *str) {
  int len = strlen((const char*)(str->value));
  return VPD_AS_LONG_AS == str->pad_len ? len : MIN(str->pad_len, len);
}


/* A helper function to export an instance of StringPair to the given buffer. */
static vpd_err_t _exportStringPairKeyValue(const struct StringPair *str,
                                           const int max_buf_len,
                                           uint8_t *buf,
                                           int *generated) {
  const void *strs[5] = {"\"", str->key, "\"=\"", str->value, "\"\n"};
  const int lens[5] = {
      1, strlen((const char*)str->key), 3, _getStringPairValueLen(str), 2};

  int retval;
  int i;

  for (i = 0; i < sizeof(lens) / sizeof(int); ++i) {
    retval = _appendToBuf(strs[i], lens[i], max_buf_len, buf, generated);
    if (VPD_OK != retval) {
      break;
    }
  }

  return retval;
}


/*
 * A helper function to escape the special character in a string and then append
 * the result into the buffer.
 */
static vpd_err_t _appendToBufWithShellEscape(const char *str_to_export,
                                             const int max_buf_len,
                                             uint8_t *buf,
                                             int *generated) {
  int len = strlen(str_to_export);
  int i;
  int retval;
  for (i = 0; i < len; ++i) {
    if ('\'' == str_to_export[i]) {
      retval = _appendToBuf("'\"'\"'", 5, max_buf_len, buf, generated);
    } else {
      retval = _appendToBuf(str_to_export + i, 1,
                            max_buf_len, buf, generated);
    }
    if (VPD_OK != retval) return retval;
  }
  return VPD_OK;
}


/*
 * A helper function to export an instance of StringPair to the given buffer
 * as the arguments for the vpd commandline tool.
 */
static vpd_err_t _exportStringPairAsParameter(const struct StringPair *str,
                                              const int max_buf_len,
                                              uint8_t *buf,
                                              int *generated) {
  int retval;

  {
    const char extra_params[] = "    -s ";
    retval = _appendToBuf(extra_params, strlen(extra_params),
                          max_buf_len, buf, generated);
    if (VPD_OK != retval) return retval;
  }

  if (*generated + 1 > max_buf_len) return VPD_ERR_OVERFLOW;
  buf[(*generated)++] = '\'';

  retval = _appendToBufWithShellEscape(
      (const char*)str->key, max_buf_len, buf, generated);
  if (VPD_OK != retval) return retval;

  if (*generated + 1 > max_buf_len) return VPD_ERR_OVERFLOW;
  buf[(*generated)++] = '=';

  retval = _appendToBufWithShellEscape(
      (const char*)str->value, max_buf_len, buf, generated);
  if (VPD_OK != retval) return retval;

  retval = _appendToBuf("' \\\n", 4, max_buf_len, buf, generated);

  return retval;
}


/*
 * A helper function to export an instance of StringPair to the given buffer
 * in a null terminate format, i.e. "<key>=<value>\0"
 */
static vpd_err_t _exportStringPairNullTerminate(const struct StringPair *str,
                                                const int max_buf_len,
                                                uint8_t *buf,
                                                int *generated) {
  int retval;

  retval = _appendToBuf(str->key, strlen((const char*)str->key),
                        max_buf_len, buf, generated);
  if (VPD_OK != retval) return retval;

  if (*generated + 1 > max_buf_len) return VPD_ERR_OVERFLOW;
  buf[(*generated)++] = '=';

  retval = _appendToBuf(str->value, _getStringPairValueLen(str),
                        max_buf_len, buf, generated);
  if (VPD_OK != retval) return retval;

  if (*generated + 1 > max_buf_len) return VPD_ERR_OVERFLOW;
  buf[(*generated)++] = '\0';

  return VPD_OK;
}


/* Export the value field of the instance of StringPair. */
vpd_err_t exportStringValue(const struct StringPair *str,
                            const int max_buf_len,
                            uint8_t *buf,
                            int *generated) {
  assert(generated);

  return _appendToBuf(str->value, _getStringPairValueLen(str),
                      max_buf_len, buf, generated);
}


/* Export the container content with human-readable text. */
vpd_err_t exportContainer(const int export_type,
                          const struct PairContainer *container,
                          const int max_buf_len,
                          uint8_t *buf,
                          int *generated) {
  struct StringPair *str;
  int index;
  int retval;

  assert(generated);
  index = *generated;

  for (str = container->first; str; str = str->next) {
    if (str->filter_out)
      continue;

    if (VPD_EXPORT_KEY_VALUE == export_type) {
      retval = _exportStringPairKeyValue(str, max_buf_len, buf, &index);
    } else if (VPD_EXPORT_AS_PARAMETER == export_type) {
      retval = _exportStringPairAsParameter(str, max_buf_len, buf, &index);
    } else if (VPD_EXPORT_NULL_TERMINATE == export_type) {
      retval = _exportStringPairNullTerminate(str, max_buf_len, buf, &index);
    } else {
      /* this block shouldn't be reached */
      assert(0);
    }
    if (VPD_OK != retval) return retval;
  }

  *generated = index;

  return VPD_OK;
}

void destroyContainer(struct PairContainer *container) {
  struct StringPair *current;

  for (current = container->first; current;) {
    struct StringPair *next;

    if (current->key) free(current->key);
    if (current->value) free(current->value);
    next = current->next;
    free(current);
    current = next;
  }
}