Skip to content

Commit

Permalink
CELS: implemented "storing"
Browse files Browse the repository at this point in the history
git-svn-id: https://freearc.svn.sourceforge.net/svnroot/freearc@452 3a4f7f31-9599-433d-91b1-573e8b61252c
  • Loading branch information
bulatz committed Jul 25, 2009
1 parent af76bc0 commit c9d3d6a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 20 deletions.
9 changes: 5 additions & 4 deletions ArcvProcessExtract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foreign.Ptr
import Foreign.Marshal.Utils
import Foreign.Storable

import TABI hiding (doNothing)
import TABI
import Utils
import Errors
import Process
Expand Down Expand Up @@ -159,7 +159,8 @@ de_compress_PROCESS1 de_compress reader times command limit_memory comprMethod n
testMalloc
#ifdef FREEARC_CELS
let callback p = do
service <- TABI.required p ""
TABI.dump p
service <- TABI.required p "request"
case service of
-- Ïðîöåäóðà ÷òåíèÿ âõîäíûõ äàííûõ ïðîöåññà óïàêîâêè/ðàñïàêîâêè
"read" -> do buf <- TABI.required p "buf"
Expand Down Expand Up @@ -190,7 +191,7 @@ de_compress_PROCESS1 de_compress reader times command limit_memory comprMethod n
else callback p
-- Non-debugging wrapper
debug f = f
debug_checked_callback what buf size = undefined --TABI.call checked_callback [Pair "" what, Pair "buf" size, Pair "buf" size]
debug_checked_callback what buf size = TABI.call (\a->fromIntegral `fmap` checked_callback a) [Pair "request" what, Pair "buf" buf, Pair "size" size]
#else
let -- Ïðîöåäóðà ÷òåíèÿ âõîäíûõ äàííûõ ïðîöåññà óïàêîâêè/ðàñïàêîâêè
callback "read" buf size = do res <- reader buf size
Expand Down Expand Up @@ -229,7 +230,7 @@ de_compress_PROCESS1 de_compress reader times command limit_memory comprMethod n
#endif

