• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include "buffer.h"
19 
buffer_init(struct Buffer * b,uint32_t len)20 bool buffer_init(struct Buffer *b, uint32_t len) {
21   if (!b)
22     return false;
23   b->buffer = calloc(len, 1);
24   if (!b->buffer)
25     return false;
26   b->len = 0;
27   b->size = len;
28   return true;
29 }
30 
buffer_free(struct Buffer * b)31 void buffer_free(struct Buffer *b) {
32   if (b && b->buffer)
33     free(b->buffer);
34 }
35 
buffer_read_hex(struct Buffer * b,FILE * fp,bool consume_newline)36 bool buffer_read_hex(struct Buffer *b, FILE *fp, bool consume_newline) {
37   int offset;
38   uint32_t i;
39 
40   if (feof(fp))
41     return false;
42   /* Consume leading whitespace. */
43   while (!feof(fp)) {
44     char c = fgetc(fp);
45     if (!consume_newline && (c == 0x0d || c == 0x0a)) {
46       return false;
47     }
48     if (!isspace(c)) {
49       ungetc(c, fp);
50       break;
51     }
52   }
53 
54   for (; !feof(fp) && b->len < b->size; ++b->len) {
55     b->buffer[b->len] = fgetc(fp);
56     if (!isalnum(b->buffer[b->len])) {
57       ungetc(b->buffer[b->len], fp);
58       break;
59     }
60   }
61   if (b->len == 0)
62     return false;
63 
64   for (offset = 0, i = 0; offset < (int)b->len && i < b->size; ++i) {
65     int last_offset = offset;
66     if (sscanf((char *)b->buffer + offset, "%2hhx%n", &b->buffer[i], &offset) !=
67         1)
68       break;
69     offset += last_offset;
70     if (offset == 0)
71       break;
72   }
73   b->len = i;
74   return true;
75 }
76 
buffer_dump(const struct Buffer * b,const char * prefix,const char * name,uint32_t limit,FILE * fp)77 void buffer_dump(const struct Buffer *b, const char *prefix, const char *name,
78                  uint32_t limit, FILE *fp) {
79   fprintf(fp, "%s%s {\n", prefix, name);
80   fprintf(fp, "%s  .length = %u\n", prefix, b->len);
81   fprintf(fp, "%s  .size = %u\n", prefix, b->size);
82   fprintf(fp, "%s  .buffer = {\n", prefix);
83   fprintf(fp, "%s    ", prefix);
84   for (uint32_t i = 0; i < b->len; ++i) {
85     if (i > 15 && i % 16 == 0) {
86       fprintf(fp, "\n%s    ", prefix);
87     }
88     if (i > limit) {
89       fprintf(fp, ". . .");
90       break;
91     }
92     fprintf(fp, "%.2x ", b->buffer[i]);
93   }
94   fprintf(fp, "\n%s}\n", prefix);
95 }
96