Fork me on GitHub

2012/01/17


このエントリーをはてなブックマークに追加
trieなんたらが話題になってたのでなんとなく書いてみた。
ベンチとかはやってない。
404 Blog Not Found:Algorithm - 連想配列の実装としてのハッシュはオワコン?

そのデータ構造は、君の魂を差し出すに足るものかい? 連想配列( Associative array )がコレクション( Collection )、すなわち数多のデータ構造をまとめるデータ構造としての覇...

http://blog.livedoor.jp/dankogai/archives/51765855.html
#include <stdio.h>
#include <stdlib.h>

typedef struct _trie {
  char c;
  unsigned int n;
  struct _trie** next;
  void* value;
} trie;

trie*
trie_new() {
  trie* p = (trie*) malloc(sizeof(trie));
  p->c = 0;
  p->n = 0;
  p->next = NULL;
  return p;
}

void
trie_free(trie* p) {
  unsigned int i;
  for (i = 0; i < p->n; i++)
    trie_free(p->next[i]);
  if (p->n)
    free(p->next);
  free(p);
}

trie*
trie_put(trie* p, const char* key, const void* value) {
  if (*key == 0) {
    p->value = (void*) value;
    return p;
  }
  trie* next = NULL;
  int i;
  for (i = 0; i < p->n; i++)
    if (p->next[i]->c == *key) {
      next = p;
      break;
    }
  if (!next) {
    if (!(next = trie_new())) return NULL;
    trie** children = (trie**) realloc(p->next, p->n * sizeof(trie*));
    if (!children) return NULL;
    p->next = children;

    next->c = *key;
    p->next[p->n] = next;
    p->n++;
  }
  return trie_put(next, key+1, value);
}

trie*
trie_get(trie* p, const char* key) {
  if (p->c) {
    if (p->c != *key)
      return NULL;
    if (p->c == *key && *(key+1) == 0)
      return p;
    key++;
  }
  int i;
  trie* value = NULL;
  for (i = 0; i < p->n; i++) {
    if ((value = trie_get(p->next[i], key)))
      return value;
  }
  return NULL;
}

void
safe_puts(const char* key, const trie* p) {
  if (!p)
    printf("%s not found\n", key);
  else if (!p->value)
    printf("%s: null\n", key);
  else
    printf("%s%s\n", key, (char*) p->value);
}

int
main(int argc, char* argv[]) {
  trie* p = trie_new();
  trie* v;

  trie_put(p, "foo""bar");
  trie_put(p, "bar""baz");

  v = trie_get(p, "baz");
  safe_puts("baz", v);

  v = trie_get(p, "foo");
  safe_puts("foo", v);

  v = trie_get(p, "bar");
  safe_puts("bar", v);

  trie_put(p, "うんこ""うんこっこー");
  v = trie_get(p, "うんこ");
  safe_puts("うんこ", v);

  v = trie_get(p, "404 blog");
  safe_puts("404 blog", v);

  trie_free(p);
  return 0;
}
baz not found
foo: bar
bar: baz
うんこ: うんこっこー
404 blog not found
Posted at 13:52 in ソフトウェア::lang::c
Tagged as: c
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2012/01/13


このエントリーをはてなブックマークに追加
追記: 良く見たらstrncpyの罠でもなんでもなく、バッファがクリアされてないのが原因だった。って事であとでpullreqでも送っとくかな。

まぁソート関数自身の問題じゃないので控えめに。

strncpyは必ず \0 で埋めてくれるとは限らない。
404 Blog Not Found:algorithm - bucketsort.[ch] - 汎用かつlibcの*sortより高速な

dankogai/c-bucketsort - GitHub

http://blog.livedoor.jp/dankogai/archives/51764911.html
dankogai/c-bucketsort - GitHub

bucketsort - bucket sort that can be used for general purpose

https://github.com/dankogai/c-bucketsort
strncpy(3)のmanページにはこう書いてある。

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

なので...
# ./bucketsort README
とかするとNULストップしてないバッファで画面が賑やかになり、時にはけたたましいビープ音で心躍る。

