[net.sources] OSSI: SIPacking

biagioni@unc.UUCP (Edoardo Biagioni) (11/06/86)

(***************************************************************************)
(***                                                                     ***)
(***                                                                     ***)
(***                        O  S  S  I                                   ***)
(***                        ==========                                   ***)
(***                                                                     ***)
(**)               DEFINITION MODULE SIPacking;                          (**)
(***               ===========================                           ***)
(***                                                                     ***)
(***   This module defines constants and procedures to handle storage    ***)
(***   in a machine- compiler- and operating-system-independent way      ***)
(***                                                                     ***)
(***---------------------------------------------------------------------***)
(***                                                                     ***)
(***   Hardware:             independent                                 ***)
(***   Operating System:     independent                                 ***)
(***   Compiler:             independent                                 ***)
(***                                                                     ***)
(***   Version:      3.0                                                 ***)
(***   Implemented:  see copyright                                       ***)
(***   Date:         1986-03-12                                          ***)
(***                                                                     ***)
(***---------------------------------------------------------------------***)
(***                                                                     ***)
(***   Copyright 1984, 1985, 1986 by                                     ***)
(***      E. S. Biagioni                                                 ***)
(***      G. Heiser                                                      ***)
(***      K. Hinrichs                                                    ***)
(***      C. Muller                                                      ***)
(***                                                                     ***)
(***   Institut fuer Informatik                                          ***)
(***   ETH Zuerich                                                       ***)
(***   CH 8092 Zuerich                                                   ***)
(***   Switzerland                                                       ***)
(***                                                                     ***)
(***   Department of Computer Science                                    ***)
(***   University of North Carolina                                      ***)
(***   Chapel Hill, North Carolina 27514                                 ***)
(***   U.S.A.                                                            ***)
(***                                                                     ***)
(*** Permission to copy without fee all of this material is granted      ***)
(*** provided that the copies are not made or distributed for direct     ***)
(*** commercial advantage, that this OSSI copyright notice is            ***)
(*** included in the copy, that the module is not modified in any way    ***)
(*** except where necessary for compilation on a particular system,      ***)
(*** and that the module is always distributed in its original form.     ***)
(*** Distribution of this module in a modified form without including    ***)
(*** the original version is a violation of this copyright notice.       ***)
(***                                                                     ***)
(***---------------------------------------------------------------------***)
(***                                                                     ***)
(***   Updates:                                                          ***)
(***                                                                     ***)
(***                                                                     ***)
(***************************************************************************)

FROM SYSTEM IMPORT
  WORD,
  ADDRESS;

FROM SISystem IMPORT
  BYTE;


EXPORT QUALIFIED
  ConvertToBytes,
  ConvertFromBytes,
  PackCard,
  UnpackCard,
  PackInt,
  UnpackInt;


PROCEDURE ConvertToBytes (VAR data:   ARRAY OF WORD;
                          VAR bytes:  ARRAY OF BYTE;
                          index:      CARDINAL);
(* ConvertToBytes copies "data" (((HIGH (data) + 1) * BytesPerWord) bytes)
   into the array "bytes" (starting at array index "index").
   The relative order of the bytes is preserved.
   The procedure does not check whether
     (HIGH (data) + 1) * BytesPerWord - 1 + index <= HIGH (bytes).
   "data" is passed as a VAR parameter for reasons of efficiency. *)

PROCEDURE ConvertFromBytes (VAR bytes:   ARRAY OF BYTE;
                            index:       CARDINAL;
                            VAR data:    ARRAY OF WORD);
(* ConvertFromBytes transfers (((HIGH (data) + 1) * BytesPerWord)  bytes)
   from the array "bytes", starting at array index "index" into array "data".
   The procedure does not check whether 
     (HIGH (data) + 1) * BytesPerWord  - 1 + index <= HIGH (bytes).
   "bytes" is passed as a VAR parameter for reasons of efficiency. *)

PROCEDURE PackCard (c:             CARDINAL;
                    VAR bytes:     ARRAY OF BYTE;
                    index,length:  CARDINAL);
(* PackCard copies the "length" ( <= "BytesPerCard" ) least significant bytes
   of "c" into "bytes" (starting at array index "index"). The relative order
   of the bytes is preserved. The procedure does not check whether
      (length <= BytesPerCard) and (index + length - 1) <= HIGH (bytes). *)

PROCEDURE UnpackCard (VAR bytes:     ARRAY OF BYTE;
                      index,length:  CARDINAL): CARDINAL;
(* UnpackCard transfers "length" ( <= "BytesPerCard") bytes from the array
   "bytes" (starting at array index "index") into a CARDINAL. The missing
   most significant bytes are set to zero. The procedure does not check that
      (length <= BytesPerCard) and (index + length - 1 <= HIGH (bytes).
   "bytes" is passed as a VAR parameter for reasons of efficiency. *)

PROCEDURE PackInt (i:            INTEGER;
                   VAR bytes:    ARRAY OF BYTE;
                   index,length: CARDINAL);
(* PackInt copies the "length" ( <= "BytesPerInt" ) least significant bytes
   of "i" into "bytes" (starting at array index "index"). The relative order
   of the bytes is preserved.
   If  - 2 ^ (BitsPerByte * length - 1) < i < 2 ^ (BitsPerByte * length - 1)
   a subsequent call to UnpackInt (with appropriate parameters) will
   reproduce i. The procedure does not check whether
     (length <= BytesPerInt) and (index + length - 1 <= HIGH (bytes). *)

PROCEDURE UnpackInt (VAR bytes:     ARRAY OF BYTE;
                     index,length:  CARDINAL): INTEGER;
(* UnpackInt transfers "length" ( <= "BytesPerInt") bytes from the array
   "bytes" (starting at array index "index") into an INTEGER. The missing
   most significant bytes (but not the sign bit) are set to zero.
   If an integer i was packed into bytes by a call to PackInt and i satisfied
     - 2 ^ (BitsPerByte * length - 1) < i < 2 ^ (BitsPerByte * length - 1)
   then UnpackInt (with appropriate parameters) will return the value of i.
   The procedure does not check whether
     (length <= BytesPerInt) and (index + length - 1 <= HIGH (bytes).
   "bytes" is passed as a VAR parameter for reasons of efficiency. *)
                              
END SIPacking.