summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/org/fox/ttcomics2/junrar/io/Raw.java
blob: 47cc067e62ee0361bf8f24c5547aa4d8768ed92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
 * Original author: Edmund Wagner
 * Creation date: 18.06.2007
 *
 * Source: $HeadURL$
 * Last changed: $LastChangedDate$
 * 
 * the unrar licence applies to all junrar source and binary distributions 
 * you are not allowed to use this source to re-create the RAR compression algorithm
 * 
 * Here some html entities which can be used for escaping javadoc tags:
 * "&":  "&" or "&"
 * "<":  "&#060;" or "&lt;"
 * ">":  "&#062;" or "&gt;"
 * "@":  "&#064;" 
 */
package org.fox.ttcomics2.junrar.io;

public class Raw {
    public static final short readShortBigEndian(byte[] array, int pos) {
        short temp = 0;
        temp |= array[pos] & 0xff;
        temp <<= 8;
        temp |= array[pos + 1] & 0xff;
        return temp;
    }

    public static final int readIntBigEndian(byte[] array, int pos) {
        int temp = 0;
        temp |= array[pos] & 0xff;
        temp <<= 8;
        temp |= array[pos + 1] & 0xff;
        temp <<= 8;
        temp |= array[pos + 2] & 0xff;
        temp <<= 8;
        temp |= array[pos + 3] & 0xff;
        return temp;
    }

    public static final long readLongBigEndian(byte[] array, int pos) {
        int temp = 0;
        temp |= array[pos] & 0xff;
        temp <<= 8;
        temp |= array[pos + 1] & 0xff;
        temp <<= 8;
        temp |= array[pos + 2] & 0xff;
        temp <<= 8;
        temp |= array[pos + 3] & 0xff;
        temp <<= 8;
        temp |= array[pos + 4] & 0xff;
        temp <<= 8;
        temp |= array[pos + 5] & 0xff;
        temp <<= 8;
        temp |= array[pos + 6] & 0xff;
        temp <<= 8;
        temp |= array[pos + 7] & 0xff;
        return temp;
    }

    public static final short readShortLittleEndian(byte[] array, int pos) {
        short result = 0;
        result += array[pos + 1] & 0xff;
        result <<= 8;
        result += array[pos] & 0xff;
        return result;
    }

    public static final int readIntLittleEndian(byte[] array, int pos) {
        return ((array[pos + 3] & 0xff) << 24)
                | ((array[pos + 2] & 0xff) << 16)
                | ((array[pos + 1] & 0xff) << 8) | ((array[pos] & 0xff));
    }

    public static final long readIntLittleEndianAsLong(byte[] array, int pos) {
        return (((long) array[pos + 3] & 0xff) << 24)
                | (((long) array[pos + 2] & 0xff) << 16)
                | (((long) array[pos + 1] & 0xff) << 8)
                | (((long) array[pos] & 0xff));
    }

    public static final long readLongLittleEndian(byte[] array, int pos) {
        int temp = 0;
        temp |= array[pos + 7] & 0xff;
        temp <<= 8;
        temp |= array[pos + 6] & 0xff;
        temp <<= 8;
        temp |= array[pos + 5] & 0xff;
        temp <<= 8;
        temp |= array[pos + 4] & 0xff;
        temp <<= 8;
        temp |= array[pos + 3] & 0xff;
        temp <<= 8;
        temp |= array[pos + 2] & 0xff;
        temp <<= 8;
        temp |= array[pos + 1] & 0xff;
        temp <<= 8;
        temp |= array[pos];
        return temp;
    }

    public static final void writeShortBigEndian(byte[] array, int pos,
                                                 short value) {
        array[pos] = (byte) (value >>> 8);
        array[pos + 1] = (byte) (value & 0xFF);

    }

    public static final void writeIntBigEndian(byte[] array, int pos, int value) {
        array[pos] = (byte) ((value >>> 24) & 0xff);
        array[pos + 1] = (byte) ((value >>> 16) & 0xff);
        array[pos + 2] = (byte) ((value >>> 8) & 0xff);
        array[pos + 3] = (byte) ((value) & 0xff);

    }

    public static final void writeLongBigEndian(byte[] array, int pos,
                                                long value) {
        array[pos] = (byte) (value >>> 56);
        array[pos + 1] = (byte) (value >>> 48);
        array[pos + 2] = (byte) (value >>> 40);
        array[pos + 3] = (byte) (value >>> 32);
        array[pos + 4] = (byte) (value >>> 24);
        array[pos + 5] = (byte) (value >>> 16);
        array[pos + 6] = (byte) (value >>> 8);
        array[pos + 7] = (byte) (value & 0xFF);

    }

    public static final void writeShortLittleEndian(byte[] array, int pos,
                                                    short value) {
        array[pos + 1] = (byte) (value >>> 8);
        array[pos] = (byte) (value & 0xFF);

    }

    public static final void incShortLittleEndian(byte[] array, int pos, int dv) {
        int c = ((array[pos] & 0xff) + (dv & 0xff)) >>> 8;
        array[pos] += dv & 0xff;
        if ((c > 0) || ((dv & 0xff00) != 0)) {
            array[pos + 1] += ((dv >>> 8) & 0xff) + c;
        }
    }

    public static final void writeIntLittleEndian(byte[] array, int pos,
                                                  int value) {
        array[pos + 3] = (byte) (value >>> 24);
        array[pos + 2] = (byte) (value >>> 16);
        array[pos + 1] = (byte) (value >>> 8);
        array[pos] = (byte) (value & 0xFF);

    }

    public static final void writeLongLittleEndian(byte[] array, int pos,
                                                   long value) {
        array[pos + 7] = (byte) (value >>> 56);
        array[pos + 6] = (byte) (value >>> 48);
        array[pos + 5] = (byte) (value >>> 40);
        array[pos + 4] = (byte) (value >>> 32);
        array[pos + 3] = (byte) (value >>> 24);
        array[pos + 2] = (byte) (value >>> 16);
        array[pos + 1] = (byte) (value >>> 8);
        array[pos] = (byte) (value & 0xFF);

    }
}