サンプルプログラムの仕様が最終行の改行コードなしでも動く様にすべきなのかどうなのかが分からないので
diff --git a/main.c b/main.c
index 67e4e00..48391b4 100644
--- a/main.c
+++ b/main.c
@@ -74,6 +74,7 @@ int main(int argc, char **argv)
        if (!lines[lcur])
            EXIT("malloc failed");
        strncpy(lines[lcur], buf, slen - 1);    // do not copy \n
+       lines[lcur][slen - 1] = 0;
     }
     size_t i;
     // for (i = 0; i < lcur; i++) printf("%s\n", lines[i]);
こういうワークアラウンドでもOKなのかもしれないが(そういう点でfork/pullreqはやめた)、おそらくツールとして扱うなら改行コードがあった場合には削除...という動きが望ましいのでstrpbrk(3)やstrchr(3)と使ったり、自作strchrぽい処理を入れていくと他のsortと比較しておられる状況も少し変わったりするのかもしれませんね。確認してないけど。
sort(1)とはやってる事が違いすぎるのでそもそもな気もしなくない。どこを比較したんだろう...。

ちなみに全然オフトピだけど、GNU textutilsに入ってるsort(1)にはコア数によって動的にスレッドを生成してソートする処理が入ってるのでそういうの興味ある人はコード読むといいと思います。
Posted at 10:13 in ソフトウェア::lang::c
Tagged as: c
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2011/02/03


このエントリーをはてなブックマークに追加
最近phantomjsなんて物が出てきて結構便利そうなのでいろいろと遊んでます。
PhantomJS: 「最小限なheadlessのWebKitベースのJavaScriptツール」 - karasuyamatenguの日記

headless=スクリーンがない=コマンドと考えればいい。要はブラウザから画面と取り除いてJavaScriptによるスクリプティングを可能にしたコマンドツール。逆に言うとDOM+JavaScript+Networkingをコマンドにしたもの。...

http://d.hatena.ne.jp/karasuyamatengu/20110126/1296066287
phantomjs - Project Hosting on Google Code

PhantomJS is a minimalistic, headless, WebKit-based, JavaScript-driven tool. It has native support f...

http://code.google.com/p/phantomjs/
ただちょっと改造したいとか、それだけの為にQt入れたくないよーとか、staticビルドすんのに4時間もかかるのかよ!とかお嘆きの方もいらっしゃると思ったので...

phantomjs を webkitgtk+ でうごかしたらいいんじゃね、とおもってやってたけど、コンパイルに必要なモジュールおおすぎてあきらめたless than a minute ago via Echofon



QtWebKitでなく、webkitgtk+で実装してみました。
mattn/specterjs - GitHub

SpecterJS is a minimalistic, headless, WebKit-based, JavaScript-driven tool.

https://github.com/mattn/specterjs
ほとんどphantomjs互換です。phantomjsのphantomは幽霊って意味だったので、妖怪という意味のspecterを使いspecterjsと名付けました。
ただまだ制限があって、renderで扱えるのはpdfのみです。png出力にはまだ対応出来ていません。これはいずれやります。おそらくcairoとGtkPrintContext使えばいけるかと思います。
次にrenderで使用する際のviewportSizeが正しく機能していません。
それ以外は動くのでphantomjsについているサンプルの殆どが動きます。
ちなみに
if (specter.state.length === 0) {
    specter.state = 'mcdonalds';
    specter.open('http://www.mcdonalds.co.jp/menu/regular/index.html');
else {
    [].forEach.call(document.querySelectorAll('ul.food-set>li img'),
        function(n) { console.log(n.getAttribute('alt')); });
    specter.exit();
}
で、マクドナルドのメニュー一覧を出すことも出来ちゃいます!
よろしかったら遊んで下さい。

2010/12/15


このエントリーをはてなブックマークに追加
記号プログラミング大好きっ子の皆さん、こんにちわ。
Advent Calendarで書いてもよかったかもしれませんが、どちらかと言うと逆かなーとか思ったのでこちらで。 aaencodeと言えば、はせがわさんが作ったjavascript向けの変態難読化スクリプトの事ですが、これの利用価値として以前から考えていたapacheモジュール化をやってみました。
サーバ上のjavascriptファイルを可視状態で編集してもクライアントからは難読化されて見えるという代物。実装としてはapacheのOutputフィルタモジュールとなります。
aaencodeは実装としては結構簡単な作りなのでソース全体を公開します。
/**
 * mod_aaencode -- apache filter module encoding to japanese style emoticons.
 *
 * AUTHOR:
 *   Yasuhiro Matsumoto <mattn.jp@gmail.com> a.k.a mattn
 *
 * ORIGINAL IDEA:
 *   Yosuke HASEGAWA, http://utf-8.jp/
 *
 * COMPILING:
 *
 *  for Windows:
 *    apxs2 -i -a -c -Wc,-O2 mod_aaencode.c \
 *      libapr-1.lib libaprutil-1.lib libhttpd.lib
 *
 *  for UNIX:
 *    apxs2 -i -a -c -Wc,-O2 mod_aaencode.c -lapr-1 -laprutil-1
 *
 * INSTALLATION:
 *   add following line into your '.htaccess'.
 *
 *   AddOutputFilter AAENCODE .js
 */
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_log.h"
#include "ap_config.h"

#define PUT(f, bb, s) \
  APR_BRIGADE_INSERT_TAIL(bb, \
                  apr_bucket_immortal_create(s, strlen(s), f->c->bucket_alloc))

#define CTOI(c) ( \
  (c >= '0' && c <= '9') ? (c - '0') : \
  (c >= 'a' && c <= 'f') ? (c - 'a' + 10) : \
  (c >= 'A' && c <= 'F') ? (c - 'A' + 10) : -1)