-- ÑÎÁÑÒÂÅÍÍÎ ÓÏÀÊÎÂÊÀ ÈËÈ ÐÀÑÏÀÊÎÂÊÀ
res <- debug_checked_callback "read" nullPtr 0 -- ýòîò âûçîâ ïîçâîëÿåò îòëîæèòü çàïóñê ñëåäóþùåãî â öåïî÷êå àëãîðèòìà óïàêîâêè/ðàñïàêîâêè äî ìîìåíòà, êîãäà ïðåäûäóùèé âîçâðàòèò õîòü êàêèå-íèáóäü äàííûå (à åñëè ýòî ïîáëî÷íûé àëãîðèòì - äî ìîìåíòà, êîãäà îí îáðàáîòàåò âåñü áëîê)
res <- debug_checked_callback "read" nullPtr (0::Int) -- ýòîò âûçîâ ïîçâîëÿåò îòëîæèòü çàïóñê ñëåäóþùåãî â öåïî÷êå àëãîðèòìà óïàêîâêè/ðàñïàêîâêè äî ìîìåíòà, êîãäà ïðåäûäóùèé âîçâðàòèò õîòü êàêèå-íèáóäü äàííûå (à åñëè ýòî ïîáëî÷íûé àëãîðèòì - äî ìîìåíòà, êîãäà îí îáðàáîòàåò âåñü áëîê)
opt_testMalloc command &&& showMemoryMap -- íàïå÷àòàåì êàðòó ïàìÿòè íåïîñðåäñòâåííî ïåðåä íà÷àëîì ñæàòèÿ
real_method <- limit_memory num comprMethod -- îáðåæåì ìåòîä ñæàòèÿ ïðè íåõâàòêå ïàìÿòè
result <- if res<0 then return res
Expand Down
97 changes: 88 additions & 9 deletions Compression/CELS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
namespace CELS
{

// ***********************************************************************************************************************
// Ðåàëèçàöèÿ êëàññà COMPRESSION_METHOD *
// ***********************************************************************************************************************

int COMPRESSION_METHOD::server()
{
char *service = p._str("service");

if (strequ (service, "encryption?")) return 1; // to do: ---

// Global services
if (strequ (service, "register")) return FREEARC_OK; //to do: Register();

Expand All @@ -31,8 +33,68 @@ int COMPRESSION_METHOD::server()
return FREEARC_ERRCODE_NOT_IMPLEMENTED;
}


// ****************************************************************************************************************************
// ÌÅÒÎÄ "ÑÆÀÒÈß" STORING *****************************************************************************************************
// ****************************************************************************************************************************

// Ôóíêöèÿ "(ðàñ)ïàêîâêè", êîïèðóþùàÿ äàííûå îäèí â îäèí
int copy_data (CALLBACK_FUNC *callback, void *auxdata)
{
char buf[BUFFER_SIZE]; int len, errcode;
for(;;)
{
READ_LEN_OR_EOF(len, buf, BUFFER_SIZE);
WRITE(buf, len);
}
finished:
return errcode;
}

// Ðåàëèçàöèÿ ìåòîäà "ñæàòèÿ" STORING
struct STORING_METHOD : COMPRESSION_METHOD
{
// Êîíñòðóêòîð, ïðèñâàèâàþùèé ïàðàìåòðàì ìåòîäà çíà÷åíèÿ ïî óìîë÷àíèþ
STORING_METHOD (TABI_ELEMENT* params) : COMPRESSION_METHOD(params) {}
// Ôóíêöèè ðàñïàêîâêè è óïàêîâêè
virtual int decompress (CALLBACK_FUNC *callback, void *auxdata) {return copy_data (callback, auxdata);}
#ifndef FREEARC_DECOMPRESS_ONLY
virtual int compress (CALLBACK_FUNC *callback, void *auxdata) {return copy_data (callback, auxdata);}

// Ðàçáèðàåò ñòðîêó ñ ïàðàìåòðàìè ìåòîäà
virtual void parse_method()
{
if (!strequ (p._str("method"), "storing"))
throw "STORING_METHOD:parse_method";
}

// Çàïèñàòü â buf[MAX_METHOD_STRLEN] ñòðîêó, îïèñûâàþùóþ ìåòîä ñæàòèÿ (ôóíêöèÿ, îáðàòíàÿ ê parse_method)
virtual void ShowCompressionMethod (char *buf) {sprintf (buf, "storing");}

// Ïîëó÷èòü/óñòàíîâèòü îáú¸ì ïàìÿòè, èñïîëüçóåìîé ïðè óïàêîâêå/ðàñïàêîâêå, ðàçìåð ñëîâàðÿ èëè ðàçìåð áëîêà
virtual MemSize GetCompressionMem (void) {return BUFFER_SIZE;}
virtual MemSize GetDictionary (void) {return 0;}
virtual MemSize GetBlockSize (void) {return 0;}
virtual void SetCompressionMem (MemSize) {}
virtual void SetDecompressionMem (MemSize) {}
virtual void SetDictionary (MemSize) {}
virtual void SetBlockSize (MemSize) {}
#endif
virtual MemSize GetDecompressionMem (void) {return BUFFER_SIZE;}
};

// Function that represents STORING compression method
int storing_server (TABI_ELEMENT* params)
{
return STORING_METHOD(params).server();
}

// Register STORING method in CELS
int storing_register = CELS_Register(storing_server);

} // namespace CELS


