суббота, 23 июля 2016 г.

Fossil: remote clone on Windows from Linux

There is interesting DSCS - Fossil, written by SQLite3 author. When I tried to use it on remote Windows machines, I hit several problems: no local ssh.exe, problems with GSSAPI, authorization...

But these problems are solvable with SSH tunnel. Let's look at picture:

SSH/TUNNEL HTTP PUTTY:7777 SSHD:22 fossil:8080 Client with repo clone

We can create SSH tunnel with Putty (SSH alternative for Windows, and not only) and connect with fossil HTTP server via it. One machine will keep fossil repo. First, we create it (I called it x):

fossil init x
fossil open x
fossil add somefile
fossil ci -m "init"
fossil ui

When fossil's UI will be opened, I add some user, for example, user and grant them permissions AND password. Then kill fossil with ^C and start it in serv mode (actually ui/serv mode lokks like the same). It will be bound to default port 8080 (you can see this on fig).

On remote machine we start Putty, create some session and in SSH tunnel section creates tunnel (in "destination" input box) with local port 7777, IP of fossile machine and fossil port 8080, result should be:

L7777 fossil_IP:8080

Also we set "Local", "Auto" (or "Dynamic" - works too).

In session section we set host to fossil IP (where we created repo!), auto-login with name of fossil machine user. Now save session and open it. We are see console window, enter password and don't close it (another way is to use Putty agent). Open new console and enter there:

fossil clone http://fossil-user@localhost:7777//path-to-x x
fossil open x

where path-to-x is the path to repo on fossil machine.

Now you have x repo locally. Works with them, commit, then push changes:

fossil push

If it ask password, enter it (this is the password of early created fossil user).

суббота, 9 июля 2016 г.

Parse hex chars string to bytes

Very simple task as for interview: to parse hex chars into bytes. Example: "123Ad" -> "\x12\x3A\x0D". This procedure can be helpfull if you want to transmit some encoding string (in UTF8, UTF16, mbcs, etc) between different processes/OS and it's probably to get broken bytes, so best solution will be to use such hex chars (it's ASCII [0-9a-fA-F] only).

Implementation in C may use sscanf() which will be slow or does not. Here is an example without sscanf:

#include <stdio.h>
#include "algutils.h"

int parse_hex_str(const char *s, int slen, char *out) {
#define __ord(c) \
    ((c) >= '0' && (c) <= '9'? (c) - '0' : \
     (c) >= 'a' && (c) <= 'f'? (c) - 'a' + 10 : \
     (c) >= 'A' && (c) <= 'F'? (c) - 'A' + 10 : -1)

    register int b, t, i;
    for (i=0; i<slen; i++) {
        t = __ord(s[i]);
        if (-1 == t) return (1);
        if (i%2) out[i/2] = (b << 4) | t;
        else     b = t;
    }
    if (i%2) out[i/2] = b;
    return (0);

#undef __ord
}

/*************************************************************/
int main() {
    unsigned char ins[] = "1230aAF0D";
    unsigned char out[20] = {0};
    int res = parse_hex_str(ins, sizeof(ins) - 1, out);
    printf("input = %s\n", ins);
    printf("res=%d\n", res);
    PRINTARRX(out, 10, "%02X");
    return (0);
}

Compilation:

gcc -ggdb -std=c99 -o hex.exe hex.c

Output:

input = 1230aAF0D
res=0
{ 12 30 AA F0 0D 00 00 00 00 00 }