type1enc.ps 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. % Copyright (C) 1992, 1993 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: type1enc.ps,v 1.4 2002/02/21 21:49:28 giles Exp $
  16. % type1enc.ps
  17. % PostScript language versions of the Type 1 encryption/decryption algorithms.
  18. % This file is normally not needed with Ghostscript, since Ghostscript
  19. % implements these algorithms in C. For the specifications, see Chapter 7 of
  20. % "Adobe Type 1 Font Format," ISBN 0-201-57044-0, published by Addison-Wesley.
  21. /.type1crypt % <R> <from> <to> <proc> .type1crypt <R'> <to>
  22. % (auxiliary procedure)
  23. { 4 1 roll
  24. 0 2 index length getinterval
  25. 0 1 2 index length 1 sub
  26. { % Stack: proc R from to index
  27. 2 index 1 index get % proc R from to index C/P
  28. 4 index -8 bitshift xor 3 copy put % proc R from to index P/C
  29. 5 index exec % proc R from to C
  30. % Compute R' = ((R + C) * 52845 + 22719) mod 65536
  31. % without exceeding a 31-bit integer magnitude, given that
  32. % 0 <= R <= 65535 and 0 <= C <= 255.
  33. 4 -1 roll add
  34. dup 20077 mul % 52845 - 32768
  35. exch 1 and 15 bitshift add % only care about 16 low-order bits
  36. 22719 add 65535 and 3 1 roll
  37. }
  38. for exch pop 3 -1 roll pop
  39. } bind def
  40. % <state> <fromString> <toString> .type1encrypt <newState> <toSubstring>
  41. % Encrypts fromString according to the algorithm for Adobe
  42. % Type 1 fonts, writing the result into toString.
  43. % toString must be at least as long as fromString or a
  44. % rangecheck error occurs. state is the initial state of
  45. % the encryption algorithm (a 16-bit non-negative
  46. % integer); newState is the new state of the algorithm.
  47. /.type1encrypt
  48. { { exch pop } .type1crypt
  49. } bind def
  50. % <state> <fromString> <toString> .type1decrypt <newState> <toSubstring>
  51. % Decrypts fromString according to the algorithm for Adobe
  52. % Type 1 fonts, writing the result into toString. Other
  53. % specifications are as for type1encrypt.
  54. /.type1decrypt
  55. { { pop 2 index exch get } .type1crypt
  56. } bind def