static char
utf8len_tab[256] = {
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1,
};

static char
utf8len_tab_zero[256] = {
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0,
};

static int
utf_ptr2len(unsigned char* p) {
  int    len;
  int    i;

  if (*p == 0)
    return 0;
  len = utf8len_tab[*p];
  for (i = 1; i < len; ++i)
    if ((p[i] & 0xc0) != 0x80)
      return 1;
  return len;
}

static int
utf_ptr2char(unsigned char* p) {
  int    len;

  if (p[0] < 0x80)  /* be quick for ASCII */
    return p[0];

  len = utf8len_tab_zero[p[0]];
  if (len > 1 && (p[1] & 0xc0) == 0x80) {
    if (len == 2)
      return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
    if ((p[2] & 0xc0) == 0x80) {
      if (len == 3)
        return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
          + (p[2] & 0x3f);
      if ((p[3] & 0xc0) == 0x80) {
        if (len == 4)
          return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
            + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
        if ((p[4] & 0xc0) == 0x80) {
          if (len == 5)
            return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
              + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
              + (p[4] & 0x3f);
          if ((p[5] & 0xc0) == 0x80 && len == 6)
            return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
              + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
              + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
        }
      }
    }
  }
  /* Illegal value, just return the first byte */
  return p[0];
}

static apr_status_t
aaencode_output_filter(ap_filter_t* f, apr_bucket_brigade* bb) {
  const char* aa[] = {
    "(c^_^o)", // (c^_^o)
    "(\xef\xbe\x9f\xce\x98\xef\xbe\x9f)", // (゚Θ゚)
    "((o^_^o) - (\xef\xbe\x9f\xce\x98\xef\xbe\x9f))", // ((o^_^o) - (゚Θ゚))
    "(o^_^o)", // (o^_^o)
    "(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)", // (゚ー゚)
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) + (\xef\xbe\x9f\xce\x98\xef\xbe\x9f))", // ((゚ー゚) + (゚Θ゚))
    "((o^_^o) +(o^_^o))", // ((o^_^o) +(o^_^o))
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) + (o^_^o))", // ((゚ー゚) + (o^_^o))
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) + (\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f))", // ((゚ー゚) + (゚ー゚))
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) + (\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) + (\xef\xbe\x9f\xce\x98\xef\xbe\x9f))", // ((゚ー゚) + (゚ー゚) + (゚Θ゚))
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) .\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89", // (゚Д゚) .゚ω゚ノ
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) .\xef\xbe\x9f\xce\x98\xef\xbe\x9f\xef\xbe\x89", // (゚Д゚) .゚Θ゚ノ
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['c']", // (゚Д゚) ['c']
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) .\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f\xef\xbe\x89", // (゚Д゚) .゚ー゚ノ
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) .\xef\xbe\x9f\xd0\x94\xef\xbe\x9f\xef\xbe\x89", // (゚Д゚) .゚Д゚ノ
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) [\xef\xbe\x9f\xce\x98\xef\xbe\x9f]", // (゚Д゚) [゚Θ゚]
  };
  const char* head =
    "\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89= /\xef\xbd\x80\xef\xbd\x8d\xc2\xb4\xef\xbc\x89\xef\xbe\x89 ~\xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb   //*\xc2\xb4\xe2\x88\x87\xef\xbd\x80*/ ['_']; o=(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)  =_=3; " // ゚ω゚ノ= /`m´)ノ ~┻━┻   //*´∇`*/ ['_']; o=(゚ー゚)  =_=3;
    "c=(\xef\xbe\x9f\xce\x98\xef\xbe\x9f) =(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)-(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f); (\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) =(\xef\xbe\x9f\xce\x98\xef\xbe\x9f)= (o^_^o)/ (o^_^o);" // c=(゚Θ゚) =(゚ー゚)-(゚ー゚); (゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)={\xef\xbe\x9f\xce\x98\xef\xbe\x9f: '_' ,\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89 : ((\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89==3) +'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f] " // (゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚]
    ",\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f\xef\xbe\x89 :(\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89+ '_')[o^_^o -(\xef\xbe\x9f\xce\x98\xef\xbe\x9f)] " // ,゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)]
    ",\xef\xbe\x9f\xd0\x94\xef\xbe\x9f\xef\xbe\x89:((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_')[\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f] }; (\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) [\xef\xbe\x9f\xce\x98\xef\xbe\x9f] =((\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89==3) +'_') [c^_^o];" // ,゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['c'] = ((\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)+'_') [ (\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)+(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)-(\xef\xbe\x9f\xce\x98\xef\xbe\x9f) ];" // (゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['o'] = ((\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)+'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f];" // (゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];
    "(\xef\xbe\x9fo\xef\xbe\x9f)=(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['c']+(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['o']+(\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89 +'_')[\xef\xbe\x9f\xce\x98\xef\xbe\x9f]+ ((\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89==3) +'_') [\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f] + " // (゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] +
    "((\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) +'_') [(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)+(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)]+ ((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f]+" // ((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_') [(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) - (\xef\xbe\x9f\xce\x98\xef\xbe\x9f)]+(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['c']+" // ((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+
    "((\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)+'_') [(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)+(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)]+ (\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['o']+" // ((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f];(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['_'] =(o^_^o) [\xef\xbe\x9fo\xef\xbe\x9f] [\xef\xbe\x9fo\xef\xbe\x9f];" // ((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];
    "(\xef\xbe\x9f\xce\xb5\xef\xbe\x9f)=((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f]+ (\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) .\xef\xbe\x9f\xd0\x94\xef\xbe\x9f\xef\xbe\x89+" // (゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+
    "((\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)+'_') [(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f) + (\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)]+((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_') [o^_^o -\xef\xbe\x9f\xce\x98\xef\xbe\x9f]+" // ((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+
    "((\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f==3) +'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f]+ (\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89 +'_') [\xef\xbe\x9f\xce\x98\xef\xbe\x9f]; " // ((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚];
    "(\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)+=(\xef\xbe\x9f\xce\x98\xef\xbe\x9f); (\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)[\xef\xbe\x9f\xce\xb5\xef\xbe\x9f]='\\\\'; " // (゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\';
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f).\xef\xbe\x9f\xce\x98\xef\xbe\x9f\xef\xbe\x89=(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f+ \xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9f)[o^_^o -(\xef\xbe\x9f\xce\x98\xef\xbe\x9f)];" // (゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];
    "(o\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9fo)=(\xef\xbe\x9f\xcf\x89\xef\xbe\x9f\xef\xbe\x89 +'_')[c^_^o];" // (o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o]; TODO
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) [\xef\xbe\x9fo\xef\xbe\x9f]='\\\"';" // (゚Д゚) [゚o゚]='\"';
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['_'] ( (\xef\xbe\x9f\xd0\x94\xef\xbe\x9f) ['_'] (\xef\xbe\x9f\xce\xb5\xef\xbe\x9f+" // (゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+
    "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)[\xef\xbe\x9fo\xef\xbe\x9f]+ "; // (゚Д゚)[゚o゚]+

  apr_status_t err;
  apr_size_t buf_len = 0;
  unsigned char* ptr;
  unsigned char* buf = 0;
  unsigned char s[16];

  err = apr_brigade_pflatten(bb, (char**)&buf, &buf_len, f->r->pool);
  if (err) return err;
  apr_brigade_cleanup(bb);

  PUT(f, bb, head);

  ptr = buf;
  while(ptr - buf <= buf_len) {
    unsigned int i, l = utf_ptr2len(ptr);
    unsigned int c = utf_ptr2char(ptr);
    if (l == 0 || c == 0) break;

    PUT(f, bb, "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)[\xef\xbe\x9f\xce\xb5\xef\xbe\x9f]+"); // (゚Д゚)[゚ε゚]+
    if (c <= 127) {
      sprintf(s, "%o", c);
      for (i = 0; i < strlen(s); i++) {
        PUT(f, bb, aa[CTOI(s[i])]);
        PUT(f, bb, "+ ");
      }
    } else {
      PUT(f, bb, "(o\xef\xbe\x9f\xef\xbd\xb0\xef\xbe\x9fo)+ "); // (o゚ー゚o)+
      sprintf(s, "%04x", c);
      for (i = 0; i < strlen(s); i++) {
        PUT(f, bb, aa[CTOI(s[i])]);
        PUT(f, bb, "+ ");
      }
    }
    ptr += l;
  }
  PUT(f, bb, "(\xef\xbe\x9f\xd0\x94\xef\xbe\x9f)[\xef\xbe\x9fo\xef\xbe\x9f]) (\xef\xbe\x9f\xce\x98\xef\xbe\x9f)) ('_');"); // (゚Д゚)[゚o゚]) (゚Θ゚)) ('_');
  APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_eos_create(f->c->bucket_alloc));

  //ap_remove_output_filter(f);
  return ap_pass_brigade(f->next, bb);
}