// ****************************************************************************************************************************
// ÏÎÄÄÅÐÆÊÀ ÒÀÁËÈÖÛ ÇÀÐÅÃÈÑÒÐÈÐÎÂÀÍÍÛÕ ÌÅÒÎÄΠÑÆÀÒÈß È ÏÎÈÑÊ Â ÝÒÎÉ ÒÀÁËÈÖÅ ÐÅÀËÈÇÀÖÈÈ ×ÈÑÒÎ ÊÎÍÊÐÅÒÍÎÃÎ ÌÅÒÎÄÀ *************
// ****************************************************************************************************************************
Expand All @@ -55,19 +117,36 @@ int CELS_Register (TABI_FUNCTION *method)
int CELS_Call (TABI_ELEMENT* params)
{
TABI_MAP p(params); p.dump();
char *service = p._str("service");

// ==== AUTO-SERVICE ======================================
// Ignore zero parameter to some Set* services
if (strequ (service, "SetCompressionMem") || strequ (service, "SetDecompressionMem") || strequ (service, "SetDictionary") || strequ (service, "SetBlockSize"))
if (p._int("mem",1)==0)
return p._return(p._str("method"));
if (start_with (service, "Limit")) return p._return(p._str("method")); // to do: get & set
if (strequ (service, "encryption?")) return 1; // to do: aes-specific
if (start_with (p._str("method",""), "aes")) return p._return(p._str("method")); // to do: aes-specific
//GetBlockSize {return 0;}
//SetBlockSize {}
//Limit get & set
// ========================================================

// Find appropriate method to service this call
for (int i=0; i<methodsCount; i++)
{
int x = methodsTable[i](params);
// some auto-actions
//SetDecompressionMem {if (mem>0) ...;}
//SetDictionary {if (dict>0) ...;}
//GetBlockSize {return 0;}
//SetBlockSize {}
//Limit get & set

if (x!=FREEARC_ERRCODE_NOT_IMPLEMENTED)
return x;
}
return FREEARC_ERRCODE_NOT_IMPLEMENTED;
}

// Temporary
extern "C" {
void tabi_dump(TABI_ELEMENT *params, int n=0)
{
TABI_MAP(params).dump(n);
}
}

14 changes: 7 additions & 7 deletions Compression/CELS.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ int CELS_Call (TABI_ELEMENT* params);
// ****************************************************************************************************************************

// Auxiliary code to read/write data blocks and 4-byte headers
#define INIT() callback (TABI_DYNAMAP("init"))
#define DONE() callback (TABI_DYNAMAP("done"))
#define INIT() callback (TABI_DYNAMAP("request","init"))
#define DONE() callback (TABI_DYNAMAP("request","done"))

#define MALLOC(type, ptr, size) \
{ \
Expand All @@ -145,22 +145,22 @@ int CELS_Call (TABI_ELEMENT* params);
{ \
void *localBuf = (buf); \
int localSize = (size); \
if (localSize && (errcode=callback(TABI_DYNAMAP("read") ("buf",localBuf) ("size",localSize))) != localSize) { \
if (localSize && (errcode=callback(TABI_DYNAMAP("request","read") ("buf",localBuf) ("size",localSize))) != localSize) { \
if (errcode>=0) errcode=FREEARC_ERRCODE_IO; \
goto finished; \
} \
}

#define READ_LEN(len, buf, size) \
{ \
if ((errcode=(len)=callback(TABI_DYNAMAP("read") ("buf",buf) ("size",size))) < 0) { \
if ((errcode=(len)=callback(TABI_DYNAMAP("request","read") ("buf",(void*)(buf)) ("size",size))) < 0) { \
goto finished; \
} \
}

#define READ_LEN_OR_EOF(len, buf, size) \
{ \
if ((errcode=(len)=callback(TABI_DYNAMAP("read") ("buf",buf) ("size",size))) <= 0) { \
if ((errcode=(len)=callback(TABI_DYNAMAP("request","read") ("buf",(void*)(buf)) ("size",size))) <= 0) { \
goto finished; \
} \
}
Expand All @@ -170,7 +170,7 @@ int CELS_Call (TABI_ELEMENT* params);
void *localBuf = (buf); \
int localSize = (size); \
/* "write" callback on success guarantees to write all the data and may return 0 */ \
if (localSize && (errcode=callback(TABI_DYNAMAP("write") ("buf",localBuf) ("size",localSize)))<0) \
if (localSize && (errcode=callback(TABI_DYNAMAP("request","write") ("buf",localBuf) ("size",localSize)))<0) \
goto finished; \
}

Expand Down Expand Up @@ -199,7 +199,7 @@ int CELS_Call (TABI_ELEMENT* params);

#define QUASIWRITE(size) \
{ \
callback(TABI_DYNAMAP("quasiwrite") ("size",size)); \
callback(TABI_DYNAMAP("request","quasiwrite") ("size",size)); \
}

#define ReturnErrorCode(x) \
Expand Down

0 comments on commit c9d3d6a

Please sign in to comment.