ooRexx logo
   1: #!/usr/bin/rexx
   2: /* --------------------------------------------------------------------------------------- */
   3: /* No freakin' copyright, no stinkin' license, no guarantees or warranties                 */
   4: /* (implied, explicit or whatever). Usage is totally and completely at your own risk.      */
   5: /* Please keep this comment block as is when modifying this code. Thanks in advance,       */
   6: /*   Ruurd Idenburg                                                                        */
   7: /* --------------------------------------------------------------------------------------- */
   8: /*   2016/04/04  Initial version - Ruurd Idenburg                                          */
   9: /* --------------------------------------------------------------------------------------- */
  10: /* Checks the architecture and bitness of a Windows executable ( EXE or DLL )              */
  11: /* --------------------------------------------------------------------------------------- */
  12: parse source os how name .
  13: parse arg file_name
  14: file_stream = .stream~new(file_name)
  15: -- read the DOS header, PE offset is a 32 bit little-endian integer 
  16: parse value file_stream~charin(,64) with id 3 . 61 PE_offset
  17: -- header should start with the magic 'MZ'
  18: if id<>'MZ' then do
  19:   raise syntax 88.917 array ("'"file_name"'", "is not a valid Windows executable")
  20: end
  21: -- read the PE header for 6 bytes at PE_offset (offset is little-endian base 0)
  22: -- the last two bytes indicate the architecture and resulting bitness
  23: parse value file_stream~charin(PE_offset~reverse~c2d+1,6) with id 3 . 5 arch
  24: -- header should start with 'PE'
  25: if id<>'PE' then do
  26:   raise syntax 88.917 array ("'"file_name"'", "is not a PE conforming executable")
  27: end
  28: -- arch is little-endian
  29: arch = arch~reverse
  30: select 
  31:   when arch=='014c'x then res = 'i386->'32
  32:   when arch=='0200'x then res = 'ia64->'64
  33:   when arch=='8664'x then res = 'amd64->'64
  34:   otherwise res = 'unknown->unknown'
  35: end
  36: if how=='COMMAND' then do
  37:   -- Assuming we're running in Windows (in upcoming release we can use .pathseparator)
  38:   say "The machine architecture->bitness for ["file_name~makearray("\")~lastitem"] is:" res
  39:   exit
  40: end
  41: return res
  42: 
  43: 
If you feel inclined to make corrections, suggestions etc., please mail me any.
All content © Ruurd Idenburg, 2007–, except where marked otherwise. All rights reserved. This page is primarily for non-commercial use only. The Idenburg website records no personal information and sets no ‘cookies’. This site is hosted on a VPS(Virtual Private System) rented from Transip.nl, a Dutch company, falling under Dutch (privacy) laws (I think).

This page updated on by Ruurd Idenburg.