int config_write(struct config_data *config_data) { FILE *config_fp; uint16_t crc; int i; crc = CRCCCITT(config_data->deflated_data, config_data->deflated_stream_size, 0xFFFF); printf("\n writing to %s - size 0x%04x crc 0x%04x 0x%04x\n", config_data->name, config_data->deflated_stream_size, config_data->crc, crc); config_fp = fopen(config_data->name, "wb"); if (!config_fp) { fprintf(stderr, "\ncan't open file %s for writing - error: %s\n", config_data->name, strerror(errno)); exit(EXIT_FAILURE); } else { for (i = 0; i < config_data->deflated_stream_size; i++) { if ((i % 8) == 0) printf("\n"); printf("%02x ", config_data->deflated_data[i]); } printf("\n"); } inflate_data(config_data); fwrite(config_data->inflated_data, 1, config_data->inflated_size, config_fp); fclose(config_fp); return 1; }
uint8_t* inflateBuffer(uint32_t* iInputTab, const uint32_t iInputSize, uint32_t& ioOutputSize) { if (iInputTab == nullptr) { throw exception::Exception("Input buffer is null."); } if (!HuffmanTreeDictInitialized) { initializeHuffmanTreeDict(); HuffmanTreeDictInitialized = true; } uint8_t* anOutputTab = nullptr; try { // Initialize state State aState; aState.input = iInputTab; aState.inputSize = iInputSize; aState.inputPos = 0; aState.head = 0; aState.bits = 0; aState.buffer = 0; // Skipping header & Getting size of the uncompressed data needBits(aState, 32); dropBits(aState, 32); // Getting size of the uncompressed data needBits(aState, 32); uint32_t anOutputSize = readBits(aState, 32); dropBits(aState, 32); if (ioOutputSize != 0) { // We do not take max here as we won't be able to have more than the output available if (anOutputSize > ioOutputSize) { anOutputSize = ioOutputSize; } } ioOutputSize = anOutputSize; anOutputTab = static_cast<uint8_t*>(malloc(sizeof(uint8_t) * anOutputSize)); inflate_data(aState, anOutputTab, anOutputSize); return anOutputTab; } catch(exception::Exception& iException) { free(anOutputTab); anOutputTab = nullptr; ioOutputSize = 0; throw iException; // Rethrow exception } catch(std::exception& iException) { free(anOutputTab); anOutputTab = nullptr; ioOutputSize = 0; throw iException; // Rethrow exception } }
status_t PackageItem::ParseAttribute(uint8 *buffer, BNode *node, char **attrName, uint32 *nameSize, uint32 *attrType, uint8 **attrData, uint64 *dataSize, uint8 **temp, uint64 *tempSize, uint64 *attrCSize, uint64 *attrOSize, bool *attrStarted, bool *done) { status_t ret = B_OK; uint32 length; if (!memcmp(buffer, "BeAI", 5)) { parser_debug(" Attribute started.\n"); if (*attrName) *attrName[0] = 0; *attrCSize = 0; *attrOSize = 0; *attrStarted = true; } else if (!memcmp(buffer, "BeAN", 5)) { if (!*attrStarted) { ret = B_ERROR; return ret; } parser_debug(" BeAN.\n"); fPackage->Read(&length, 4); swap_data(B_UINT32_TYPE, &length, sizeof(uint32), B_SWAP_BENDIAN_TO_HOST); if (*nameSize < (length + 1)) { delete *attrName; *nameSize = length + 1; *attrName = new char[*nameSize]; } fPackage->Read(*attrName, length); (*attrName)[length] = 0; parser_debug(" (%ld) = %s\n", length, *attrName); } else if (!memcmp(buffer, "BeAT", 5)) { if (!*attrStarted) { ret = B_ERROR; return ret; } parser_debug(" BeAT.\n"); fPackage->Read(attrType, 4); swap_data(B_UINT32_TYPE, attrType, sizeof(*attrType), B_SWAP_BENDIAN_TO_HOST); } else if (!memcmp(buffer, "BeAD", 5)) { if (!*attrStarted) { ret = B_ERROR; return ret; } parser_debug(" BeAD.\n"); fPackage->Read(attrCSize, 8); swap_data(B_UINT64_TYPE, attrCSize, sizeof(*attrCSize), B_SWAP_BENDIAN_TO_HOST); fPackage->Read(attrOSize, 8); swap_data(B_UINT64_TYPE, attrOSize, sizeof(*attrOSize), B_SWAP_BENDIAN_TO_HOST); fPackage->Seek(4, SEEK_CUR); // TODO: Check what this means if (*tempSize < *attrCSize) { delete *temp; *tempSize = *attrCSize; *temp = new uint8[*tempSize]; } if (*dataSize < *attrOSize) { delete *attrData; *dataSize = *attrOSize; *attrData = new uint8[*dataSize]; } if (fPackage->Read(*temp, *attrCSize) != static_cast<ssize_t>(*attrCSize)) { ret = B_ERROR; return ret; } parser_debug(" Data read successfuly. Inflating!\n"); ret = inflate_data(*temp, *tempSize, *attrData, *dataSize); if (ret != B_OK) return ret; } else if (!memcmp(buffer, padding, 7)) { if (!*attrStarted) { *done = true; return ret; } parser_debug(" Padding.\n"); ssize_t wrote = node->WriteAttr(*attrName, *attrType, 0, *attrData, *attrOSize); if (wrote != static_cast<ssize_t>(*attrOSize)) { parser_debug("Failed to write attribute %s %s\n", *attrName, strerror(wrote)); return B_ERROR; } *attrStarted = false; if (*attrName) *attrName[0] = 0; *attrCSize = 0; *attrOSize = 0; parser_debug(" > Attribute added.\n"); } else { parser_debug(" Unknown attribute\n"); ret = B_ERROR; } return ret; }