| 1087 | } |
| 1088 | |
| 1089 | static int cbfstool_convert_raw(struct buffer *buffer, |
| 1090 | unused uint32_t *offset, struct cbfs_file *header) |
| 1091 | { |
| 1092 | char *compressed; |
| 1093 | int decompressed_size, compressed_size; |
| 1094 | comp_func_ptr compress; |
| 1095 | |
| 1096 | decompressed_size = buffer->size; |
| 1097 | if (param.precompression) { |
| 1098 | param.compression = read_le32(buffer->data); |
| 1099 | decompressed_size = read_le32(buffer->data + sizeof(uint32_t)); |
| 1100 | compressed_size = buffer->size - 8; |
| 1101 | compressed = malloc(compressed_size); |
| 1102 | if (!compressed) |
| 1103 | return -1; |
| 1104 | memcpy(compressed, buffer->data + 8, compressed_size); |
| 1105 | } else { |
| 1106 | if (param.compression == CBFS_COMPRESS_NONE) |
| 1107 | goto out; |
| 1108 | |
| 1109 | compress = compression_function(param.compression); |
| 1110 | if (!compress) |
| 1111 | return -1; |
| 1112 | compressed = calloc(buffer->size, 1); |
| 1113 | if (!compressed) |
| 1114 | return -1; |
| 1115 | |
| 1116 | if (compress(buffer->data, buffer->size, |
| 1117 | compressed, &compressed_size)) { |
| 1118 | WARN("Compression failed - disabled\n"); |
| 1119 | free(compressed); |
| 1120 | goto out; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | struct cbfs_file_attr_compression *attrs = |
| 1125 | (struct cbfs_file_attr_compression *) |
| 1126 | cbfs_add_file_attr(header, |
| 1127 | CBFS_FILE_ATTR_TAG_COMPRESSION, |
| 1128 | sizeof(struct cbfs_file_attr_compression)); |
| 1129 | if (attrs == NULL) { |
| 1130 | free(compressed); |
| 1131 | return -1; |
| 1132 | } |
| 1133 | attrs->compression = htobe32(param.compression); |
| 1134 | attrs->decompressed_size = htobe32(decompressed_size); |
| 1135 | |
| 1136 | free(buffer->data); |
| 1137 | buffer->data = compressed; |
| 1138 | buffer->size = compressed_size; |
| 1139 | |
| 1140 | out: |
| 1141 | header->len = htobe32(buffer->size); |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static int cbfstool_convert_fsp(struct buffer *buffer, |
| 1146 | uint32_t *offset, struct cbfs_file *header) |
no test coverage detected