Beispiel #1
0
int SetupTMDInfoRecord(tmd_content_info_record *info_record, u8 *content_record, u16 ContentCount)
{
	memset(info_record,0x0,sizeof(tmd_content_info_record)*0x40);
	u16_to_u8(info_record->contentIndexOffset,0x0,BE);
	u16_to_u8(info_record->contentCommandCount,ContentCount,BE);
	ctr_sha(content_record,sizeof(tmd_content_chunk)*ContentCount,info_record->contentChunkHash,CTR_SHA_256);
	return 0;
}
Beispiel #2
0
static int
check (const uint16_t *input, size_t input_length,
       const uint8_t *expected, size_t expected_length)
{
  size_t length;
  uint8_t *result;

  /* Test return conventions with resultbuf == NULL.  */
  result = u16_to_u8 (input, input_length, NULL, &length);
  if (!(result != NULL))
    return 1;
  if (!(length == expected_length))
    return 2;
  if (!(u8_cmp (result, expected, expected_length) == 0))
    return 3;
  free (result);

  /* Test return conventions with resultbuf too small.  */
  if (expected_length > 0)
    {
      uint8_t *preallocated;

      length = expected_length - 1;
      preallocated = (uint8_t *) malloc (length * sizeof (uint8_t));
      result = u16_to_u8 (input, input_length, preallocated, &length);
      if (!(result != NULL))
        return 4;
      if (!(result != preallocated))
        return 5;
      if (!(length == expected_length))
        return 6;
      if (!(u8_cmp (result, expected, expected_length) == 0))
        return 7;
      free (result);
      free (preallocated);
    }

  /* Test return conventions with resultbuf large enough.  */
  {
    uint8_t *preallocated;

    length = expected_length;
    preallocated = (uint8_t *) malloc (length * sizeof (uint8_t));
    result = u16_to_u8 (input, input_length, preallocated, &length);
    if (!(result != NULL))
      return 8;
    if (!(preallocated == NULL || result == preallocated))
      return 9;
    if (!(length == expected_length))
      return 10;
    if (!(u8_cmp (result, expected, expected_length) == 0))
      return 11;
    free (preallocated);
  }

  return 0;
}
Beispiel #3
0
int SetupTMDContentRecord(u8 *content_record, cia_settings *ciaset)
{
	for(int i = 0; i < ciaset->content.count; i++){
		tmd_content_chunk *ptr = (tmd_content_chunk*)(content_record+sizeof(tmd_content_chunk)*i);
		u32_to_u8(ptr->contentID,ciaset->content.id[i],BE);
		u16_to_u8(ptr->contentIndex,ciaset->content.index[i],BE);
		u16_to_u8(ptr->contentFlags,ciaset->content.flags[i],BE);
		u64_to_u8(ptr->contentSize,ciaset->content.size[i],BE);
		memcpy(ptr->contentHash,ciaset->content.hash[i],0x20);
	}
	return 0;
}
Beispiel #4
0
int SetupTMDHeader(tmd_hdr *hdr, tmd_content_info_record *info_record, cia_settings *ciaset)
{
	memset(hdr,0,sizeof(tmd_hdr));

	memcpy(hdr->issuer,ciaset->tmd.issuer,0x40);
	hdr->formatVersion = ciaset->tmd.formatVersion;
	hdr->caCrlVersion = ciaset->cert.caCrlVersion;
	hdr->signerCrlVersion = ciaset->cert.signerCrlVersion;
	memcpy(hdr->titleID,ciaset->common.titleId,8);
	memcpy(hdr->titleType,ciaset->tmd.titleType,4);
	memcpy(hdr->savedataSize,ciaset->tmd.savedataSize,4);
	memcpy(hdr->privSavedataSize,ciaset->tmd.privSavedataSize,4);
	hdr->twlFlag = ciaset->tmd.twlFlag;
	u16_to_u8(hdr->titleVersion,ciaset->tmd.version,BE);
	u16_to_u8(hdr->contentCount,ciaset->content.count,BE);
	ctr_sha(info_record,sizeof(tmd_content_info_record)*64,hdr->infoRecordHash,CTR_SHA_256);
	return 0;
}
Beispiel #5
0
int SetCardInfoNotes(cardinfo_hdr *hdr, cci_settings *set)
{
	u64_to_u8(hdr->notes.mediaSizeUsed,set->romInfo.usedSize,LE);
	u32_to_u8(hdr->notes.unknown,0,LE);
	
	if(set->options.tmdHdr){
		u64_to_u8(hdr->notes.cverTitleId,GetTmdTitleId(set->options.tmdHdr),LE);
		u16_to_u8(hdr->notes.cverTitleId,GetTmdVersion(set->options.tmdHdr),LE);
	}
		
	return 0;
}
Beispiel #6
0
static char *
print_chars(const widechar *buffer, int length) {
	static uint8_t result_buf[BUFSIZE];
	size_t result_len = BUFSIZE - 1;
#ifdef WIDECHARS_ARE_UCS4
	u32_to_u8(buffer, length, result_buf, &result_len);
#else
	u16_to_u8(buffer, length, result_buf, &result_len);
#endif
	result_buf[result_len] = 0;
	return result_buf;
}
Beispiel #7
0
static void
print_widechars(widechar *buffer, int length) {
	uint8_t *result_buf;
	size_t result_len;

#ifdef WIDECHARS_ARE_UCS4
	result_buf = u32_to_u8(buffer, length, NULL, &result_len);
#else
	result_buf = u16_to_u8(buffer, length, NULL, &result_len);
#endif
	fprintf(stderr, "%.*s", (int)result_len, result_buf);
	free(result_buf);
}
Beispiel #8
0
/**
 * "asctime", except for GNUnet time.  Converts a GNUnet internal
 * absolute time (which is in UTC) to a string in local time.
 * Note that the returned value will be overwritten if this function
 * is called again.
 *
 * @param t the absolute time to convert
 * @return timestamp in human-readable form in local time
 */
