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

4.104.115
Last change on this file since 74e89db was db216fe, checked in by Chris Johns <chrisj@…>, on 12/19/12 at 05:22:37

Decompression support added.

The compressor can now decompress LZ77 files.

  • Property mode set to 100644
File size: 5.6 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 out The compressor is compressing.
46       * @param compress Set to false to disable compression.
47       */
48      compressor (files::image& image,
49                  size_t        size,
50                  bool          out = true,
51                  bool          compress = true);
52
53      /**
54       * Destruct the compressor.
55       */
56      ~compressor ();
57
58      /**
59       * Write the data to the output buffer and once the image buffer is full
60       * compress and write the compressed data to the image.
61       *
62       * @param data The data to write to the image compressed
63       * @param length The mount of data in bytes to write.
64       */
65      void write (const void* data, size_t length);
66
67      /**
68       * Write the section of the input image file to the output buffer and
69       * once the image buffer is full compress and write the compressed data
70       * to the image.
71       *
72       * @param input The input image.
73       * @param offset The input image offset to read from.
74       * @param length The mount of data in bytes to write.
75       */
76      void write (files::image& input, off_t offset, size_t length);
77
78      /**
79       * Flush the output buffer is data is present.
80       */
81      void flush ();
82
83      /**
84       * Read the compressed data into the input buffer and return the section
85       * requested.
86       *
87       * @param data Write the decompressed data here.
88       * @param length The mount of data in bytes to read.
89       */
90      void read (void* data, size_t length);
91
92      /**
93       * Read the decompressed data writing it to the image.
94       *
95       * @param output The output image.
96       * @param offset The output image offset to write from.
97       * @param length The mount of data in bytes to read.
98       */
99      void read (files::image& output_, off_t offset, size_t length);
100
101      /**
102       * The amount of uncompressed data transferred.
103       *
104       * @param return size_t The amount of data tranferred.
105       */
106      size_t transferred () const;
107
108      /**
109       * The amount of compressed data transferred.
110       *
111       * @param return size_t The amount of compressed data tranferred.
112       */
113      size_t compressed () const;
114
115    private:
116
117      /**
118       * Output the block of data to the output file with the block header.
119       *
120       * @param forced If true output the buffer.
121       */
122      void output (bool forced = false);
123
124      /**
125       * Input a block of compressed data and decompress it.
126       */
127      void input ();
128
129      files::image& image;            //< The image to read or write to or from.
130      size_t        size;             //< The size of the buffer.
131      bool          out;              //< If true the it is compression.
132      bool          compress;         //< If true compress the data.
133      uint8_t*      buffer;           //< The decompressed buffer
134      uint8_t*      io;               //< The I/O buffer.
135      size_t        level;            //< The amount of data in the buffer.
136      size_t        total;            //< The amount of uncompressed data
137                                      //  transferred.
138      size_t        total_compressed; //< The amount of compressed data
139                                      //  transferred.
140    };
141
142    /**
143     * Compressor template function for writing data to the compressor..
144     */
145    template < typename T >
146    void write (compressor& comp, const T value)
147    {
148      uint8_t bytes[sizeof (T)];
149      T       v = value;
150      int     b = sizeof (T) - 1;
151      while (b >= 0)
152      {
153        bytes[b--] = (uint8_t) v;
154        v >>= 8;
155      }
156      comp.write (bytes, sizeof (T));
157    }
158
159  }
160}
161
162static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp,
163                                                     const uint64_t             value) {
164  rld::compress::write < uint64_t > (comp, value);
165  return comp;
166}
167
168static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp,
169                                                     const uint32_t             value) {
170  rld::compress::write < uint32_t > (comp, value);
171  return comp;
172}
173
174static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp,
175                                                     const std::string&         str) {
176  comp.write (str.c_str (), str.size ());
177  return comp;
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.