static void
aaencode_insert_output_filter(request_rec* r) {
  ap_add_output_filter("AAENCODE", NULL, r, r->connection);
}

static void
aaencode_register_hooks(apr_pool_t *p) {
  ap_register_output_filter("AAENCODE", aaencode_output_filter, NULL, AP_FTYPE_CONTENT_SET);
}

module AP_MODULE_DECLARE_DATA aaencode_module = {
  STANDARD20_MODULE_STUFF,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  aaencode_register_hooks
};
一応リポジトリのリンクも貼っておきますね。
mattn/mod_aaencode - GitHub

apache filter module encoding to japanese style emoticons.

https://github.com/mattn/mod_aaencode
コンパイルは
apxs2 -i -a -c -Wc,-O2 mod_aaencode.c -lapr-1 -laprutil-1
Windowsなら
apxs2 -i -a -c -Wc,-O2 mod_aaencode.c libapr-1.lib libaprutil-1.lib libhttpd.lib
でインストールまでやってくれます。apacheを再起動すれば準備完了。
使い方は .htaccess に
AddOutputFilter AAENCODE .js
と書くだけ。これで例えば
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <script type="text/javascript" src="hello.js"></script>
</head>
<body>
  はろーわーるど
</body>
</html>
こんなソースがあって、「ほほーん。jsのソースはhello.jsか」と解析する人がいたとします。で、ブラウザのURL欄に直接 hello.js を足して中身を閲覧しようとする訳です。

だがしかし!
mod_aaencode
えっ!?
となる想定。
見た方はビックリするか、はたまた和むか...

ちなみにjqueryを食わすと結果が11メガバイトになったり、30秒近くレスポンスが返ってこないのはもちろん仕様です

ただ最近、aadecodeなんで物を作った人がいて、こちらもそろそろ難読化レベルが下がってきているので、そろそろ新しい難読化技術を公開して欲しいですね!はせがわさん

Javascript ハンドブック (Next Generation Web Style) Javascript ハンドブック (Next Generation Web Style)
清野 克行
ソフトバンククリエイティブ / ¥ 2,499 (2010-12-17)
 
発送可能時間:在庫あり。

Posted at 03:10 in ソフトウェア::lang::c
Tagged as: aaencode, apache, c, mod_aaencode
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip