Module: Snappy
- Defined in:
- lib/snappy.rb,
ext/snappy/snappy.c
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
-
.compress(*args) ⇒ String
Compress a string using Snappy compression.
-
.compress_file(*args) ⇒ Object
Compress a file using Snappy compression.
-
.decompress(data) ⇒ String
Decompress Snappy compressed data.
-
.decompress_file(input_path, output_path) ⇒ Object
Decompress a Snappy compressed file.
Class Method Details
.compress(*args) ⇒ String
Compress a string using Snappy compression
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 |
# File 'ext/snappy/snappy.c', line 22 static VALUE rb_snappy_compress(int argc, VALUE *argv, VALUE self) { VALUE data, ; int level = DEFAULT_COMPRESSION_LEVEL; rb_scan_args(argc, argv, "11", &data, &); // Validate input Check_Type(data, T_STRING); // Parse if (!NIL_P()) { Check_Type(, T_HASH); VALUE level_val = rb_hash_aref(, ID2SYM(rb_intern("level"))); if (!NIL_P(level_val)) { level = NUM2INT(level_val); if (level < MIN_COMPRESSION_LEVEL || level > MAX_COMPRESSION_LEVEL) { rb_raise(rb_eArgError, "compression level must be between %d and %d", MIN_COMPRESSION_LEVEL, MAX_COMPRESSION_LEVEL); } } } const char *input = RSTRING_PTR(data); size_t input_length = RSTRING_LEN(data); // Get maximum compressed length size_t max_compressed_length = snappy_max_compressed_length(input_length); // Allocate buffer for compressed data char *compressed = ALLOC_N(char, max_compressed_length); size_t compressed_length = max_compressed_length; // Perform compression snappy_status status = snappy_compress(input, input_length, compressed, &compressed_length); if (status != SNAPPY_OK) { xfree(compressed); rb_raise(rb_eSnappyError, "compression failed with status: %d", status); } // Create Ruby string with compressed data VALUE result = rb_str_new(compressed, compressed_length); xfree(compressed); return result; } |
.compress_file(*args) ⇒ Object
Compress a file using Snappy compression
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 198 199 200 201 |
# File 'ext/snappy/snappy.c', line 126 static VALUE rb_snappy_compress_file(int argc, VALUE *argv, VALUE self) { VALUE input_path, output_path, ; int level = DEFAULT_COMPRESSION_LEVEL; rb_scan_args(argc, argv, "21", &input_path, &output_path, &); Check_Type(input_path, T_STRING); Check_Type(output_path, T_STRING); // Parse if (!NIL_P()) { Check_Type(, T_HASH); VALUE level_val = rb_hash_aref(, ID2SYM(rb_intern("level"))); if (!NIL_P(level_val)) { level = NUM2INT(level_val); if (level < MIN_COMPRESSION_LEVEL || level > MAX_COMPRESSION_LEVEL) { rb_raise(rb_eArgError, "compression level must be between %d and %d", MIN_COMPRESSION_LEVEL, MAX_COMPRESSION_LEVEL); } } } const char *input_file = StringValueCStr(input_path); const char *output_file = StringValueCStr(output_path); // Open input file FILE *in = fopen(input_file, "rb"); if (!in) { rb_sys_fail(input_file); } // Get file size fseek(in, 0, SEEK_END); long file_size = ftell(in); fseek(in, 0, SEEK_SET); // Read file contents char *input_data = ALLOC_N(char, file_size); size_t bytes_read = fread(input_data, 1, file_size, in); fclose(in); if (bytes_read != file_size) { xfree(input_data); rb_raise(rb_eIOError, "failed to read complete file"); } // Compress data size_t max_compressed_length = snappy_max_compressed_length(file_size); char *compressed = ALLOC_N(char, max_compressed_length); size_t compressed_length = max_compressed_length; snappy_status status = snappy_compress(input_data, file_size, compressed, &compressed_length); xfree(input_data); if (status != SNAPPY_OK) { xfree(compressed); rb_raise(rb_eSnappyError, "compression failed with status: %d", status); } // Write compressed data to output file FILE *out = fopen(output_file, "wb"); if (!out) { xfree(compressed); rb_sys_fail(output_file); } size_t bytes_written = fwrite(compressed, 1, compressed_length, out); fclose(out); xfree(compressed); if (bytes_written != compressed_length) { rb_raise(rb_eIOError, "failed to write complete compressed data"); } return Qnil; } |
.decompress(data) ⇒ String
Decompress Snappy compressed data
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 |
# File 'ext/snappy/snappy.c', line 75 static VALUE rb_snappy_decompress(VALUE self, VALUE data) { Check_Type(data, T_STRING); const char *compressed = RSTRING_PTR(data); size_t compressed_length = RSTRING_LEN(data); // Get uncompressed length size_t uncompressed_length; snappy_status status = snappy_uncompressed_length(compressed, compressed_length, &uncompressed_length); if (status != SNAPPY_OK) { rb_raise(rb_eSnappyError, "failed to get uncompressed length, status: %d", status); } // Allocate buffer for decompressed data char *uncompressed = ALLOC_N(char, uncompressed_length); // Perform decompression status = snappy_uncompress(compressed, compressed_length, uncompressed, &uncompressed_length); if (status != SNAPPY_OK) { xfree(uncompressed); rb_raise(rb_eSnappyError, "decompression failed with status: %d", status); } // Create Ruby string with decompressed data VALUE result = rb_str_new(uncompressed, uncompressed_length); xfree(uncompressed); // Try to set UTF-8 encoding if the data is valid UTF-8 // Otherwise, keep it as binary (ASCII-8BIT) int utf8_index = rb_utf8_encindex(); rb_enc_associate_index(result, utf8_index); // Check if the string is valid in the UTF-8 encoding // If not, fall back to binary encoding if (rb_enc_str_coderange(result) == ENC_CODERANGE_BROKEN) { rb_enc_associate_index(result, rb_ascii8bit_encindex()); } return result; } |
.decompress_file(input_path, output_path) ⇒ Object
Decompress a Snappy compressed file
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/snappy/snappy.c', line 209 static VALUE rb_snappy_decompress_file(VALUE self, VALUE input_path, VALUE output_path) { Check_Type(input_path, T_STRING); Check_Type(output_path, T_STRING); const char *input_file = StringValueCStr(input_path); const char *output_file = StringValueCStr(output_path); // Open input file FILE *in = fopen(input_file, "rb"); if (!in) { rb_sys_fail(input_file); } // Get file size fseek(in, 0, SEEK_END); long file_size = ftell(in); fseek(in, 0, SEEK_SET); // Read compressed file contents char *compressed_data = ALLOC_N(char, file_size); size_t bytes_read = fread(compressed_data, 1, file_size, in); fclose(in); if (bytes_read != file_size) { xfree(compressed_data); rb_raise(rb_eIOError, "failed to read complete file"); } // Get uncompressed length size_t uncompressed_length; snappy_status status = snappy_uncompressed_length(compressed_data, file_size, &uncompressed_length); if (status != SNAPPY_OK) { xfree(compressed_data); rb_raise(rb_eSnappyError, "failed to get uncompressed length, status: %d", status); } // Decompress data char *uncompressed = ALLOC_N(char, uncompressed_length); status = snappy_uncompress(compressed_data, file_size, uncompressed, &uncompressed_length); xfree(compressed_data); if (status != SNAPPY_OK) { xfree(uncompressed); rb_raise(rb_eSnappyError, "decompression failed with status: %d", status); } // Write decompressed data to output file FILE *out = fopen(output_file, "wb"); if (!out) { xfree(uncompressed); rb_sys_fail(output_file); } size_t bytes_written = fwrite(uncompressed, 1, uncompressed_length, out); fclose(out); xfree(uncompressed); if (bytes_written != uncompressed_length) { rb_raise(rb_eIOError, "failed to write complete decompressed data"); } return Qnil; } |