Ejemplo n.º 1
0
void		__recalc_multiline(t_CEditor *pme, gu16 width)
{
	if(BIT_TEST(pme->flags, IDF_TEXT_AUTO_WRAP)){
		ucs2_t *ptr = pme->buffer;
		t_CTextLine line;
		int start;
		int len;
		
		len = pme->nchars;
		start = 0;
		
		g_array_reset(pme->lines, NULL);
		pme->cursor_r = 0;
		pme->cursor_c = pme->cursor;
		while(len > 0 && *ptr){
			line.num = font_measure_ustring(ptr, len, width, NULL, NULL);
			if(line.num == 0)
				break;
			line.start = start;
			start += line.num;
			if(pme->cursor >=  line.start && pme->cursor <= start){
				pme->cursor_r = g_array_size(pme->lines);
				pme->cursor_c = pme->cursor-line.start;
			}
			g_array_append(pme->lines, &line);
			len -= line.num;
			ptr += line.num;
		}
	}else{
		//2 单行显示,需要使光标可见
		
	}
}
Ejemplo n.º 2
0
GArray *lightmagic_detect(const unsigned char *buffer, unsigned int buffer_size, unsigned int window_size)
{
  prong_assert(buffer != NULL);

  if (window_size < buffer_size)
  {
    buffer_size = window_size;
  }

  if (text_lookup_table_filled == 0)
  {
    make_text_lookup_table();
    text_lookup_table_filled = 1;
  }

  GArray *array = g_array_new(FALSE, TRUE, sizeof(int));

  // Minimum size of buffer to identify is 32
  if (window_size < 32)
  {
    g_array_append(array, MAGIC_TYPE_UNIDENTIFIED);
    return array;
  }

  int flags = LUT_TEXT_TEXT | LUT_TEXT_BASE64 | LUT_TEXT_BASE85 | LUT_TEXT_HEX;

  for (int i = 0; (flags != 0) && (i < 32); i++)
  {
    flags &= text_lookup_table[buffer[i]];
    if ((flags & LUT_TEXT_TEXT) == 0)
    {
      flags = 0;
    }
  }

  if (flags & LUT_TEXT_TEXT)
  {
    g_array_append(array, MAGIC_TYPE_TEXT);
    if (flags & LUT_TEXT_BASE64)
    {
      g_array_append(array, MAGIC_TYPE_BASE64);
    }
    if (flags & LUT_TEXT_BASE85)
    {
      g_array_append(array, MAGIC_TYPE_BASE85);
    }
    if (flags & LUT_TEXT_HEX)
    {
      g_array_append(array, MAGIC_TYPE_HEX);
    }
  }
  // Check for filesystems
  const char FAT_MAGIC_1[] = "FAT1";

  if ((window_size >= (54 + strlen(FAT_MAGIC_1))) && (memcmp(buffer + 54, FAT_MAGIC_1, strlen(FAT_MAGIC_1)) == 0))
  {
    g_array_append(array, MAGIC_TYPE_FAT12);
    g_array_append(array, MAGIC_TYPE_FAT16);
  }
  const char FAT_MAGIC_2[] = "FAT32";

  if ((window_size >= (82 + strlen(FAT_MAGIC_2))) && (memcmp(buffer + 82, FAT_MAGIC_2, strlen(FAT_MAGIC_2)) == 0))
  {
    g_array_append(array, MAGIC_TYPE_FAT32);
  }

  const char NTFS_MAGIC[] = "NTFS";

  if ((window_size >= (3 + strlen(NTFS_MAGIC))) && (memcmp(buffer + 3, NTFS_MAGIC, strlen(NTFS_MAGIC)) == 0))
  {
    g_array_append(array, MAGIC_TYPE_NTFS);
  }

  const char EXT_MAGIC[] = "\x53\xef";

  if ((window_size >= (0x438 + strlen(EXT_MAGIC))) && (memcmp(buffer + 0x438, EXT_MAGIC, strlen(EXT_MAGIC)) == 0))
  {
    g_array_append(array, MAGIC_TYPE_EXT);
  }
  // Check for constants. If a constant is found, processing stops as the others below cannot be true
  // Check for 8/16/32 bit consts!
  int next_check_size = 0;
  int processed_size = 4;

  do
  {
    processed_size += next_check_size;
    if ((processed_size << 1) > buffer_size)
    {
      next_check_size = buffer_size - processed_size;
    } else
    {
      next_check_size = processed_size;
    }
  }
  while ((processed_size != buffer_size) && (memcmp(buffer, buffer + processed_size, next_check_size) == 0));

  if (processed_size == buffer_size)
  {
    g_array_append(array, MAGIC_TYPE_CONSTANT);
    return array;
  } else
  {
    g_array_append(array, MAGIC_TYPE_UNIDENTIFIED);
  }


  // I think this is correct... if a bit hacky
  // Converts data to big endian to make the 
  // magic numbers look better
  unsigned int magic_tiny_fingerprint = ntohl(*((int *) buffer));

  switch (magic_tiny_fingerprint)
  {
  case 0x89504E47:             // 0x89 PNG
    g_array_append(array, MAGIC_TYPE_PNG);
    break;
  case 0x47494638:             // GIF8
    g_array_append(array, MAGIC_TYPE_GIF);
    break;
  case 0x504B0304:             // PK 0x03 0x04
    g_array_append(array, MAGIC_TYPE_ZIP);
    break;
    // case 0x25504446:             // %PDF
    // g_array_append(array, MAGIC_TYPE_PDF);
    // break;
  case 0xD0CF11E0:             // 0xd0 0xcf 0x11 0xe0
    g_array_append(array, MAGIC_TYPE_OLE);
    break;
	case 0x00000020:						 // 0x00 0x00 0x00 0x20
  case 0x41444946:             // ADIF
    g_array_append(array, MAGIC_TYPE_AAC);
    break;
  case 0x00000001:             // 0x00 0x00 0x00 0x01
  case 0x47400010:             // 0x47 0x40 0x00 0x10
    g_array_append(array, MAGIC_TYPE_MPEG);
    break;
  case 0x52494646:             // RIFF
    g_array_append(array, MAGIC_TYPE_AVI);
    break;
  case 0x3026B275:             // 0x30 0x26 0xb2 0x75
    g_array_append(array, MAGIC_TYPE_WMV);
    break;
  case 0x464C5601:             // 0x46 0x4c 0x56 0x01
    g_array_append(array, MAGIC_TYPE_FLV);
    break;
  }

  switch (magic_tiny_fingerprint & 0xFFFFFFF0)
  {
  case 0x000001B0:             // 0x00 0x00 0x01 0xB0
    g_array_append(array, MAGIC_TYPE_MPEG);
    break;
  }

  switch (magic_tiny_fingerprint & 0xFFFFFF00)
  {
  case 0xffd8ff00:             // 0xff 0xd8 0xff
    g_array_append(array, MAGIC_TYPE_JPG);
    break;
  case 0x425A6800:             // BZh
    g_array_append(array, MAGIC_TYPE_BZIP2);
    break;
  }

  switch (magic_tiny_fingerprint & 0xFFFF0000)
  {
  case 0x1F8B0000:             // 0x1f 0x8b
    g_array_append(array, MAGIC_TYPE_GZIP);
    break;
  case 0x1F9D0000:             // 0x1f 0x9d
    g_array_append(array, MAGIC_TYPE_COMPRESS);
    break;
  case 0x68DE0000:             // 0x68 0xde
  case 0x48890000:             // 0x48 0x89
  case 0x78DA0000:             // 0x78 0xda
  case 0x789c0000:             // 0x78 0x9c
    g_array_append(array, MAGIC_TYPE_ZLIB);
    break;
  }

  switch (magic_tiny_fingerprint & 0xFFF00000)
  {
  case 0xFFE00000:             // 0xff 0xf?, or
  case 0xFFF00000:             // 0xff 0xe?
    g_array_append(array, MAGIC_TYPE_MP3);
    break;
  }

  // FAX?
  // JBIG?

  // Check if we have a BR/PT
  if (window_size >= 512)
  {
    if ((buffer[510] == 0x55) && (buffer[511] == 0xaa)) // High chance we have an BR block
    {
      g_array_append(array, MAGIC_TYPE_PART);
    }
  }
  // PDF files can be valid as long as %PDF appears in the 1st 1KB of the file
  int pdf_process_size = 1024;

  if (window_size < 1024)
  {
    pdf_process_size = window_size;
  }

  pdf_process_size -= 4;

  unsigned int offset = 0;

  while (offset < pdf_process_size)
  {
    unsigned char *ptr = (unsigned char *) memchr(buffer + offset, '%', pdf_process_size - offset);

    if (ptr == NULL)
    {
      break;
    }
    if (memcmp(ptr, "%PDF", 4) == 0)
    {
      g_array_append(array, MAGIC_TYPE_PDF);
      break;
    }
    offset += (ptr - buffer) + 1;
  }

  return array;
}