Technetra

CodeMagic: serialize - an approach to serializing and de-serializing arbitrary data entirely within bash

Robert Adkins,  November 9th, 2008 at 10:02 pm

The program serialize demonstrates an approach to serializing and de-serializing arbitrary data entirely within the native facilities of bash. Performance, however, is sufficiently slow that this approach is infeasible for anything but small datasets.

After making it executable, a simple way to run the script is by invoking:

./serialize.sh

serialize then runs a small self-test harness. The test includes a timing cycle of deflating and re-inflating the contents of the file holding the script itself as well as serializing the entire ASCII sequence except for x’00′. Although performance is painfully slow, this demonstration exposes scripting techniques for sophisticated data handling including advanced array manipulation, as well as binary and hex string processing. Cache effects are explored in a second, related script named serialize_with_cache.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
#
# serialize demonstrates an approach to serializing and de-serializing
# arbitrary data entirely within the native facilities of bash.
# Performance, however, is sufficiently slow that this approach is
# infeasible for anything but small datasets.
#
# A simple way to run the script is by invoking:
#
#     ./serialize.sh 
#
# serialize then runs a small self-test harness. The test includes a
# timing cycle of deflating and re-inflating the contents of the file
# holding the script itself as well as serializing the entire ASCII
# sequence except for x'00'. Although performance is painfully slow,
# the demonstration exposes scripting techniques for sophisticated data
# handling including advanced array manipulation, as well as binary
# and hex string processing. Cache effects are explored in a second,
# related script named serialize_with_cache.
# 
# Copyright (c) 2008 Technetra Corp
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
PASSED=0
TOTAL=0
find_char() {
  local hex_result=$1
  local in_char=$2
  local hex_digits1 hex_digits2=(0 1 2 3 4 5 6 7 8 9 A B C D E F)
  if [[ "$in_char" > $'\x6f' ]]; then
    hex_digits1=(7 8 9 A B C D E F)
  elif [[ "$in_char" > $'\x5f' ]]; then
    hex_digits1=(6)
  elif [[ "$in_char" > $'\x4f' ]]; then
    hex_digits1=(5)
  elif [[ "$in_char" > $'\x3f' ]]; then
    hex_digits1=(4)
  elif [[ "$in_char" > $'\x2f' ]]; then
    hex_digits1=(3)
  elif [[ "$in_char" > $'\x1f' ]]; then
    hex_digits1=(2)
  else
    hex_digits1=(0 1)
  fi
  local finished=0
  local c t targ hxi hxj
  for hxi in ${hex_digits1[@]}; do
    for hxj in ${hex_digits2[@]}; do
      c=$hxi$hxj
      #t="`eval echo z\$\'\\\x$c\'z`"
      t="`echo -e "z\x${c}z"`"
      targ=${t:1:1}
      if [[ "$in_char" == "$targ" ]]; then
        finished=1
        break
      fi
    done
    [[ $finished -eq 1 ]] && break
  done
  eval $hex_result=$c
}
deflate() {
  local result=$1
  local s="$2"
  local i=0 deflated out_hex
  local c="${s:i++:1}"
  while [[ "$c" ]]; do
    find_char out_hex "$c"
    deflated=$deflated$out_hex
    c="${s:i++:1}"
  done 
  eval $result=$deflated
}
inflate() {
  local result=$1
  local hex_string=$2 
  local i=0 inflated t targ
  local h=${hex_string:i:2}
  i=$((i+2))
  while [[ "$h" ]]; do
    #t="`eval echo z\$\'\\\x$h\'z`"
    t="`echo -e "z\x${h}z"`"
    targ=${t:1:1}
    [[ "$targ" == '"' ]] && targ='\"'
    inflated="$inflated$targ"
    h=${hex_string:i:2}
    i=$((i+2))
  done
  eval "$result=\"$inflated\""
}
test_full_hex_table() {
  local hex_string=$1 
  local i=0 inflated t hex stat=failed
  local h=${hex_string:i:2}
  i=$((i+2))
  while [[ "$h" ]]; do
    t="`echo -e "z\x${h}z"`"
    inflated="$inflated${t:1:1}"
    h=${hex_string:i:2}
    i=$((i+2))
  done
  deflate hex "$inflated"
  if [[ "$hex_string" == "$hex" ]]; then
    stat=passed
    ((PASSED++))
  fi
  ((TOTAL++))
  echo -n "$stat test $TOTAL, hex=$hex, original=\"$(echo $inflated | cat -v)\"\n"
}
test_file_content() {
  local s="$1"
  local i=0 deflated out_hex inflated t hex stat=failed
  local c="${s:i++:1}"
  while [[ "$c" ]]; do
    find_char out_hex "$c"
    deflated=$deflated$out_hex
    c="${s:i++:1}"
  done 
  i=0
  local h=${deflated:i:2}
  i=$((i+2))
  while [[ "$h" ]]; do
    t="`echo -e "z\x${h}z"`"
    inflated="$inflated${t:1:1}"
    h=${deflated:i:2}
    i=$((i+2))
  done
  deflate hex "$inflated"
  if [[ "$deflated" == "$hex" ]]; then
    stat=passed
    ((PASSED++))
  fi
  ((TOTAL++))
  echo "$stat test $TOTAL, hex=$hex, original=\"$s\""
}
test_serialize () {
  local in_orig="$1"
  local stat hex copy
  deflate hex "$in_orig"
  inflate copy $hex
  if [[ "$in_orig" == "$copy" ]]; then
    stat=passed
    ((PASSED++))
  else
    stat=failed
  fi
  ((TOTAL++))
  echo "$stat test $TOTAL, hex=$hex, original=\"$in_orig\""
}
time_file () {
  local file=$1
  echo "timing serialization on file $file, may take several minutes..."
  time test_file_content "$(cat $file)"
}
time_full_hex_range () {
  local hex_digits=(0 1 2 3 4 5 6 7 8 9 A B C D E F)
  local copy hstr
  for hxi in ${hex_digits[@]}; do
    for hxj in ${hex_digits[@]}; do
      [[ $hxi == "0" && $hxj == "0" ]] || hstr=$hstr$hxi$hxj
    done
  done
  echo "timing serialization of fully populated (except NULL) ASCII table..."
  time test_full_hex_table $hstr
}
###
### MAIN
###
test_serialize "a\"c"
test_serialize "a\'c"
test_serialize "$(echo -e " a\n\tb")"
test_serialize "'%' in a prerequisite of a pattern rule stands for the same stem that was matched by the"
time_file $0
time_file /etc/passwd
time_full_hex_range
echo "TEST SUMMARY: passed $PASSED of $TOTAL tests"
# end serialize

Copyright © 2008 Technetra. This code is covered by the MIT license. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pings are currently closed.

CodeMagic: serialize_with_cache - an approach to serializing and de-serializing arbitrary data in bash using an MRU cache Article Index CodeMagic: bashtail - a script version of the Unix tail command

Comments

Be the first to post a comment.

Add a comment

Leave a comment or send a note
  1. (required)
  2. (valid email required)
  3. (required)
  4. Send
  5. Captcha
 

cforms contact form by delicious:days

© 2000-2009 Technetra. All rights reserved. Contact | Terms of Use

WordPress