2021年3月17日水曜日

拙作のRestfulサーバ(Saraswatiと命名)

Hyper-VのUbuntuでSaraswatiを起動し、 WindowsかからHTML(JQuery)でHTTPRequestを投げ、 戻ってきたJSONから値を取得し、Imageに表示できた。 クライアント側のHTMLもこんだけ 何も工夫してない

Base64再び

 Base64というのは、文字列とバイナリの置き換えなのだけど、

文字列もバイナリも、長さ固定だと使いにくい

文字列ならstd::string

バイナリなら、std::vector<BYTE>を使えばいい

それで探してみたら以下のようなものを見つけた。
===base64.h===

 #pragma once

#include 
#include 
typedef unsigned char BYTE;

std::string base64_encode(BYTE const* buf, unsigned int bufLen);
std::vector base64_decode(std::string const&);
 
===base64.cpp===
#include "base64.h"
#include 

static const std::string base64_chars =
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             "abcdefghijklmnopqrstuvwxyz"
             "0123456789+/";


static inline bool is_base64(BYTE c) {
  return (isalnum(c) || (c == '+') || (c == '/'));
}

std::string base64_encode(BYTE const* buf, unsigned int bufLen) {
  std::string ret;
  int i = 0;
  int j = 0;
  BYTE char_array_3[3];
  BYTE char_array_4[4];

  while (bufLen--) {
    char_array_3[i++] = *(buf++);
    if (i == 3) {
      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
      char_array_4[3] = char_array_3[2] & 0x3f;

      for(i = 0; (i <4 0xfc="" 3="" base64_chars="" char_array_3="" char_array_4="" for="" i="" if="" j="" ret="">> 2;
    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
    char_array_4[3] = char_array_3[2] & 0x3f;

    for (j = 0; (j < i + 1); j++)
      ret += base64_chars[char_array_4[j]];

    while((i++ < 3))
      ret += '=';
  }

  return ret;
}

std::vector base64_decode(std::string const& encoded_string) {
  int in_len = encoded_string.size();
  int i = 0;
  int j = 0;
  int in_ = 0;
  BYTE char_array_4[4], char_array_3[3];
  std::vector ret;

  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
    char_array_4[i++] = encoded_string[in_]; in_++;
    if (i ==4) {
      for (i = 0; i <4 0x30="" 2="" base64_chars.find="" char_array_3="" char_array_4="" i="">> 4);
      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

      for (i = 0; (i < 3); i++)
          ret.push_back(char_array_3[i]);
      i = 0;
    }
  }

  if (i) {
    for (j = i; j <4 0="" 0x30="" 2="" base64_chars.find="" char_array_3="" char_array_4="" for="" j="">> 4);
    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

    for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
  }

  return ret;
}


使い方
===test.cpp===
#include 
#include "base64.h"


int main(void)
{
	std::string str = "abcde";
	std::string out = base64_encode( (BYTE *)str.c_str(),str.length());
	printf("%s\n",out.c_str());
	std::vector buff = base64_decode(out);
	printf("%s\n",(char*)buff.data());
}

2021年2月25日木曜日

SednaのXQueryバージョンがちょっと古い・・・まぁ問題ないんだけど

 golangで作ったRESTfulサーバにアクセスし、結果をJSONで戻してみた。

takahiro@takahiro-Virtual-Machine:~/gorest/src$ curl -i localhost:5000/foodstuff?foodname="りんご"

HTTP/1.1 200 OK

Content-Tye: application/json

Date: Thu, 25 Feb 2021 10:47:59 GMT

Content-Length: 769

Content-Type: text/plain; charset=utf-8

{"food": [{"food_id": "7148", "foodname": "りんご 皮なし 生"}, {"food_id": "7176", "foodname": "りんご 皮つき 生"}, {"foodname": "りんご 皮つき 焼き", "food_id": "7180"}, {"food_id": "7149", "foodname": "りんご 果実飲料 ストレートジュース"}, {"food_id": "7150", "foodname": "りんご 果実飲料 濃縮還元ジュース"}, {"food_id": "7151", "foodname": "りんご 果実飲料 50%果汁入り飲料"}, {"food_id": "7152", "foodname": "りんご 果実飲料 30%果汁入り飲料"}, {"food_id": "7153", "foodname": "りんご 缶詰"}, {"food_id": "7154", "foodname": "りんご ジャム"}, {"food_id": "17018", "foodname": "<調味料類> (食酢類) 果実酢 りんご酢"}]}

実はSedna XML DatabaseのSerialization機能を使ってJSONを戻そうとしたのだけど、Sedna の XQuryのバージョンが1.0らしく、3.0以降の機能が使えない、、、

つまり、SerializationでJSONとか指定できない、、、

仕方ないので、一度 Sedna から受け取ったXMLをgolangでJSONに変換し、ヘッダにapplication/jsonを設定して戻すこととした。

動きは上記の通り、

Curlでリクエストを投げ、結果がJSONでちゃんと戻っている。

今回はこれで事足りるんだけど、いずれ、何でもできるように SednaのXQueryを3.1などに対応させないとダメかもしれない。

Go でRestAPI (DBはSedna XML Database)


今作ってるやつ
golangでRestAPI

cgoからsedna xml databaseのクエリ実行

結果を取得まで

まだ結果をJSONにしてない
XQueryのシリアライズで出来るかも?

まだ脆弱性ありまくり

2021年2月7日日曜日

Sedba XML Database 再び

ちょっと作りたいサイトがあって、マイクロサービスを用意しようと思って、golang使ってたんだけど、DBどうしようかなぁと思ってデータを眺めていたら、XMLデータベースを使いたくなってきた。
しかしながら、今動くのはJavaベースのしょぼいのばかり、 やはりC系言語でガリガリ書かれたものを使いたい。

あー、俺の中でJavaはクソ遅いんで、異論は認めない。

まぁ、そういうことで、選択しとして Sedna XML Database しかないのだ。
githubからソースを取得し、コンパイル、途中少しエラーが出たので、 BISION絡みのところと、Stringのインクルードガードを直し、ポインタの使い方を多分間違っている部分がコンパイラに怒られたので直し、 まぁそのくらいでコンパイルは通るようになった。

んで、データをエクセル使ってXMLに直し、バルクインサートし、XQueryで取り出してみた。
まぁ、よく動くよこれ、UTF-8にも対応してるんで、日本語も無問題だ。
今はXQueryを勉強し直してる。
Sedna XML Databaseは、C言語用インターフェースは用意されてるから、 golangからでも行けるだろうと思ってる。
ダメならマイクロサービスをC系の言語で書くわ
というか、なんで XML Database 無くなったんだろうか?
こんないい物が無くなるなんてまぢで勿体ない。

2020年5月25日月曜日

NTC thermistor Setting for Duet

I changed settings for HT-NTC100K thermistor.

I get thermistor from here.





I found this PDF Datasheet.

I set the value to configurator
and I get the result as above.

Settings for Duet firmware Ver3.0 is bellow.
M308 S1 P"e0_temp" Y"thermistor" T100000 B5007 C1.687660e-7 ; HT-NTC100K


2020年4月17日金曜日

Use XSL as Model of MVC

This is a sample of xslt which transform xml to sql.
I confirmed its result by using this link
https://www.freeformatter.com/xsl-transformer.html#ad-output

XSL having basic function and selection method of XPath.
It is possible to build Logic of Translation from GUI values to SQL.

Followings are the example.

XML
<Models>
  <model_1>
    <table>TBL_SAMPLE</table>
    <param_1>101</param_1>
    <param_2>102</param_2>
  </model_1>
</Models>

XSL
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:template match="//model_1">
        SELECT *
        FROM <xsl:value-of select="table" />
        WHERE p1 = <xsl:value-of select="param_1" />
        AND   p2 = '<xsl:value-of select="param_2" />'
    </xsl:template>
</xsl:transform>

Result
SELECT * FROM TBL_SAMPLE WHERE p1 = 101 AND p2 = '102'



2020年4月6日月曜日

Duet Maestro Firmware 2.0 to 3.0 settings changes and Precision Piezo Orion settings

;EndStops
2.0
M574 X2 Y2 Z2 S1                            ; set active high endstops
3.0
; Endstops
;M574 X2 Y2 Z2 S1                            ; set active high endstops
M574 X2 S1 P"xstop"   ; X min active high endstop switch
M574 Y2 S1 P"ystop"   ; Y min active high endstop switch
M574 Z2 S1 P"zstop"   ; Z min active high endstop switch

; Z-Probe
2.0
M558 P5 I1 H5 R0.4 F300 X0 Y0 Z0 T10000              ; set Z probe type to switch and the dive height + speeds

3.0
M558 P5 I1 H5 R0.4 F300 X0 Y0 Z0 T10000 C"^zprobe.in" ; Piezo connected to Z probe IN pin, free up MOD pin
G31 X0 Y0 Z-0.1 P100                                  ; set Z probe trigger value, offset and trigger height
M557 R140 S20                                         ; define mesh grid

; Heaters
2.0
; Heaters
M305 P0 T100000 B4138 R2200                 ; set thermistor + ADC parameters for heater 0
M143 H0 S120                                ; set temperature limit for heater 0 to 120C
M305 P1 T100000 B4138 R2200                 ; set thermistor + ADC parameters for heater 1
M143 H1 S280                                ; set temperature limit for heater 1 to 280C

3.0
M308 S0 P"bed_temp" Y"thermistor" T100000 B4607 ; define bed temperature sensor
M308 S1 P"e0_temp" Y"thermistor" T100000 B3950 C7.06e-8 ; define E0 temperature sensor
M950 H0 C"bed_heat" T0 ; heater 0 uses the bed_heat pin, sensor 0
M950 H1 C"e0_heat" T1 ; heater 1 uses the e0_heat pin and sensor 1






2020年2月3日月曜日

VirtualBox not launches VM

Open .vbox file and find following lines.

<Machine uuid="{xxxxxxxxxx-xxxx-xxxx-xxxxxxxx}" name="Windows7" OSType="Windows7_64" stateFile="{xxxxxxxxxx-xxxx-xxxx-xxxxxxxx}.sav" currentSnapshot="{xxxxxxxxxx-xxxx-xxxx-xxxxxxxx}" snapshotFolder="Snapshots" lastStateChange="2020-02-01T11:11:111">

and remove red part.


2019年9月25日水曜日

reinterprit_cast

I met strange situations.
There were reinterprit_cast in the code.
The code was working when it was developed by Visual Studio 2008.
But I changed to Visual Studio 2010, the code make access violations at delete the pointer and its memory.
The class was derived from 2 classes.
I am not sure if it were related on this issue.
I inspected the address of the pointer.
The pointer address was shift 2 byte from first pointed address at deletion.
I changed from reinterprit_cast to Simple Cast.
After that, access violations were blowed out.
Strange...