]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/sparc.c
09bc67f15f5e6d5bac5110825a14995abd8661b9
[icculus/xz.git] / src / liblzma / simple / sparc.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       sparc.c
4 /// \brief      Filter for SPARC binaries
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2007 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #include "simple_private.h"
22
23
24 static size_t
25 sparc_code(lzma_simple *simple lzma_attribute((unused)),
26                 uint32_t now_pos, bool is_encoder,
27                 uint8_t *buffer, size_t size)
28 {
29         size_t i;
30         for (i = 0; i + 4 <= size; i += 4) {
31
32                 if ((buffer[i] == 0x40 && (buffer[i + 1] & 0xC0) == 0x00)
33                                 || (buffer[i] == 0x7F
34                                 && (buffer[i + 1] & 0xC0) == 0xC0)) {
35
36                         uint32_t src = ((uint32_t)buffer[i + 0] << 24)
37                                         | ((uint32_t)buffer[i + 1] << 16)
38                                         | ((uint32_t)buffer[i + 2] << 8)
39                                         | ((uint32_t)buffer[i + 3]);
40
41                         src <<= 2;
42
43                         uint32_t dest;
44                         if (is_encoder)
45                                 dest = now_pos + (uint32_t)(i) + src;
46                         else
47                                 dest = src - (now_pos + (uint32_t)(i));
48
49                         dest >>= 2;
50
51                         dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF)
52                                         | (dest & 0x3FFFFF)
53                                         | 0x40000000;
54
55                         buffer[i + 0] = (uint8_t)(dest >> 24);
56                         buffer[i + 1] = (uint8_t)(dest >> 16);
57                         buffer[i + 2] = (uint8_t)(dest >> 8);
58                         buffer[i + 3] = (uint8_t)(dest);
59                 }
60         }
61
62         return i;
63 }
64
65
66 static lzma_ret
67 sparc_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
68                 const lzma_filter_info *filters, bool is_encoder)
69 {
70         return lzma_simple_coder_init(next, allocator, filters,
71                         &sparc_code, 0, 4, is_encoder);
72 }
73
74
75 extern lzma_ret
76 lzma_simple_sparc_encoder_init(lzma_next_coder *next,
77                 lzma_allocator *allocator, const lzma_filter_info *filters)
78 {
79         return sparc_coder_init(next, allocator, filters, true);
80 }
81
82
83 extern lzma_ret
84 lzma_simple_sparc_decoder_init(lzma_next_coder *next,
85                 lzma_allocator *allocator, const lzma_filter_info *filters)
86 {
87         return sparc_coder_init(next, allocator, filters, false);
88 }