Commit f42338922df03bdf16894ca2d43099f4a0dba1fd

Authored by Cédric RICARD
1 parent 45641a15

- Fix extension library handling.

- Working 1st extension for Anubis : base64 streamed (still in beta phase)
anubis_dev/compiler/src/main.cpp
... ... @@ -605,9 +605,9 @@ void read_configuration_file()
605 605 /* main function (Anubis compiler) */
606 606 int main(int argc, char **argv)
607 607 {
608   -// if(sizeof(int) != 4)
  608 + if(sizeof(long) != 4)
609 609 {
610   - printf("WARNING: 'int' have a size of %d bytes!\n", sizeof(int));
  610 + printf("WARNING: 'long' have a size of %d bytes!\n", sizeof(long));
611 611 }
612 612  
613 613 /* check if we don't have too many virtual machine instructions */
... ...
anubis_dev/extensions/cipher/base64.cpp
1   -#include "stdafx.h"
2 1 #include <stdio.h>
3 2  
4 3 /* ENCODE -- Encode binary file into base64. */
... ... @@ -91,6 +90,7 @@ int b64_StreamEncode (struct buffer_st *b, const unsigned char *source, size_t l
91 90 {
92 91 encodeblock(source, ptr);
93 92 source += 3;
  93 + src_offset += 3;
94 94 }
95 95 ptr += 4;
96 96 dst_offset += 4;
... ... @@ -105,10 +105,10 @@ int b64_StreamEncode (struct buffer_st *b, const unsigned char *source, size_t l
105 105 {
106 106 igroup[inPos] = (unsigned char) *source++;
107 107 src_offset++;
  108 + len++;
108 109 }
109 110 else
110 111 igroup[inPos] = 0;
111   - len++;
112 112 }
113 113 encodeblock(igroup, ptr);
114 114 if (len < 3)
... ... @@ -210,14 +210,14 @@ int b64_StreamDecode (struct buffer_st *b, const unsigned char *source, size_t l
210 210 if(len == 4 || finalize)
211 211 {
212 212 decodeblock(igroup, ptr);
213   - ptr += len;
214   - dst_offset += len;
  213 + ptr += len - 1;
  214 + dst_offset += len - 1;
215 215 }
216 216 else
217 217 {
218 218 for(int i = 0; i < (int)len; i++)
219 219 b->input[i] = igroup[i];
220   - b->input_length = len;
  220 + b->input_length = len - 1;
221 221 }
222 222 }
223 223 inPos = 0;
... ... @@ -256,26 +256,27 @@ void b64_del_buffer(void * handle)
256 256 }
257 257  
258 258 /* parameters are:
  259 + - 1 dummy octet inserted by serialization of mixed type (Opaque, Bool, ByteArray)
259 260 - Opaque(struct buffer_st *) (12 bytes)
260 261 - Boolean : finalize flag (1 byte)
261 262 - ByteArray : input data (4 + n bytes)
262 263 */
263 264 int b64_encode(const void * input, size_t in_size, void * output, size_t * out_size)
264 265 {
265   - if(in_size < 12 + 1 + 4)
  266 + if(in_size < 1 + 12 + 1 + 4)
266 267 {
267 268 fprintf(stderr, "b64_decode: Wrong input parameters\n");
268 269 return -1;
269 270 }
270 271  
271   - const unsigned char* args = (const unsigned char*)input;
  272 + const unsigned char* args = (const unsigned char*)input + 1;
272 273 if(*(int*)(args+4) != 4)
273 274 {
274 275 fprintf(stderr, "b64_decode: Incoherent input data. Call aborted.\n");
275 276 return -1;
276 277 }
277 278  
278   - struct buffer_st * b = (struct buffer_st *)(args + 8);
  279 + struct buffer_st * b = *(struct buffer_st **)(args + 8);
279 280 bool finalize = *(args + 12) != 0;
280 281 size_t input_size = *(const size_t*)(args + 13);
281 282 const unsigned char * input_buffer = args + 13 + 4;
... ... @@ -298,20 +299,20 @@ int b64_encode(const void * input, size_t in_size, void * output, size_t * out_s
298 299 */
299 300 int b64_decode(const void * input, size_t in_size, void * output, size_t * out_size)
300 301 {
301   - if(in_size < 12 + 1 + 4)
  302 + if(in_size < 1+12 + 1 + 4)
302 303 {
303 304 fprintf(stderr, "b64_decode: Wrong input parameters\n");
304 305 return -1;
305 306 }
306 307  
307   - const unsigned char* args = (const unsigned char*)input;
  308 + const unsigned char* args = (const unsigned char*)input + 1;
308 309 if(*(int*)(args+4) != 4)
309 310 {
310 311 fprintf(stderr, "b64_decode: Incoherent input data. Call aborted.\n");
311 312 return -1;
312 313 }
313 314  
314   - struct buffer_st * b = (struct buffer_st *)(args + 8);
  315 + struct buffer_st * b = *(struct buffer_st **)(args + 8);
315 316 bool finalize = *(args + 12) != 0;
316 317 size_t input_size = *(const size_t*)(args + 13);
317 318 const unsigned char * input_buffer = args + 13 + 4;
... ...
anubis_dev/extensions/cipher/base64.h
... ... @@ -13,9 +13,9 @@ void buffer_delete (struct buffer_st *b);
13 13 int b64_StreamEncode (struct buffer_st *b, const unsigned char *source, size_t length, unsigned char *target, size_t * target_size, bool finalize);
14 14 int b64_StreamDecode (struct buffer_st *b, const unsigned char *source, size_t length, unsigned char *target, size_t * target_size, bool finalize);
15 15  
16   -int CIPHER_API b64_init(void * input, size_t in_size, void * output, size_t * out_size);
17   -void CIPHER_API b64_del_buffer(void * handle);
18   -int CIPHER_API b64_encode(const void * input, size_t in_size, void * output, size_t * out_size);
19   -int CIPHER_API b64_decode(const void * input, size_t in_size, void * output, size_t * out_size);
  16 +extern "C" int CIPHER_API b64_init(void * input, size_t in_size, void * output, size_t * out_size);
  17 +extern "C" void CIPHER_API b64_del_buffer(void * handle);
  18 +extern "C" int CIPHER_API b64_encode(const void * input, size_t in_size, void * output, size_t * out_size);
  19 +extern "C" int CIPHER_API b64_decode(const void * input, size_t in_size, void * output, size_t * out_size);
20 20  
21 21 #endif
22 22 \ No newline at end of file
... ...
anubis_dev/library/cipher/base64.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: ricard
  4 + * Date: 04/01/2009
  5 + * Time: 21:13
  6 + *
  7 + */
  8 +
  9 +read tools/basis.anubis
  10 +
  11 +public type Base64:
  12 + base64(Library library,
  13 + Opaque(Base64) handle).
  14 +
  15 +
  16 +public define Result(LibraryError, Base64)
  17 + base64_prepare
  18 + =
  19 + if load_library("cipher.dll") is
  20 + {
  21 + error(err) then error(cant_load_library),
  22 + ok(lib) then
  23 + if extension_library_call(lib, "b64_init", constant_byte_array(0,0), 12) is
  24 + {
  25 + error(err) then error(err),
  26 + ok(ba) then
  27 + if (Maybe(Opaque(Base64)))unserialize(ba) is
  28 + {
  29 + failure then error(unknown_error),
  30 + success(handle) then ok(base64(lib, handle))
  31 + }
  32 + }
  33 + }.
  34 +
  35 +define String
  36 + lib_error_to_string
  37 + (
  38 + LibraryError err
  39 + )=
  40 + if err is
  41 + {
  42 + cant_load_library then "Can't load library.",
  43 + symbol_not_found then "Symbol not found.",
  44 + external_call_failed then "External call failed.",
  45 + unknown_error then "Unknown error."
  46 + }.
  47 +
  48 +
  49 +public define String
  50 + base64_encode
  51 + (
  52 + Base64 b64,
  53 + ByteArray input,
  54 + Bool finalize
  55 + ) =
  56 + if b64 is base64(lib, handle) then
  57 + if extension_library_call(lib, "b64_encode", serialize((handle, finalize, input)), length(input) \ 3 * 4 + 4 + 1) is
  58 + {
  59 + error(err) then
  60 + println("Can't encode data to base64");
  61 + println(" --> " + lib_error_to_string(err));
  62 + constant_string(0,0),
  63 + ok(ba) then to_string(ba)
  64 + }.
  65 +
  66 +public define ByteArray
  67 + base64_decode
  68 + (
  69 + Base64 b64,
  70 + String input,
  71 + Bool finalize
  72 + ) =
  73 + if b64 is base64(lib, handle) then
  74 + if extension_library_call(lib, "b64_decode", serialize((handle, finalize, to_byte_array(input))), length(input) \ 4 * 3 + 3) is
  75 + {
  76 + error(err) then
  77 + println("Can't decode data from base64");
  78 + println(" --> " + lib_error_to_string(err));
  79 + constant_byte_array(0,0),
  80 + ok(ba) then ba
  81 + }.
  82 +
  83 +
  84 +global define One
  85 + test_quick_base64
  86 + (
  87 + List(String) args
  88 + ) =
  89 + with mb_lib = load_library("cipher.dll"),
  90 + if base64_prepare is
  91 + {
  92 + error(err) then
  93 + println("Can't prepare base64");
  94 + println(" --> " + lib_error_to_string(err)),
  95 + ok(b64) then
  96 + with src = "Test of Base64",
  97 + encoded = base64_encode(b64, to_byte_array(src), true),
  98 + decoded = base64_decode(b64, encoded, true),
  99 + result = if to_string(decoded) = src then "OK" else "ERROR",
  100 + println(result + "\n source = " + src + "\n encoded = " + encoded + "\n decoded = " + to_string(decoded))
  101 + }.
  102 +
... ...
anubis_dev/library/predefined.anubis
... ... @@ -4533,9 +4533,10 @@ public define MakeFastLexerResult
4533 4533 public type Library:... an opaque type
4534 4534  
4535 4535 public type LibraryError:
  4536 + cant_load_library,
4536 4537 symbol_not_found,
4537 4538 external_call_failed,
4538   - unknown_errro.
  4539 + unknown_error.
4539 4540  
4540 4541 public define Result((Word32, String), Library)
4541 4542 load_library
... ...
anubis_dev/library/test/Anubis UnitTest.aproj
... ... @@ -33,6 +33,7 @@
33 33 <ItemGroup>
34 34 <Compile Include="all_unit_test.anubis" />
35 35 <Compile Include="lexical_analysis\fast_lexer.ut.anubis" />
  36 + <Compile Include="predefined\ByteArray.ut.anubis" />
36 37 <Compile Include="predefined\date_and_time.unit_test.anubis" />
37 38 <Compile Include="predefined\decimal_scan.unit_test.anubis" />
38 39 <Compile Include="predefined\fast_lexer.unit_test.anubis" />
... ...
anubis_dev/library/test/Anubis UnitTest.ide
1 1 
2 2 Microsoft Visual Studio Solution File, Format Version 9.00
3 3 # Visual Studio 2005
4   -# SharpDevelop 1.0.9.98
  4 +# SharpDevelop 1.0.10.102
5 5 Project("{B6712916-30DE-4a3e-A54C-2A79AC984AEE}") = "Anubis UnitTest", "Anubis UnitTest.aproj", "{B3F890FB-FBDA-46B4-92E8-9AF1EC349AD2}"
6 6 EndProject
7 7 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Library", "Library", "{1F556345-E955-43F3-9BAC-EA0091627D9C}"
... ... @@ -11,6 +11,11 @@ Project(&quot;{2150E333-8FDC-42A3-9474-1A3956D46DE8}&quot;) = &quot;Library&quot;, &quot;Library&quot;, &quot;{1F55
11 11 ..\compil_all_beta.anubis = ..\compil_all_beta.anubis
12 12 EndProjectSection
13 13 EndProject
  14 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "cipher", "cipher", "{FD164ED1-E39F-4527-994C-F372A2BC59F5}"
  15 + ProjectSection(SolutionItems) = postProject
  16 + ..\cipher\base64.anubis = ..\cipher\base64.anubis
  17 + EndProjectSection
  18 +EndProject
14 19 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{98405377-F929-4D76-AB66-6F0F4E98F0F9}"
15 20 ProjectSection(SolutionItems) = postProject
16 21 ..\examples\_99_bottles_of_beer.anubis = ..\examples\_99_bottles_of_beer.anubis
... ... @@ -269,6 +274,7 @@ Global
269 274 {0BAB369E-80F2-48CA-9F23-DEED6011B642} = {1F556345-E955-43F3-9BAC-EA0091627D9C}
270 275 {C6AD752D-2943-48FB-A3A2-2D1E3E4DEE2C} = {1F556345-E955-43F3-9BAC-EA0091627D9C}
271 276 {98405377-F929-4D76-AB66-6F0F4E98F0F9} = {1F556345-E955-43F3-9BAC-EA0091627D9C}
  277 + {FD164ED1-E39F-4527-994C-F372A2BC59F5} = {1F556345-E955-43F3-9BAC-EA0091627D9C}
272 278 {BDDBC8EA-A9E2-4D98-8D09-9EE285237CCF} = {98405377-F929-4D76-AB66-6F0F4E98F0F9}
273 279 {DF34519E-52E5-4068-9E2C-5F79BC3BBEB6} = {98405377-F929-4D76-AB66-6F0F4E98F0F9}
274 280 {73CFA6CA-4F09-4F16-821F-815471CE8CDA} = {98405377-F929-4D76-AB66-6F0F4E98F0F9}
... ...
anubis_dev/library/test/all_unit_test.anubis
... ... @@ -4,6 +4,7 @@ read tools/unit_test.anubis
4 4 read serialize_test.anubis
5 5 read test/predefined/decimal_scan.unit_test.anubis
6 6 read test/predefined/date_and_time.unit_test.anubis
  7 +read test/predefined/ByteArray.ut.anubis
7 8 read test/predefined/Int.unit_test.anubis
8 9 read test/predefined/sqlite.obsolete.unit_test.anubis
9 10 read test/predefined/sqlite.unit_test.anubis
... ... @@ -29,6 +30,7 @@ define List(UnitTestSuite)
29 30 make_convert_test_suite,
30 31 make_message_queue_test_suite,
31 32 make_wordXX_test_suite,
  33 + make_ByteArray_test_suite,
32 34 make_Int_test_suite,
33 35 make_decimal_scan_test_suite,
34 36 make_string_test_suite,
... ...
anubis_dev/library/test/predefined/ByteArray.ut.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: ricard
  4 + * Date: 05/01/2009
  5 + * Time: 11:33
  6 + *
  7 + */
  8 +
  9 +read tools/unit_test.anubis
  10 +
  11 +define One
  12 + string_to_BA
  13 + (
  14 + UnitTestContext ut
  15 + ) =
  16 + with s = "This is my string.",
  17 + ba = to_byte_array(s),
  18 + assertIsSame(ut, length(ba), length(s), abs_to_decimal, "Comparing length");
  19 + unique.
  20 +
  21 +
  22 +public define UnitTestSuite
  23 + make_ByteArray_test_suite
  24 + =
  25 + ut_suite("predefined.ByteArray",
  26 + [
  27 + ut_fixture("string_to_BA", string_to_BA),
  28 + ]).
... ...
anubis_dev/vm/src/Library.cpp
... ... @@ -95,7 +95,7 @@ Library::Load(const char* libname)
95 95 mLastError = iwDllErrorCode;
96 96 mLastErrorStr = iwDllErrorString(mLastError);
97 97  
98   - LOGERROR("Failed to load shared library '%s': %s", libname, mLastErrorStr.Cstr());
  98 + LOGERROR("Failed to load shared library '%s': %s\n", libname, mLastErrorStr.Cstr());
99 99 return mLastError;
100 100 }
101 101  
... ...
anubis_dev/vm/src/syscall.cpp
... ... @@ -11943,7 +11943,7 @@ fflush(stdout);
11943 11943 */
11944 11944 syscall_case(dl_find_symbol)
11945 11945 {
11946   - Library *lib = (Library *)(((U8 *)(*(MAM(m_SP)-1)))+4);
  11946 + Library *lib = (Library *)(((U32 *)(*(MAM(m_SP)-1)))[1]);
11947 11947 const char *symbol_name = ((const char *)(*(MAM(m_SP)-2)))+4;
11948 11948 vm_alloc1(MAM(m_R), 2);
11949 11949 void * symbol = lib->GetSymbol(symbol_name);
... ... @@ -11954,6 +11954,8 @@ fflush(stdout);
11954 11954 }
11955 11955 else
11956 11956 {
  11957 + LOGERROR("Symbol '%s' not found into library '%s': %d - %s\n", symbol_name, lib->GetName().Cstr(), lib->GetLastError(), lib->GetLastErrorMessage().Cstr());
  11958 +
11957 11959 vm_free1(MAM(m_R));
11958 11960 MAM(m_R) = 0; /* failure */
11959 11961 }
... ... @@ -11983,7 +11985,8 @@ fflush(stdout);
11983 11985 {
11984 11986 typedef int (*ext_fn_t)(void*, size_t, void*, size_t*);
11985 11987 ext_fn_t fnct = (ext_fn_t)(*(MAM(m_SP)-1));
11986   - U8* in_ba = (U8*)*(MAM(m_SP)-2), out_ba = NULL;
  11988 + U8* in_ba = (U8*)*(MAM(m_SP)-2);
  11989 + U32 out_ba = 0;
11987 11990 size_t in_size = *(((U32*)in_ba) + 1);
11988 11991 size_t out_size = 0;
11989 11992  
... ... @@ -11998,6 +12001,7 @@ fflush(stdout);
11998 12001 out_size = (size_t)Anubis_Int_to_C_int(*(MAM(m_SP)-3));
11999 12002 vm_alloc2(MAM(m_R), 2, out_ba, byte_size_to_word_size(4+4+out_size));
12000 12003 }
  12004 + *(U32*)(out_ba + 4) = out_size;
12001 12005  
12002 12006 status_t result = B_ERROR;
12003 12007 try {
... ... @@ -12005,7 +12009,7 @@ fflush(stdout);
12005 12009 }
12006 12010 catch(...)
12007 12011 {
12008   - LOGERROR("Exception catched from Extension Library." );
  12012 + LOGERROR("Exception catched from Extension Library.\n" );
12009 12013 result = B_ERROR;
12010 12014 }
12011 12015 switch(result)
... ... @@ -12014,17 +12018,24 @@ fflush(stdout);
12014 12018 assert(*(U32*)(out_ba + 4) >= out_size);
12015 12019 if(*(U32*)(out_ba + 4) < out_size)
12016 12020 {
12017   - LOGERROR("Extension library call: incoherent output size (buffer size = %d; output size = %d)", *(U32*)(out_ba + 4), out_size);
12018   - LOGERROR("Possibly corrupted memory. Output ByteArray will be truncated for security.");
  12021 + LOGERROR("Extension library call: incoherent output size (buffer size = %d; output size = %d)\n", *(U32*)(out_ba + 4), out_size);
  12022 + LOGERROR("Possibly corrupted memory. Output ByteArray will be truncated for security.\n");
  12023 + vm_free2(MAM(m_R), out_ba);
  12024 + MAM(m_duc_non_empty) = 0;
  12025 + MAM(m_R) = 0;
12019 12026 }
12020 12027 else
  12028 + {
12021 12029 *(U32*)(out_ba + 4) = out_size;
  12030 + ((U32 *)(MAM(m_R)))[1] = out_ba;
  12031 + MAM(m_R) |= 1; // Select 'success' alternative
  12032 + }
12022 12033 break;
12023 12034 case NEED_MORE_MEMORY:
12024 12035 if(MAM(m_duc_non_empty))
12025 12036 {
12026   - LOGERROR("Extension library call: NEED_MORE_MEMORY already returned from previous call.");
12027   - LOGERROR(" Abording call to prevent infinite loop.");
  12037 + LOGERROR("Extension library call: NEED_MORE_MEMORY already returned from previous call.\n");
  12038 + LOGERROR(" Abording call to prevent infinite loop.\n");
12028 12039 vm_free2(MAM(m_R), out_ba);
12029 12040 MAM(m_duc_non_empty) = 0;
12030 12041 MAM(m_R) = 0;
... ... @@ -12037,10 +12048,11 @@ fflush(stdout);
12037 12048 MAM(m_duc_non_empty) = 1;
12038 12049 MAM(m_steps) = 0;
12039 12050 MAM(m_status) = need_more_memory;
  12051 + return;
12040 12052 }
12041 12053 break;
12042 12054 default: // all other errors
12043   - LOGERROR("Extension library call: ERROR %s returned by library.", result);
  12055 + LOGERROR("Extension library call: ERROR %d returned by library.\n", result);
12044 12056 vm_free2(MAM(m_R), out_ba);
12045 12057 MAM(m_duc_non_empty) = 0;
12046 12058 MAM(m_R) = 0;
... ...