const char *
GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
{
  static char buf[255];
  time_t tt;
  struct tm *tp;

  if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
    return _("end of time");
  tt = t.abs_value_us / 1000LL / 1000LL;
  tp = localtime (&tt);
  /* This is hacky, but i don't know a way to detect libc character encoding.
   * Just expect utf8 from glibc these days.
   * As for msvcrt, use the wide variant, which always returns utf16
   * (otherwise we'd have to detect current codepage or use W32API character
   * set conversion routines to convert to UTF8).
   */
#ifndef WINDOWS
  strftime (buf, sizeof (buf), "%a %b %d %H:%M:%S %Y", tp);
#else
  {
    static wchar_t wbuf[255];
    uint8_t *conved;
    size_t ssize;

    wcsftime (wbuf, sizeof (wbuf) / sizeof (wchar_t),
        L"%a %b %d %H:%M:%S %Y", tp);

    ssize = sizeof (buf);
    conved = u16_to_u8 (wbuf, sizeof (wbuf) / sizeof (wchar_t),
        (uint8_t *) buf, &ssize);
    if (conved != (uint8_t *) buf)
    {
      strncpy (buf, (char *) conved, sizeof (buf));
      buf[255 - 1] = '\0';
      free (conved);
    }
  }
#endif
  return buf;
}
Beispiel #9
0
int
main ()
{
  /* Empty string.  */
  ASSERT (check (NULL, 0, NULL, 0) == 0);

  /* Simple string.  */
  { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
    static const uint16_t input[] =
      { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
        0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
        0x0439, 0x0442, 0x0435, '!', ' ',
        'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
        '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
        0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
      };
    static const uint8_t expected[] =
      { 'G', 'r', 0xC3, 0xBC, 0xC3, 0x9F, ' ', 'G', 'o', 't', 't', '.', ' ',
        0xD0, 0x97, 0xD0, 0xB4, 0xD1, 0x80, 0xD0, 0xB0, 0xD0, 0xB2, 0xD1, 0x81,
        0xD1, 0x82, 0xD0, 0xB2, 0xD1, 0x83, 0xD0, 0xB9, 0xD1, 0x82, 0xD0, 0xB5,
        '!', ' ', 'x', '=', '(', '-', 'b', 0xC2, 0xB1, 's', 'q', 'r', 't', '(',
        'b', 0xC2, 0xB2, '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')',
        ' ', ' ', 0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E, ',',
        0xE4, 0xB8, 0xAD, 0xE6, 0x96, 0x87, ',',
        0xED, 0x95, 0x9C, 0xEA, 0xB8, 0x80, '\n'
      };
    ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
  }

  /* String with characters outside the BMP.  */
  {
    static const uint16_t input[] =
      { '-', '(', 0xD835, 0xDD1E, 0x00D7, 0xD835, 0xDD1F, ')', '=',
        0xD835, 0xDD1F, 0x00D7, 0xD835, 0xDD1E
      };
    static const uint8_t expected[] =
      { '-', '(', 0xF0, 0x9D, 0x94, 0x9E, 0xC3, 0x97, 0xF0, 0x9D, 0x94, 0x9F,
        ')', '=', 0xF0, 0x9D, 0x94, 0x9F, 0xC3, 0x97, 0xF0, 0x9D, 0x94, 0x9E
      };
    ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
  }

  /* Invalid input.  */
  {
    static const uint16_t input[] = { 'x', 0xDD1E, 0xD835, 'y' };
#if 0 /* Currently invalid input is rejected, not accommodated.  */
    static const uint8_t expected[] =
      { 'x', 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 'y' };
    ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
#else
    size_t length;
    uint8_t *result;
    uint8_t preallocated[10];

    /* Test return conventions with resultbuf == NULL.  */
    result = u16_to_u8 (input, SIZEOF (input), NULL, &length);
    ASSERT (result == NULL);
    ASSERT (errno == EILSEQ);

    /* Test return conventions with resultbuf too small.  */
    length = 1;
    result = u16_to_u8 (input, SIZEOF (input), preallocated, &length);
    ASSERT (result == NULL);
    ASSERT (errno == EILSEQ);

    /* Test return conventions with resultbuf large enough.  */
    length = SIZEOF (preallocated);
    result = u16_to_u8 (input, SIZEOF (input), preallocated, &length);
    ASSERT (result == NULL);
    ASSERT (errno == EILSEQ);
#endif
  }

  return 0;
}
Beispiel #10
0
int
check_base(const char *tableList, const char *input, const char *expected,
		optional_test_params in) {

	int i, retval = 0;
	int direction = in.direction;
	const int *expected_inputPos = in.expected_inputPos;
	const int *expected_outputPos = in.expected_outputPos;
	if (in.direction < 0 || in.direction > 2) {
		fprintf(stderr, "Invalid direction.\n");
		return 1;
	}
	if (in.direction != 0 && in.typeform != NULL) {
		// Currently, in backward translation, nothing is done with the initial value of
		// the typeform argument, and on return it always contains all zeros, so it
		// doesn't make any sense to use typeforms in backward translation tests.
		fprintf(stderr, "typeforms only supported with testmode 'forward'\n");
		return 1;
	}
	if (in.direction == 2 && in.cursorPos >= 0) {
		fprintf(stderr, "cursorPos not supported with testmode 'bothDirections'\n");
		return 1;
	}
	if (in.direction == 2 && in.max_outlen >= 0) {
		fprintf(stderr, "maxOutputLength not supported with testmode 'bothDirections'\n");
		return 1;
	}
	if (in.real_inlen >= 0 && in.max_outlen < 0) {
		fprintf(stderr,
				"realInputLength not supported when maxOutputLength is not specified\n");
		return 1;
	}
	while (1) {
		widechar *inbuf, *outbuf, *expectedbuf;
		int inlen = strlen(input);
		int actualInlen;
		const int outlen_multiplier = 4 + sizeof(widechar) * 2;
		int outlen = inlen * outlen_multiplier;
		int expectedlen = strlen(expected);
		int funcStatus = 0;
		formtype *typeformbuf = NULL;
		int *inputPos = NULL;
		int *outputPos = NULL;
		int cursorPos = 0;
		inbuf = malloc(sizeof(widechar) * inlen);
		outbuf = malloc(sizeof(widechar) * outlen);
		expectedbuf = malloc(sizeof(widechar) * expectedlen);
		if (in.typeform != NULL) {
			typeformbuf = malloc(outlen * sizeof(formtype));
			memcpy(typeformbuf, in.typeform, inlen * sizeof(formtype));
		}
		if (in.cursorPos >= 0) {
			cursorPos = in.cursorPos;
		}
		if (in.max_outlen >= 0) {
			outlen = in.max_outlen;
		}
		inlen = _lou_extParseChars(input, inbuf);
		if (!inlen) {
			fprintf(stderr, "Cannot parse input string.\n");
			retval = 1;
			goto fail;
		}
		if (in.real_inlen > inlen) {
			fprintf(stderr,
					"expected realInputLength (%d) may not exceed total input length "
					"(%d)\n",
					in.real_inlen, inlen);
			return 1;
		}
		if (expected_inputPos) {
			inputPos = malloc(sizeof(int) * outlen);
		}
		if (expected_outputPos) {
			outputPos = malloc(sizeof(int) * inlen);
		}
		actualInlen = inlen;
		// Note that this loop is not strictly needed to make the current tests pass, but
		// in the general case it is needed because it is theoretically possible that we
		// provided a too short output buffer.
		for (int k = 1; k <= 3; k++) {
			if (direction == 1) {
				funcStatus = lou_backTranslate(tableList, inbuf, &actualInlen, outbuf,
						&outlen, typeformbuf, NULL, outputPos, inputPos, &cursorPos,
						in.mode);
			} else {
				funcStatus = lou_translate(tableList, inbuf, &actualInlen, outbuf,
						&outlen, typeformbuf, NULL, outputPos, inputPos, &cursorPos,
						in.mode);
			}
			if (!funcStatus) {
				fprintf(stderr, "Translation failed.\n");
				retval = 1;
				goto fail;
			}
			if (in.max_outlen >= 0 || inlen == actualInlen) {
				break;
			} else if (k < 3) {
				// Hm, something is not quite right. Try again with a larger outbuf
				free(outbuf);
				outlen = inlen * outlen_multiplier * (k + 1);
				outbuf = malloc(sizeof(widechar) * outlen);
				if (expected_inputPos) {
					free(inputPos);
					inputPos = malloc(sizeof(int) * outlen);
				}
				fprintf(stderr,
						"Warning: For %s: returned inlen (%d) differs from passed inlen "
						"(%d) "
						"using outbuf of size %d. Trying again with bigger outbuf "
						"(%d).\n",
						input, actualInlen, inlen, inlen * outlen_multiplier * k, outlen);
				actualInlen = inlen;
			}
		}
		expectedlen = _lou_extParseChars(expected, expectedbuf);
		for (i = 0; i < outlen && i < expectedlen && expectedbuf[i] == outbuf[i]; i++)
			;
		if (i < outlen || i < expectedlen) {
			retval = 1;
			if (in.diagnostics) {
				outbuf[outlen] = 0;
				fprintf(stderr, "Input:    '%s'\n", input);
				/* Print the original typeform not the typeformbuf, as the
				 * latter has been modified by the translation and contains some
				 * information about outbuf */
				if (in.typeform != NULL) print_typeform(in.typeform, inlen);
				if (in.cursorPos >= 0) fprintf(stderr, "Cursor:   %d\n", in.cursorPos);
				fprintf(stderr, "Expected: '%s' (length %d)\n", expected, expectedlen);
				fprintf(stderr, "Received: '");
				print_widechars(outbuf, outlen);
				fprintf(stderr, "' (length %d)\n", outlen);

				uint8_t *expected_utf8;
				uint8_t *out_utf8;
				size_t expected_utf8_len;
				size_t out_utf8_len;
#ifdef WIDECHARS_ARE_UCS4
				expected_utf8 = u32_to_u8(&expectedbuf[i], 1, NULL, &expected_utf8_len);
				out_utf8 = u32_to_u8(&outbuf[i], 1, NULL, &out_utf8_len);
#else
				expected_utf8 = u16_to_u8(&expectedbuf[i], 1, NULL, &expected_utf8_len);
				out_utf8 = u16_to_u8(&outbuf[i], 1, NULL, &out_utf8_len);
#endif

				if (i < outlen && i < expectedlen) {
					fprintf(stderr,
							"Diff:     Expected '%.*s' but received '%.*s' in index %d\n",
							(int)expected_utf8_len, expected_utf8, (int)out_utf8_len,
							out_utf8, i);
				} else if (i < expectedlen) {
					fprintf(stderr,
							"Diff:     Expected '%.*s' but received nothing in index "
							"%d\n",
							(int)expected_utf8_len, expected_utf8, i);
				} else {
					fprintf(stderr,
							"Diff:     Expected nothing but received '%.*s' in index "
							"%d\n",
							(int)out_utf8_len, out_utf8, i);
				}
				free(expected_utf8);
				free(out_utf8);
			}
		}
		if (expected_inputPos) {
			int error_printed = 0;
			for (i = 0; i < outlen; i++) {
				if (expected_inputPos[i] != inputPos[i]) {
					if (!error_printed) {  // Print only once
						fprintf(stderr, "Input position failure:\n");
						error_printed = 1;
					}
					fprintf(stderr, "Expected %d, received %d in index %d\n",
							expected_inputPos[i], inputPos[i], i);
					retval = 1;
				}
			}
		}
		if (expected_outputPos) {
			int error_printed = 0;
			for (i = 0; i < inlen; i++) {
				if (expected_outputPos[i] != outputPos[i]) {
					if (!error_printed) {  // Print only once
						fprintf(stderr, "Output position failure:\n");
						error_printed = 1;
					}
					fprintf(stderr, "Expected %d, received %d in index %d\n",
							expected_outputPos[i], outputPos[i], i);
					retval = 1;
				}
			}
		}
		if ((in.expected_cursorPos >= 0) && (cursorPos != in.expected_cursorPos)) {
			fprintf(stderr, "Cursor position failure:\n");
			fprintf(stderr, "Initial:%d Expected:%d Actual:%d \n", in.cursorPos,
					in.expected_cursorPos, cursorPos);
			retval = 1;
		}
		if (in.max_outlen < 0 && inlen != actualInlen) {
			fprintf(stderr,
					"Unexpected error happened: input length is not the same before as "
					"after the translation:\n");
			fprintf(stderr, "Before: %d After: %d \n", inlen, actualInlen);
			retval = 1;
		} else if (actualInlen > inlen) {
			fprintf(stderr,
					"Unexpected error happened: returned input length (%d) exceeds "
					"total input length (%d)\n",
					actualInlen, inlen);
			retval = 1;
		} else if (in.real_inlen >= 0 && in.real_inlen != actualInlen) {
			fprintf(stderr, "Real input length failure:\n");
			fprintf(stderr, "Expected: %d, received: %d\n", in.real_inlen, actualInlen);
			retval = 1;
		}

	fail:
		free(inbuf);
		free(outbuf);
		free(expectedbuf);
		free(typeformbuf);
		free(inputPos);
		free(outputPos);

		if (direction == 2) {
			const char *tmp = input;
			input = expected;
			expected = tmp;
			expected_inputPos = in.expected_outputPos;
			expected_outputPos = in.expected_inputPos;
			direction = 1;
			continue;
		} else {
			break;
		}
	}

	return retval;
}
Beispiel #11
0
/** Check if a string is hyphenated as expected.
 *
 * @return 0 if the hyphenation is as expected and 1 otherwise.
 */
