MT Decode MIME J : Source

Name:MT Decode MIME J
Event Class:XUVJ
Event ID:djhd
Resource:AEVTXUVJdjhd

Source

--- This source is written in HyperTalk for CompileIt!

global theAEEvent:R,theReply:R, handerRefCon:LongInt
global tempPtr:Pointer, myStrHandle:Pointer, stOff:LongInt

codeResource "osax"

pascal function DecodeJHeader:I theAEEvent:R, theReply:R, handerRefCon:L

  put NewPtrClear(10) into myWorkPtr
  put myWorkPtr into myDescPtr
  put myWorkPtr + 8 into tempPtr

  put AEGetParamDesc(theAEEvent@,keyDirectObject,typeChar,myDescPtr@) into err

  if err <> 0 then
    DisposPtr myWorkPtr
    return err
  end if

  if myDescPtr@.descriptorType = typeNull then
    put AEDisposeDesc(myDescPtr@) into err
    DisposPtr myWorkPtr
    return -1708
  end if

  put myDescPtr@.dataHandle into myStrHandle
  put GetHandleSize(myStrHandle) into pSize

  if pSize < 20 then
    if theReply@.descriptorType <> typeNull then
      put AEPutParamDesc(theReply@, keyDirectObject,myDescPtr@) into err
    end if
    put AEDisposeDesc(myDescPtr@) into err
    DisposPtr myWorkPtr
    return 0
  end if

  put 0 into baseOff
  put -1 into stOff

  repeat while baseOff < (pSize-1)

    put CharToNum(myStrHandle@@.charType[baseOff]) into tgx

    if tgX = 61 and (baseOff <= (pSize-22)) then

      if CharToNum(myStrHandle@@.charType[baseOff+1]) = 63 ¬
      and (CharToNum(myStrHandle@@.charType[baseOff+2]) = 73 ¬
      or CharToNum(myStrHandle@@.charType[baseOff+2]) = 105) ¬
      and (CharToNum(myStrHandle@@.charType[baseOff+3]) = 83 ¬
      or CharToNum(myStrHandle@@.charType[baseOff+3]) = 115) ¬
      and (CharToNum(myStrHandle@@.charType[baseOff+4]) = 79 ¬
      or CharToNum(myStrHandle@@.charType[baseOff+4]) = 111) ¬
      and CharToNum(myStrHandle@@.charType[baseOff+5]) = 45 ¬
      and CharToNum(myStrHandle@@.charType[baseOff+6]) = 50 ¬
      and CharToNum(myStrHandle@@.charType[baseOff+7]) = 48 ¬
      and CharToNum(myStrHandle@@.charType[baseOff+8]) = 50 ¬
      and CharToNum(myStrHandle@@.charType[baseOff+9]) = 50 ¬
      and CharToNum(myStrHandle@@.charType[baseOff+10]) = 45 ¬
      and (CharToNum(myStrHandle@@.charType[baseOff+11]) = 74 ¬
      or CharToNum(myStrHandle@@.charType[baseOff+11]) = 106) ¬
      and (CharToNum(myStrHandle@@.charType[baseOff+12]) = 80 ¬
      or CharToNum(myStrHandle@@.charType[baseOff+12]) = 112) ¬
      and CharToNum(myStrHandle@@.charType[baseOff+13]) = 63 ¬
      and (CharToNum(myStrHandle@@.charType[baseOff+14]) = 66 ¬
      or CharToNum(myStrHandle@@.charType[baseOff+14]) = 98) ¬
      and CharToNum(myStrHandle@@.charType[baseOff+15]) = 63 then

        put baseOff into stOff
        add 16 to baseOff
        next repeat

      end if
    else if tgX = 63 and stOff <> -1 then

      if CharToNum(myStrHandle@@.charType[baseOff+1]) = 61 then

        put baseOff-stOff-16 into tgLen

        if (tgLen mod 4) = 0 then

          put DecodeBase64(stOff,tgLen) into difByte

          if difByte < 0 then
            put AEDisposeDesc(myDescPtr@) into err
            DisposPtr myWorkPtr
            return -50
          end if

          put pSize-difByte into pSize
          put baseOff-difByte into baseOff
        end if

        put -1 into stOff
        add 2 to baseOff

        next repeat
      end if

    end if

    add 1 to baseOff
  end repeat

  -----

  if theReply@.descriptorType <> typeNull then
    put AEPutParamDesc(theReply@, keyDirectObject,myDescPtr@) into err
  end if

  put AEDisposeDesc(myDescPtr@) into err
  DisposPtr myWorkPtr

  return 0

end DecodeJHeader


function  DecodeBase64 stOff,tgLen

  put (tgLen*6)/8 into tgSize
  put Munger(myStrHandle,stOff,nil,16,tempPtr,0) into stOff
  put myStrHandle@ + stOff into aPtr

  put tgLen-1 into myUnits

  repeat with x = 0 to myUnits

    put charToNum(aPtr@.charType[x]) into tgXX

    if tgXX = 61 then
      repeat with z = 0 to 5
        BitClr aPtr,(z+(x*6))
      end repeat
      exit repeat
    end if
    ---
    put decTable(tgXX) into tgy
    if tgy = 255 then
      return -1
    else
      put NumToChar(tgy) into tempPtr@.charType
    end if
    ---
    repeat with z = 0 to 5
      if Bittst(tempPtr,z+2) then
        BitSet aPtr,(z+(x*6))
      else
        BitClr aPtr,(z+(x*6))
      end if
    end repeat

  end repeat


  if CharToNum(myStrHandle@@.charType[stOff+tgLen-1]) = 61 then
    if CharToNum(myStrHandle@@.charType[stOff+tgLen-2]) = 61 then
      put tgSize-2 into tgSize
    else
      put tgSize-1 into tgSize
    end if
  end if

  put (tgLen-tgSize) into dXX
  put Munger(myStrHandle,(stOff+tgSize),nil,(dXX+2),tempPtr,0) into myResult

  return (dXX+18)

end DecodeBase64

function decTable tgX

  if tgx = 47 then
    return 63
  else if tgx = 43 then
    return 62
  else if (48 <= tgx and tgx <= 57) then
    return tgx+4
  else if (65 <= tgx and tgx <= 90) then
    return tgx-65
  else if (97 <= tgx and tgx <= 122) then
    return tgx-71
  else
    return 255
  end if

end decTable

Tanaka's osax : Source
Tanaka's osax