Revision: 6057
Author: iratqq
Date: Thu Nov 12 05:26:02 2009
Log: * scm/lolevel.scm:
- New file.
* scm/Makefile.am (SCM_FILES):
- Add lolevel.scm.
* uim/lolevel.c (c_Xlist_to_pointer, c_htonl, c_ntohs, c_ntohl)
(c_u16_to_u8list, c_u32_to_u8list, c_string_to_u8list)
(c_u8list_to_u16, c_u8list_to_u32, c_u8list_to_string):
- New functions.
(uim_plugin_instance_init):
- Add functions.
* uim/Makefile.am (uim_plugin_LTLIBRARIES):
- Install lolevel module by default.
http://code.google.com/p/uim/source/detail?r=6057
Added:
/trunk/scm/lolevel.scm
Modified:
/trunk/scm/Makefile.am
/trunk/uim/Makefile.am
/trunk/uim/lolevel.c
=======================================
--- /dev/null
+++ /trunk/scm/lolevel.scm Thu Nov 12 05:26:02 2009
@@ -0,0 +1,91 @@
+;;; lolevel.scm: low level access utility
+;;;
+;;; Copyright (c) 2009- uim Project http://code.google.com/p/uim/
+;;;
+;;; All rights reserved.
+;;;
+;;; Redistribution and use in source and binary forms, with or without
+;;; modification, are permitted provided that the following conditions
+;;; are met:
+;;; 1. Redistributions of source code must retain the above copyright
+;;; notice, this list of conditions and the following disclaimer.
+;;; 2. Redistributions in binary form must reproduce the above copyright
+;;; notice, this list of conditions and the following disclaimer in the
+;;; documentation and/or other materials provided with the distribution.
+;;; 3. Neither the name of authors nor the names of its contributors
+;;; may be used to endorse or promote products derived from this
software
+;;; without specific prior written permission.
+;;;
+;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND
+;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE
+;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
+;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT
+;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY
+;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+;;; SUCH DAMAGE.
+;;;;
+
+(use srfi-1)
+(module-load "lolevel")
+
+(define (u8list-pack fmt . args)
+ (apply append
+ (map (lambda (f)
+ (cond ((eq? (car f) 'u8)
+ (list (cadr f)))
+ ((eq? (car f) 'u16)
+ (u16->u8list (cadr f)))
+ ((eq? (car f) 'u32)
+ (u32->u8list (cadr f)))
+ ((eq? (car f) 's8)
+ (string->u8list (cadr f)))
+ ((eq? (car f) 's16)
+ (append (string->u8list (cadr f)) '(0))) ;; XXX
+ ((eq? (car f) 'u8list)
+ (append (cadr f)))
+ (else
+ (uim-notify-fatal (N_ "unknown byte operator")))))
+ (zip fmt args))))
+
+(define (u8list-unpack fmt arg)
+ (let loop ((fmt fmt)
+ (arg arg)
+ (rest '()))
+ (define (call-with-n-byte n thunk)
+ (call-with-values (lambda () (split-at arg n))
+ (lambda (h t)
+ (loop (cdr fmt) t (cons (thunk h) rest)))))
+ (cond ((null? fmt)
+ (reverse rest))
+ ((eq? 'u8 (car fmt))
+ (call-with-n-byte 1 car))
+ ((eq? 'u16 (car fmt))
+ (call-with-n-byte 2 u8list->u16))
+ ((eq? 'u32 (car fmt))
+ (call-with-n-byte 4 u8list->u32))
+ ((eq? 's8 (car fmt))
+ (let ((ret (take-while (lambda (x) (not (= x 0))) arg)))
+ (loop (cdr fmt)
+ (drop arg (+ 1 (length ret)))
+ (cons (list->string (map integer->char ret)) rest))))
+ ((eq? 's16 (car fmt))
+ (let ((ret (take-while (lambda (x) (not (= x 0))) arg)))
+ (loop (cdr fmt)
+ (drop arg (+ 2 (length ret))) ;; XXX
+ (cons (list->string (map integer->char ret)) rest))))
+ ((eq? 'u8list (car fmt))
+ (loop (cdr fmt) '() (cons ret rest)))
+ (else
+ (uim-notify-fatal (N_ "unknown byte operator"))))))
+
+(define (call-with-u8list-unpack fmt arg thunk)
+ (apply thunk (u8list-unpack fmt arg)))
+
+(define (u8list->string-buf l)
+ (map integer->char l))
+(define (string-buf->u8list l)
+ (map char->integer l))
=======================================
--- /trunk/scm/Makefile.am Thu Oct 8 19:50:19 2009
+++ /trunk/scm/Makefile.am Thu Nov 12 05:26:02 2009
@@ -49,6 +49,7 @@
fileio.scm socket.scm http-client.scm process.scm \
openssl.scm \
sqlite3.scm \
+ lolevel.scm \
input-parse.scm match.scm
ETAGS_ARGS=$(SCM_FILES) $(GENERATED_SCM_FILES)
=======================================
--- /trunk/uim/Makefile.am Sat Oct 10 22:22:36 2009
+++ /trunk/uim/Makefile.am Thu Nov 12 05:26:02 2009
@@ -188,12 +188,6 @@
libuim_ffi_la_LIBADD = @FFI_LIBS@ libuim.la
libuim_ffi_la_LDFLAGS = -rpath $(uim_plugindir) -avoid-version -module
libuim_ffi_la_CPPFLAGS = -I$(top_srcdir) @FFI_CFLAGS@
-
-uim_plugin_LTLIBRARIES += libuim-lolevel.la
-libuim_lolevel_la_SOURCES = lolevel.c
-libuim_lolevel_la_LIBADD = libuim.la
-libuim_lolevel_la_LDFLAGS = -rpath $(uim_plugindir) -avoid-version -module
-libuim_lolevel_la_CPPFLAGS = -I$(top_srcdir)
endif
uim_plugin_LTLIBRARIES += libuim-skk.la
@@ -212,6 +206,12 @@
libuim_bsdlook_la_LIBADD =
libuim_bsdlook_la_CPPFLAGS = -I$(top_srcdir)
+uim_plugin_LTLIBRARIES += libuim-lolevel.la
+libuim_lolevel_la_SOURCES = lolevel.c
+libuim_lolevel_la_LIBADD = libuim.la
+libuim_lolevel_la_LDFLAGS = -rpath $(uim_plugindir) -avoid-version -module
+libuim_lolevel_la_CPPFLAGS = -I$(top_srcdir)
+
libuimincludedir = $(includedir)/uim
libuim_scm_la_SOURCES = uim-scm-sigscheme.c
=======================================
--- /trunk/uim/lolevel.c Sat Oct 10 23:16:21 2009
+++ /trunk/uim/lolevel.c Thu Nov 12 05:26:02 2009
@@ -37,6 +37,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
+#include <sys/types.h>
#include "uim.h"
#include "uim-scm.h"
@@ -190,6 +191,102 @@
c_pointer_to_Xlist(u64, uint64_t)
c_pointer_to_Xlist(s64, int64_t)
+
+static uim_lisp
+c_htons(uim_lisp u16_)
+{
+ return MAKE_INT(htons(C_INT(u16_)));
+}
+static uim_lisp
+c_htonl(uim_lisp u32_)
+{
+ return MAKE_INT(htonl(C_INT(u32_)));
+}
+
+static uim_lisp
+c_ntohs(uim_lisp u16_)
+{
+ return MAKE_INT(ntohs(C_INT(u16_)));
+}
+static uim_lisp
+c_ntohl(uim_lisp u32_)
+{
+ return MAKE_INT(ntohl(C_INT(u32_)));
+}
+
+
+static uim_lisp
+c_u16_to_u8list(uim_lisp u16_)
+{
+ u_int16_t u16 = htons(C_INT(u16_));
+
+ return LIST2(MAKE_INT(u16 & 0xff),
+ MAKE_INT((u16 >> 8) & 0xff));
+}
+static uim_lisp
+c_u32_to_u8list(uim_lisp u32_)
+{
+ u_int32_t u32 = htonl(C_INT(u32_));
+
+ return LIST4(MAKE_INT(u32 & 0xff),
+ MAKE_INT((u32 >> 8) & 0xff),
+ MAKE_INT((u32 >> 16) & 0xff),
+ MAKE_INT((u32 >> 24) & 0xff));
+}
+
+/* (map char->integer (string->list str)) */
+static uim_lisp
+c_string_to_u8list(uim_lisp str_)
+{
+ const char *str = REFER_C_STR(str_);
+ uim_lisp ret_ = uim_scm_null();
+
+ while (*str) {
+ ret_ = CONS(MAKE_INT(*str & 0xff), ret_);
+ str++;
+ }
+ ret_ = CONS(MAKE_INT(0), ret_);
+ return uim_scm_callf("reverse", "o", ret_);
+}
+
+static uim_lisp
+c_u8list_to_u16(uim_lisp u8list_)
+{
+ u_int8_t u8_1, u8_2;
+
+ u8_1 = C_INT(CAR(u8list_));
+ u8_2 = C_INT(CAR(CDR(u8list_)));
+ return MAKE_INT(ntohs(u8_1 |
+ (u8_2 << 8)));
+}
+static uim_lisp
+c_u8list_to_u32(uim_lisp u8list_)
+{
+ u_int8_t u8_1, u8_2, u8_3, u8_4;
+
+ u8_1 = C_INT(CAR(u8list_));
+ u8_2 = C_INT(CAR(CDR(u8list_)));
+ u8_3 = C_INT(CAR(CDR(CDR(u8list_))));
+ u8_4 = C_INT(CAR(CDR(CDR(CDR(u8list_)))));
+ return MAKE_INT(ntohl(u8_1 |
+ (u8_2 << 8) |
+ (u8_3 << 16) |
+ (u8_4 << 24)));
+}
+static uim_lisp
+c_u8list_to_string(uim_lisp u8list_)
+{
+ int len = uim_scm_length(u8list_);
+ int i;
+ char *str = uim_malloc(len + 1);
+
+ for (i = 0; i < len; i++) {
+ str[i] = (char)C_INT(CAR(u8list_));
+ u8list_ = CDR(u8list_);
+ }
+ str[len] = '\0';
+ return MAKE_STR_DIRECTLY(str);
+}
void
uim_plugin_instance_init(void)
@@ -242,6 +339,19 @@
uim_scm_init_proc2("pointer->s32list", c_pointer_to_s32list);
uim_scm_init_proc2("pointer->u64list", c_pointer_to_u64list);
uim_scm_init_proc2("pointer->s64list", c_pointer_to_s64list);
+
+ uim_scm_init_proc1("htons", c_htons);
+ uim_scm_init_proc1("htonl", c_htonl);
+ uim_scm_init_proc1("ntohs", c_ntohs);
+ uim_scm_init_proc1("ntohl", c_ntohl);
+
+ uim_scm_init_proc1("u16->u8list", c_u16_to_u8list);
+ uim_scm_init_proc1("u32->u8list", c_u32_to_u8list);
+ uim_scm_init_proc1("string->u8list", c_string_to_u8list);
+
+ uim_scm_init_proc1("u8list->u16", c_u8list_to_u16);
+ uim_scm_init_proc1("u8list->u32", c_u8list_to_u32);
+ uim_scm_init_proc1("u8list->string", c_u8list_to_string);
}
void