1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file stream_flags_encoder.c 4207753Smm/// \brief Encodes Stream Header and Stream Footer for .xz files 5207753Smm// 6207753Smm// Author: Lasse Collin 7207753Smm// 8207753Smm// This file has been put into the public domain. 9207753Smm// You can do whatever you want with this file. 10207753Smm// 11207753Smm/////////////////////////////////////////////////////////////////////////////// 12207753Smm 13207753Smm#include "stream_flags_common.h" 14207753Smm 15207753Smm 16207753Smmstatic bool 17207753Smmstream_flags_encode(const lzma_stream_flags *options, uint8_t *out) 18207753Smm{ 19207753Smm if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX) 20207753Smm return true; 21207753Smm 22207753Smm out[0] = 0x00; 23207753Smm out[1] = options->check; 24207753Smm 25207753Smm return false; 26207753Smm} 27207753Smm 28207753Smm 29207753Smmextern LZMA_API(lzma_ret) 30207753Smmlzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out) 31207753Smm{ 32207753Smm assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE 33207753Smm + 4 == LZMA_STREAM_HEADER_SIZE); 34207753Smm 35207753Smm if (options->version != 0) 36207753Smm return LZMA_OPTIONS_ERROR; 37207753Smm 38207753Smm // Magic 39207753Smm memcpy(out, lzma_header_magic, sizeof(lzma_header_magic)); 40207753Smm 41207753Smm // Stream Flags 42207753Smm if (stream_flags_encode(options, out + sizeof(lzma_header_magic))) 43207753Smm return LZMA_PROG_ERROR; 44207753Smm 45207753Smm // CRC32 of the Stream Header 46207753Smm const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic), 47207753Smm LZMA_STREAM_FLAGS_SIZE, 0); 48207753Smm 49207753Smm unaligned_write32le(out + sizeof(lzma_header_magic) 50207753Smm + LZMA_STREAM_FLAGS_SIZE, crc); 51207753Smm 52207753Smm return LZMA_OK; 53207753Smm} 54207753Smm 55207753Smm 56207753Smmextern LZMA_API(lzma_ret) 57207753Smmlzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out) 58207753Smm{ 59207753Smm assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic) 60207753Smm == LZMA_STREAM_HEADER_SIZE); 61207753Smm 62207753Smm if (options->version != 0) 63207753Smm return LZMA_OPTIONS_ERROR; 64207753Smm 65207753Smm // Backward Size 66207753Smm if (!is_backward_size_valid(options)) 67207753Smm return LZMA_PROG_ERROR; 68207753Smm 69207753Smm unaligned_write32le(out + 4, options->backward_size / 4 - 1); 70207753Smm 71207753Smm // Stream Flags 72207753Smm if (stream_flags_encode(options, out + 2 * 4)) 73207753Smm return LZMA_PROG_ERROR; 74207753Smm 75207753Smm // CRC32 76207753Smm const uint32_t crc = lzma_crc32( 77207753Smm out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0); 78207753Smm 79207753Smm unaligned_write32le(out, crc); 80207753Smm 81207753Smm // Magic 82207753Smm memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE, 83207753Smm lzma_footer_magic, sizeof(lzma_footer_magic)); 84207753Smm 85207753Smm return LZMA_OK; 86207753Smm} 87