int
check_hyphenation(const char *tableList, const char *str, const char *expected) {
	widechar *inbuf;
	widechar *hyphenatedbuf = NULL;
	uint8_t *hyphenated = NULL;
	char *hyphens = NULL;
	int inlen = strlen(str);
	size_t hyphenatedlen = inlen * 2;
	int retval = 0;

	inbuf = malloc(sizeof(widechar) * inlen);
	inlen = _lou_extParseChars(str, inbuf);
	if (!inlen) {
		fprintf(stderr, "Cannot parse input string.\n");
		retval = 1;
		goto fail;
	}
	hyphens = calloc(inlen + 1, sizeof(char));

	if (!lou_hyphenate(tableList, inbuf, inlen, hyphens, 0)) {
		fprintf(stderr, "Hyphenation failed.\n");
		retval = 1;
		goto fail;
	}
	if (hyphens[0] != '0') {
		fprintf(stderr, "Unexpected output from lou_hyphenate.\n");
		retval = 1;
		goto fail;
	}

	hyphenatedbuf = malloc(sizeof(widechar) * hyphenatedlen);
	int i = 0;
	int j = 0;
	hyphenatedbuf[i++] = inbuf[j++];
	for (; j < inlen; j++) {
		if (hyphens[j] != '0') hyphenatedbuf[i++] = (widechar)'-';
		hyphenatedbuf[i++] = inbuf[j];
	}

#ifdef WIDECHARS_ARE_UCS4
	hyphenated = u32_to_u8(hyphenatedbuf, i, NULL, &hyphenatedlen);
#else
	hyphenated = u16_to_u8(hyphenatedbuf, i, NULL, &hyphenatedlen);
#endif

	if (!hyphenated) {
		fprintf(stderr, "Unexpected error during UTF-8 encoding\n");
		free(hyphenatedbuf);
		retval = 2;
		goto fail;
	}

	if (strlen(expected) != hyphenatedlen ||
			strncmp(expected, (const char *)hyphenated, hyphenatedlen)) {
		fprintf(stderr, "Input:    '%s'\n", str);
		fprintf(stderr, "Expected: '%s'\n", expected);
		fprintf(stderr, "Received: '%.*s'\n", (int)hyphenatedlen, hyphenated);
		retval = 1;
	}

	free(hyphenatedbuf);
	free(hyphenated);

fail:
	free(inbuf);
	free(hyphens);
	return retval;
}
Beispiel #12
0
static inline u8 dispatch_t177(t177 a178, u16 a179)
{
    return u16_to_u8(a179);
}
Beispiel #13
0
int
main(int argc, char **argv) {
	uint8_t *charbuf;
	size_t charlen;
	widechar inbuf[BUFSIZE];
	widechar transbuf[BUFSIZE];
	widechar outbuf[BUFSIZE];
	int outputPos[BUFSIZE];
	int inputPos[BUFSIZE];
	int inlen;
	int translen;
	int outlen;
	int cursorPos = -1;
	int realInlen = 0;
	int optc;

	set_program_name(argv[0]);

	while ((optc = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) {
		switch (optc) {
		/* --help and --version exit immediately, per GNU coding standards. */
		case 'v':
			version_etc(
					stdout, program_name, PACKAGE_NAME, VERSION, AUTHORS, (char *)NULL);
			exit(EXIT_SUCCESS);
			break;
		case 'h':
			print_help();
			exit(EXIT_SUCCESS);
			break;
		default:
			fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
			exit(EXIT_FAILURE);
			break;
		}
	}

	if (optind < argc) {
		/* Print error message and exit. */
		fprintf(stderr, "%s: extra operand: %s\n", program_name, argv[optind]);
		fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
		exit(EXIT_FAILURE);
	}

	validTable = NULL;
	enteredCursorPos = -1;
	mode = 0;
	while (1) {
		getCommands();
		printf("Type something, press enter, and view the results.\n");
		printf("A blank line returns to command entry.\n");
		if (minimalist)
			while (1) {
				translen = outputSize;
				outlen = outputSize;
				inlen = getInput();
				if (inlen == 0) break;
				if (!(realInlen = _lou_extParseChars(inputBuffer, inbuf))) break;
				inlen = realInlen;
				if (!lou_translateString(
							table, inbuf, &inlen, transbuf, &translen, NULL, NULL, 0))
					break;
				transbuf[translen] = 0;
				printf("Translation:\n");
#ifdef WIDECHARS_ARE_UCS4
				charbuf = u32_to_u8(transbuf, translen, NULL, &charlen);
#else
				charbuf = u16_to_u8(transbuf, translen, NULL, &charlen);
#endif
				printf("%.*s\n", (int)charlen, charbuf);
				free(charbuf);
				if (showSizes)
					printf("input length = %d; output length = %d\n", inlen, translen);
				lou_backTranslateString(
						table, transbuf, &translen, outbuf, &outlen, NULL, NULL, 0);
				printf("Back-translation:\n");
#ifdef WIDECHARS_ARE_UCS4
				charbuf = u32_to_u8(outbuf, outlen, NULL, &charlen);
#else
				charbuf = u16_to_u8(outbuf, outlen, NULL, &charlen);
#endif
				printf("%.*s\n", (int)charlen, charbuf);
				free(charbuf);
				if (showSizes)
					printf("input length = %d; output length = %d.\n", translen, outlen);
				if (outlen == realInlen) {
					int k;
					for (k = 0; k < realInlen; k++)
						if (inbuf[k] != outbuf[k]) break;
					if (k == realInlen) printf("Perfect roundtrip!\n");
				}
			}
		else
			while (1) {
				memset(emphasis, 0, sizeof(formtype) * BUFSIZE);
				{
					size_t k = 0;
					for (k = 0; k < strlen(enteredEmphasis); k++)
						emphasis[k] = (formtype)enteredEmphasis[k] - '0';
					emphasis[k] = 0;
				}
				strcpy(spacing, enteredSpacing);
				cursorPos = enteredCursorPos;
				inlen = getInput();
				if (inlen == 0) break;
				outlen = outputSize;
				if (backOnly) {
					if (!(translen = _lou_extParseChars(inputBuffer, transbuf))) break;
					inlen = realInlen;
				} else {
					translen = outputSize;
					if (!(realInlen = _lou_extParseChars(inputBuffer, inbuf))) break;
					inlen = realInlen;
					if (!lou_translate(table, inbuf, &inlen, transbuf, &translen,
								emphasis, spacing, &outputPos[0], &inputPos[0],
								&cursorPos, mode))
						break;
					transbuf[translen] = 0;
					if (mode & dotsIO) {
						printf("Translation dot patterns:\n");
						printf("%s\n", _lou_showDots(transbuf, translen));
					} else {
						printf("Translation:\n");
#ifdef WIDECHARS_ARE_UCS4
						charbuf = u32_to_u8(transbuf, translen, NULL, &charlen);
#else
						charbuf = u16_to_u8(transbuf, translen, NULL, &charlen);
#endif
						printf("%.*s\n", (int)charlen, charbuf);
						free(charbuf);
						if (showSizes)
							printf("input length = %d; output length = %d\n", inlen,
									translen);
					}
				}
				if (cursorPos != -1) printf("Cursor position: %d\n", cursorPos);
				if (enteredSpacing[0]) printf("Returned spacing: %s\n", spacing);
				if (showPositions) {
					printf("Output positions:\n");
					for (int k = 0; k < inlen; k++) printf("%d ", outputPos[k]);
					printf("\n");
					printf("Input positions:\n");
					for (int k = 0; k < translen; k++) printf("%d ", inputPos[k]);
					printf("\n");
				}
				if (!forwardOnly) {
					if (!lou_backTranslate(table, transbuf, &translen, outbuf, &outlen,
								emphasis, spacing, &outputPos[0], &inputPos[0],
								&cursorPos, mode))
						break;
					printf("Back-translation:\n");
#ifdef WIDECHARS_ARE_UCS4
					charbuf = u32_to_u8(outbuf, outlen, NULL, &charlen);
#else
					charbuf = u16_to_u8(outbuf, outlen, NULL, &charlen);
#endif
					printf("%.*s\n", (int)charlen, charbuf);
					free(charbuf);
					if (showSizes)
						printf("input length = %d; output length = %d\n", translen,
								outlen);
					if (cursorPos != -1) printf("Cursor position: %d\n", cursorPos);
					if (enteredSpacing[0]) printf("Returned spacing: %s\n", spacing);
					if (showPositions) {
						printf("Output positions:\n");
						for (int k = 0; k < translen; k++) printf("%d ", outputPos[k]);
						printf("\n");
						printf("Input positions:\n");
						for (int k = 0; k < outlen; k++) printf("%d ", inputPos[k]);
						printf("\n");
					}
				}
				if (!(forwardOnly || backOnly)) {
					if (outlen == realInlen) {
						int k;
						for (k = 0; k < realInlen; k++)
							if (inbuf[k] != outbuf[k]) break;
						if (k == realInlen) printf("Perfect roundtrip!\n");
					}
				}
			}
	}
	lou_free();
	exit(EXIT_SUCCESS);
}
Beispiel #14
0
int SetCommonHeaderBasicData(ncch_settings *set, ncch_hdr *hdr)
{
	/* NCCH Magic */
	memcpy(hdr->magic,"NCCH",4);

	/* NCCH Format Version */
	if(!set->options.IsCfa)
		u16_to_u8(hdr->formatVersion,0x2,LE);

	
	/* Setting ProgramId/TitleId */
	u64 programId = 0;
	int result = GetProgramID(&programId,set->rsfSet,false); 
	if(result) return result;

	u64_to_u8(hdr->programId,programId,LE);
	u64_to_u8(hdr->titleId,programId,LE);

	/* Get Product Code and Maker Code */
	if(set->rsfSet->BasicInfo.ProductCode){
		if(!IsValidProductCode((char*)set->rsfSet->BasicInfo.ProductCode,set->options.FreeProductCode)){
			fprintf(stderr,"[NCCH ERROR] Invalid Product Code\n");
			return NCCH_BAD_RSF_SET;
		}
		memcpy(hdr->productCode,set->rsfSet->BasicInfo.ProductCode,strlen((char*)set->rsfSet->BasicInfo.ProductCode));
	}
	else memcpy(hdr->productCode,"CTR-P-CTAP",10);

	if(set->rsfSet->BasicInfo.CompanyCode){
		if(strlen((char*)set->rsfSet->BasicInfo.CompanyCode) != 2){
			fprintf(stderr,"[NCCH ERROR] CompanyCode length must be 2\n");
			return NCCH_BAD_RSF_SET;
		}
		memcpy(hdr->makerCode,set->rsfSet->BasicInfo.CompanyCode,2);
	}
	else memcpy(hdr->makerCode,"00",2);

	// Setting Encryption Settings
	if(!set->options.Encrypt)
		hdr->flags[ncchflag_OTHER_FLAG] = (otherflag_NoCrypto|otherflag_FixedCryptoKey);
	else if(set->options.useSecCrypto){
		hdr->flags[ncchflag_OTHER_FLAG] = otherflag_Clear;
		hdr->flags[ncchflag_CONTENT_KEYX] = set->options.keyXID;
	}
	else
		hdr->flags[ncchflag_OTHER_FLAG] = otherflag_FixedCryptoKey;	
		
	if(!SetNcchKeys(set->keys,hdr) && set->options.Encrypt){
		hdr->flags[ncchflag_OTHER_FLAG] = (otherflag_NoCrypto|otherflag_FixedCryptoKey);
		hdr->flags[ncchflag_CONTENT_KEYX] = 0;
		set->options.Encrypt = false;
		fprintf(stderr,"[NCCH WARNING] NCCH AES Key could not be loaded, NCCH will not be encrypted\n");
	}

	/* Set ContentUnitSize */
	hdr->flags[ncchflag_CONTENT_BLOCK_SIZE] = GetCtrBlockSizeFlag(set->options.blockSize);

	/* Setting ContentPlatform */
	hdr->flags[ncchflag_CONTENT_PLATFORM] = 1; // CTR

	/* Setting OtherFlag */
	if(!set->options.UseRomFS) 
		hdr->flags[ncchflag_OTHER_FLAG] |= otherflag_NoMountRomFs;


	/* Setting ContentType */
	hdr->flags[ncchflag_CONTENT_TYPE] = 0;
	if(set->options.UseRomFS) hdr->flags[ncchflag_CONTENT_TYPE] |= content_Data;
	if(!set->options.IsCfa) hdr->flags[ncchflag_CONTENT_TYPE] |= content_Executable;
	if(set->rsfSet->BasicInfo.ContentType){
		if(strcmp(set->rsfSet->BasicInfo.ContentType,"Application") == 0) hdr->flags[ncchflag_CONTENT_TYPE] |= 0;
		else if(strcmp(set->rsfSet->BasicInfo.ContentType,"SystemUpdate") == 0) hdr->flags[ncchflag_CONTENT_TYPE] |= content_SystemUpdate;
		else if(strcmp(set->rsfSet->BasicInfo.ContentType,"Manual") == 0) hdr->flags[ncchflag_CONTENT_TYPE] |= content_Manual;
		else if(strcmp(set->rsfSet->BasicInfo.ContentType,"Child") == 0) hdr->flags[ncchflag_CONTENT_TYPE] |= content_Child;
		else if(strcmp(set->rsfSet->BasicInfo.ContentType,"Trial") == 0) hdr->flags[ncchflag_CONTENT_TYPE] |= content_Trial;
		else{
			fprintf(stderr,"[NCCH ERROR] Invalid ContentType '%s'\n",set->rsfSet->BasicInfo.ContentType);
			return NCCH_BAD_RSF_SET;
		}
	}

	return 0;
}