Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Validate Image File by Signature

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validate Image File by Signature

    Discussion in: https://forum.powerbasic.com/forum/u...ure#post796527

    Validate image signature
    Code:
    'DECLARE FUNCTION isImgVal LIB "your.DLL" ALIAS "isimgval" (BYREF ifilename as string, BYVAL itype as long) as long
    'DECLARE FUNCTION isImg_ErrorText LIB "your.DLL" ALIAS "isimg_errtext" (BYREF ierr as long) export AS string
    '******************************************************************
    '*    File: isimgVal.bas
    '*   Descr: Test a file to see if it is a valid image based on the type expected
    '*    NOTE: Use the list of signatures as of 20200603.1112
    '*          Wikipedia: https://en.wikipedia.org/wiki/List_of_file_signatures
    '*          File Signatures: https://www.filesignatures.net/index.php
    '*
    '* Copyright 2020-2020 III Software LLC
    '*   All rights reserved worldwide.
    '******************************************************************
    '*     Program Author: Raymond W. Leech, III
    '*            Project: webx_lowlevel
    '*             Client: ALL
    '*================================================================*
    '*               Revision History
    '*================================================================*
    '* Ver   Date       Who Comment
    '* ---- ---------- --- -----------------------------------------------
    '* 1    06-01-2020 RWL Production (iii.313)
    '*
    '******************************************************************
    '*
    '* License: MIT License
    '* Permission is hereby granted, free of charge, to any person obtaining a copy
    '* of this software and associated documentation files (the "Software"), to deal
    '* in the Software without restriction, including without limitation the rights
    '* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    '* copies of the Software, and to permit persons to whom the Software is
    '* furnished to do so, subject to the following conditions:
    '*
    '* The above copyright notice and this permission notice shall be included in all
    '* copies or substantial portions of the Software.
    '*
    '* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    '* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    '* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    '* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    '* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    '* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    '* SOFTWARE.
    '******************************************************************
    ' Constants
    '******************************************************************
    %isimage_error_FileNameMissing = -1 ' No file name provided
    %isimage_error_FileNotFound = -2 ' File given does not exist
    %isimage_error_FileUnavailable = -3 ' File cannot be opened/read
    %isimage_error_UnsupportedExtension = -4 ' The extension does not match any supported image type
    %isimage_error_InvalidSignature = -5 ' The signature does not match the expected image type
    %isimage_error_ExtensionMismatch = -6 ' The extension does not match that expected by the signature
    %isimage_error_NotExpectedType = -7 ' The signature does not match the image type you requested
    %isimage_error_UnrecognizedImage = -8 ' Not a recognized image type
    
    %isimage_True = 1 ' Image is valid for type
    %isimage_False = 0 ' Image is NOT valid for type
    
    $isimage_errorx_FileNameMissing = "No file name provided"
    $isimage_errorx_FileNotFound = "File given does not exist"
    $isimage_errorx_FileUnavailable = "File cannot be opened/read"
    $isimage_errorx_UnsupportedExtension = "The extension does not match any supported image type"
    $isimage_errorx_InvalidSignature = "The signature does not match the expected image type"
    $isimage_errorx_ExtensionMismatch = "The extension does not match the signature"
    $isimage_errorx_NotExpectedType = "The signature does not match the image type you requested"
    $isimage_errorx_UnrecognizedImage = "Not a recognized image type"
    $isimage_errorx_InvalidError = "Not a recognized error"
    $isimage_errorx_ico = "Success: ICO"
    $isimage_errorx_Gif87a = "Success: Gif87a"
    $isimage_errorx_Gif89a = "Success: Gif89a"
    $isimage_errorx_TIFF = "Success: TIFF"
    $isimage_errorx_jpg = "Success: JPG"
    $isimage_errorx_pdf = "Success: PDF"
    $isimage_errorx_png = "Success: PNG"
    $isimage_errorx_bmp = "Success: BMP"
    
    %isImage_ValExtension_No  = 0
    %isImage_ValExtension_Yes = 1
    
    %isimage_ico = 1
    %isimage_Gif87a = 2
    %isimage_Gif89a = 3
    %isimage_Gif = 4 ' Matches either gif format (only for isImgVal)
    %isimage_TIFF = 5
    %isimage_jpg = 6
    %isimage_pdf = 7
    %isimage_png = 8
    %isimage_bmp = 9
    
    '******************************************************************
    ' Image validation routine
    '******************************************************************
    FUNCTION isImgVal _
       ALIAS "isimgval" _
             ( _
               BYREF ifilename as string, _ 'Fully qualified filename, or short filename relative to current directory
               BYVAL itype as long _ ' Type of image expected (use constants)
             ) _
             export _
          AS long
    
      ' Local storage
      local ierr as long
      local FoundImgType as long
      local ifile as long
      local signature as string
      local sigStart as long
      local sigLen as long
      local ws_extension as string
    
    '------------------------------------------------------------------
    program_begin:
    
      ' Initialize
      FoundImgType = %isimage_False ' Set the return code as bad until we find out otherwise
    
      ' see if the file exists
      if len(ifilename) = 0 then
         FoundImgType = %isimage_error_FileNameMissing
         goto program_exit
      end if
      if isFile(ifilename) = 0 then
         FoundImgType = %isimage_error_FileNotFound
         goto program_exit
      end if
    
      ' Open the file
      ifile = freefile
      errclear
      open ifilename for binary access read lock shared as #ifile len=512 base=0
      if errclear <> 0 then
         FoundImgType = %isimage_error_FileUnavailable
         goto program_exit
      end if
    
      ' Validate the signature and extension match the file type requested
      ws_extension = pathname$(extn, ifilename)
      select case as const iType
        case %isimage_ico
             sigStart = 0
             sigLen = 4
             gosub get_signature
             if signature = Hex2Asc("00000100") then
                if ws_extension = ".ico" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
        case %isimage_Gif87a, %isimage_Gif89a, %isimage_Gif
             ' This routines both versions of GIF the same. When looking for only one version, use isimg
             sigStart = 0
             sigLen = 6
             gosub get_signature
             if signature = Hex2Asc("474946383761") _
             or signature = Hex2Asc("474946383961") then
                if ws_extension = ".gif" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
        case %isimage_TIFF
             sigStart = 0
             sigLen = 4
             gosub get_signature
             if signature = Hex2Asc("49492A00") _
             or signature = Hex2Asc("4D4D002A") _
             or signature = Hex2Asc("4D4D002B") _
             or left$(signature,3) = Hex2Asc("492049") then
                if ws_extension = ".tif" or ws_extension = ".tiff" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
        case %isimage_jpg
             ' Took a shortcut here. Full match should be first four, skip 2, next four
             ' Like: FF D8 FF E0 <skip two bytes> 4A 46 49 46 00
             sigStart = 0
             sigLen = 4
             gosub get_signature
             if signature = Hex2Asc("FFD8FFDB") _
             or signature = Hex2Asc("FFD8FFEE") _
             or signature = Hex2Asc("FFD8FFE0") _
             or signature = Hex2Asc("FFD8FFE1") then
                if ws_extension = ".jpg" or ws_extension <> ".jpeg" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
        case %isimage_pdf
             sigStart = 0
             sigLen = 5
             gosub get_signature
             if signature = Hex2Asc("255044462D") then
                if ws_extension = ".pdf" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
        case %isimage_png
             sigStart = 0
             sigLen = 8
             gosub get_signature
             if signature = Hex2Asc("89504E470D0A1A0A") then
                if ws_extension = ".png" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
        case %isimage_bmp
             sigStart = 0
             sigLen = 2
             gosub get_signature
             if signature = Hex2Asc("424D") then
                if ws_extension = ".bmp" then
                   FoundImgType = %isimage_True
                else
                   FoundImgType = %isimage_error_ExtensionMismatch
                end if
             else
                FoundImgType = %isimage_error_InvalidSignature
             end if
      end select
    
    
    '------------------------------------------------------------------
    program_exit:
      close #ifile
      function = FoundImgType
      exit function
    
    '------------------------------------------------------------------
    get_signature:
      seek #ifile, sigStart
      get$ #ifile, sigLen, signature
      return
    
    END FUNCTION
    
    '******************************************************************
    ' Convert error number to error message text
    '******************************************************************
    FUNCTION isimg_ErrorText ALIAS "isimg_errortext" (BYREF ierr as long) export AS string
     '------------------------------------------------------------------
      select case as const ierr
        case %isimage_error_FileNameMissing
             function = $isimage_errorx_FileNameMissing
        case %isimage_error_FileNotFound
             function = $isimage_errorx_FileNotFound
        case %isimage_error_FileUnavailable
             function = $isimage_errorx_FileUnavailable
        case %isimage_error_UnsupportedExtension
             function = $isimage_errorx_UnsupportedExtension
        case %isimage_error_InvalidSignature
             function = $isimage_errorx_InvalidSignature
        case %isimage_error_ExtensionMismatch
             function = $isimage_errorx_ExtensionMismatch
        case %isimage_error_NotExpectedType
             function = $isimage_errorx_NotExpectedType
        case %isimage_error_UnrecognizedImage
             function = $isimage_errorx_UnrecognizedImage
        case %isimage_ico
             function = $isimage_errorx_ico
        case %isimage_Gif87a
             function = $isimage_errorx_gif87a
        case %isimage_Gif89a
             function = $isimage_errorx_gif89a
        case %isimage_TIFF
             function = $isimage_errorx_tiff
        case %isimage_jpg
             function = $isimage_errorx_jpg
        case %isimage_pdf
             function = $isimage_errorx_pdf
        case %isimage_png
             function = $isimage_errorx_png
        case %isimage_bmp
             function = $isimage_errorx_bmp
        case else
             function = $isimage_errorx_InvalidError
      end select
    end function
    '******************************************************************
    Test Program
    Code:
    #COMPILE EXE
    #stack 4194304
    #DIM ALL
    '#TOOLS ON
    '******************************************************************
    '*    File: test_isImgVal.bas
    '*   Descr: Test the image validation routine (isImgVal)
    '*
    '* Copyright 2020 III Software, LLC
    '*   All rights reserved worldwide.
    '******************************************************************
    '*     Program Author: Raymond W. Leech, III
    '*            Project: webx_test
    '*             Client: ALL
    '*================================================================*
    '*               Revision History
    '*================================================================*
    '* Ver  Date       Who Comment
    '* ---- ---------- --- -----------------------------------------------
    '* 1    06-01-2020 RWL Production (iii.313)
    '*
    '******************************************************************
    '*
    '* License: Released into the Public Domain: 06/01/2020
    '******************************************************************
    ' Constants and declares
    #include "isImgVal.bas"
    #include "hex2asc.bas"
    
    '******************************************************************
    SUB test_isImgVal ALIAS "test_isimgval" () EXPORT
    
      ' local storage
      local ierr as long
      local itype as long
    
      ' Command line parms
      local ws_command_line AS STRING
      DIM ws_command_parm(1 TO 10) AS local STRING
      local ws_command_parm_count AS LONG
      local ws_type as string
      local ws_filename as string
    
    '-------------------------------------------------------------------------------
    program_begin:
    
       ' Initialize
       ierr = 0 ' No error
    
       ?"Command line: test_isimgVal [imgtype] [filename]  (Where [imgtype] is one of: ico, gif, tif, jpg, pdf, png, bmp)"
    
       ' Get token from command line
       ws_command_line = COMMAND$
       ws_command_parm_count = PARSECOUNT(ws_command_line, ANY " ")
       PARSE ws_command_line, ws_command_parm(), ANY " "
       ws_type = lcase$(ws_command_parm(1))
       ws_filename = ws_command_parm(2)
       select case as const$ ws_type
         case "ico"
              itype = %isimage_ico
         case "gif"
              itype = %isimage_Gif
         case "tif"
              itype = %isimage_TIFF
         case "jpg"
              itype = %isimage_jpg
         case "pdf"
              itype = %isimage_pdf
         case "png"
              itype = %isimage_png
         case "bmp"
              itype = %isimage_bmp
         case else
              ?"Not a valid image type on command line: " + ws_type
              goto program_exit
       end select
       if len(ws_filename) = 0 then
          ?"Did not provide an image file to test on the command line"
          goto program_exit
       end if
    
       ' Test1 -- Ensure the file contains the correct signature AND the extension matches the image type
       ierr = isimgval(ws_filename, itype)
       ?" == Test File:"+ws_filename+" =="
       select case as const ierr
         case 0
              ?"Return=False: Image signature and extension DO NOT match expected type"
         case 1
              ?"Return=True: Image signature and extension match expected type"
         case else
              ?"Return="+Trim$(ierr)+": "+isimg_ErrorText(ierr)
       end select
    
       ' Done
       goto program_exit
    
    '-------------------------------------------------------------------------------
    program_exit:
       EXIT SUB
    
    END SUB
    
    '******************************************************************
    FUNCTION PBMAIN()
        CALL test_isImgVal()
    END FUNCTION
    '******************************************************************
    Stuart's Hex2Asc code
    Code:
    '******************************************************************
    ' From winapi\WinCrypt
    ' dwFlags has the following defines
    ' certenrolld_begin -- CRYPT_STRING_*
    %CRYPT_STRING_BASE64HEADER         = &H00000000
    %CRYPT_STRING_BASE64               = &H00000001
    %CRYPT_STRING_BINARY               = &H00000002
    %CRYPT_STRING_BASE64REQUESTHEADER  = &H00000003
    %CRYPT_STRING_HEX                  = &H00000004
    %CRYPT_STRING_HEXASCII             = &H00000005
    %CRYPT_STRING_BASE64_ANY           = &H00000006
    %CRYPT_STRING_ANY                  = &H00000007
    %CRYPT_STRING_HEX_ANY              = &H00000008
    %CRYPT_STRING_BASE64X509CRLHEADER  = &H00000009
    %CRYPT_STRING_HEXADDR              = &H0000000a
    %CRYPT_STRING_HEXASCIIADDR         = &H0000000b
    %CRYPT_STRING_HEXRAW               = &H0000000c
    %CRYPT_STRING_NOCRLF               = &H40000000
    %CRYPT_STRING_NOCR                 = &H80000000???
    DECLARE FUNCTION CryptStringToBinaryA LIB "Crypt32.dll" _
        ALIAS "CryptStringToBinaryA" (pszString AS ASCIIZ, _
        BYVAL cchString AS DWORD, BYVAL dwFlags AS DWORD, _
        BYVAL pbBinary AS BYTE PTR, pcbBinary AS DWORD, pdwSkip AS DWORD, _
        pdwFlags AS DWORD) AS LONG
    
    '******************************************************************
    ' Stuart McLachlan: https://forum.powerbasic.com/forum/user-to-user-discussions/programming/785347-is-there-a-native-pb-function-to-convert-a-hex-string-to-ascii-characters?p=785393#post785393
     FUNCTION Hex2Asc(sHex AS STRING) AS STRING
     LOCAL sAsc AS STRING
     sAsc = NUL$(LEN(sHex) / 2)
     CryptStringToBinaryA(BYVAL STRPTR(sHex),   LEN(sHex), %CRYPT_STRING_HEXRAW, _
             BYVAL STRPTR(sAsc), LEN(sAsc), BYVAL 0, BYVAL 0)
     FUNCTION = sAsc
    END FUNCTION

  • #2
    Discussion in: https://forum.powerbasic.com/forum/u...ure#post796527

    This function tries to identify the image type from the list of supported signatures. You can paste it below the "isImgVal" function in the prior post.

    Code:
    'DECLARE FUNCTION isimg LIB "your.DLL" ALIAS "isimg" (BYREF ifilename as string, BYVAL ivalext as long, optional BYREF itype as long) as long
    '******************************************************************
    '*    File: isimg.bas
    '*   Descr: Test a file to see if it is any of the supported image types
    '*    NOTE: Use the list of signatures as of 20200603.1112
    '*          Wikipedia: https://en.wikipedia.org/wiki/List_of_file_signatures
    '*          File Signatures: https://www.filesignatures.net/index.php
    '*
    '* Copyright 2020-2020 III Software LLC
    '*   All rights reserved worldwide.
    '******************************************************************
    '*     Program Author: Raymond W. Leech, III
    '*            Project: webx_lowlevel
    '*             Client: ALL
    '*================================================================*
    '*               Revision History
    '*================================================================*
    '* Ver   Date       Who Comment
    '* ---- ---------- --- -----------------------------------------------
    '* 1    06-03-2020 RWL Production
    '* 2    06-05-2020 RWL Add extension validation (iii.313)
    '*
    '******************************************************************
    '*
    '* License: MIT License
    '* Permission is hereby granted, free of charge, to any person obtaining a copy
    '* of this software and associated documentation files (the "Software"), to deal
    '* in the Software without restriction, including without limitation the rights
    '* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    '* copies of the Software, and to permit persons to whom the Software is
    '* furnished to do so, subject to the following conditions:
    '*
    '* The above copyright notice and this permission notice shall be included in all
    '* copies or substantial portions of the Software.
    '*
    '* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    '* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    '* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    '* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    '* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    '* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    '* SOFTWARE.
    '******************************************************************
    FUNCTION isImg _
       ALIAS "isimg" _
             ( _
               BYREF ifilename as string, _ 'Fully qualified filename, or short filename relative to current directory
               BYVAL ivalext as long, _ ' Should the extension be validated against file type? (0=no, 1=yes)
               Optional BYREF itype as long _ ' Only required when you expect a specific type, match found type against requested type
             ) _
             export _
          AS long
    
      ' Local storage
      local ierr as long
      local FoundImgType as long
      local ifile as long
      local signature as string
      local sigStart as long
      local sigLen as long
      local ws_extension as string
    
    '------------------------------------------------------------------
    program_begin:
    
      ' Initialize
      FoundImgType = 0 ' Set the return code as good until we find out otherwise
    
      ' see if the file exists
      if len(ifilename) = 0 then
         FoundImgType = %isimage_error_FileNameMissing
         goto program_exit
      end if
      if isFile(ifilename) = 0 then
         FoundImgType = %isimage_error_FileNotFound
         goto program_exit
      end if
    
      ' Open the file
      ifile = freefile
      errclear
      open ifilename for binary access read lock shared as #ifile len=512 base=0
      if errclear <> 0 then
         FoundImgType = %isimage_error_FileUnavailable
         goto program_exit
      end if
    
      ' Don't care about the extension, try to identify the file by signature
      gosub match_signatures
      if FoundImgType = 0 then
         FoundImgType = %isimage_error_UnrecognizedImage
         goto program_exit
      end if
    
      ' Based on extension, validate the signature
      if ivalext = 1 then
         ws_extension = pathname$(extn, ifilename)
         select case as const FoundImgType
           case %isimage_ico
                if ws_extension <> ".ico" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_Gif87a
                if ws_extension <> ".gif" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_Gif89a
                if ws_extension <> ".gif" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_TIFF
                if ws_extension <> ".tif" and ws_extension <> ".tiff" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_jpg
                if ws_extension <> ".jpg" and ws_extension <> ".jpeg" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_pdf
                if ws_extension <> ".pdf" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_png
                if ws_extension <> ".png" then FoundImgType = %isimage_error_ExtensionMismatch
           case %isimage_bmp
                if ws_extension <> ".bmp" then FoundImgType = %isimage_error_ExtensionMismatch
         end select
      end if
      goto program_exit
    
    
    '------------------------------------------------------------------
    get_signature:
      seek #ifile, sigStart
      get$ #ifile, sigLen, signature
      return
    
    '------------------------------------------------------------------
    match_signatures:
      ' Test the two-byte signatures
      sigStart = 0
      sigLen = 2
      gosub get_signature
      if signature = Hex2Asc("424D") then
         FoundImgType = %isimage_bmp
         return
      end if
    
      ' Test the four-byte sigs
      sigStart = 0
      sigLen = 4
      gosub get_signature
      if signature = Hex2Asc("00000100") then
         FoundImgType = %isimage_ico
         return
      end if
      if signature = Hex2Asc("49492A00") _
      or signature = Hex2Asc("4D4D002A") _
      or signature = Hex2Asc("4D4D002B") then
         FoundImgType = %isimage_TIFF
         return
      end if
      if signature = Hex2Asc("FFD8FFDB") _
      or signature = Hex2Asc("FFD8FFEE") _
      or signature = Hex2Asc("FFD8FFE0") _
      or signature = Hex2Asc("FFD8FFE1") then
         FoundImgType = %isimage_jpg
         return
      end if
    
      ' Test the five-byte sigs
      sigStart = 0
      sigLen = 5
      gosub get_signature
      if signature = Hex2Asc("255044462D") then
         FoundImgType = %isimage_pdf
         return
      end if
    
      ' Test the six-byte sigs
      sigStart = 0
      sigLen = 6
      gosub get_signature
      if signature = Hex2Asc("474946383761") then
         FoundImgType = %isimage_Gif87a
         return
      end if
      if signature = Hex2Asc("474946383961") then
         FoundImgType = %isimage_Gif89a
         return
      end if
    
      ' Test the eight-byte sigs
      sigStart = 0
      sigLen = 8
      gosub get_signature
      if signature = Hex2Asc("89504E470D0A1A0A") then
         FoundImgType = %isimage_png
         return
      end if
      return
    
    '------------------------------------------------------------------
    program_exit:
      close #ifile
      if FoundImgType > 0 then
         if not isMissing(itype) then
            if itype <> FoundImgType then
               FoundImgType = %isimage_error_NotExpectedType
            end if
         end if
      end if
      function = FoundImgType
      exit function
    
    END FUNCTION
    
    '******************************************************************
    '******************************************************************

    Test program. Requires a filename on the command line.
    Code:
    #COMPILE EXE
    #stack 4194304
    #DIM ALL
    '#TOOLS ON
    '******************************************************************
    '*    File: test_isimg.bas
    '*   Descr: Test the image validation routine
    '*
    '* Copyright 2020 III Software, LLC
    '*   All rights reserved worldwide.
    '******************************************************************
    '*     Program Author: Raymond W. Leech, III
    '*            Project: webx_lowlevel / webx_test
    '*             Client: ALL
    '*================================================================*
    '*               Revision History
    '*================================================================*
    '* Ver  Date       Who Comment
    '* ---- ---------- --- -----------------------------------------------
    '* 1    06-04-2020 RWL Production (iii.313)
    '*
    '******************************************************************
    '*
    '* License: MIT License
    '* Permission is hereby granted, free of charge, to any person obtaining a copy
    '* of this software and associated documentation files (the "Software"), to deal
    '* in the Software without restriction, including without limitation the rights
    '* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    '* copies of the Software, and to permit persons to whom the Software is
    '* furnished to do so, subject to the following conditions:
    '*
    '* The above copyright notice and this permission notice shall be included in all
    '* copies or substantial portions of the Software.
    '*
    '* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    '* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    '* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    '* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    '* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    '* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    '* SOFTWARE.
    '******************************************************************
    ' Constants and declares
    #include "isImg.const.inc"
    #include "isImg.bas"
    #include "isImg_ErrorText.bas"
    #include "hex2asc.bas"
    
    '******************************************************************
    SUB test_isimg ALIAS "test_isimg" () EXPORT
    
      ' local storage
      local i as long
      local ierr as long
    
      ' Command line parms
      local ws_command_line AS STRING
      DIM ws_command_parm(1 TO 10) AS local STRING
      local ws_command_parm_count AS LONG
      local ws_filename as string
    
    '-------------------------------------------------------------------------------
    program_begin:
    
       ' Initialize
       ierr = 0 ' No error
    
       ' Get token from command line
       ws_command_line = COMMAND$
       ws_command_parm_count = PARSECOUNT(ws_command_line, ANY " ")
       PARSE ws_command_line, ws_command_parm(), ANY " "
       ws_filename = ws_command_parm(1)
       if len(ws_filename) = 0 then
          ?"Did not provide an image file to test on the command line"
          goto program_exit
       end if
    
       ' Test1 -- Just find out the type of the file based on extension
       ierr = isimg(ws_filename, 1)
       ?" == Test File:"+ws_filename+" =="
       ?"Return Code="+trim$(ierr)+": "+isimg_ErrorText(ierr)
    
       ' Test2 -- Find out the type of the file based on extension and test against the type you expect (use constants)
       ierr = isimg(ws_filename, 1, %isImage_pdf)
       ?" == Test File:"+ws_filename+" as PDF =="
       ?"Return Code="+trim$(ierr)+": "+isimg_ErrorText(ierr)
    
       ' Done
       goto program_exit
    
    '-------------------------------------------------------------------------------
    program_exit:
       EXIT SUB
    
    END SUB
    
    '******************************************************************
    FUNCTION PBMAIN()
        CALL test_isimg()
    END FUNCTION
    '******************************************************************
    '******************************************************************

    Comment

    Working...
    X