コード例 #1
0
ファイル: Syscalls.cs プロジェクト: meniz/WazeWP7
    //    public static int NOPH_ZLib_compress(int i_in_path, int i_in_file_name, int i_out_path, int i_out_file_name, int compression_level)
    //{
    // String in_path = CRunTime.charPtrToString(i_in_path);
    // String in_file_name = CRunTime.charPtrToString(i_in_file_name);
    // String out_path = CRunTime.charPtrToString(i_out_path);
    // String out_file_name = CRunTime.charPtrToString(i_out_file_name);
    // net.rim.device.api.compress.GZIPOutputStream zout = null;
    // javax.microedition.io.file.FileConnection fConnIn = null;
    // javax.microedition.io.file.FileConnection fConnOut = null;
    // Stream os = null;
    // Stream iis = null;
    // try {
    //       long in_file_size;
    //       fConnIn = (javax.microedition.io.file.FileConnection)Connector.open(in_path + "/" + in_file_name);
    //    fConnOut = (javax.microedition.io.file.FileConnection)Connector.open(out_path + "/" + out_file_name);
    //    if (!fConnOut.exists())
    //     fConnOut.create(); // create the file if it doesn't exist
    //    iis = fConnIn.openInputStream();
    //    in_file_size = fConnIn.fileSize();
    //    os = fConnOut.openOutputStream();
    //    zout = new net.rim.device.api.compress.GZIPOutputStream(os, compression_level, net.rim.device.api.compress.GZIPOutputStream.MAX_LOG2_WINDOW_LENGTH);
    //    // write into file
    //    for (long i = 0; i < in_file_size; i++){
    //     zout.write(iis.read());
    //    }
    //    // flush before end
    //    zout.flush();
    //    // success
    //    return 0;
    // }
    // catch (Exception e) {
    //  UIWorker.addUIEventLog("NOPH_ZLib_compress Exception in compressing "+ e.toString());
    // }finally {
    //    // try to close stream
    //    try{
    //    if(zout!=null)
    //          zout.close();
    //       if(fConnIn!=null)
    //       fConnIn.close();
    //       if(fConnOut!=null)
    //       fConnOut.close();
    //       if(os!=null)
    //       os.close();
    //       if(iis!=null)
    //       iis.close();
    //  }catch(Exception e){
    //      Console.WriteLine("zout.close()");
    //  }
    // }
    // return 1;
    //}
    public static int NOPH_ZLib_uncompress(int uncompressedAddr, int uncompressedSizeAddr, int compressedAddr, int compressedSize)
    {
        //todomt - do better job
        int uncompressedSizeRead = 0;
        FileStream fs = null;
        try
        {
        /*            var store = IsolatedStorageFile.GetUserStoreForApplication();
            CRunTimeMemoryInputStream mis = new CRunTimeMemoryInputStream(compressedAddr, compressedSize);
            fs = new IsolatedStorageFileStream("temp.gz", FileMode.Create, FileAccess.Write, store);
            int b = mis.read();
            while (b != -1)
            {
                fs.WriteByte((byte)b);
                b = mis.read();
            }

            fs.Close();

            fs = new IsolatedStorageFileStream("temp.gz", FileMode.Open, FileAccess.Read, store);
            ZInputStream zis = new ZInputStream(fs);*/
            CRunTimeMemoryInputStream mis = new CRunTimeMemoryInputStream(compressedAddr, compressedSize);
            ZInputStream zis = new ZInputStream(mis);

            int uncompressedSizeAllocated = CRunTime.memoryReadWord(uncompressedSizeAddr);

            int val = 0;
            while (((uncompressedAddr & 0x3) != 0) && (uncompressedSizeAllocated > 0) && ((val = zis.ReadByte()) != -1))
            {
                CRunTime.memoryWriteByte(uncompressedAddr++, val);
                ++uncompressedSizeRead;
                --uncompressedSizeAllocated;
            }

            while ((val != -1) && (uncompressedSizeAllocated > 3))
            {
                int i = 0;
                for (int j = 0; j < 4; j++)
                {
                    i = i << 8;

                    // replace the below with val = zis.ReadByte() & 0xff; if changing zlib implementation
                    val = zis.Read() & 0xff;

                    i |= val;
                }

                CRunTime.memoryWriteWord(uncompressedAddr, i);
                uncompressedAddr += 4;
                uncompressedSizeRead += 4;
                uncompressedSizeAllocated -= 4;
            }

            while ((uncompressedSizeAllocated > 0) && (val = zis.ReadByte()) != -1)
            {
                CRunTime.memoryWriteByte(uncompressedAddr++, val);
                ++uncompressedSizeRead;
                --uncompressedSizeAllocated;
            }

            CRunTime.memoryWriteWord(uncompressedSizeAddr, uncompressedSizeRead);

            return 1;
        }
        catch (Exception ex)
        {
            Logger.log("Exception: " + ex.ToString());
            Logger.log("compressedSize " + compressedSize + " uncompressedSizeRead " + uncompressedSizeRead + " Exception: " + ex);

            return 0;
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
        }
    }