Transformer.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* Transformer.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.assembly;
  32. import gnu.javax.crypto.pad.IPad;
  33. import java.io.ByteArrayOutputStream;
  34. import java.util.Map;
  35. /**
  36. * A <code>Transformer</code> is an abstract representation of a two-way
  37. * <i>transformation</i> that can be chained together with other instances of
  38. * this type. Examples of such transformations in this library are:
  39. * {@link Cascade} cipher, {@link gnu.javax.crypto.pad.IPad} algorithm, and a
  40. * ZLib-based deflater/inflater algorithm. A special implementation of a
  41. * <code>Transformer</code> to close a chain is also provided.
  42. * <p>
  43. * A <code>Transformer</code> is characterised by the followings:
  44. * <ul>
  45. * <li>It can be chained to other instances, to form an {@link Assembly}.</li>
  46. * <li>When configured in an {@link Assembly}, it can be set to apply its
  47. * internal transformation on the input data stream before (pre-processing) or
  48. * after (post-processing) passing the input data to the next element in the
  49. * chain. Note that the same type <code>Transformer</code> can be used as
  50. * either in pre-processing or a post-processing modes.</li>
  51. * <li>A special transformer --<code>LoopbackTransformer</code>-- is used
  52. * to close the chain.</li>
  53. * <li>A useful type of <code>Transformer</code> --one we're interested in--
  54. * has internal buffers. The distinction between a casual push (update)
  55. * operation and the last one allows to correctly flush any intermediate bytes
  56. * that may exist in those buffers.</li>
  57. * </ul>
  58. * <p>
  59. * To allow wiring <code>Transformer</code> instances together, a
  60. * <i>minimal-output-size</i> in bytes is necessary. The trivial case of a
  61. * value of <code>1</code> for such attribute practically means that no output
  62. * buffering, from the previous element, is needed --which is independant of
  63. * buffering the input if the <code>Transformer</code> implementation itself
  64. * is block-based.
  65. *
  66. * @see CascadeTransformer
  67. * @see PaddingTransformer
  68. * @see DeflateTransformer
  69. */
  70. public abstract class Transformer
  71. {
  72. public static final String DIRECTION = "gnu.crypto.assembly.transformer.direction";
  73. protected Direction wired;
  74. protected Operation mode;
  75. protected Transformer tail = null;
  76. protected ByteArrayOutputStream inBuffer = new ByteArrayOutputStream(2048);
  77. protected ByteArrayOutputStream outBuffer = new ByteArrayOutputStream(2048);
  78. /** Trivial protected constructor. */
  79. protected Transformer()
  80. {
  81. super();
  82. this.wired = null;
  83. }
  84. public static final Transformer getCascadeTransformer(Cascade cascade)
  85. {
  86. return new CascadeTransformer(cascade);
  87. }
  88. public static final Transformer getPaddingTransformer(IPad padding)
  89. {
  90. return new PaddingTransformer(padding);
  91. }
  92. public static final Transformer getDeflateTransformer()
  93. {
  94. return new DeflateTransformer();
  95. }
  96. /**
  97. * Sets the operational mode of this <code>Transformer</code>.
  98. *
  99. * @param mode the processing mode this <code>Transformer</code> is required
  100. * to operate in.
  101. * @throws IllegalStateException if this instance has already been assigned an
  102. * operational mode.
  103. */
  104. public void setMode(final Operation mode)
  105. {
  106. if (this.mode != null)
  107. throw new IllegalStateException();
  108. this.mode = mode;
  109. }
  110. /**
  111. * Returns <code>true</code> if this <code>Transformer</code> was wired in
  112. * pre-processing mode; <code>false</code> otherwise.
  113. *
  114. * @return <code>true</code> if this <code>Transformer</code> has been
  115. * wired in pre-processing mode; <code>false</code> otherwise.
  116. * @throws IllegalStateException if this instance has not yet been assigned an
  117. * operational <i>type</i>.
  118. */
  119. public boolean isPreProcessing()
  120. {
  121. if (mode == null)
  122. throw new IllegalStateException();
  123. return (mode == Operation.PRE_PROCESSING);
  124. }
  125. /**
  126. * Returns <code>true</code> if this <code>Transformer</code> was wired in
  127. * post-processing mode; <code>false</code> otherwise.
  128. *
  129. * @return <code>true</code> if this <code>Transformer</code> has been
  130. * wired in post-processing mode; <code>false</code> otherwise.
  131. * @throws IllegalStateException if this instance has not yet been assigned an
  132. * operational <i>type</i>.
  133. */
  134. public boolean isPostProcessing()
  135. {
  136. return ! isPreProcessing();
  137. }
  138. /**
  139. * Initialises the <code>Transformer</code> for operation with specific
  140. * characteristics.
  141. *
  142. * @param attributes a set of name-value pairs that describes the desired
  143. * future behaviour of this instance.
  144. * @throws IllegalStateException if the instance is already initialised.
  145. */
  146. public void init(Map attributes) throws TransformerException
  147. {
  148. if (wired != null)
  149. throw new IllegalStateException();
  150. Direction flow = (Direction) attributes.get(DIRECTION);
  151. if (flow == null)
  152. flow = Direction.FORWARD;
  153. wired = flow;
  154. inBuffer.reset();
  155. outBuffer.reset();
  156. tail.init(attributes); // initialise tail first
  157. initDelegate(attributes); // initialise this instance
  158. }
  159. /**
  160. * Returns the block-size of this <code>Transformer</code>. A value of
  161. * <code>1</code> indicates that this instance is block-agnostic.
  162. *
  163. * @return the current minimal required block size.
  164. */
  165. public int currentBlockSize()
  166. {
  167. if (wired == null)
  168. throw new IllegalStateException();
  169. return delegateBlockSize();
  170. }
  171. /**
  172. * Resets the <code>Transformer</code> for re-initialisation and use with
  173. * other characteristics. This method always succeeds.
  174. */
  175. public void reset()
  176. {
  177. resetDelegate();
  178. wired = null;
  179. inBuffer.reset();
  180. outBuffer.reset();
  181. tail.reset(); // reset tail last
  182. }
  183. /**
  184. * Convenience method that calls the method with same name and three
  185. * arguments, using a byte array of length <code>1</code> whose contents are
  186. * the designated byte.
  187. *
  188. * @param b the byte to process.
  189. * @return the result of transformation.
  190. * @throws IllegalStateException if the instance is not initialised.
  191. * @throws TransformerException if a transformation-related exception occurs
  192. * during the operation.
  193. * @see #update(byte[], int, int)
  194. */
  195. public byte[] update(byte b) throws TransformerException
  196. {
  197. return update(new byte[] { b }, 0, 1);
  198. }
  199. /**
  200. * Convenience method that calls the same method with three arguments. All
  201. * bytes in <code>in</code>, starting from index position <code>0</code>
  202. * are considered.
  203. *
  204. * @param in the input data bytes.
  205. * @return the result of transformation.
  206. * @throws IllegalStateException if the instance is not initialised.
  207. * @throws TransformerException if a transformation-related exception occurs
  208. * during the operation.
  209. * @see #update(byte[], int, int)
  210. */
  211. public byte[] update(byte[] in) throws TransformerException
  212. {
  213. return update(in, 0, in.length);
  214. }
  215. /**
  216. * Processes a designated number of bytes from a given byte array.
  217. *
  218. * @param in the input data bytes.
  219. * @param offset index of <code>in</code> from which to start considering
  220. * data.
  221. * @param length the count of bytes to process.
  222. * @return the result of transformation.
  223. * @throws IllegalStateException if the instance is not initialised.
  224. * @throws TransformerException if a transformation-related exception occurs
  225. * during the operation.
  226. */
  227. public byte[] update(byte[] in, int offset, int length)
  228. throws TransformerException
  229. {
  230. if (wired == null)
  231. throw new IllegalStateException();
  232. byte[] result = (wired == Direction.FORWARD ? forwardUpdate(in, offset, length)
  233. : inverseUpdate(in, offset, length));
  234. return result;
  235. }
  236. /**
  237. * Convenience method that calls the same method with three arguments. A
  238. * zero-long byte array is used.
  239. *
  240. * @return the result of transformation.
  241. * @throws IllegalStateException if the instance is not initialised.
  242. * @throws TransformerException if a transformation-related exception occurs
  243. * during the operation.
  244. * @see #lastUpdate(byte[], int, int)
  245. */
  246. public byte[] lastUpdate() throws TransformerException
  247. {
  248. byte[] result = (wired == Direction.FORWARD ? lastForwardUpdate()
  249. : lastInverseUpdate());
  250. if (inBuffer.size() != 0) // we still have some buffered bytes
  251. throw new TransformerException("lastUpdate(): input buffer not empty");
  252. return result;
  253. }
  254. /**
  255. * Convenience method that calls the method with same name and three
  256. * arguments, using a byte array of length <code>1</code> whose contents are
  257. * the designated byte.
  258. *
  259. * @param b the byte to process.
  260. * @return the result of transformation.
  261. * @throws IllegalStateException if the instance is not initialised.
  262. * @throws TransformerException if a transformation-related exception occurs
  263. * during the operation.
  264. * @see #lastUpdate(byte[], int, int)
  265. */
  266. public byte[] lastUpdate(byte b) throws TransformerException
  267. {
  268. return lastUpdate(new byte[] { b }, 0, 1);
  269. }
  270. /**
  271. * Convenience method that calls the same method with three arguments. All
  272. * bytes in <code>in</code>, starting from index position <code>0</code>
  273. * are considered.
  274. *
  275. * @param in the input data bytes.
  276. * @return the result of transformation.
  277. * @throws IllegalStateException if the instance is not initialised.
  278. * @throws TransformerException if a transformation-related exception occurs
  279. * during the operation.
  280. * @see #lastUpdate(byte[], int, int)
  281. */
  282. public byte[] lastUpdate(byte[] in) throws TransformerException
  283. {
  284. return lastUpdate(in, 0, in.length);
  285. }
  286. /**
  287. * Processes a designated number of bytes from a given byte array and signals,
  288. * at the same time, that this is the last <i>push</i> operation on this
  289. * <code>Transformer</code>.
  290. *
  291. * @param in the input data bytes.
  292. * @param offset index of <code>in</code> from which to start considering
  293. * data.
  294. * @param length the count of bytes to process.
  295. * @return the result of transformation.
  296. * @throws IllegalStateException if the instance is not initialised.
  297. * @throws TransformerException if a transformation-related exception occurs
  298. * during the operation.
  299. */
  300. public byte[] lastUpdate(byte[] in, int offset, int length)
  301. throws TransformerException
  302. {
  303. byte[] result = update(in, offset, length);
  304. byte[] rest = lastUpdate();
  305. if (rest.length > 0)
  306. {
  307. byte[] newResult = new byte[result.length + rest.length];
  308. System.arraycopy(result, 0, newResult, 0, result.length);
  309. System.arraycopy(rest, 0, newResult, result.length, rest.length);
  310. result = newResult;
  311. }
  312. return result;
  313. }
  314. private byte[] forwardUpdate(byte[] in, int off, int len)
  315. throws TransformerException
  316. {
  317. return (isPreProcessing() ? preTransform(in, off, len)
  318. : postTransform(in, off, len));
  319. }
  320. private byte[] inverseUpdate(byte[] in, int off, int len)
  321. throws TransformerException
  322. {
  323. return (isPreProcessing() ? postTransform(in, off, len)
  324. : preTransform(in, off, len));
  325. }
  326. private byte[] preTransform(byte[] in, int off, int len)
  327. throws TransformerException
  328. {
  329. byte[] result = updateDelegate(in, off, len);
  330. result = tail.update(result);
  331. return result;
  332. }
  333. private byte[] postTransform(byte[] in, int off, int len)
  334. throws TransformerException
  335. {
  336. byte[] result = tail.update(in, off, len);
  337. result = updateDelegate(result, 0, result.length);
  338. return result;
  339. }
  340. private byte[] lastForwardUpdate() throws TransformerException
  341. {
  342. return (isPreProcessing() ? preLastTransform() : postLastTransform());
  343. }
  344. private byte[] lastInverseUpdate() throws TransformerException
  345. {
  346. return (isPreProcessing() ? postLastTransform() : preLastTransform());
  347. }
  348. private byte[] preLastTransform() throws TransformerException
  349. {
  350. byte[] result = lastUpdateDelegate();
  351. result = tail.lastUpdate(result);
  352. return result;
  353. }
  354. private byte[] postLastTransform() throws TransformerException
  355. {
  356. byte[] result = tail.lastUpdate();
  357. result = updateDelegate(result, 0, result.length);
  358. byte[] rest = lastUpdateDelegate();
  359. if (rest.length > 0)
  360. {
  361. byte[] newResult = new byte[result.length + rest.length];
  362. System.arraycopy(result, 0, newResult, 0, result.length);
  363. System.arraycopy(rest, 0, newResult, result.length, rest.length);
  364. result = newResult;
  365. }
  366. return result;
  367. }
  368. abstract void initDelegate(Map attributes) throws TransformerException;
  369. abstract int delegateBlockSize();
  370. abstract void resetDelegate();
  371. abstract byte[] updateDelegate(byte[] in, int off, int len)
  372. throws TransformerException;
  373. abstract byte[] lastUpdateDelegate() throws TransformerException;
  374. }