Compress a byte array known to contain JSON. Note that we don't do any compression beyond 07 because old firmware doesn't support it.
(normal []byte)
| 139 | // Compress a byte array known to contain JSON. Note that we don't do any compression |
| 140 | // beyond 07 because old firmware doesn't support it. |
| 141 | func jsonCompress(normal []byte) (compressed []byte, err error) { |
| 142 | // Begin generating output by using header byte specifying what kind of compression. |
| 143 | // If it becomes advantageous to do so, this can be determined dynamically based upon |
| 144 | // the compressability of the data using different algorithms. |
| 145 | compressed = append([]byte{}, jcCurrent) |
| 146 | |
| 147 | // No compression |
| 148 | if jcCurrent == jc0 { |
| 149 | |
| 150 | compressed = append(compressed, normal...) |
| 151 | |
| 152 | // Debug |
| 153 | if debugCompress { |
| 154 | logDebug(context.Background(), "JSON no compression (%d)", len(normal)+1) |
| 155 | } |
| 156 | |
| 157 | return |
| 158 | |
| 159 | } |
| 160 | |
| 161 | // Generate the compression length table for jc3 |
| 162 | from1 := []byte("") |
| 163 | from2 := []byte("") |
| 164 | substTable := []byte{} |
| 165 | if jcCurrent == jc3 { |
| 166 | from1 = scanForJSONValue(normal, "w") |
| 167 | from2 = scanForJSONValue(normal, "l") |
| 168 | substTable = make([]byte, 1+(2*2)) // Number of "from/to" entries, plus fromlen/tolen for each |
| 169 | substTable[0] = 2 |
| 170 | substTable[1] = byte(len(from1)) |
| 171 | substTable[2] = byte(len(to1)) |
| 172 | substTable[3] = byte(len(from2)) |
| 173 | substTable[4] = byte(len(to2)) |
| 174 | substTable = append(substTable, from1...) |
| 175 | substTable = append(substTable, to1...) |
| 176 | substTable = append(substTable, from2...) |
| 177 | substTable = append(substTable, to2...) |
| 178 | } |
| 179 | |
| 180 | // Do the substitutions |
| 181 | str := string(normal) |
| 182 | if jcCurrent == jc3 { |
| 183 | if len(from1) > 0 { |
| 184 | str = strings.Replace(str, string(from1), to1, -1) |
| 185 | } |
| 186 | if len(from2) > 0 { |
| 187 | str = strings.Replace(str, string(from2), to2, -1) |
| 188 | } |
| 189 | } |
| 190 | str = strings.Replace(str, from3, to3, -1) |
| 191 | str = strings.Replace(str, from4, to4, -1) |
| 192 | str = strings.Replace(str, from5, to5, -1) |
| 193 | str = strings.Replace(str, from6, to6, -1) |
| 194 | str = strings.Replace(str, from7, to7, -1) |
| 195 | jcompressed := []byte(str) |
| 196 | |
| 197 | // Now that the other replacements have been done, insert the subst table at the front |
| 198 | if jcCurrent == jc3 { |
no test coverage detected