OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [crypto/] [assembly/] [PaddingTransformer.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* PaddingTransformer.java --
2
   Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3
 
4
This file is a part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or (at
9
your option) any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19
USA
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version.  */
37
 
38
 
39
package gnu.javax.crypto.assembly;
40
 
41
import gnu.javax.crypto.pad.IPad;
42
import gnu.javax.crypto.pad.WrongPaddingException;
43
 
44
import java.util.Map;
45
 
46
/**
47
 * An Adapter to use any {@link IPad} as a {@link Transformer} in an
48
 * {@link Assembly}.
49
 * <p>
50
 * When using such a {@link Transformer}, in an {@link Assembly}, there must
51
 * be at least one element behind this instance in the constructed chain;
52
 * otherwise, a {@link TransformerException} is thrown at initialisation time.
53
 */
54
class PaddingTransformer
55
    extends Transformer
56
{
57
  private IPad delegate;
58
 
59
  private int outputBlockSize = 1;
60
 
61
  PaddingTransformer(IPad padding)
62
  {
63
    super();
64
 
65
    this.delegate = padding;
66
  }
67
 
68
  void initDelegate(Map attributes) throws TransformerException
69
  {
70
    if (tail == null)
71
      {
72
        IllegalStateException cause = new IllegalStateException(
73
            "Padding transformer missing its tail!");
74
        throw new TransformerException("initDelegate()", cause);
75
      }
76
    outputBlockSize = tail.currentBlockSize();
77
    delegate.init(outputBlockSize);
78
  }
79
 
80
  int delegateBlockSize()
81
  {
82
    return outputBlockSize;
83
  }
84
 
85
  void resetDelegate()
86
  {
87
    delegate.reset();
88
    outputBlockSize = 1;
89
  }
90
 
91
  byte[] updateDelegate(byte[] in, int offset, int length)
92
      throws TransformerException
93
  {
94
    inBuffer.write(in, offset, length);
95
    byte[] tmp = inBuffer.toByteArray();
96
    inBuffer.reset();
97
    byte[] result;
98
    if (wired == Direction.FORWARD) // padding
99
      {
100
        // buffers remaining bytes from (inBuffer + in) that are less than 1
101
        // block
102
        if (tmp.length < outputBlockSize)
103
          {
104
            inBuffer.write(tmp, 0, tmp.length);
105
            result = new byte[0];
106
          }
107
        else
108
          {
109
            int newlen = outputBlockSize * (tmp.length / outputBlockSize);
110
            inBuffer.write(tmp, newlen, tmp.length - newlen);
111
            result = new byte[newlen];
112
            System.arraycopy(tmp, 0, result, 0, newlen);
113
          }
114
      }
115
    else // unpadding
116
      {
117
        // always keep in own buffer a max of 1 block to cater for lastUpdate
118
        if (tmp.length < outputBlockSize)
119
          {
120
            inBuffer.write(tmp, 0, tmp.length);
121
            result = new byte[0];
122
          }
123
        else
124
          {
125
            result = new byte[tmp.length - outputBlockSize];
126
            System.arraycopy(tmp, 0, result, 0, result.length);
127
            inBuffer.write(tmp, result.length, outputBlockSize);
128
          }
129
      }
130
    return result;
131
  }
132
 
133
  byte[] lastUpdateDelegate() throws TransformerException
134
  {
135
    byte[] result;
136
    // process multiples of blocksize as much as possible
137
    // catenate result from processing inBuffer with last-update( tail )
138
    if (wired == Direction.FORWARD) // padding
139
      {
140
        result = inBuffer.toByteArray();
141
        byte[] padding = delegate.pad(result, 0, result.length);
142
        inBuffer.write(padding, 0, padding.length);
143
      }
144
    else // unpadding
145
      {
146
        byte[] tmp = inBuffer.toByteArray();
147
        inBuffer.reset();
148
        int realLength;
149
        try
150
          {
151
            realLength = tmp.length; // should be outputBlockSize
152
            realLength -= delegate.unpad(tmp, 0, tmp.length);
153
          }
154
        catch (WrongPaddingException x)
155
          {
156
            throw new TransformerException("lastUpdateDelegate()", x);
157
          }
158
        inBuffer.write(tmp, 0, realLength);
159
      }
160
    result = inBuffer.toByteArray();
161
    inBuffer.reset();
162
    return result;
163
  }
164
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.