static int mac_put_params(gx_device *dev, gs_param_list *plist) { gx_device_macos *mdev = (gx_device_macos *)dev; int isOpen = mdev->is_open; int code; int depth; gs_param_string outputFile; // Get the BitsPerPixel Parameter code = param_read_int(plist, "BitsPerPixel", &depth); if (!code) { code = mac_set_colordepth(dev, depth); if (code) param_return_error(plist, "BitsPerPixel", gs_error_rangecheck); } // Get OutputFile code = param_read_string(plist, "OutputFile", &outputFile); if (code < 0) { param_signal_error(plist, "OutputFile", code); return code; } else if (code == 0) { if (dev->LockSafetyParams && bytes_compare(outputFile.data, outputFile.size, (const byte *)mdev->outputFileName, strlen(mdev->outputFileName))) { param_signal_error(plist, "OutputFile", gs_error_invalidaccess); return gs_error_invalidaccess; } if (outputFile.size > (gp_file_name_sizeof - 1)) { param_signal_error(plist, "OutputFile", gs_error_limitcheck); return gs_error_limitcheck; } /* If filename changed, close file. */ if (outputFile.data != 0 && bytes_compare(outputFile.data, outputFile.size, (const byte *)mdev->outputFileName, strlen(mdev->outputFileName))) { /* Close the file if it's open. */ if (mdev->outputFile != NULL) { gx_device_close_output_file(dev, mdev->outputFileName, mdev->outputFile); memcpy(mdev->outputFileName, outputFile.data, outputFile.size); mdev->outputFileName[outputFile.size] = 0; gx_device_open_output_file(dev, mdev->outputFileName, true, true, &(mdev->outputFile)); } else { memcpy(mdev->outputFileName, outputFile.data, outputFile.size); mdev->outputFileName[outputFile.size] = 0; } } } // Get the Default Parameters mdev->is_open = 0; code = gx_default_put_params( dev, plist ); mdev->is_open = isOpen; return code; }
/* Get the current values of a parameter set to the stack. */ static bool pname_matches(const char *pname, const ref * psref) { return (psref == 0 || !bytes_compare((const byte *)pname, strlen(pname), psref->value.const_bytes, r_size(psref))); }
static int _mtbl_sorter_compare(const void *va, const void *vb) { const struct entry *a = *((const struct entry **) va); const struct entry *b = *((const struct entry **) vb); return (bytes_compare(entry_key(a), a->len_key, entry_key(b), b->len_key)); }
void block_iter_seek(struct block_iter *bi, const uint8_t *target, size_t target_len) { /* binary search in restart array to find the first restart point * with a key >= target */ uint32_t left = 0; uint32_t right = bi->num_restarts - 1; while (left < right) { uint32_t mid = (left + right + 1) / 2; uint32_t region_offset = get_restart_point(bi, mid); uint32_t shared, non_shared, value_length; const uint8_t *key_ptr = decode_entry(bi->data + region_offset, bi->data + bi->restarts, &shared, &non_shared, &value_length); if (key_ptr == NULL || (shared != 0)) { /* corruption */ return; } if (bytes_compare(key_ptr, non_shared, target, target_len) < 0) { /* key at "mid" is smaller than "target", therefore all * keys before "mid" are uninteresting */ left = mid; } else { /* key at "mid" is larger than "target", therefore all * keys at or before "mid" are uninteresting */ right = mid - 1; } } /* linear search within restart block for first key >= target */ seek_to_restart_point(bi, left); for (;;) { if (!parse_next_key(bi)) return; if (bytes_compare(ubuf_data(bi->key), ubuf_size(bi->key), target, target_len) >= 0) { return; } } }
int tiff_compression_id(uint16 *id, gs_param_string *param) { struct compression_string *c; for (c = compression_strings; c->str; c++) if (!bytes_compare(param->data, param->size, (const byte *)c->str, strlen(c->str))) { *id = c->id; return 0; } return gs_error_undefined; }
/* * Get the value for a scanner option. * return -1 if no such option, 1/0 for on/off and option's name in *pname as a C string */ int ztoken_get_scanner_option(const ref *psref, int options, const char **pname) { const named_scanner_option_t *pnso; for (pnso = named_options + countof(named_options); pnso-- != named_options;) { if (!bytes_compare((const byte *)pnso->pname, strlen(pnso->pname), psref->value.const_bytes, r_size(psref))) { *pname = pnso->pname; return (options & pnso->option ? 1 : 0); } } return -1; }
/* Put parameters. */ static int cgm_put_params(gx_device * dev, gs_param_list * plist) { gx_device_cgm *cdev = (gx_device_cgm *) dev; int ecode = 0; int code; const char *param_name; gs_param_string ofs; switch (code = param_read_string(plist, (param_name = "OutputFile"), &ofs)) { case 0: if (dev->LockSafetyParams && bytes_compare(ofs.data, ofs.size, (const byte *)cdev->fname, strlen(cdev->fname))) { ecode = gs_note_error(gs_error_invalidaccess); goto ofe; } if (ofs.size >= gp_file_name_sizeof) ecode = gs_error_limitcheck; else break; goto ofe; default: ecode = code; ofe:param_signal_error(plist, param_name, ecode); case 1: ofs.data = 0; break; } if (ecode < 0) return ecode; code = gx_default_put_params(dev, plist); if (code < 0) return code; if (ofs.data != 0) { /* Close the file if it's open. */ if (cdev->file != 0) { fclose(cdev->file); cdev->file = 0; } memcpy(cdev->fname, ofs.data, ofs.size); cdev->fname[ofs.size] = 0; cdev->file = fopen(cdev->fname, "wb"); if (cdev->file == 0) return_error(gs_error_ioerror); } return 0; }
/* Return 0 if OK, 1 if not OK, or an error code. */ int param_check_password(gs_param_list * plist, const password * ppass) { if (ppass->size != 0) { password pass; int code = param_read_password(plist, "Password", &pass); if (code) return code; if (pass.size != ppass->size || bytes_compare(&pass.data[0], pass.size, &ppass->data[0], ppass->size) != 0 ) return 1; } return 0; }
int dict_write_password(const password * ppass, ref * pdref, const char *pkey, bool change_allowed) { ref *pvalue; int code = dict_find_password(&pvalue, pdref, pkey); if (code < 0) return code; if (ppass->size >= r_size(pvalue)) return_error(e_rangecheck); if (!change_allowed && bytes_compare(pvalue->value.bytes + 1, pvalue->value.bytes[0], ppass->data, ppass->size) != 0) return_error(e_invalidaccess); memcpy(pvalue->value.bytes + 1, ppass->data, (pvalue->value.bytes[0] = ppass->size)); return 0; }
static void read_message(void *v_i) { struct incoming *i = v_i; struct receiver *r; bool_t done; for (;;) { switch (stream_read_growbuf(i->input, &i->buf, &i->tasklet, &i->err)) { case STREAM_END: goto finished_read; case STREAM_ERROR: die("%s", error_message(&i->err)); case STREAM_WAITING: return; } } finished_read: if (bytes_compare(c_string_bytes("Hello"), growbuf_to_bytes(&i->buf))) die("Message did not contain expected data"); r = i->receiver; stream_destroy(i->input); error_fini(&i->err); growbuf_fini(&i->buf); tasklet_fini(&i->tasklet); mutex_unlock_fini(&i->mutex); free(i); mutex_lock(&r->mutex); done = (++r->received == MESSAGES); mutex_unlock(&r->mutex); if (done) application_stop(); }
bool obj_eq(const gs_memory_t *mem, const ref * pref1, const ref * pref2) { ref nref; if (r_type(pref1) != r_type(pref2)) { /* * Only a few cases need be considered here: * integer/real (and vice versa), name/string (and vice versa), * arrays, and extended operators. */ switch (r_type(pref1)) { case t_integer: return (r_has_type(pref2, t_real) && pref2->value.realval == pref1->value.intval); case t_real: return (r_has_type(pref2, t_integer) && pref2->value.intval == pref1->value.realval); case t_name: if (!r_has_type(pref2, t_string)) return false; name_string_ref(mem, pref1, &nref); pref1 = &nref; break; case t_string: if (!r_has_type(pref2, t_name)) return false; name_string_ref(mem, pref2, &nref); pref2 = &nref; break; /* * Differing implementations of packedarray can be eq, * if the length is zero, but an array is never eq to a * packedarray. */ case t_mixedarray: case t_shortarray: /* * Since r_type(pref1) is one of the above, this is a * clever fast check for r_type(pref2) being the other. */ return ((int)r_type(pref1) + (int)r_type(pref2) == t_mixedarray + t_shortarray) && r_size(pref1) == 0 && r_size(pref2) == 0; default: if (r_btype(pref1) != r_btype(pref2)) return false; } } /* * Now do a type-dependent comparison. This would be very simple if we * always filled in all the bytes of a ref, but we currently don't. */ switch (r_btype(pref1)) { case t_array: return ((pref1->value.refs == pref2->value.refs || r_size(pref1) == 0) && r_size(pref1) == r_size(pref2)); case t_mixedarray: case t_shortarray: return ((pref1->value.packed == pref2->value.packed || r_size(pref1) == 0) && r_size(pref1) == r_size(pref2)); case t_boolean: return (pref1->value.boolval == pref2->value.boolval); case t_dictionary: return (pref1->value.pdict == pref2->value.pdict); case t_file: return (pref1->value.pfile == pref2->value.pfile && r_size(pref1) == r_size(pref2)); case t_integer: return (pref1->value.intval == pref2->value.intval); case t_mark: case t_null: return true; case t_name: return (pref1->value.pname == pref2->value.pname); case t_oparray: case t_operator: return (op_index(pref1) == op_index(pref2)); case t_real: return (pref1->value.realval == pref2->value.realval); case t_save: return (pref2->value.saveid == pref1->value.saveid); case t_string: return (!bytes_compare(pref1->value.bytes, r_size(pref1), pref2->value.bytes, r_size(pref2))); case t_device: return (pref1->value.pdevice == pref2->value.pdevice); case t_struct: case t_astruct: return (pref1->value.pstruct == pref2->value.pstruct); case t_fontID: /* This is complicated enough to deserve a separate procedure. */ return fid_eq(mem, r_ptr(pref1, gs_font), r_ptr(pref2, gs_font)); } return false; /* shouldn't happen! */ }
static void test_iter(struct mtbl_iter *iter) { /* Iterate completely through the mtbl */ for (uint32_t i = 0; i < NUM_KEYS; i++) { ubuf *expected_key = ubuf_init(1); ubuf_add_fmt(expected_key, KEY_FMT, i); const uint8_t *key, *value; size_t len_key, len_value; assert(mtbl_iter_next(iter, &key, &len_key, &value, &len_value) == mtbl_res_success); assert(bytes_compare(ubuf_data(expected_key), ubuf_size(expected_key), key, len_key) == 0); ubuf_destroy(&expected_key); } /* Ensure that we have completely iterated through the set. */ { const uint8_t *key, *value; size_t len_key, len_value; assert(mtbl_iter_next(iter, &key, &len_key, &value, &len_value) == mtbl_res_failure); } /* Seek to a key, ensure that we get the one we want and that we go * all the way to the end. */ for (uint32_t i = NUM_KEYS; i-- > 0; ) { ubuf *seek_key = ubuf_init(1); ubuf_add_fmt(seek_key, KEY_FMT, i); const uint8_t *key, *value; size_t len_key, len_value; assert(mtbl_iter_seek(iter, ubuf_data(seek_key), ubuf_size(seek_key)) == mtbl_res_success); for (uint32_t j = i; j < NUM_KEYS; j++) { ubuf *expected_key = ubuf_init(1); ubuf_add_fmt(expected_key, KEY_FMT, j); assert(mtbl_iter_next(iter, &key, &len_key, &value, &len_value) == mtbl_res_success); assert(bytes_compare(ubuf_data(expected_key), ubuf_size(expected_key), key, len_key) == 0); ubuf_destroy(&expected_key); } assert(mtbl_iter_next(iter, &key, &len_key, &value, &len_value) == mtbl_res_failure); ubuf_destroy(&seek_key); } /* Attempt to seek past end of iterator */ ubuf *seek_key = ubuf_init(1); const uint8_t *key, *value; size_t len_key, len_value; ubuf_add_fmt(seek_key, KEY_FMT, NUM_KEYS + 1); assert(mtbl_iter_seek(iter, ubuf_data(seek_key), ubuf_size(seek_key)) == mtbl_res_success); assert(mtbl_iter_next(iter, &key, &len_key, &value, &len_value) == mtbl_res_failure); }