| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
#include <linux/kernel.h> |
| 7 |
#include <linux/string.h> |
| 8 |
#include "apfs.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
int apfs_find_xfield(u8 *xfields, int len, u8 xtype, char **xval) |
| 21 |
{ |
| 22 |
struct apfs_xf_blob *blob; |
| 23 |
struct apfs_x_field *xfield; |
| 24 |
int count; |
| 25 |
int rest = len; |
| 26 |
int i; |
| 27 |
|
| 28 |
if (!len) |
| 29 |
return 0; |
| 30 |
|
| 31 |
rest -= sizeof(*blob); |
| 32 |
if (rest < 0) |
| 33 |
return 0; |
| 34 |
blob = (struct apfs_xf_blob *)xfields; |
| 35 |
|
| 36 |
count = le16_to_cpu(blob->xf_num_exts); |
| 37 |
rest -= count * sizeof(*xfield); |
| 38 |
if (rest < 0) |
| 39 |
return 0; |
| 40 |
xfield = (struct apfs_x_field *)blob->xf_data; |
| 41 |
|
| 42 |
for (i = 0; i < count; ++i) { |
| 43 |
int xlen; |
| 44 |
|
| 45 |
|
| 46 |
xlen = round_up(le16_to_cpu(xfield[i].x_size), 8); |
| 47 |
if (xlen > rest) |
| 48 |
return 0; |
| 49 |
|
| 50 |
if (xfield[i].x_type == xtype) { |
| 51 |
*xval = (char *)xfields + len - rest; |
| 52 |
return xlen; |
| 53 |
} |
| 54 |
rest -= xlen; |
| 55 |
} |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
int apfs_init_xfields(u8 *buffer, int buflen) |
| 67 |
{ |
| 68 |
struct apfs_xf_blob *blob; |
| 69 |
|
| 70 |
if (buflen < sizeof(*blob)) |
| 71 |
return -1; |
| 72 |
blob = (struct apfs_xf_blob *)buffer; |
| 73 |
|
| 74 |
blob->xf_num_exts = 0; |
| 75 |
blob->xf_used_data = 0; |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
int apfs_insert_xfield(u8 *buffer, int buflen, const struct apfs_x_field *xkey, |
| 90 |
const void *xval) |
| 91 |
{ |
| 92 |
struct apfs_xf_blob *blob; |
| 93 |
struct apfs_x_field *curr_xkey; |
| 94 |
void *curr_xval; |
| 95 |
int count; |
| 96 |
int rest = buflen; |
| 97 |
u16 used_data; |
| 98 |
int xlen, padded_xlen; |
| 99 |
int meta_len, total_len; |
| 100 |
int i; |
| 101 |
|
| 102 |
xlen = le16_to_cpu(xkey->x_size); |
| 103 |
padded_xlen = round_up(xlen, 8); |
| 104 |
|
| 105 |
if (!buflen) |
| 106 |
return 0; |
| 107 |
|
| 108 |
rest -= sizeof(*blob); |
| 109 |
if (rest < 0) |
| 110 |
return 0; |
| 111 |
blob = (struct apfs_xf_blob *)buffer; |
| 112 |
used_data = le16_to_cpu(blob->xf_used_data); |
| 113 |
|
| 114 |
count = le16_to_cpu(blob->xf_num_exts); |
| 115 |
rest -= count * sizeof(*curr_xkey); |
| 116 |
if (rest < 0) |
| 117 |
return 0; |
| 118 |
meta_len = buflen - rest; |
| 119 |
curr_xkey = (struct apfs_x_field *)blob->xf_data; |
| 120 |
|
| 121 |
for (i = 0; i < count; ++i, ++curr_xkey) { |
| 122 |
int curr_xlen; |
| 123 |
|
| 124 |
|
| 125 |
curr_xlen = round_up(le16_to_cpu(curr_xkey->x_size), 8); |
| 126 |
if (curr_xlen > rest) |
| 127 |
return 0; |
| 128 |
if (curr_xkey->x_type != xkey->x_type) { |
| 129 |
rest -= curr_xlen; |
| 130 |
continue; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
memcpy(curr_xkey, xkey, sizeof(*curr_xkey)); |
| 135 |
if (padded_xlen > rest) |
| 136 |
return 0; |
| 137 |
curr_xval = buffer + buflen - rest; |
| 138 |
rest -= max(padded_xlen, curr_xlen); |
| 139 |
memmove(curr_xval + padded_xlen, curr_xval + curr_xlen, rest); |
| 140 |
memcpy(curr_xval, xval, xlen); |
| 141 |
memset(curr_xval + xlen, 0, padded_xlen - xlen); |
| 142 |
used_data += padded_xlen - curr_xlen; |
| 143 |
|
| 144 |
goto done; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
rest -= sizeof(*curr_xkey); |
| 149 |
if (rest < 0) |
| 150 |
return 0; |
| 151 |
meta_len += sizeof(*curr_xkey); |
| 152 |
memmove(curr_xkey + 1, curr_xkey, buflen - meta_len); |
| 153 |
memcpy(curr_xkey, xkey, sizeof(*curr_xkey)); |
| 154 |
++count; |
| 155 |
|
| 156 |
|
| 157 |
if (padded_xlen > rest) |
| 158 |
return 0; |
| 159 |
curr_xval = buffer + buflen - rest; |
| 160 |
memcpy(curr_xval, xval, xlen); |
| 161 |
memset(curr_xval + xlen, 0, padded_xlen - xlen); |
| 162 |
used_data += padded_xlen; |
| 163 |
|
| 164 |
done: |
| 165 |
total_len = used_data + meta_len; |
| 166 |
if (total_len > buflen) |
| 167 |
return 0; |
| 168 |
blob->xf_num_exts = cpu_to_le16(count); |
| 169 |
blob->xf_used_data = cpu_to_le16(used_data); |
| 170 |
return total_len; |
| 171 |
} |