source: rtems-tools/linkers/rld-compression.h @ d54e81c

4.104.115
Last change on this file since d54e81c was 93e80d5, checked in by Chris Johns <chrisj@…>, on 11/29/12 at 08:04:12

Compress as blocks.

The LZ77 compressor works with blocks. Each block is prefixed with
a header that defines the output size of the block being compressed.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2012, Chris Johns <chrisj@rtems.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16/**
17 * @file
18 *
19 * @ingroup rtems-ld
20 *
21 * @brief RTEMS Linker compression handles compressed images.
22 *
23 */
24
25#if !defined (_RLD_COMPRESSION_H_)
26#define _RLD_COMPRESSION_H_
27
28#include <rld-files.h>
29
30namespace rld
31{
32  namespace compress
33  {
34    /**
35     * A compressor.
36     */
37    class compressor
38    {
39    public:
40      /**
41       * Construct the compressor for the given image.
42       *
43       * @param image The image to read or write to.
44       * @param size The size of the input and output buffers.
45       * @param compress Set to false to disable compression.
46       */
47      compressor (files::image& image,
48                  size_t        size,
49                  bool          compress = true);
50
51      /**
52       * Destruct the compressor.
53       */
54      ~compressor ();
55
56      /**
57       * Write the data to the output buffer and once the image buffer is full
58       * compress and write the compressed data to the image.
59       *
60       * @param data The data to write to the image compressed
61       * @param length The mount of data in bytes to write.
62       */
63      void write (const void* data, size_t length);
64
65      /**
66       * Write the section of the input image file to the output buffer and
67       * once the image buffer is full compress and write the compressed data
68       * to the image.
69       *
70       * @param input The input image.
71       * @param offset The input image offset to read from.
72       * @param length The mount of data in bytes to write.
73       */
74      void write (files::image& input, off_t offset, size_t length);
75
76      /**
77       * Flush the output buffer is data is present.
78       */
79      void flush ();
80
81      /**
82       * The amount of uncompressed data transferred.
83       *
84       * @param return size_t The amount of data tranferred.
85       */
86      size_t transferred () const;
87
88      /**
89       * The amount of compressed data transferred.
90       *
91       * @param return size_t The amount of compressed data tranferred.
92       */
93      size_t compressed () const;
94
95    private:
96
97      /**
98       * Output the block of data to the output file with the block header.
99       *
100       * @param forced If true output the buffer.
101       */
102      void output (bool forced = false);
103
104      files::image& image;            //< The image to read or write to or from.
105      size_t        size;             //< The size of the buffer.
106      bool          compress;         //< If true compress the data.
107      uint8_t*      buffer;           //< The decompressed buffer
108      uint8_t*      io;               //< The I/O buffer.
109      size_t        level;            //< The amount of data in the buffer.
110      size_t        total;            //< The amount of uncompressed data
111                                      //  transferred.
112      size_t        total_compressed; //< The amount of compressed data
113                                      //  transferred.
114    };
115
116    /**
117     * Compressor template function for writing data to the compressor..
118     */
119    template < typename T >
120    void write (compressor& comp, const T value)
121    {
122      uint8_t bytes[sizeof (T)];
123      T       v = value;
124      int     b = sizeof (T) - 1;
125      while (b >= 0)
126      {
127        bytes[b--] = (uint8_t) v;
128        v >>= 8;
129      }
130      comp.write (bytes, sizeof (T));
131    }
132
133  }
134}
135
136static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp,
137                                                     const uint64_t             value) {
138  rld::compress::write < uint64_t > (comp, value);
139  return comp;
140}
141
142static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp,
143                                                     const uint32_t             value) {
144  rld::compress::write < uint32_t > (comp, value);
145  return comp;
146}
147
148static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp,
149                                                     const std::string&         str) {
150  comp.write (str.c_str (), str.size ());
151  return comp;
152}
153
154#endif
Note: See TracBrowser for help on using the repository browser.