1. --**************************************
  2. -- requirements:
  3. -- * saving to/loading from file: fileIO xtra
  4. -- * encryption: crypto xtra
  5. -- * encoding #media: crypto xtra
  6. --
  7. --**************************************
  8.  
  9. ----------------------------------------
  10. -- ENCODE TO FILE
  11. -- usage:
  12. --   v = [1,2,"hellö<>#+~",["foo":"bar", #foo2:#bar2], member("img").image]
  13. --   saveDataToDisk( _movie.path&"test.dat", v, 1)
  14. ----------------------------------------
  15. on saveDataToDisk( tFileName, tValue, tCompressFlag, tEncryptKey )
  16.  
  17.   enc = script("BYTEARRAY_ENCODER")
  18.   ba = enc.mEncodeData(tValue)
  19.   if tCompressFlag then ba.compress()
  20.   if not voidP(tEncryptKey) then
  21.     aesKey = cx_md5_string(tEncryptKey, 1)
  22.     ba = cx_aes_encrypt_string(ba, aesKey)
  23.   end if
  24.   file_put_bytes(tFileName, ba)
  25.  
  26. end
  27.  
  28. ----------------------------------------
  29. -- DECODE FROM FILE
  30. -- usage:
  31. --   put loadDataFromDisk( _movie.path&"test.dat", 1)
  32. ----------------------------------------
  33. on loadDataFromDisk( tFileName, tCompressFlag, tEncryptKey )
  34.  
  35.   enc = script("BYTEARRAY_ENCODER")
  36.   ba = file_get_bytes(tFileName)
  37.   if not voidP(tEncryptKey) then
  38.     aesKey = cx_md5_string(tEncryptKey, 1)
  39.     ba = cx_aes_decrypt_string(ba, aesKey)
  40.   end if
  41.   if tCompressFlag then ba.uncompress()
  42.   return enc.mDecodeData(ba)
  43.  
  44. end
  45.  
  46. ----------------------------------------
  47. -- ENCODE TO MEMBER
  48. -- usage:
  49. --   v = [1,2,"hellö<>#+~",["foo":"bar", #foo2:#bar2], member("img").image]
  50. --   saveDataToMember( member(8), v, true, "foobar" )
  51. ----------------------------------------
  52. on saveDataToMember( tMemberRef, tValue, tCompressFlag, tEncryptKey )
  53.  
  54.   enc = script("BYTEARRAY_ENCODER")
  55.   ba = enc.mEncodeData(tValue)
  56.   if tCompressFlag then ba.compress()
  57.   if not voidP(tEncryptKey) then
  58.     aesKey = cx_md5_string(tEncryptKey, 1)
  59.     ba = cx_aes_encrypt_string(ba, aesKey)
  60.   end if
  61.   m = new(#bytearray, tMemberRef)
  62.   m.bytearray = ba
  63.  
  64. end
  65.  
  66. ----------------------------------------
  67. -- DECODE FROM MEMBER
  68. -- usage:
  69. --   put loadDataFromMember( member(8), true, "foobar" )
  70. ----------------------------------------
  71. on loadDataFromMember( tMemberRef, tCompressFlag, tEncryptKey )
  72.  
  73.   enc = script("BYTEARRAY_ENCODER")
  74.   if tMemberRef.type<>#bytearray then return
  75.   ba = tMemberRef.bytearray
  76.   if not voidP(tEncryptKey) then  
  77.     aesKey = cx_md5_string(tEncryptKey, 1)
  78.     ba = cx_aes_decrypt_string(ba, aesKey)
  79.   end if
  80.   if tCompressFlag then ba.uncompress()
  81.   return enc.mDecodeData(ba)
  82.  
  83. end
  84.  
  85. ----------------------------------------
  86. -- returns file as ByteArray
  87. ----------------------------------------
  88. on file_get_bytes (tFile)
  89.  
  90.   fp = xtra("fileIO").new()
  91.   if not objectP(fp) then return -1
  92.  
  93.   fp.openFile(tFile, 1)
  94.   err = fp.status()
  95.   if (err) then return err
  96.  
  97.   data = fp.readByteArray(fp.getLength())
  98.   fp.closeFile()
  99.   fp = 0
  100.  
  101.   return data
  102. end
  103.  
  104. ----------------------------------------
  105. -- saves ByteArray to file
  106. ----------------------------------------
  107. on file_put_bytes (tFile, tByteArray, tType, tCreator)
  108.  
  109.   fp = xtra("fileIO").new()
  110.   if not objectP(fp) then return -1
  111.  
  112.   fp.openFile(tFile, 1)
  113.   err = fp.status()
  114.   if not (err) then fp.delete()
  115.   else if (err and not (err = -37)) then return err
  116.  
  117.   fp.createFile(tFile)
  118.   err = fp.status()
  119.   if (err) then return err
  120.  
  121.   fp.openFile(tFile, 2)
  122.  
  123.   err = fp.status()
  124.   if (err) then return err
  125.  
  126.   ok = fp.writeByteArray(tByteArray)
  127.  
  128.   if stringP(tType) and stringP(tCreator) then fp.setFinderInfo(tType & tCreator)
  129.   fp.closeFile()
  130.   fp=0
  131.  
  132.   return ok
  133. end
[raw code]