--- /dev/null
+/*
+ * taddecoder.c
+ *
+ * Copyright (c) 2014 project bchan
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source
+ * distribution.
+ *
+ */
+
+#include "taddecoder.h"
+
+#include <basic.h>
+#include <bstdio.h>
+#include <btron/dp.h>
+#include <tad.h>
+
+#include "tadlangcode.h"
+#include "tadsegment.h"
+
+#ifdef BCHAN_CONFIG_DEBUG
+# define DP(arg) printf arg
+# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
+#else
+# define DP(arg) /**/
+# define DP_ER(msg, err) /**/
+#endif
+
+EXPORT Bool taddecoder_next(taddecoder_t *iterator, tadsegment *result)
+{
+ TC *ch;
+ W rawlen_by_TC;
+ LTADSEG *seg0;
+ tadlangcode_parser_t langparser;
+ TADLANGCODE_PARSER_RESULT langparser_result;
+
+ ch = iterator->bin + iterator->index;
+
+ if (iterator->index >= iterator->len) {
+ return False;
+ }
+
+ if ((*ch & 0xFF80) == 0xFF80) {
+ result->type = TADSEGMENT_TYPE_VARIABLE;
+ result->value.variable.raw = (UB*)ch;
+ result->value.variable.segid = *ch & 0xFF;
+ seg0 = (LTADSEG*)ch;
+ if (seg0->len == 0xffff) {
+ rawlen_by_TC = 1 + seg0->llen / 2 + 3;
+ result->value.variable.seglen = seg0->llen;
+ result->value.variable.segdata = ((UB*)seg0) + 8;
+ } else {
+ rawlen_by_TC = 1 + seg0->len / 2 + 1;
+ result->value.variable.seglen = seg0->len;
+ result->value.variable.segdata = ((UB*)seg0) + 4;
+ }
+ iterator->index += rawlen_by_TC;
+ result->value.variable.rawlen = rawlen_by_TC * sizeof(TC);
+ } else if ((*ch & 0xFF00) == 0xFE00) {
+ result->type = TADSEGMENT_TYPE_LANGCODE;
+ tadlangcode_parser_initialize(&langparser);
+ for (;;) {
+ langparser_result = tadlangcode_parser_inputchar(&langparser, *ch);
+ iterator->index++;
+ if (langparser_result == TADLANGCODE_PARSER_RESULT_DETERMINED) {
+ break;
+ } else if (langparser_result == TADLANGCODE_PARSER_RESULT_INVALID) {
+ tadlangcode_parser_finalize(&langparser);
+ return False;
+ }
+ ch = iterator->bin + iterator->index;
+ }
+ tadlangcode_parser_getlangcode(&langparser, &result->value.lang);
+ tadlangcode_parser_finalize(&langparser);
+ } else {
+ result->type = TADSEGMENT_TYPE_CHARACTOR;
+ result->value.ch = *ch;
+ iterator->index++;
+ }
+
+ return True;
+}
+
+EXPORT VOID taddecoder_initialize(taddecoder_t *iterator, TC *bin, W strlen)
+{
+ iterator->bin = bin;
+ iterator->len = strlen;
+ iterator->index = 0;
+}
+
+EXPORT VOID taddecoder_finalize(taddecoder_t *iterator)
+{
+}
--- /dev/null
+/*
+ * test_taddecoder.c
+ *
+ * Copyright (c) 2014 project bchan
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source
+ * distribution.
+ *
+ */
+
+#include "test_tad.h"
+
+#include "taddecoder.h"
+
+#include <basic.h>
+#include <bstdio.h>
+#include <bstdlib.h>
+#include <bstring.h>
+#include <btron/dp.h>
+#include <tad.h>
+
+#include "tadsegment.h"
+
+#include <unittest_driver.h>
+
+LOCAL UB test_taddecoder_testdata01[] = {
+ 0xe0, 0xff, 0x06, 0x00, 0x00, 0x00, 0x02, 0x00,
+ 0x22, 0x01, 0xe1, 0xff, 0x18, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0xff,
+ 0x88, 0xff, 0x21, 0x00, 0x00, 0x00, 0xa0, 0xff,
+ 0x0e, 0x00, 0x00, 0x00, 0x7b, 0x05, 0xe0, 0x03,
+ 0x5e, 0x00, 0x46, 0x00, 0x6c, 0x00, 0x55, 0x00,
+ 0xa0, 0xff, 0x0a, 0x00, 0x00, 0x01, 0x5e, 0x00,
+ 0x76, 0x00, 0x6c, 0x00, 0x55, 0x00, 0xa0, 0xff,
+ 0x22, 0x00, 0x00, 0x03, 0xa0, 0xff, 0x02, 0x00,
+ 0x00, 0x08, 0xa1, 0xff, 0x04, 0x00, 0x00, 0x00,
+ 0x00, 0x80, 0xa1, 0xff, 0x02, 0x00, 0x01, 0x01,
+ 0x5d, 0x21, 0xad, 0xff, 0x04, 0x00, 0x00, 0x00,
+ 0xc8, 0x00, 0x5d, 0x21, 0xa0, 0xff, 0x04, 0x00,
+ 0x00, 0x04, 0x00, 0x80, 0xa4, 0xff, 0x24, 0x00,
+ 0x11, 0x08, 0x22, 0x21, 0x23, 0x21, 0x24, 0x21,
+ 0x25, 0x21, 0x2b, 0x21, 0x2c, 0x21, 0x47, 0x21,
+ 0x49, 0x21, 0x4b, 0x21, 0x4d, 0x21, 0x4f, 0x21,
+ 0x51, 0x21, 0x53, 0x21, 0x55, 0x21, 0x57, 0x21,
+ 0x59, 0x21, 0x5b, 0x21, 0xa4, 0xff, 0x18, 0x00,
+ 0x11, 0x09, 0x46, 0x21, 0x48, 0x21, 0x4a, 0x21,
+ 0x4c, 0x21, 0x4e, 0x21, 0x50, 0x21, 0x52, 0x21,
+ 0x54, 0x21, 0x56, 0x21, 0x58, 0x21, 0x5a, 0x21,
+ 0xa1, 0xff, 0x24, 0x00, 0x00, 0x02, 0x04, 0x01,
+ 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x0b, 0x00, 0x48, 0x00, 0x90, 0x00, 0xd8, 0x00,
+ 0x20, 0x01, 0x68, 0x01, 0xb0, 0x01, 0xf8, 0x01,
+ 0x40, 0x02, 0x88, 0x02, 0xd0, 0x02, 0x18, 0x03,
+ 0xa1, 0xff, 0x04, 0x00, 0x01, 0x00, 0x04, 0x01,
+ 0x46, 0x23, 0x52, 0x23, 0x4f, 0x23, 0x4d, 0x23,
+ 0x27, 0x21, 0x21, 0x21, 0x6e, 0x23, 0x61, 0x23,
+ 0x6d, 0x23, 0x65, 0x23, 0x0a, 0x00, 0x6d, 0x23,
+ 0x61, 0x23, 0x69, 0x23, 0x6c, 0x23, 0x27, 0x21,
+ 0x21, 0x21, 0x6d, 0x23, 0x61, 0x23, 0x69, 0x23,
+ 0x6c, 0x23, 0x0a, 0x00, 0x0a, 0x00, 0x62, 0x23,
+ 0x6f, 0x23, 0x64, 0x23, 0x79, 0x23, 0xe2, 0xff,
+ 0x00, 0x00,
+};
+
+LOCAL UNITTEST_RESULT test_taddecoder_1()
+{
+ taddecoder_t iter;
+ tadsegment result;
+ Bool cont;
+
+ taddecoder_initialize(&iter, (TC*)test_taddecoder_testdata01, sizeof(test_taddecoder_testdata01)/sizeof(TC));
+
+ for (;;) {
+ cont = taddecoder_next(&iter, &result);
+ if (cont == False) {
+ break;
+ }
+ }
+
+ taddecoder_finalize(&iter);
+
+ return UNITTEST_RESULT_PASS;
+}
+
+EXPORT VOID test_taddecoder_main(unittest_driver_t *driver)
+{
+ UNITTEST_DRIVER_REGIST(driver, test_taddecoder_1);
+}